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
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion compiler/src/dotty/tools/dotc/core/Annotations.scala
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
5 changes: 5 additions & 0 deletions tests/pos/dependent-annot-type-param.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
class annot[T] extends annotation.Annotation
class Box[T]()
def f(x: Int): Int @annot[Box[x.type]] = x
def test =
val foo = f(42)