Skip to content

Prevent block types with references to local symbols #320

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Feb 11, 2015
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/dotty/tools/dotc/typer/Typer.scala
Original file line number Diff line number Diff line change
Expand Up @@ -472,7 +472,7 @@ class Typer extends Namer with TypeAssigner with Applications with Implicits wit
val Block(stats, expr) = block
val leaks = escapingRefs(block)
if (leaks.isEmpty) block
else if (isFullyDefined(pt, ForceDegree.all)) {
else if (isFullyDefined(pt, ForceDegree.none)) {
val expr1 = Typed(expr, TypeTree(pt))
cpy.Block(block)(stats, expr1) withType expr1.tpe // no assignType here because avoid is redundant
} else if (!forcedDefined) {
Expand Down
2 changes: 2 additions & 0 deletions test/dotc/tests.scala
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,7 @@ class tests extends CompilerTest {
@Test def pos_subtyping = compileFile(posDir, "subtyping")
@Test def pos_t2613 = compileFile(posSpecialDir, "t2613")(allowDeepSubtypes)
@Test def pos_packageObj = compileFile(posDir, "i0239")
@Test def pos_anonClassSubtyping = compileFile(posDir, "anonClassSubtyping")

@Test def pos_all = compileFiles(posDir, failedOther)

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

@Test def dotc = compileDir(dotcDir + "tools/dotc", failedOther)(allowDeepSubtypes)
@Test def dotc_ast = compileDir(dotcDir + "tools/dotc/ast", failedOther) // similar to dotc_config
Expand Down
10 changes: 10 additions & 0 deletions tests/neg/escapingRefs.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
object O {
class A
class B
def f[T](x: T, y: T): T = y

val x: A = f(new A { }, new B { })

val y = f({ class C { def member: Int = 1 }; new C }, { class C { def member: Int = 1 }; new C })
val z = y.member
}
9 changes: 9 additions & 0 deletions tests/pos/anonClassSubtyping.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
object O {
class A
class B
def f[T](x: T, y: T): T = x

val x: A = f(new A { }, new A)

val y: A | B = f(new A { }, new B)
}