Skip to content

Commit e11e9e1

Browse files
committed
Refactor tryHeal
1 parent bc8832c commit e11e9e1

File tree

2 files changed

+27
-38
lines changed

2 files changed

+27
-38
lines changed

compiler/src/dotty/tools/dotc/transform/PCPCheckAndHeal.scala

Lines changed: 24 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -176,7 +176,12 @@ class PCPCheckAndHeal(@constructorOnly ictx: Context) extends TreeMapWithStages(
176176
if (!sym.exists || levelOK(sym) || isStaticPathOK(tp) || isStaticNew(tp))
177177
None
178178
else if (!sym.isStaticOwner && !isClassRef)
179-
tryHeal(sym, tp, pos)
179+
tp match
180+
case tp: TypeRef =>
181+
if levelOf(sym).getOrElse(0) < level then tryHeal(sym, tp, pos)
182+
else None
183+
case _ =>
184+
levelError(sym, tp, pos, "")
180185
else if (!sym.owner.isStaticOwner) // non-top level class reference that is phase inconsistent
181186
levelError(sym, tp, pos, "")
182187
else
@@ -202,42 +207,29 @@ class PCPCheckAndHeal(@constructorOnly ictx: Context) extends TreeMapWithStages(
202207
sym.is(Package) || sym.owner.isStaticOwner || levelOK(sym.owner)
203208
}
204209

205-
/** Try to heal phase-inconsistent reference to type `T` using a local type definition.
210+
/** Try to heal reference to type `T` used in a higher level than its definition.
206211
* @return None if successful
207212
* @return Some(msg) if unsuccessful where `msg` is a potentially empty error message
208213
* to be added to the "inconsistent phase" message.
209214
*/
210-
protected def tryHeal(sym: Symbol, tp: Type, pos: SourcePosition)(implicit ctx: Context): Option[Tree] =
211-
tp match {
212-
case tp: TypeRef =>
213-
if (level == -1) {
214-
assert(ctx.inInlineMethod)
215-
None
216-
}
217-
else if (levelOf(sym).getOrElse(0) < level) {
218-
val reqType = defn.QuotedTypeClass.typeRef.appliedTo(tp)
219-
val tag = ctx.typer.inferImplicitArg(reqType, pos.span)
220-
tag.tpe match {
221-
case _: TermRef =>
222-
Some(tag.select(tpnme.splice))
223-
case _: SearchFailureType =>
224-
levelError(sym, tp, pos,
225-
i"""
226-
|
227-
| The access would be accepted with the right type tag, but
228-
| ${ctx.typer.missingArgMsg(tag, reqType, "")}""")
229-
case _ =>
230-
levelError(sym, tp, pos,
231-
i"""
232-
|
233-
| The access would be accepted with an implict $reqType""")
234-
}
235-
} else {
236-
None
237-
}
215+
protected def tryHeal(sym: Symbol, tp: TypeRef, pos: SourcePosition)(implicit ctx: Context): Option[Tree] = {
216+
val reqType = defn.QuotedTypeClass.typeRef.appliedTo(tp)
217+
val tag = ctx.typer.inferImplicitArg(reqType, pos.span)
218+
tag.tpe match
219+
case _: TermRef =>
220+
Some(tag.select(tpnme.splice))
221+
case _: SearchFailureType =>
222+
levelError(sym, tp, pos,
223+
i"""
224+
|
225+
| The access would be accepted with the right type tag, but
226+
| ${ctx.typer.missingArgMsg(tag, reqType, "")}""")
238227
case _ =>
239-
levelError(sym, tp, pos, "")
240-
}
228+
levelError(sym, tp, pos,
229+
i"""
230+
|
231+
| The access would be accepted with a given $reqType""")
232+
}
241233

242234
private def levelError(sym: Symbol, tp: Type, pos: SourcePosition, errMsg: String)(given Context) = {
243235
def symStr =

compiler/src/dotty/tools/dotc/transform/Staging.scala

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -43,15 +43,12 @@ class Staging extends MacroTransform {
4343
tree match {
4444
case PackageDef(pid, _) if tree.symbol.owner == defn.RootClass =>
4545
val checker = new PCPCheckAndHeal(freshStagingContext) {
46-
override protected def tryHeal(sym: Symbol, tp: Type, pos: SourcePosition)(implicit ctx: Context): Option[tpd.Tree] = {
47-
46+
override protected def tryHeal(sym: Symbol, tp: TypeRef, pos: SourcePosition)(implicit ctx: Context): Option[tpd.Tree] = {
4847
def symStr =
49-
if (!tp.isInstanceOf[ThisType]) sym.show
50-
else if (sym.is(ModuleClass)) sym.sourceModule.show
48+
if (sym.is(ModuleClass)) sym.sourceModule.show
5149
else i"${sym.name}.this"
52-
5350
val errMsg = s"\nin ${ctx.owner.fullName}"
54-
assert(levelOf(sym).getOrElse(0) >= level,
51+
assert(false,
5552
em"""access to $symStr from wrong staging level:
5653
| - the definition is at level ${levelOf(sym).getOrElse(0)},
5754
| - but the access is at level $level.$errMsg""")

0 commit comments

Comments
 (0)