-
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
Consider all arguments in Annotations.refersToParamOf #22001
Conversation
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.
LGTM, but I pointed out another missing case in a comment.
@@ -73,7 +73,7 @@ object Annotations { | |||
|
|||
/** Does this annotation refer to a parameter of `tl`? */ | |||
def refersToParamOf(tl: TermLambda)(using Context): Boolean = | |||
val args = arguments | |||
val args = tpd.allArguments(tree) | |||
if args.isEmpty then false | |||
else tree.existsSubTree: | |||
case id: (Ident | This) => id.tpe.stripped match |
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.
When I was playing with annotations and macros, I also noticed that this check was not enough: when we write down in source code a singleton type x.type
, it will be parsed as SingletonTypeTree(Ident(x))
, but in a macro we usually create a reference to x.type
with just a TermRef
, so no Ident
tree node will be involved.
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 actually started by writing this:
def refersToParamOf(tl: TermLambda)(using Context): Boolean =
tree.existsSubTree:
_.tpe.existsPart:
_.stripped match
case TermParamRef(tl1, _) => tl eq tl1
case _ => false
But couldn't come up with test-cases where that was necessary.
I didn't think of macros.
I guess we'd need something like the above if we want to handle user-generated types that don't have corresponding trees.
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.
When generated from a macro, wouldn't the generated type always be encapsulated in a TypeTree
?
Maybe something like that would be sufficient?
def refersToParamOf(tl: TermLambda)(using Context): Boolean =
def referToParam(tp: Type) =
tp.stripped match
case TermParamRef(tl1, _) => tl eq tl1
case _ => false
val args = tpd.allArguments(tree)
if args.isEmpty then false
else tree.existsSubTree:
case id: (Ident | This) => referToParam(id.tpe)
case tpt: TypeTree => tpt.tpe.existsPart(referToParam)
case _ => false
Thanks a lot for the quick and pertinent review! Let's already merge the arguments fix. I created an issue for the param refs search issue: #22008. |
We should also include type arguments in
Annotations.refersToParamOf
.