-
Notifications
You must be signed in to change notification settings - Fork 1.1k
Refactor level checking / type healing logic #17082
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
nicolasstucki
merged 14 commits into
scala:main
from
dotty-staging:refactor-level-checking-and-type-healing
Mar 14, 2023
Merged
Changes from 5 commits
Commits
Show all changes
14 commits
Select commit
Hold shift + click to select a range
4172269
Strip annotations from the type of a quoted expression
nicolasstucki 8362d14
Refactor level checking / type healing logic
nicolasstucki 67a6475
Extract StagingLevel from TreeMapWithStages
nicolasstucki 527a9ec
Remove mutable state from StagingLevel
nicolasstucki 6d351f9
Move PCPCheckAndHeal to dotty.tools.dotc.staging
nicolasstucki aa73a70
Move TreeMapWithStages to dotty.tools.dotc.staging
nicolasstucki bd5e3b0
Remove Checking from PCPCheckAndHeal
nicolasstucki f32e204
Split annotation splice transformation from type healing
nicolasstucki 915c44c
Extract HealType from PCPCheckAndHeal
nicolasstucki 540277b
Move level tracking into StagingLevel
nicolasstucki 9cbc14c
Move StagingContext to dotty.tools.dotc.staging
nicolasstucki 6d02874
Extract QuoteTypeTags
nicolasstucki 3cbc635
Rename StagingContext to QuoteContext
nicolasstucki 61165cc
Update compiler/src/dotty/tools/dotc/staging/HealType.scala
nicolasstucki File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file was deleted.
Oops, something went wrong.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,99 @@ | ||
package dotty.tools.dotc | ||
package staging | ||
|
||
import dotty.tools.dotc.core.Contexts._ | ||
import dotty.tools.dotc.core.Decorators._ | ||
import dotty.tools.dotc.core.Flags._ | ||
import dotty.tools.dotc.core.Symbols._ | ||
import dotty.tools.dotc.core.Types._ | ||
import dotty.tools.dotc.staging.QuoteContext.* | ||
import dotty.tools.dotc.staging.StagingLevel.* | ||
import dotty.tools.dotc.staging.QuoteTypeTags.* | ||
import dotty.tools.dotc.transform.SymUtils._ | ||
import dotty.tools.dotc.typer.Implicits.SearchFailureType | ||
import dotty.tools.dotc.util.SrcPos | ||
|
||
class HealType(pos: SrcPos)(using Context) extends TypeMap { | ||
|
||
/** If the type refers to a locally defined symbol (either directly, or in a pickled type), | ||
* check that its staging level matches the current level. | ||
* - Static types and term are allowed at any level. | ||
* - If a type reference is used a higher level, then it is inconsistent. | ||
* Will attempt to heal before failing. | ||
* - If a term reference is used a higher level, then it is inconsistent. | ||
* It cannot be healed because the term will not exist in any future stage. | ||
* | ||
* If `T` is a reference to a type at the wrong level, try to heal it by replacing it with | ||
* a type tag of type `quoted.Type[T]`. | ||
* The tag is generated by an instance of `QuoteTypeTags` directly if the splice is explicit | ||
* or indirectly by `tryHeal`. | ||
*/ | ||
def apply(tp: Type): Type = | ||
tp match | ||
case tp: TypeRef => | ||
healTypeRef(tp) | ||
case tp @ TermRef(NoPrefix, _) if !tp.symbol.isStatic && level > levelOf(tp.symbol) => | ||
levelError(tp.symbol, tp, pos) | ||
case tp: AnnotatedType => | ||
derivedAnnotatedType(tp, apply(tp.parent), tp.annot) | ||
case _ => | ||
mapOver(tp) | ||
|
||
private def healTypeRef(tp: TypeRef): Type = | ||
tp.prefix match | ||
case prefix: TermRef if tp.symbol.isTypeSplice => | ||
checkNotWildcardSplice(tp) | ||
if level == 0 then tp else getQuoteTypeTags.getTagRef(prefix) | ||
case prefix: TermRef if !prefix.symbol.isStatic && level > levelOf(prefix.symbol) => | ||
dealiasAndTryHeal(prefix.symbol, tp, pos) | ||
case NoPrefix if level > levelOf(tp.symbol) && !tp.typeSymbol.hasAnnotation(defn.QuotedRuntime_SplicedTypeAnnot) => | ||
dealiasAndTryHeal(tp.symbol, tp, pos) | ||
case prefix: ThisType if level > levelOf(prefix.cls) && !tp.symbol.isStatic => | ||
dealiasAndTryHeal(tp.symbol, tp, pos) | ||
case _ => | ||
mapOver(tp) | ||
|
||
private def checkNotWildcardSplice(splice: TypeRef): Unit = | ||
splice.prefix.termSymbol.info.argInfos match | ||
case (tb: TypeBounds) :: _ => report.error(em"Cannot splice $splice because it is a wildcard type", pos) | ||
case _ => | ||
|
||
private def dealiasAndTryHeal(sym: Symbol, tp: TypeRef, pos: SrcPos): Type = | ||
val tp1 = tp.dealias | ||
if tp1 != tp then apply(tp1) | ||
else tryHeal(tp.symbol, tp, pos) | ||
|
||
/** Try to heal reference to type `T` used in a higher level than its definition. | ||
* Returns a reference to a type tag generated by `QuoteTypeTags` that contains a | ||
* reference to a type alias containing the equivalent of `${summon[quoted.Type[T]]}`. | ||
* Emits and error if `T` cannot be healed and returns `T`. | ||
*/ | ||
protected def tryHeal(sym: Symbol, tp: TypeRef, pos: SrcPos): TypeRef = { | ||
val reqType = defn.QuotedTypeClass.typeRef.appliedTo(tp) | ||
val tag = ctx.typer.inferImplicitArg(reqType, pos.span) | ||
tag.tpe match | ||
case tp: TermRef => | ||
ctx.typer.checkStable(tp, pos, "type witness") | ||
getQuoteTypeTags.getTagRef(tp) | ||
case _: SearchFailureType => | ||
report.error( | ||
ctx.typer.missingArgMsg(tag, reqType, "") | ||
.prepend(i"Reference to $tp within quotes requires a given $reqType in scope.\n") | ||
.append("\n"), | ||
pos) | ||
tp | ||
case _ => | ||
report.error(em"""Reference to $tp within quotes requires a given $reqType in scope. | ||
| | ||
|""", pos) | ||
tp | ||
} | ||
|
||
private def levelError(sym: Symbol, tp: Type, pos: SrcPos): tp.type = { | ||
report.error( | ||
em"""access to $sym from wrong staging level: | ||
| - the definition is at level ${levelOf(sym)}, | ||
| - but the access is at level $level""", pos) | ||
tp | ||
} | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.