Skip to content

Commit 61296bc

Browse files
committed
Fix compilation of scodec
Use correct klass for evaluating rhs of local variables.
1 parent 99ec6b3 commit 61296bc

File tree

2 files changed

+33
-3
lines changed

2 files changed

+33
-3
lines changed

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

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -745,7 +745,7 @@ object Semantic {
745745

746746
case Cold => Result(Cold, Nil)
747747

748-
case ref: Ref => eval(vdef.rhs, ref, klass)
748+
case ref: Ref => eval(vdef.rhs, ref, enclosingClass)
749749

750750
case _ =>
751751
report.error("unexpected defTree when accessing local variable, sym = " + sym.show + ", defTree = " + sym.defTree.show, source)
@@ -992,7 +992,7 @@ object Semantic {
992992
*
993993
* This method only handles cache logic and delegates the work to `cases`.
994994
*/
995-
def eval(expr: Tree, thisV: Ref, klass: ClassSymbol, cacheResult: Boolean = false): Contextual[Result] = log("evaluating " + expr.show + ", this = " + thisV.show, printer, (_: Result).show) {
995+
def eval(expr: Tree, thisV: Ref, klass: ClassSymbol, cacheResult: Boolean = false): Contextual[Result] = log("evaluating " + expr.show + ", this = " + thisV.show + " in " + klass.show, printer, (_: Result).show) {
996996
if (cache.contains(thisV, expr)) Result(cache(thisV, expr), Errors.empty)
997997
else cache.assume(thisV, expr, cacheResult) { cases(expr, thisV, klass) }
998998
}
@@ -1237,7 +1237,7 @@ object Semantic {
12371237
val obj = ref.objekt
12381238
val outerCls = klass.owner.lexicallyEnclosingClass.asClass
12391239
if !obj.hasOuter(klass) then
1240-
val error = PromoteError("outer not yet initialized, target = " + target + ", klass = " + klass, source, trace.toVector)
1240+
val error = PromoteError("outer not yet initialized, target = " + target + ", klass = " + klass + ", object = " + obj, source, trace.toVector)
12411241
report.error(error.show + error.stacktrace, source)
12421242
Hot
12431243
else

tests/init/neg/scodec.scala

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
trait Codec[A] { self =>
2+
final def withContext(context: String): Codec[A] =
3+
class X extends Codec[A] {
4+
def decode(bits: String) = 10
5+
override def toString = s"$self"
6+
}
7+
new X
8+
9+
def decode(bits: String): Int
10+
11+
def decodeOnly[AA >: A]: Codec[AA] = {
12+
val sup = this.decodeOnly[AA]
13+
class Y extends Codec[AA] {
14+
def decode(bits: String) = sup.decode(bits)
15+
}
16+
new Y
17+
}
18+
19+
}
20+
21+
object codecs {
22+
class Z extends Codec[String] {
23+
override def decode(bits: String): Int = 0
24+
}
25+
val codec = new Z
26+
27+
println(codec) // error
28+
29+
val n = 10 // prevent early promotion
30+
}

0 commit comments

Comments
 (0)