Aladdin - Scala Bugtracking
[#623] project: compiler priority: low category: bug
submitter assigned to status date submitted
Sean Martin not repro 2006-06-07 16:38:07.0
subject more NPE madness
code
package mylib;
object constr {
class Quantity{
  private var value:Option[double]=None;
  private var contraints:List[Contraint]=List();
  private var informant:Contraint=NoContraint;
  def getValue:Option[double]=value;
  def setValue(v:double,setter:Contraint):unit=value match{
    case Some(v1)=>if(v!=v1) error("Error!Contradiction:"+v+" and " +v1);
    case None=>{
      informant=setter;
      value=Some(v);
      for(val c<-contraints;c!=informant) 
        c.newValue;
    }
    }
  def setValue(v:double):unit=setValue(v,NoContraint);
  def forgetValue(retractor:Contraint):unit={
    if(retractor==informant)
      {
      value=None;
      for(val c<-contraints;c!=informant)c.dropValue;
      }
  }
  def forgetValue:unit=forgetValue(NoContraint);
  def connect(c:Contraint):unit={
    contraints=c::contraints;
    value match{
      case Some(_)=>c.newValue
      case None=>}
    }
  }



trait Option[+a];
case class Some[+a](value:a)extends Option[a];
case object None extends Option[Nothing];

abstract class Contraint{
  def newValue:unit;
  def dropValue:unit;
}

case class Adder(al:Quantity,a2:Quantity,sum:Quantity)extends Contraint{
  def newValue:unit={Triple(al.getValue,a2.getValue,sum.getValue) match{
    case Triple(Some(x1),Some(x2),_)=>sum.setValue(x1+x2,this);
    case Triple(Some(x1),_,Some(r))=>a2.setValue(r-x1,this);
    case Triple(_,Some(x2),Some(r))=>al.setValue(r-x2,this);
    case _=>}}
  def dropValue:unit={
    al.forgetValue(this);
    a2.forgetValue(this);
    sum.forgetValue(this);
  }
  al connect this;
  a2 connect this;
  sum connect this;
}

case class Constant(q:Quantity,v:double)extends Contraint{
  def newValue:unit=error("Constant.newValue");
  def dropValue:unit=error("Constant.dropValue");
  q connect this;
  q.setValue(v,this);
}
case class NoContraint extends Contraint{
  def newValue:unit=error("Constant.newValue");
  def dropValue:unit=error("Constant.dropValue");
}
case class Multiplier(al:Quantity,a2:Quantity,sum:Quantity) extends Contraint{
  def newValue:unit={Triple(al.getValue,a2.getValue,sum.getValue)match{
  case Triple(Some(x1),Some(x2),_)=>sum.setValue(x1*x2,this)
  case Triple(Some(x1),_,Some(r))=>a2.setValue(r/x1,this)
  case Triple(_,Some(x2),Some(r))=>al.setValue(r/x2,this)
  case Triple(Some(0),_,_)=>al.setValue(0,this)
  case Triple(_,Some(0),_)=>al.setValue(0,this)
  case _=>}
  }
  def dropValue:unit={
    al.forgetValue(this);
    a2.forgetValue(this);
    sum.forgetValue(this);
  }
}

case class Probe(name:String,q:Quantity) extends Contraint{
  def newValue:unit=printProbe(q.getValue);
  def dropValue:unit=printProbe(None);
  private def printProbe(v:Option[double]):unit={
    val vstr=v match{
      case Some(x)=>x.toString();
      case None=>"?";
    }
    Console.println("Probe:"+name+"="+vstr);
  }
}

  def CFconverter(c:Quantity,f:Quantity)={
    val u,v,w,x,y=new Quantity;
    Constant(x,9);
    Multiplier(c,x,u);
    Constant(w,-32);
    Adder(f,w,v)
    Constant(y,5);
    Multiplier(v,y,u);
  }
def main(args : Array[String]):Unit = {
  val C,F=new Quantity;
  CFconverter(C,F);
  Probe("Celsius temp",C);
  Probe("Fahrenheit temp",F);
  C setValue 25;
  
}
}
what happened
Note: bug was encountered by a student during progammiation lab. Someone should tell them to report compiler bug\
s right away and not waste too much time trying to debug the compiler crash. 

nsc> test/Test.scala
error: class file /home/mcdirmid/runtime-workspace/scala/dists/latest/lib/scala-library.jar(scala/Option.class) \
does not define object Option

nsc> test/Test.scala
Exception in thread "main" java.lang.NullPointerException
        at scala.tools.nsc.typechecker.SuperAccessors$SuperAccTransformer.transform(SuperAccessors.scala:67)
        at scala.tools.nsc.typechecker.SuperAccessors$SuperAccTransformer$$anonfun$4.apply(SuperAccessors.scala:\
36)
        at scala.tools.nsc.typechecker.SuperAccessors$SuperAccTransformer$$anonfun$4.apply(SuperAccessors.scala:\
36)
        at scala.List.map(List.scala:652)
        at scala.tools.nsc.typechecker.SuperAccessors$SuperAccTransformer.transformArgs(SuperAccessors.scala:36)\

        at scala.tools.nsc.typechecker.SuperAccessors$SuperAccTransformer.transform(SuperAccessors.scala:67)
        at scala.tools.nsc.ast.Trees$Transformer$$anonfun$25.apply(Trees.scala:1038)
        at scala.tools.nsc.ast.Trees$Transformer$$anonfun$25.apply(Trees.scala:1036)
        at scala.List$.loop$0(List.scala:232)
        at scala.List$.mapConserve(List.scala:249)
        at scala.tools.nsc.ast.Trees$Transformer.transformStats(Trees.scala:1036)
        at scala.tools.nsc.ast.Trees$Transformer.transform(Trees.scala:964)
        at scala.tools.nsc.typechecker.SuperAccessors$SuperAccTransformer.transform(SuperAccessors.scala:73)
        at scala.tools.nsc.ast.Trees$Transformer.transform(Trees.scala:966)
        at scala.tools.nsc.typechecker.SuperAccessors$SuperAccTransformer.transform(SuperAccessors.scala:73)
        at scala.tools.nsc.ast.Trees$Transformer$$anonfun$23.apply(Trees.scala:1032)
        at scala.tools.nsc.ast.Trees$Transformer$$anonfun$23.apply(Trees.scala:1032)
        at scala.List$.loop$0(List.scala:232)
        at scala.List$.mapConserve(List.scala:249)
        at scala.tools.nsc.ast.Trees$Transformer.transformCaseDefs(Trees.scala:1032)
        at scala.tools.nsc.ast.Trees$Transformer.transform(Trees.scala:984)
        at scala.tools.nsc.typechecker.SuperAccessors$SuperAccTransformer.transform(SuperAccessors.scala:73)
        at scala.tools.nsc.ast.Trees$Transformer$$anonfun$16.apply(Trees.scala:943)
        at scala.tools.nsc.ast.Trees$Transformer$$anonfun$16.apply(Trees.scala:942)
        at scala.tools.nsc.ast.Trees$Transformer.atOwner(Trees.scala:1044)
        at scala.tools.nsc.typechecker.SuperAccessors$SuperAccTransformer.atOwner(SuperAccessors.scala:78)
        at scala.tools.nsc.ast.Trees$Transformer.transform(Trees.scala:941)
        at scala.tools.nsc.typechecker.SuperAccessors$SuperAccTransformer.transform(SuperAccessors.scala:73)
        at scala.tools.nsc.ast.Trees$Transformer$$anonfun$19.apply(Trees.scala:1022)
        at scala.tools.nsc.ast.Trees$Transformer$$anonfun$19.apply(Trees.scala:1022)
        at scala.List$.loop$0(List.scala:232)
        at scala.List$.mapConserve(List.scala:249)
        at scala.tools.nsc.ast.Trees$Transformer.transformTrees(Trees.scala:1022)
        at scala.tools.nsc.typechecker.SuperAccessors$SuperAccTransformer.transform(SuperAccessors.scala:43)
        at scala.tools.nsc.ast.Trees$Transformer.transformTemplate(Trees.scala:1024)
        at scala.tools.nsc.ast.Trees$Transformer$$anonfun$13.apply(Trees.scala:930)
        at scala.tools.nsc.ast.Trees$Transformer$$anonfun$13.apply(Trees.scala:930)
        at scala.tools.nsc.ast.Trees$Transformer.atOwner(Trees.scala:1044)
        at scala.tools.nsc.typechecker.SuperAccessors$SuperAccTransformer.atOwner(SuperAccessors.scala:78)
        at scala.tools.nsc.ast.Trees$Transformer.transform(Trees.scala:929)
        at scala.tools.nsc.typechecker.SuperAccessors$SuperAccTransformer.transform(SuperAccessors.scala:73)
        at scala.tools.nsc.ast.Trees$Transformer$$anonfun$19.apply(Trees.scala:1022)
        at scala.tools.nsc.ast.Trees$Transformer$$anonfun$19.apply(Trees.scala:1022)
        at scala.List$.loop$0(List.scala:232)
        at scala.List$.mapConserve(List.scala:249)
        at scala.tools.nsc.ast.Trees$Transformer.transformTrees(Trees.scala:1022)
        at scala.tools.nsc.typechecker.SuperAccessors$SuperAccTransformer.transform(SuperAccessors.scala:43)
        at scala.tools.nsc.ast.Trees$Transformer.transformTemplate(Trees.scala:1024)
        at scala.tools.nsc.ast.Trees$Transformer$$anonfun$14.apply(Trees.scala:934)
        at scala.tools.nsc.ast.Trees$Transformer$$anonfun$14.apply(Trees.scala:934)
        at scala.tools.nsc.ast.Trees$Transformer.atOwner(Trees.scala:1044)
        at scala.tools.nsc.typechecker.SuperAccessors$SuperAccTransformer.atOwner(SuperAccessors.scala:78)
        at scala.tools.nsc.ast.Trees$Transformer.transform(Trees.scala:933)
        at scala.tools.nsc.typechecker.SuperAccessors$SuperAccTransformer.transform(SuperAccessors.scala:73)
        at scala.tools.nsc.ast.Trees$Transformer$$anonfun$25.apply(Trees.scala:1038)
        at scala.tools.nsc.ast.Trees$Transformer$$anonfun$25.apply(Trees.scala:1036)
        at scala.List$.loop$0(List.scala:232)
        at scala.List$.mapConserve(List.scala:249)
        at scala.tools.nsc.ast.Trees$Transformer.transformStats(Trees.scala:1036)
        at scala.tools.nsc.ast.Trees$Transformer$$anonfun$12.apply(Trees.scala:926)
        at scala.tools.nsc.ast.Trees$Transformer$$anonfun$12.apply(Trees.scala:926)
        at scala.tools.nsc.ast.Trees$Transformer.atOwner(Trees.scala:1044)
        at scala.tools.nsc.typechecker.SuperAccessors$SuperAccTransformer.atOwner(SuperAccessors.scala:78)
        at scala.tools.nsc.ast.Trees$Transformer.transform(Trees.scala:925)
        at scala.tools.nsc.typechecker.SuperAccessors$SuperAccTransformer.transform(SuperAccessors.scala:73)
        at scala.tools.nsc.ast.Trees$Transformer.transformUnit(Trees.scala:1039)
        at scala.tools.nsc.transform.Transform$Phase.apply(Transform.scala:22)
        at scala.tools.nsc.Global$GlobalPhase.applyPhase(Global.scala:215)
        at scala.tools.nsc.Global$GlobalPhase$$anonfun$1.apply(Global.scala:204)
        at scala.tools.nsc.Global$GlobalPhase$$anonfun$1.apply(Global.scala:204)
        at scala.Iterator$class.foreach(Iterator.scala:304)
        at scala.collection.mutable.ListBuffer$$anon$0.foreach(ListBuffer.scala:216)
        at scala.tools.nsc.Global$GlobalPhase.run(Global.scala:204)
        at scala.tools.nsc.Global$Run.compileSources(Global.scala:455)
        at scala.tools.nsc.Global$Run.compile(Global.scala:514)
        at scala.tools.nsc.Main$$anonfun$0.apply(Main.scala:39)
        at scala.tools.nsc.Main$$anonfun$0.apply(Main.scala:36)
        at scala.tools.nsc.EvalLoop$class.loop(EvalLoop.scala:17)
        at scala.tools.nsc.Main$.loop(Main.scala:16)
        at scala.tools.nsc.EvalLoop$class.loop(EvalLoop.scala:18)
        at scala.tools.nsc.Main$.loop(Main.scala:16)
        at scala.tools.nsc.Main$.resident(Main.scala:36)
        at scala.tools.nsc.Main$.process(Main.scala:55)
        at scala.tools.nsc.Main$.main(Main.scala:82)
        at scala.tools.nsc.Main.main(Main.scala)
bash-3.00$ 
what expected
[back to overview]
Changes of this bug report
Martin  edited on  2006-06-07 17:08:40.0
I could not reproduce this one either. The program has two errors. It should be Some(x) instead of Option(x) and Multiplier misses a dropValue. I have corrected the code and then it compiles OK.
Sean  edited on  2006-06-07 17:10:40.0
Strange. I'll build a distribution so I can try from the command line, maybe its a resident compiler bug.
Sean  edited on  2006-06-07 17:15:22.0
Sean  edited on  2006-06-07 17:16:41.0
Looks like a resident compiler bug. Sorry I didn't find this out before.
Martin  edited on  2006-06-08 15:11:20.0
I still can't reporduce it.