Skip to content

Commit 5150cad

Browse files
committed
Prevent block types with references to local symbols
1 parent 03a2c6e commit 5150cad

File tree

4 files changed

+22
-1
lines changed

4 files changed

+22
-1
lines changed

src/dotty/tools/dotc/typer/Typer.scala

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -472,7 +472,7 @@ class Typer extends Namer with TypeAssigner with Applications with Implicits wit
472472
val Block(stats, expr) = block
473473
val leaks = escapingRefs(block)
474474
if (leaks.isEmpty) block
475-
else if (isFullyDefined(pt, ForceDegree.all)) {
475+
else if (isFullyDefined(pt, ForceDegree.none)) {
476476
val expr1 = Typed(expr, TypeTree(pt))
477477
cpy.Block(block)(stats, expr1) withType expr1.tpe // no assignType here because avoid is redundant
478478
} else if (!forcedDefined) {

test/dotc/tests.scala

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,7 @@ class tests extends CompilerTest {
6767
@Test def pos_subtyping = compileFile(posDir, "subtyping")
6868
@Test def pos_t2613 = compileFile(posSpecialDir, "t2613")(allowDeepSubtypes)
6969
@Test def pos_packageObj = compileFile(posDir, "i0239")
70+
@Test def pos_anonClassSubtyping = compileFile(posDir, "anonClassSubtyping")
7071

7172
@Test def pos_all = compileFiles(posDir, failedOther)
7273

@@ -119,6 +120,7 @@ class tests extends CompilerTest {
119120
@Test def neg_i0248_inherit_refined = compileFile(negDir, "i0248-inherit-refined", xerrors = 4)
120121
@Test def neg_i0281 = compileFile(negDir, "i0281-null-primitive-conforms", xerrors = 3)
121122
@Test def neg_moduleSubtyping = compileFile(negDir, "moduleSubtyping", xerrors = 4)
123+
@Test def neg_escapingRefs = compileFile(negDir, "escapingRefs", xerrors = 2)
122124

123125
@Test def dotc = compileDir(dotcDir + "tools/dotc", failedOther)(allowDeepSubtypes)
124126
@Test def dotc_ast = compileDir(dotcDir + "tools/dotc/ast", failedOther) // similar to dotc_config

tests/neg/escapingRefs.scala

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
object O {
2+
class A
3+
class B
4+
def f[T](x: T, y: T): T = y
5+
6+
val x: A = f(new A { }, new B { })
7+
8+
val y = f({ class C { def member: Int = 1 }; new C }, { class C { def member: Int = 1 }; new C })
9+
val z = y.member
10+
}

tests/pos/anonClassSubtyping.scala

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
object O {
2+
class A
3+
class B
4+
def f[T](x: T, y: T): T = x
5+
6+
val x: A = f(new A { }, new A)
7+
8+
val y: A | B = f(new A { }, new B)
9+
}

0 commit comments

Comments
 (0)