|
| 1 | +package dotty.tools.dotc |
| 2 | +package staging |
| 3 | + |
| 4 | +import dotty.tools.dotc.core.Contexts._ |
| 5 | +import dotty.tools.dotc.core.Decorators._ |
| 6 | +import dotty.tools.dotc.core.Flags._ |
| 7 | +import dotty.tools.dotc.core.Symbols._ |
| 8 | +import dotty.tools.dotc.core.Types._ |
| 9 | +import dotty.tools.dotc.staging.QuoteContext.* |
| 10 | +import dotty.tools.dotc.staging.StagingLevel.* |
| 11 | +import dotty.tools.dotc.staging.QuoteTypeTags.* |
| 12 | +import dotty.tools.dotc.transform.SymUtils._ |
| 13 | +import dotty.tools.dotc.typer.Implicits.SearchFailureType |
| 14 | +import dotty.tools.dotc.util.SrcPos |
| 15 | + |
| 16 | +class HealType(pos: SrcPos)(using Context) extends TypeMap { |
| 17 | + |
| 18 | + /** If the type refers to a locally defined symbol (either directly, or in a pickled type), |
| 19 | + * check that its staging level matches the current level. |
| 20 | + * - Static types and term are allowed at any level. |
| 21 | + * - If a type reference is used a higher level, then it is inconsistent. |
| 22 | + * Will attempt to heal before failing. |
| 23 | + * - If a term reference is used a higher level, then it is inconsistent. |
| 24 | + * It cannot be healed because the term will not exist in any future stage. |
| 25 | + * |
| 26 | + * If `T` is a reference to a type at the wrong level, try to heal it by replacing it with |
| 27 | + * a type tag of type `quoted.Type[T]`. |
| 28 | + * The tag is generated by an instance of `QuoteTypeTags` directly if the splice is explicit |
| 29 | + * or indirectly by `tryHeal`. |
| 30 | + */ |
| 31 | + def apply(tp: Type): Type = |
| 32 | + tp match |
| 33 | + case tp: TypeRef => |
| 34 | + healTypeRef(tp) |
| 35 | + case tp @ TermRef(NoPrefix, _) if !tp.symbol.isStatic && level > levelOf(tp.symbol) => |
| 36 | + levelError(tp.symbol, tp, pos) |
| 37 | + case tp: AnnotatedType => |
| 38 | + derivedAnnotatedType(tp, apply(tp.parent), tp.annot) |
| 39 | + case _ => |
| 40 | + mapOver(tp) |
| 41 | + |
| 42 | + private def healTypeRef(tp: TypeRef): Type = |
| 43 | + tp.prefix match |
| 44 | + case prefix: TermRef if tp.symbol.isTypeSplice => |
| 45 | + checkNotWildcardSplice(tp) |
| 46 | + if level == 0 then tp else getQuoteTypeTags.getTagRef(prefix) |
| 47 | + case prefix: TermRef if !prefix.symbol.isStatic && level > levelOf(prefix.symbol) => |
| 48 | + dealiasAndTryHeal(prefix.symbol, tp, pos) |
| 49 | + case NoPrefix if level > levelOf(tp.symbol) && !tp.typeSymbol.hasAnnotation(defn.QuotedRuntime_SplicedTypeAnnot) => |
| 50 | + dealiasAndTryHeal(tp.symbol, tp, pos) |
| 51 | + case prefix: ThisType if level > levelOf(prefix.cls) && !tp.symbol.isStatic => |
| 52 | + dealiasAndTryHeal(tp.symbol, tp, pos) |
| 53 | + case _ => |
| 54 | + mapOver(tp) |
| 55 | + |
| 56 | + private def checkNotWildcardSplice(splice: TypeRef): Unit = |
| 57 | + splice.prefix.termSymbol.info.argInfos match |
| 58 | + case (tb: TypeBounds) :: _ => report.error(em"Cannot splice $splice because it is a wildcard type", pos) |
| 59 | + case _ => |
| 60 | + |
| 61 | + private def dealiasAndTryHeal(sym: Symbol, tp: TypeRef, pos: SrcPos): Type = |
| 62 | + val tp1 = tp.dealias |
| 63 | + if tp1 != tp then apply(tp1) |
| 64 | + else tryHeal(tp.symbol, tp, pos) |
| 65 | + |
| 66 | + /** Try to heal reference to type `T` used in a higher level than its definition. |
| 67 | + * Returns a reference to a type tag generated by `QuoteTypeTags` that contains a |
| 68 | + * reference to a type alias containing the equivalent of `${summon[quoted.Type[T]]}`. |
| 69 | + * Emits an error if `T` cannot be healed and returns `T`. |
| 70 | + */ |
| 71 | + protected def tryHeal(sym: Symbol, tp: TypeRef, pos: SrcPos): TypeRef = { |
| 72 | + val reqType = defn.QuotedTypeClass.typeRef.appliedTo(tp) |
| 73 | + val tag = ctx.typer.inferImplicitArg(reqType, pos.span) |
| 74 | + tag.tpe match |
| 75 | + case tp: TermRef => |
| 76 | + ctx.typer.checkStable(tp, pos, "type witness") |
| 77 | + getQuoteTypeTags.getTagRef(tp) |
| 78 | + case _: SearchFailureType => |
| 79 | + report.error( |
| 80 | + ctx.typer.missingArgMsg(tag, reqType, "") |
| 81 | + .prepend(i"Reference to $tp within quotes requires a given $reqType in scope.\n") |
| 82 | + .append("\n"), |
| 83 | + pos) |
| 84 | + tp |
| 85 | + case _ => |
| 86 | + report.error(em"""Reference to $tp within quotes requires a given $reqType in scope. |
| 87 | + | |
| 88 | + |""", pos) |
| 89 | + tp |
| 90 | + } |
| 91 | + |
| 92 | + private def levelError(sym: Symbol, tp: Type, pos: SrcPos): tp.type = { |
| 93 | + report.error( |
| 94 | + em"""access to $sym from wrong staging level: |
| 95 | + | - the definition is at level ${levelOf(sym)}, |
| 96 | + | - but the access is at level $level""", pos) |
| 97 | + tp |
| 98 | + } |
| 99 | +} |
0 commit comments