diff options
-rw-r--r-- | src/compiler/scala/tools/nsc/typechecker/RefChecks.scala | 9 | ||||
-rw-r--r-- | test/files/neg/t4098.check | 13 | ||||
-rw-r--r-- | test/files/neg/t4098.scala | 22 |
3 files changed, 44 insertions, 0 deletions
diff --git a/src/compiler/scala/tools/nsc/typechecker/RefChecks.scala b/src/compiler/scala/tools/nsc/typechecker/RefChecks.scala index 6bed0b1228..b04d768baa 100644 --- a/src/compiler/scala/tools/nsc/typechecker/RefChecks.scala +++ b/src/compiler/scala/tools/nsc/typechecker/RefChecks.scala @@ -1279,6 +1279,15 @@ abstract class RefChecks extends InfoTransform with reflect.internal.transform.R } def transformStat(tree: Tree, index: Int): List[Tree] = tree match { + case Apply(Select(This(_), nme.CONSTRUCTOR), args) => + assert(index == 0, index) + val t = transform(tree) + if (currentLevel.maxindex > 0) { + // An implementation restriction to avoid VerifyErrors and lazyvals mishaps; see SI-4717 + debuglog("refsym = " + currentLevel.refsym) + unit.error(currentLevel.refpos, "forward reference not allowed from self constructor invocation") + } + List(t) case ModuleDef(_, _, _) => eliminateModuleDefs(tree) case ValDef(_, _, _, _) => val tree1 @ ValDef(_, _, _, rhs) = transform(tree) // important to do before forward reference check diff --git a/test/files/neg/t4098.check b/test/files/neg/t4098.check new file mode 100644 index 0000000000..7d69cf151c --- /dev/null +++ b/test/files/neg/t4098.check @@ -0,0 +1,13 @@ +t4098.scala:3: error: forward reference not allowed from self constructor invocation + this(b) + ^ +t4098.scala:8: error: forward reference not allowed from self constructor invocation + this(b) + ^ +t4098.scala:13: error: forward reference not allowed from self constructor invocation + this(b) + ^ +t4098.scala:18: error: forward reference not allowed from self constructor invocation + this(b) + ^ +four errors found diff --git a/test/files/neg/t4098.scala b/test/files/neg/t4098.scala new file mode 100644 index 0000000000..744d6191b5 --- /dev/null +++ b/test/files/neg/t4098.scala @@ -0,0 +1,22 @@ +class A(a: Any) { + def this() = { + this(b) + def b = new {} + } + + def this(x: Int) = { + this(b) + lazy val b = new {} + } + + def this(x: Int, y: Int) = { + this(b) + val b = new {} + } + + def this(x: Int, y: Int, z: Int) = { + this(b) + println(".") + def b = new {} + } +} |