Aladdin - Scala Bugtracking
[#307] project: compiler priority: low category: bug
submitter assigned to status date submitted
Michel Martin fixed 2004-03-15 16:12:37.0
subject Constant folder turns incorrect programs into correct ones
code
object Test {
  def main(args: Array[String]): Unit = {
    val x = y;
    val y = 10;
    System.out.println("x=" + x + " y=" + y);
  }
}
what happened
No error
what expected An error saying that there's a forward reference to y. If one simply adds an explicit type to the declaration of y to prevent the constant folder to do its jobs, one gets:
bug_constantfolder_3.scala:3: forward reference extends over definition of value x
    val x = y;
            ^
one error found
[back to overview]
Changes of this bug report
Michel  edited on  2004-03-15 16:28:10.0
I have an even nastier example where the constant folder enables type incorrect code to be accepted! Here is the code:
object Test {
  class S;
  class C[T <: S];

  def f[T <: S](x: C[T]) = false;

  def main(args: Array[String]): Unit = {
    val x = 1;
    (x == 0) && f[String](new C[String])
  }
}
The problem here is that C[String] should not be legal as String is not a subclass of S. But this is not checked, because the constant folder actually removes the code before checking it. (It seems to me that, since the constant folder runs before phase RefCheck, many checks performed by RefCheck can be disabled in a similar fashion). Like before, you can disable the constant folder just by explicitely stating that function f returns a boolean, and then you get an error.
Erik  edited on  2004-03-15 16:52:44.0
I do not know what is said about object creation in the spec, but the constant folder can circumvent the constructor code for the object.
object Test with Application {
  object o {
    Console.println("Foo");
    val a = '*';
  }

  Console.println(o.a);
 }
Prints only
*
But
object Test with Application {
  object o {
    Console.println("Foo");
    val a:char = '*';
  }

  Console.println(o.a);
 }
Prints
Foo
*
Martin  edited on  2006-03-30 18:51:30.0
Fixed in version 2