Skip to content

Commit 1d5e95e

Browse files
authored
Merge pull request #7200 from dotty-staging/fix-3559
Fix #3559: fix scope for typing this(...) in 2nd constructor
2 parents 1ffe5f1 + 42adb61 commit 1d5e95e

File tree

9 files changed

+49
-0
lines changed

9 files changed

+49
-0
lines changed

compiler/src/dotty/tools/dotc/core/Contexts.scala

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -381,6 +381,8 @@ object Contexts {
381381
superOrThisCallContext(owner, constrCtx.scope)
382382
.setTyperState(typerState)
383383
.setGadt(gadt)
384+
.fresh
385+
.setScope(this.scope)
384386
}
385387

386388
/** The super- or this-call context with given owner and locals. */

tests/neg/3559.check

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
-- Error: tests/neg/3559.scala:3:9 -------------------------------------------------------------------------------------
2+
3 | this(b) // error: forward reference not allowed from self constructor invocation
3+
| ^
4+
| forward reference not allowed from self constructor invocation

tests/neg/3559.scala

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
class A(a: Any) {
2+
def this() = {
3+
this(b) // error: forward reference not allowed from self constructor invocation
4+
def b = new {}
5+
}
6+
}

tests/neg/3559b.check

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
-- Error: tests/neg/3559b.scala:3:9 ------------------------------------------------------------------------------------
2+
3 | this(b) // error
3+
| ^
4+
| b is not accessible from constructor arguments

tests/neg/3559b.scala

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
class A(a: Any) {
2+
def this() = {
3+
this(b) // error
4+
}
5+
6+
def b = new {}
7+
}

tests/neg/3559c.check

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
-- Error: tests/neg/3559c.scala:3:9 ------------------------------------------------------------------------------------
2+
3 | this(a) // error
3+
| ^
4+
| a is not accessible from constructor arguments

tests/neg/3559c.scala

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
class A(a: Any) {
2+
def this() = {
3+
this(a) // error
4+
}
5+
6+
def b = new {}
7+
}

tests/neg/3559d.check

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
-- [E006] Unbound Identifier Error: tests/neg/3559d.scala:7:9 ----------------------------------------------------------
2+
7 | this(f) // error
3+
| ^
4+
| Not found: f
5+
6+
longer explanation available when compiling with `-explain`

tests/neg/3559d.scala

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
class B {
2+
def f: String = "hello"
3+
}
4+
5+
class A(a: Any) extends B {
6+
def this() = {
7+
this(f) // error
8+
}
9+
}

0 commit comments

Comments
 (0)