Skip to content

Commit

Permalink
Handle O.this outside of it's definition
Browse files Browse the repository at this point in the history
  • Loading branch information
liufengyun committed Jun 4, 2021
1 parent 02c6f81 commit ff2455a
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 3 deletions.
14 changes: 11 additions & 3 deletions compiler/src/dotty/tools/dotc/transform/init/Semantic.scala
Original file line number Diff line number Diff line change
Expand Up @@ -868,8 +868,11 @@ class Semantic {
cases(tmref.prefix, thisV, klass, source).select(tmref.symbol, source)

case tp @ ThisType(tref) =>
val value = resolveThis(tref.classSymbol.asClass, thisV, klass, source)
Result(value, Errors.empty)
val cls = tref.classSymbol.asClass
if cls.isStaticOwner && !klass.isContainedIn(cls) then Result(Hot, Nil)
else
val value = resolveThis(cls, thisV, klass, source)
Result(value, Errors.empty)

case _: TermParamRef | _: RecThis =>
// possible from checking effects of types
Expand All @@ -890,7 +893,12 @@ class Semantic {
case addr: Addr =>
val obj = heap(addr)
val outerCls = klass.owner.enclosingClass.asClass
resolveThis(target, obj.outers(klass), outerCls, source)
if !obj.outers.contains(klass) then
val error = PromoteError("outer not yet initialized, target = " + target + ", klass = " + klass, source, trace.toVector)
report.error(error.show + error.stacktrace, source)
Hot
else
resolveThis(target, obj.outers(klass), outerCls, source)
case RefSet(refs) =>
refs.map(ref => resolveThis(target, ref, klass, source)).join
case fun: Fun =>
Expand Down
10 changes: 10 additions & 0 deletions tests/init/pos/Parsers.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
object Parsers {
enum Location(val inParens: Boolean, val inPattern: Boolean, val inArgs: Boolean):
case InParens extends Location(true, false, false)
case InArgs extends Location(true, false, true)
case InPattern extends Location(false, true, false)
case InGuard extends Location(false, false, false)
case InPatternArgs extends Location(false, true, true) // InParens not true, since it might be an alternative
case InBlock extends Location(false, false, false)
case ElseWhere extends Location(false, false, false)
}

0 comments on commit ff2455a

Please sign in to comment.