-
Notifications
You must be signed in to change notification settings - Fork 1.1k
Add add informative scope extrusion check #11442
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 3 commits into
scala:master
from
dotty-staging:add-stonger-scope-extrusion-check
Mar 1, 2021
Merged
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
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 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
34 changes: 34 additions & 0 deletions
34
compiler/src/scala/quoted/runtime/impl/ScopeException.scala
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 |
---|---|---|
@@ -1,3 +1,37 @@ | ||
package scala.quoted.runtime.impl | ||
|
||
import dotty.tools.dotc.ast.tpd.Tree | ||
import dotty.tools.dotc.core.Contexts._ | ||
|
||
class ScopeException(msg: String) extends Exception(msg) | ||
|
||
object ScopeException: | ||
def checkInCorrectScope(scope: Scope, currentScope: Scope, tree: Tree, kind: String)(using Context): Unit = | ||
if scope.root != currentScope.root then | ||
throw new ScopeException(s"Cannot use $kind oustide of the macro splice `$${...}` or the scala.quoted.staging.run(...)` where it was defined") | ||
|
||
val yCheck = ctx.settings.Ycheck.value(using ctx).exists(x => x == "all" || x == "macros") | ||
if yCheck && !scope.isOuterScopeOf(currentScope) then | ||
throw new ScopeException( | ||
if scope.atSameLocation(currentScope) then | ||
s"""Type created in a splice, extruded from that splice and then used in a subsequent evaluation of that same splice. | ||
|Splice: $scope | ||
|$kind: ${tree.show} | ||
| | ||
| | ||
|Splice stack: | ||
|${scope.stack.mkString("\t", "\n\t", "\n")} | ||
""".stripMargin | ||
else | ||
s"""Expression created in a splice was used outside of that splice. | ||
|Created in: $scope | ||
|Used in: $currentScope | ||
|$kind: ${tree.show} | ||
| | ||
| | ||
|Creation stack: | ||
|${scope.stack.mkString("\t", "\n\t", "\n")} | ||
| | ||
|Use stack: | ||
|${currentScope.stack.mkString("\t", "\n\t", "\n")} | ||
""".stripMargin) |
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,66 @@ | ||
package scala.quoted | ||
package runtime.impl | ||
|
||
import dotty.tools.dotc.core.Contexts._ | ||
import dotty.tools.dotc.util.Property | ||
import dotty.tools.dotc.util.SourcePosition | ||
|
||
/** A scope uniquely identifies the context for evaluating a splice | ||
* | ||
* A nested splice gets a new scope with the enclosing scope as its `outer`. | ||
* This also applies for recursive splices. | ||
*/ | ||
trait Scope { | ||
/** Outer scope that was used to create the quote containing this splice. | ||
* NoScope otherwise. | ||
*/ | ||
def outer: Scope = NoScope | ||
/** Is this is a outer scope of the given scope */ | ||
def isOuterScopeOf(scope: Scope): Boolean = | ||
this.eq(scope) || (scope.ne(NoScope) && isOuterScopeOf(scope.outer)) | ||
/** Scope of the top level splice or staging `run` */ | ||
def root: Scope = | ||
if outer.eq(NoScope) then this else outer.root | ||
/** Stack of locations where scopes where evaluated */ | ||
def stack: List[String] = | ||
this.toString :: (if outer.eq(NoScope) then Nil else outer.stack) | ||
/** If the two scopes correspond to the same splice in source. */ | ||
def atSameLocation(scope: Scope): Boolean = false | ||
} | ||
nicolasstucki marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
/** Only used for outer scope of top level splice and staging `run` */ | ||
object NoScope extends Scope: | ||
override def root: Scope = this | ||
override def outer: Scope = throw UnsupportedOperationException("NoScope.outer") | ||
|
||
class SpliceScope(val pos: SourcePosition, override val outer: Scope) extends Scope: | ||
|
||
override def atSameLocation(scope: Scope): Boolean = scope match | ||
case scope: SpliceScope => this.pos == scope.pos | ||
case _ => false | ||
|
||
override def toString = | ||
if pos.exists then | ||
s"${pos.source.toString}:${pos.startLine + 1} at column ${pos.startColumn + 1}" | ||
else | ||
"Unknown location" | ||
|
||
end SpliceScope | ||
|
||
|
||
object SpliceScope: | ||
|
||
/** A key to be used in a context property that tracks current splices we are evaluating */ | ||
private val ScopeKey = new Property.Key[Scope] | ||
|
||
def setSpliceScope(scope: Scope)(using Context): Context = | ||
ctx.fresh.setProperty(ScopeKey, scope) | ||
|
||
def contextWithNewSpliceScope(pos: SourcePosition)(using Context): Context = | ||
ctx.fresh.setProperty(ScopeKey, new SpliceScope(pos, getCurrent)) | ||
|
||
/** Context with an incremented quotation level. */ | ||
def getCurrent(using Context): Scope = | ||
ctx.property(ScopeKey).getOrElse(NoScope) | ||
|
||
end SpliceScope |
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
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
What about
isOuterScopeOf
->contains
?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I avoided
contains
because it can be misinterpreted. Even I made a couple of mistakes and inverted the arguments when it was called contains.