Skip to content

Commit cc9dfe6

Browse files
committed
Do not lift annotation arguments
1 parent c3d6c48 commit cc9dfe6

File tree

7 files changed

+54
-23
lines changed

7 files changed

+54
-23
lines changed

compiler/src/dotty/tools/dotc/ast/TreeInfo.scala

+5-5
Original file line numberDiff line numberDiff line change
@@ -142,17 +142,17 @@ trait TreeInfo[T <: Untyped] { self: Trees.Instance[T] =>
142142

143143
/** All term arguments of an application in a single flattened list */
144144
def allTermArguments(tree: Tree): List[Tree] = unsplice(tree) match {
145-
case Apply(fn, args) => allTermArguments(fn) ::: args
145+
case Apply(fn, args) => allTermArguments(fn) ::: args.map(stripNamedArg)
146146
case TypeApply(fn, args) => allTermArguments(fn)
147-
case Block(_, expr) => allTermArguments(expr)
147+
case Block(Nil, expr) => allTermArguments(expr)
148148
case _ => Nil
149149
}
150150

151151
/** All type and term arguments of an application in a single flattened list */
152152
def allArguments(tree: Tree): List[Tree] = unsplice(tree) match {
153-
case Apply(fn, args) => allArguments(fn) ::: args
154-
case TypeApply(fn, args) => allArguments(fn) ::: args
155-
case Block(_, expr) => allArguments(expr)
153+
case Apply(fn, args) => allArguments(fn) ::: args.map(stripNamedArg)
154+
case TypeApply(fn, args) => allArguments(fn) ::: args.map(stripNamedArg)
155+
case Block(Nil, expr) => allArguments(expr)
156156
case _ => Nil
157157
}
158158

compiler/src/dotty/tools/dotc/cc/CaptureOps.scala

+8-7
Original file line numberDiff line numberDiff line change
@@ -160,13 +160,14 @@ extension (tree: Tree)
160160
refs
161161

162162
/** The arguments of a @retains, @retainsCap or @retainsByName annotation */
163-
def retainedElems(using Context): List[Tree] = tree match
164-
case Apply(_, Typed(SeqLiteral(elems, _), _) :: Nil) =>
165-
elems
166-
case _ =>
167-
if tree.symbol.maybeOwner == defn.RetainsCapAnnot
168-
then ref(defn.captureRoot.termRef) :: Nil
169-
else Nil
163+
def retainedElems(using Context): List[Tree] =
164+
tpd.allTermArguments(tree) match
165+
case List(Typed(SeqLiteral(elems, _), _)) =>
166+
elems
167+
case _ =>
168+
if tree.symbol.maybeOwner == defn.RetainsCapAnnot
169+
then ref(defn.captureRoot.termRef) :: Nil
170+
else Nil
170171

171172
extension (tp: Type)
172173

compiler/src/dotty/tools/dotc/printing/RefinedPrinter.scala

+1-1
Original file line numberDiff line numberDiff line change
@@ -1124,7 +1124,7 @@ class RefinedPrinter(_ctx: Context) extends PlainPrinter(_ctx) {
11241124
def recur(t: untpd.Tree): Text = t match
11251125
case Apply(fn, Nil) => recur(fn)
11261126
case Apply(fn, args) =>
1127-
val explicitArgs = args.filterNot(_.symbol.name.is(DefaultGetterName))
1127+
val explicitArgs = args.filterNot(untpd.stripNamedArg(_).symbol.name.is(DefaultGetterName))
11281128
recur(fn) ~ "(" ~ toTextGlobal(explicitArgs, ", ") ~ ")"
11291129
case TypeApply(fn, args) => recur(fn) ~ "[" ~ toTextGlobal(args, ", ") ~ "]"
11301130
case Select(qual, nme.CONSTRUCTOR) => recur(qual)

compiler/src/dotty/tools/dotc/typer/Applications.scala

+6-1
Original file line numberDiff line numberDiff line change
@@ -513,7 +513,7 @@ trait Applications extends Compatibility {
513513
case tp => args.size
514514
}
515515

516-
!isJavaAnnotConstr(methRef.symbol) &&
516+
!isAnnotConstr(methRef.symbol) &&
517517
args.size < requiredArgNum(funType)
518518
}
519519

@@ -662,6 +662,11 @@ trait Applications extends Compatibility {
662662
def isJavaAnnotConstr(sym: Symbol): Boolean =
663663
sym.is(JavaDefined) && sym.isConstructor && sym.owner.is(JavaAnnotation)
664664

665+
666+
/** Is `sym` a constructor of an annotation? */
667+
def isAnnotConstr(sym: Symbol): Boolean =
668+
sym.isConstructor && sym.owner.isAnnotation
669+
665670
/** Match re-ordered arguments against formal parameters
666671
* @param n The position of the first parameter in formals in `methType`.
667672
*/

compiler/src/dotty/tools/dotc/typer/Checking.scala

+4-9
Original file line numberDiff line numberDiff line change
@@ -1407,15 +1407,10 @@ trait Checking {
14071407
/** Check arguments of compiler-defined annotations */
14081408
def checkAnnotArgs(tree: Tree)(using Context): tree.type =
14091409
val cls = Annotations.annotClass(tree)
1410-
tree match
1411-
case Apply(tycon, arg :: Nil) if cls == defn.TargetNameAnnot =>
1412-
arg match
1413-
case Literal(Constant("")) =>
1414-
report.error(em"target name cannot be empty", arg.srcPos)
1415-
case Literal(_) => // ok
1416-
case _ =>
1417-
report.error(em"@${cls.name} needs a string literal as argument", arg.srcPos)
1418-
case _ =>
1410+
if cls == defn.TargetNameAnnot then
1411+
allArguments(tree) match
1412+
case List(Literal(Constant(name: String))) if !name.isEmpty => () // ok
1413+
case _ => report.error(em"@${cls.name} needs a non-empty string literal as argument", tree.srcPos)
14191414
tree
14201415

14211416
/** 1. Check that all case classes that extend `scala.reflect.Enum` are `enum` cases
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
[[syntax trees at end of typer]] // tests/printing/dependent-annot-default-args.scala
2+
package <empty> {
3+
class annot(x: Any, y: Any) extends annotation.Annotation() {
4+
private[this] val x: Any
5+
private[this] val y: Any
6+
}
7+
final lazy module val annot: annot = new annot()
8+
final module class annot() extends AnyRef() { this: annot.type =>
9+
def $lessinit$greater$default$2: Any @uncheckedVariance = 42
10+
}
11+
final lazy module val dependent-annot-default-args$package:
12+
dependent-annot-default-args$package =
13+
new dependent-annot-default-args$package()
14+
final module class dependent-annot-default-args$package() extends Object() {
15+
this: dependent-annot-default-args$package.type =>
16+
def f(x: Int): Int @annot(x) = x
17+
def test: Unit =
18+
{
19+
val y: Int = ???
20+
val z: Int @annot(y) = f(y)
21+
()
22+
}
23+
}
24+
}
25+
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
class annot(x: Any, y: Any = 42) extends annotation.Annotation
2+
def f(x: Int): Int @annot(x) = x
3+
def test =
4+
val y: Int = ???
5+
val z = f(y)

0 commit comments

Comments
 (0)