From b53660ae13b028c231ddc38edfcb5b6ee8892b89 Mon Sep 17 00:00:00 2001 From: Nicolas Stucki Date: Thu, 11 Jan 2018 14:08:48 +0100 Subject: [PATCH 1/3] Fix #2631: Add custom error message --- compiler/src/dotty/tools/dotc/typer/Typer.scala | 2 ++ compiler/test-resources/repl/i2631 | 4 ++++ 2 files changed, 6 insertions(+) create mode 100644 compiler/test-resources/repl/i2631 diff --git a/compiler/src/dotty/tools/dotc/typer/Typer.scala b/compiler/src/dotty/tools/dotc/typer/Typer.scala index 077bb05b63a9..cd4b8e7f4d0d 100644 --- a/compiler/src/dotty/tools/dotc/typer/Typer.scala +++ b/compiler/src/dotty/tools/dotc/typer/Typer.scala @@ -396,6 +396,8 @@ class Typer extends Namer } else if (name.toTermName == nme.ERROR) UnspecifiedErrorType + else if (ctx.owner.isConstructor && ctx.owner.owner.unforcedDecls.toList.exists(_.name == tree.name)) + errorType(ex"$tree is not accessible from super constructor arguments", tree.pos) else errorType(new MissingIdent(tree, kind, name.show), tree.pos) diff --git a/compiler/test-resources/repl/i2631 b/compiler/test-resources/repl/i2631 new file mode 100644 index 000000000000..229fc74c2843 --- /dev/null +++ b/compiler/test-resources/repl/i2631 @@ -0,0 +1,4 @@ +scala> class Foo(x : Any) { val foo : Integer = 0; def this() = { this(foo) } } +1 | class Foo(x : Any) { val foo : Integer = 0; def this() = { this(foo) } } + | ^^^ + | foo is not accessible from super constructor arguments From d83d09d787b5af7bb544bf6aae418f40f19f1c23 Mon Sep 17 00:00:00 2001 From: Nicolas Stucki Date: Mon, 22 Jan 2018 17:06:39 +0100 Subject: [PATCH 2/3] Add doc and use lookup insted of exists --- compiler/src/dotty/tools/dotc/typer/Typer.scala | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/compiler/src/dotty/tools/dotc/typer/Typer.scala b/compiler/src/dotty/tools/dotc/typer/Typer.scala index cd4b8e7f4d0d..031d744904bf 100644 --- a/compiler/src/dotty/tools/dotc/typer/Typer.scala +++ b/compiler/src/dotty/tools/dotc/typer/Typer.scala @@ -396,9 +396,11 @@ class Typer extends Namer } else if (name.toTermName == nme.ERROR) UnspecifiedErrorType - else if (ctx.owner.isConstructor && ctx.owner.owner.unforcedDecls.toList.exists(_.name == tree.name)) + else if (ctx.owner.isConstructor && ctx.owner.owner.unforcedDecls.lookup(tree.name).exists) { + // If the field existed but was not found we assume that + // we where in the context of an argument of the super constructor errorType(ex"$tree is not accessible from super constructor arguments", tree.pos) - else + } else errorType(new MissingIdent(tree, kind, name.show), tree.pos) val tree1 = ownType match { From ffd28b3cc23fe1268213e0ec02adbc1a1551996f Mon Sep 17 00:00:00 2001 From: Nicolas Stucki Date: Tue, 23 Jan 2018 07:58:48 +0100 Subject: [PATCH 3/3] Check for InSuperCall mode --- compiler/src/dotty/tools/dotc/typer/Typer.scala | 9 +++++---- compiler/test-resources/repl/i2631 | 2 +- 2 files changed, 6 insertions(+), 5 deletions(-) diff --git a/compiler/src/dotty/tools/dotc/typer/Typer.scala b/compiler/src/dotty/tools/dotc/typer/Typer.scala index 031d744904bf..70e31d2ffc02 100644 --- a/compiler/src/dotty/tools/dotc/typer/Typer.scala +++ b/compiler/src/dotty/tools/dotc/typer/Typer.scala @@ -396,10 +396,11 @@ class Typer extends Namer } else if (name.toTermName == nme.ERROR) UnspecifiedErrorType - else if (ctx.owner.isConstructor && ctx.owner.owner.unforcedDecls.lookup(tree.name).exists) { - // If the field existed but was not found we assume that - // we where in the context of an argument of the super constructor - errorType(ex"$tree is not accessible from super constructor arguments", tree.pos) + else if (ctx.owner.isConstructor && ctx.mode.is(Mode.InSuperCall) && + ctx.owner.owner.unforcedDecls.lookup(tree.name).exists) { + // When InSuperCall mode and in a constructor we are in the arguments + // of a this(...) constructor call + errorType(ex"$tree is not accessible from constructor arguments", tree.pos) } else errorType(new MissingIdent(tree, kind, name.show), tree.pos) diff --git a/compiler/test-resources/repl/i2631 b/compiler/test-resources/repl/i2631 index 229fc74c2843..f68b430d829d 100644 --- a/compiler/test-resources/repl/i2631 +++ b/compiler/test-resources/repl/i2631 @@ -1,4 +1,4 @@ scala> class Foo(x : Any) { val foo : Integer = 0; def this() = { this(foo) } } 1 | class Foo(x : Any) { val foo : Integer = 0; def this() = { this(foo) } } | ^^^ - | foo is not accessible from super constructor arguments + | foo is not accessible from constructor arguments \ No newline at end of file