Skip to content
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

Merged
merged 1 commit into from
Nov 22, 2024

Conversation

mbovel
Copy link
Member

@mbovel mbovel commented Nov 21, 2024

We should also include type arguments in Annotations.refersToParamOf.

@mbovel mbovel requested a review from smarter November 21, 2024 21:57
Copy link
Member

@smarter smarter left a 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
Copy link
Member

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.

Copy link
Member Author

@mbovel mbovel Nov 21, 2024

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.

Copy link
Member Author

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

@mbovel
Copy link
Member Author

mbovel commented Nov 22, 2024

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.

@mbovel mbovel merged commit 7ba88ad into scala:main Nov 22, 2024
29 checks passed
@mbovel mbovel deleted the mb/annots/refers-to-type-param branch November 22, 2024 12:12
@WojciechMazur WojciechMazur added this to the 3.6.3 milestone Nov 25, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants