Skip to content

Commit 4cf7494

Browse files
committed
Fix resolution of definition environment
1 parent 870609c commit 4cf7494

File tree

1 file changed

+21
-15
lines changed

1 file changed

+21
-15
lines changed

compiler/src/dotty/tools/dotc/transform/init/Objects.scala

Lines changed: 21 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -198,6 +198,12 @@ object Objects:
198198

199199
def widen(height: Int)(using Context): Data =
200200
new LocalEnv(params.map(_ -> _.widen(height)), owner, outer.widen(height))
201+
202+
override def toString() =
203+
"params: " + params + "\n" +
204+
"locals: " + locals + "\n" +
205+
"outer {\n" + outer + "\n}"
206+
201207
end LocalEnv
202208

203209
object NoEnv extends Data:
@@ -249,19 +255,19 @@ object Objects:
249255
* @param thisV The value for `this` of the enclosing class where the local variable is referenced.
250256
* @param env The local environment where the local variable is referenced.
251257
*/
252-
def resolveDefinitionEnv(sym: Symbol, thisV: Value, env: Data): Option[(Value, Data)] =
253-
if env.contains(sym) then Some(thisV -> env)
254-
else
255-
env match
256-
case localEnv: LocalEnv =>
257-
resolveDefinitionEnv(sym, thisV, localEnv.outer)
258-
case NoEnv =>
259-
// TODO: handle RefSet
260-
thisV match
261-
case ref: OfClass =>
262-
resolveDefinitionEnv(sym, ref.outer, ref.env)
263-
case _ =>
264-
None
258+
def resolveDefinitionEnv(sym: Symbol, thisV: Value, env: Data)(using Context): Option[(Value, Data)] =
259+
env match
260+
case localEnv: LocalEnv =>
261+
if localEnv.owner == sym.owner then Some(thisV -> env)
262+
else resolveDefinitionEnv(sym, thisV, localEnv.outer)
263+
case NoEnv =>
264+
// TODO: handle RefSet
265+
thisV match
266+
case ref: OfClass =>
267+
resolveDefinitionEnv(sym, ref.outer, ref.env)
268+
case _ =>
269+
None
270+
end resolveDefinitionEnv
265271
end Env
266272

267273
/** Abstract heap for mutable fields
@@ -791,7 +797,7 @@ object Objects:
791797
val value = eval(rhs, thisV, klass)
792798

793799
if isLocal then
794-
writeLocal(receiver.asInstanceOf[Ref], lhs.symbol, value)
800+
writeLocal(thisV, lhs.symbol, value)
795801
else
796802
withTrace(trace2) { assign(receiver, lhs.symbol, value, rhs.tpe) }
797803

@@ -847,7 +853,7 @@ object Objects:
847853
// local val definition
848854
val rhs = eval(vdef.rhs, thisV, klass)
849855
val sym = vdef.symbol
850-
initLocal(ref.asInstanceOf[Ref], vdef.symbol, rhs)
856+
initLocal(thisV.asInstanceOf[Ref], vdef.symbol, rhs)
851857
Bottom
852858

853859
case ddef : DefDef =>

0 commit comments

Comments
 (0)