-
Notifications
You must be signed in to change notification settings - Fork 1.1k
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
Disallow local term references in staged types #16362
Merged
Merged
Changes from all commits
Commits
Show all changes
2 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 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 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,6 @@ | ||
import scala.quoted.* | ||
|
||
def m(using Quotes): Expr[Option[_]] = | ||
val s = 3 | ||
type st = s.type | ||
'{ Some(${ Expr(s) }: st) } // error |
This file contains 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,35 @@ | ||
//> using scala "3.2.1" | ||
import scala.quoted.Expr | ||
import scala.quoted.Type | ||
import scala.quoted.quotes | ||
import scala.quoted.Quotes | ||
|
||
object macros { | ||
|
||
inline transparent def mkNames[A]: List[Any] = ${ mkNamesImpl[A] } | ||
|
||
def mkNamesImpl[A: Type](using Quotes): Expr[List[Any]] = { | ||
import quotes.reflect._ | ||
|
||
val fieldNames = TypeRepr.of[A].typeSymbol.declaredFields.map(_.name) | ||
|
||
val types = fieldNames | ||
.map { f => | ||
val t1 = ConstantType(StringConstant(f)) | ||
t1.asType match { | ||
case '[t1Type] => TypeRepr.of[(t1Type, "aa")] | ||
} | ||
} | ||
.reduceLeft[TypeRepr](OrType(_, _)) | ||
|
||
types.asType match { | ||
case '[ttt] => | ||
Expr.ofList[ttt]( | ||
fieldNames.map { v => | ||
Expr[(v.type, "aa")](v -> "aa").asExprOf[ttt] // error | ||
} | ||
) | ||
} | ||
} | ||
|
||
} |
This file contains 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,4 @@ | ||
import scala.quoted._ | ||
def test(v: String)(using Quotes): Any = | ||
Type.of : Type[v.type] // error | ||
Type.of[v.type] // error |
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.
why do we check
level >
here butlevel !=
inhealTypeOfTerm
?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.
In
healTypeOfTerm
we are looking for terms that are used in other stages. Terms must be used at the same stage they are defined, hence thelevel !=
.For types the rules are a bit different. For types in general it is always safe to use the type in a previous stage and one needs a type
Type[T]
for generic types if it is used in a future stage. This is in general where the>
comes in for type. But in this particular case, we have a type reference to a local variable that will not exist in the next stage and we cannot create a validType[T]
for it. Therefore it emmits an error.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.
Thanks for the explanation, I think it'd be helpful to add something like that as a comment in the code
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.
Added to the comment.