From 8ee0ec54024d3378426098aed77b783d3066f4c2 Mon Sep 17 00:00:00 2001 From: Liu Fengyun Date: Wed, 11 Sep 2019 21:18:30 +0200 Subject: [PATCH 1/3] Fix #3559: fix scope for typing this(...) in 2nd constructor The scope for typing `this(...)` should include both constructor params and local defs. The latter are included to provide more friendly error messages for references to local definitions. --- compiler/src/dotty/tools/dotc/core/Contexts.scala | 2 ++ tests/neg/3559.check | 4 ++++ tests/neg/3559.scala | 6 ++++++ 3 files changed, 12 insertions(+) create mode 100644 tests/neg/3559.check create mode 100644 tests/neg/3559.scala diff --git a/compiler/src/dotty/tools/dotc/core/Contexts.scala b/compiler/src/dotty/tools/dotc/core/Contexts.scala index 67e08f14f411..fb1db920d65b 100644 --- a/compiler/src/dotty/tools/dotc/core/Contexts.scala +++ b/compiler/src/dotty/tools/dotc/core/Contexts.scala @@ -381,6 +381,8 @@ object Contexts { superOrThisCallContext(owner, constrCtx.scope) .setTyperState(typerState) .setGadt(gadt) + .fresh + .setScope(this.scope) } /** The super- or this-call context with given owner and locals. */ diff --git a/tests/neg/3559.check b/tests/neg/3559.check new file mode 100644 index 000000000000..d385b1523478 --- /dev/null +++ b/tests/neg/3559.check @@ -0,0 +1,4 @@ +-- Error: tests/neg/3559.scala:3:9 ------------------------------------------------------------------------------------- +3 | this(b) // error: forward reference not allowed from self constructor invocation + | ^ + | forward reference not allowed from self constructor invocation diff --git a/tests/neg/3559.scala b/tests/neg/3559.scala new file mode 100644 index 000000000000..66eafa8a43f3 --- /dev/null +++ b/tests/neg/3559.scala @@ -0,0 +1,6 @@ +class A(a: Any) { + def this() = { + this(b) // error: forward reference not allowed from self constructor invocation + def b = new {} + } +} From 4d9c0c59daef5113f279711fe3f753dd148f4ab0 Mon Sep 17 00:00:00 2001 From: Liu Fengyun Date: Thu, 12 Sep 2019 15:39:35 +0200 Subject: [PATCH 2/3] Add more tests --- tests/neg/3559b.check | 4 ++++ tests/neg/3559b.scala | 7 +++++++ tests/neg/3559c.check | 4 ++++ tests/neg/3559c.scala | 7 +++++++ 4 files changed, 22 insertions(+) create mode 100644 tests/neg/3559b.check create mode 100644 tests/neg/3559b.scala create mode 100644 tests/neg/3559c.check create mode 100644 tests/neg/3559c.scala diff --git a/tests/neg/3559b.check b/tests/neg/3559b.check new file mode 100644 index 000000000000..a147136e1210 --- /dev/null +++ b/tests/neg/3559b.check @@ -0,0 +1,4 @@ +-- Error: tests/neg/3559b.scala:3:9 ------------------------------------------------------------------------------------ +3 | this(b) // error + | ^ + | b is not accessible from constructor arguments diff --git a/tests/neg/3559b.scala b/tests/neg/3559b.scala new file mode 100644 index 000000000000..7bfbc16dc4d4 --- /dev/null +++ b/tests/neg/3559b.scala @@ -0,0 +1,7 @@ +class A(a: Any) { + def this() = { + this(b) // error + } + + def b = new {} +} diff --git a/tests/neg/3559c.check b/tests/neg/3559c.check new file mode 100644 index 000000000000..0526712e2977 --- /dev/null +++ b/tests/neg/3559c.check @@ -0,0 +1,4 @@ +-- Error: tests/neg/3559c.scala:3:9 ------------------------------------------------------------------------------------ +3 | this(a) // error + | ^ + | a is not accessible from constructor arguments diff --git a/tests/neg/3559c.scala b/tests/neg/3559c.scala new file mode 100644 index 000000000000..de54b3a097ce --- /dev/null +++ b/tests/neg/3559c.scala @@ -0,0 +1,7 @@ +class A(a: Any) { + def this() = { + this(a) // error + } + + def b = new {} +} From 42adb611a6530276e37cc8b0fd4cfcfca15929d0 Mon Sep 17 00:00:00 2001 From: Liu Fengyun Date: Thu, 12 Sep 2019 15:44:26 +0200 Subject: [PATCH 3/3] Add inheritance test --- tests/neg/3559d.check | 6 ++++++ tests/neg/3559d.scala | 9 +++++++++ 2 files changed, 15 insertions(+) create mode 100644 tests/neg/3559d.check create mode 100644 tests/neg/3559d.scala diff --git a/tests/neg/3559d.check b/tests/neg/3559d.check new file mode 100644 index 000000000000..213f4f165f68 --- /dev/null +++ b/tests/neg/3559d.check @@ -0,0 +1,6 @@ +-- [E006] Unbound Identifier Error: tests/neg/3559d.scala:7:9 ---------------------------------------------------------- +7 | this(f) // error + | ^ + | Not found: f + +longer explanation available when compiling with `-explain` diff --git a/tests/neg/3559d.scala b/tests/neg/3559d.scala new file mode 100644 index 000000000000..b4306427ad02 --- /dev/null +++ b/tests/neg/3559d.scala @@ -0,0 +1,9 @@ +class B { + def f: String = "hello" +} + +class A(a: Any) extends B { + def this() = { + this(f) // error + } +}