Skip to content

Commit 4b1f5da

Browse files
committed
Fix scala#1664: Refine isOuterRef condition
We forgot the case where a hoistable method can still refer to free variables that have to be passed using outer pointers. Fixes scala#1664.
1 parent 28c2e04 commit 4b1f5da

File tree

2 files changed

+24
-8
lines changed

2 files changed

+24
-8
lines changed

src/dotty/tools/dotc/transform/ExplicitOuter.scala

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -226,14 +226,18 @@ object ExplicitOuter {
226226
case ref: TermRef =>
227227
if (ref.prefix ne NoPrefix)
228228
!ref.symbol.isStatic && isOuterRef(ref.prefix)
229-
else if (ref.symbol is Hoistable)
230-
// ref.symbol will be placed in enclosing class scope by LambdaLift, so it might need
231-
// an outer path then.
232-
isOuterSym(ref.symbol.owner.enclosingClass)
233-
else
234-
// ref.symbol will get a proxy in immediately enclosing class. If this properly
235-
// contains the current class, it needs an outer path.
236-
ctx.owner.enclosingClass.owner.enclosingClass.isContainedIn(ref.symbol.owner)
229+
else (
230+
(ref.symbol is Hoistable) &&
231+
// ref.symbol will be placed in enclosing class scope by LambdaLift, so it might need
232+
// an outer path then.
233+
isOuterSym(ref.symbol.owner.enclosingClass)
234+
||
235+
// If not hoistable, ref.symbol will get a proxy in immediately enclosing class. If this properly
236+
// contains the current class, it needs an outer path.
237+
// If the symbol is hoistable, it might have free variables for which the same
238+
// reasoning applies. See pos/i1664.scala
239+
ctx.owner.enclosingClass.owner.enclosingClass.isContainedIn(ref.symbol.owner)
240+
)
237241
case _ => false
238242
}
239243
def hasOuterPrefix(tp: Type) = tp match {

tests/pos/i1664.scala

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
object test {
2+
def f[a](x: a) = {
3+
def print = x
4+
class A {
5+
def f() = {
6+
class B { def h = print }
7+
new B
8+
}
9+
f()
10+
}
11+
}
12+
}

0 commit comments

Comments
 (0)