From 49f287ac081fb3d10a18fdcc56e2bfb75c5054d4 Mon Sep 17 00:00:00 2001 From: Matt Bovel Date: Tue, 26 Nov 2024 20:26:20 +0000 Subject: [PATCH] Do not lift annotation arguments --- .../src/dotty/tools/dotc/ast/TreeInfo.scala | 4 +-- .../tools/dotc/printing/RefinedPrinter.scala | 2 +- .../dotty/tools/dotc/typer/Applications.scala | 7 +++++- .../dependent-annot-default-args.check | 25 +++++++++++++++++++ .../dependent-annot-default-args.scala | 5 ++++ 5 files changed, 39 insertions(+), 4 deletions(-) create mode 100644 tests/printing/dependent-annot-default-args.check create mode 100644 tests/printing/dependent-annot-default-args.scala diff --git a/compiler/src/dotty/tools/dotc/ast/TreeInfo.scala b/compiler/src/dotty/tools/dotc/ast/TreeInfo.scala index 09c855847fac..e0fe17755257 100644 --- a/compiler/src/dotty/tools/dotc/ast/TreeInfo.scala +++ b/compiler/src/dotty/tools/dotc/ast/TreeInfo.scala @@ -144,7 +144,7 @@ trait TreeInfo[T <: Untyped] { self: Trees.Instance[T] => def allTermArguments(tree: Tree): List[Tree] = unsplice(tree) match { case Apply(fn, args) => allTermArguments(fn) ::: args case TypeApply(fn, args) => allTermArguments(fn) - case Block(_, expr) => allTermArguments(expr) + case Block(Nil, expr) => allTermArguments(expr) case _ => Nil } @@ -152,7 +152,7 @@ trait TreeInfo[T <: Untyped] { self: Trees.Instance[T] => def allArguments(tree: Tree): List[Tree] = unsplice(tree) match { case Apply(fn, args) => allArguments(fn) ::: args case TypeApply(fn, args) => allArguments(fn) ::: args - case Block(_, expr) => allArguments(expr) + case Block(Nil, expr) => allArguments(expr) case _ => Nil } diff --git a/compiler/src/dotty/tools/dotc/printing/RefinedPrinter.scala b/compiler/src/dotty/tools/dotc/printing/RefinedPrinter.scala index a399b8173b5d..b7f2eef8c8f9 100644 --- a/compiler/src/dotty/tools/dotc/printing/RefinedPrinter.scala +++ b/compiler/src/dotty/tools/dotc/printing/RefinedPrinter.scala @@ -1124,7 +1124,7 @@ class RefinedPrinter(_ctx: Context) extends PlainPrinter(_ctx) { def recur(t: untpd.Tree): Text = t match case Apply(fn, Nil) => recur(fn) case Apply(fn, args) => - val explicitArgs = args.filterNot(_.symbol.name.is(DefaultGetterName)) + val explicitArgs = args.filterNot(untpd.stripNamedArg(_).symbol.name.is(DefaultGetterName)) recur(fn) ~ "(" ~ toTextGlobal(explicitArgs, ", ") ~ ")" case TypeApply(fn, args) => recur(fn) ~ "[" ~ toTextGlobal(args, ", ") ~ "]" case Select(qual, nme.CONSTRUCTOR) => recur(qual) diff --git a/compiler/src/dotty/tools/dotc/typer/Applications.scala b/compiler/src/dotty/tools/dotc/typer/Applications.scala index 5fb91694b8a6..41e48f7595dc 100644 --- a/compiler/src/dotty/tools/dotc/typer/Applications.scala +++ b/compiler/src/dotty/tools/dotc/typer/Applications.scala @@ -513,7 +513,7 @@ trait Applications extends Compatibility { case tp => args.size } - !isJavaAnnotConstr(methRef.symbol) && + !isAnnotConstr(methRef.symbol) && args.size < requiredArgNum(funType) } @@ -662,6 +662,11 @@ trait Applications extends Compatibility { def isJavaAnnotConstr(sym: Symbol): Boolean = sym.is(JavaDefined) && sym.isConstructor && sym.owner.is(JavaAnnotation) + + /** Is `sym` a constructor of an annotation? */ + def isAnnotConstr(sym: Symbol): Boolean = + sym.isConstructor && sym.owner.isAnnotation + /** Match re-ordered arguments against formal parameters * @param n The position of the first parameter in formals in `methType`. */ diff --git a/tests/printing/dependent-annot-default-args.check b/tests/printing/dependent-annot-default-args.check new file mode 100644 index 000000000000..44c1fe31e2d1 --- /dev/null +++ b/tests/printing/dependent-annot-default-args.check @@ -0,0 +1,25 @@ +[[syntax trees at end of typer]] // tests/printing/dependent-annot-default-args.scala +package { + class annot(x: Any, y: Any) extends annotation.Annotation() { + private[this] val x: Any + private[this] val y: Any + } + final lazy module val annot: annot = new annot() + final module class annot() extends AnyRef() { this: annot.type => + def $lessinit$greater$default$2: Any @uncheckedVariance = 42 + } + final lazy module val dependent-annot-default-args$package: + dependent-annot-default-args$package = + new dependent-annot-default-args$package() + final module class dependent-annot-default-args$package() extends Object() { + this: dependent-annot-default-args$package.type => + def f(x: Int): Int @annot(x) = x + def test: Unit = + { + val y: Int = ??? + val z: Int @annot(y) = f(y) + () + } + } +} + diff --git a/tests/printing/dependent-annot-default-args.scala b/tests/printing/dependent-annot-default-args.scala new file mode 100644 index 000000000000..7ddce711fedc --- /dev/null +++ b/tests/printing/dependent-annot-default-args.scala @@ -0,0 +1,5 @@ +class annot(x: Any, y: Any = 42) extends annotation.Annotation +def f(x: Int): Int @annot(x) = x +def test = + val y: Int = ??? + val z = f(y)