Skip to content

Commit 798f531

Browse files
committed
Workaround bug in typer
1 parent c2ef24e commit 798f531

File tree

2 files changed

+31
-7
lines changed

2 files changed

+31
-7
lines changed

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

Lines changed: 26 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -184,6 +184,8 @@ object Objects:
184184

185185
def level: Int
186186

187+
def show(using Context): String
188+
187189
/** Local environments can be deeply nested, therefore we need `outer`. */
188190
private case class LocalEnv(private[Env] val params: Map[Symbol, Value], owner: Symbol, outer: Data)(using Context) extends Data:
189191
val level = outer.level + 1
@@ -203,10 +205,11 @@ object Objects:
203205
def widen(height: Int)(using Context): Data =
204206
new LocalEnv(params.map(_ -> _.widen(height)), owner, outer.widen(height))
205207

206-
override def toString() =
207-
"params: " + params + "\n" +
208-
"locals: " + locals + "\n" +
209-
"outer {\n" + outer + "\n}"
208+
def show(using Context) =
209+
"owner: " + owner.show + "\n" +
210+
"params: " + params.map(_.show + " ->" + _.show).mkString("{", ", ", "}") + "\n" +
211+
"locals: " + locals.map(_.show + " ->" + _.show).mkString("{", ", ", "}") + "\n" +
212+
"outer = {\n" + outer.show + "\n}"
210213

211214
end LocalEnv
212215

@@ -220,6 +223,8 @@ object Objects:
220223
throw new RuntimeException("Invalid usage of non-existent env")
221224

222225
def widen(height: Int)(using Context): Data = this
226+
227+
def show(using Context): String = "NoEnv"
223228
end NoEnv
224229

225230
/** An empty environment can be used for non-method environments, e.g., field initializers.
@@ -614,7 +619,11 @@ object Objects:
614619
Cold
615620
else
616621
given Env.Data = env
617-
Env(sym)
622+
try
623+
Env(sym)
624+
catch ex =>
625+
report.warning("[Internal error] Not found " + sym.show + "\nenv = " + env.show + ". Calling trace:\n" + Trace.show, Trace.position)
626+
Bottom
618627

619628
case _ => Cold
620629
}
@@ -916,8 +925,18 @@ object Objects:
916925

917926
case tmref: TermRef if tmref.prefix == NoPrefix =>
918927
val sym = tmref.symbol
919-
if sym.is(Flags.Package) then Bottom
920-
else readLocal(thisV, sym)
928+
if sym.is(Flags.Package) then
929+
Bottom
930+
else if sym.owner.isClass then
931+
// The typer incorrectly assigns a TermRef with NoPrefix for `config`,
932+
// while the actual denotation points to the symbol of the class member
933+
// instead of the parameter symbol for the primary constructor.
934+
//
935+
// abstract class Base(implicit config: Int)
936+
// case class A(x: Int)(implicit config: Int) extends Base
937+
evalType(sym.termRef, thisV, klass, elideObjectAccess)
938+
else
939+
readLocal(thisV, sym)
921940

922941
case tmref: TermRef =>
923942
val sym = tmref.symbol
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
object Test:
2+
abstract class Base(implicit config: Int)
3+
case class A(x: Int)(implicit config: Int) extends Base
4+
5+
val a: A = A(3)(using 5)

0 commit comments

Comments
 (0)