Skip to content

Commit 337856c

Browse files
committed
Handle nested explicitly constructed annotations
1 parent 5f8ae12 commit 337856c

File tree

5 files changed

+114
-12
lines changed

5 files changed

+114
-12
lines changed

compiler/src/dotty/tools/dotc/core/Mode.scala

+28-1
Original file line numberDiff line numberDiff line change
@@ -166,7 +166,34 @@ object Mode {
166166
*/
167167
val ForceInline: Mode = newMode(29, "ForceInline")
168168

169-
/** Are we typing an annotation? */
169+
/** Are we typing the argument of an annotation?
170+
*
171+
* This mode is used through [[Applications.isAnnotConstr]] to avoid lifting
172+
* arguments of annotation constructors. This mode is disabled in nested
173+
* applications (from [[ProtoTypes.typedArg]]) and in "explicit" annotation
174+
* constructors applications (annotation classes constructed with `new`).
175+
*
176+
* In the following example:
177+
*
178+
* ```scala
179+
* @annot(y = new annot(y = Array("World"), x = 1), x = 2)
180+
* ```
181+
*
182+
* the mode will be set when typing `@annot(...)` but not when typing
183+
* `new annot(...)`, such that the arguments of the former are not lifted but
184+
* the arguments of the later can be:
185+
*
186+
* ```scala
187+
* @annot(x = 2, y = {
188+
* val y$3: Array[String] =
189+
* Array.apply[String](["World" : String]*)(
190+
* scala.reflect.ClassTag.apply[String](classOf[String]))
191+
* new annot(x = 1, y = y$3)
192+
* })
193+
* ```
194+
*
195+
* See #22035, #22526, #22553 and `dependent-annot-default-args.scala`.
196+
*/
170197
val InAnnotation: Mode = newMode(30, "InAnnotation")
171198

172199
/** Skip inlining of methods. */

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

+1-2
Original file line numberDiff line numberDiff line change
@@ -695,8 +695,7 @@ trait Applications extends Compatibility {
695695

696696

697697
/** Is `sym` a constructor of an annotation class, and are we in an
698-
* annotation? If so, we don't lift arguments.
699-
* See #22035, #22526 and `dependent-annot-default-args.scala`.
698+
* annotation? If so, we don't lift arguments. See [[Mode.InAnnotation]].
700699
*/
701700
protected final def isAnnotConstr(sym: Symbol): Boolean =
702701
ctx.mode.is(Mode.InAnnotation) && sym.isConstructor && sym.owner.isAnnotation

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

+2-2
Original file line numberDiff line numberDiff line change
@@ -536,8 +536,8 @@ object ProtoTypes {
536536
def typedArg(arg: untpd.Tree, formal: Type)(using Context): Tree = {
537537
val wideFormal = formal.widenExpr
538538
val argCtx =
539-
if wideFormal eq formal then ctx
540-
else ctx.withNotNullInfos(ctx.notNullInfos.retractMutables)
539+
if wideFormal eq formal then ctx.retractMode(Mode.InAnnotation)
540+
else ctx.retractMode(Mode.InAnnotation).withNotNullInfos(ctx.notNullInfos.retractMutables)
541541
val locked = ctx.typerState.ownedVars
542542
val targ = cacheTypedArg(arg,
543543
typer.typedUnadapted(_, wideFormal, locked)(using argCtx),

tests/printing/dependent-annot-default-args.check

+72-5
Original file line numberDiff line numberDiff line change
@@ -23,15 +23,16 @@ package <empty> {
2323
new dependent-annot-default-args$package()
2424
final module class dependent-annot-default-args$package() extends Object() {
2525
this: dependent-annot-default-args$package.type =>
26-
def f(x: Int): Int @annot(x) = x
26+
def f(x: Any): Any @annot(x) = x
2727
def f2(x: Int):
2828
Int @annot2(
2929
y = Array.apply[Any](["Hello",x : Any]*)(scala.reflect.ClassTag.Any))
3030
= x
31+
def f3(x: Any, y: Any): Any @annot(x = x, y = y) = x
3132
def test: Unit =
3233
{
3334
val y: Int = ???
34-
val z: Int @annot(y) = f(y)
35+
val z: Any @annot(y) = f(y)
3536
val z2:
3637
Int @annot2(
3738
y = Array.apply[Any](["Hello",y : Any]*)(scala.reflect.ClassTag.Any)
@@ -41,11 +42,77 @@ package <empty> {
4142
@annot2(
4243
y = Array.apply[Any](["Hello",y : Any]*)(scala.reflect.ClassTag.Any))
4344
val z4: Int = 45
44-
val z5: annot2 =
45+
val z5: annot =
4546
{
46-
val y$1: Array[Any] =
47+
val y$1: Array[String] =
48+
Array.apply[String](["World" : String]*)(
49+
scala.reflect.ClassTag.apply[String](classOf[String]))
50+
new annot(x = 1, y = y$1)
51+
}
52+
val z6: annot2 =
53+
{
54+
val y$2: Array[Any] =
4755
Array.apply[Any](["World" : Any]*)(scala.reflect.ClassTag.Any)
48-
new annot2(x = 1, y = y$1)
56+
new annot2(x = 1, y = y$2)
57+
}
58+
@annot(x = 2,
59+
y =
60+
{
61+
val y$3: Array[String] =
62+
Array.apply[String](["World" : String]*)(
63+
scala.reflect.ClassTag.apply[String](classOf[String]))
64+
new annot(x = 1, y = y$3)
65+
}
66+
) val z7: Int = 45
67+
@annot(x = 4,
68+
y =
69+
3:
70+
Int @annot(x = 1,
71+
y =
72+
Array.apply[String](["World" : String]*)(
73+
scala.reflect.ClassTag.apply[String](classOf[String]))
74+
)
75+
) val z8: Int = 45
76+
val z9:
77+
Int @annot(x = 2,
78+
y =
79+
{
80+
val y$4: Array[String] =
81+
Array.apply[String](["World" : String]*)(
82+
scala.reflect.ClassTag.apply[String](classOf[String]))
83+
new annot(x = 1, y = y$4)
84+
}
85+
)
86+
= 46
87+
@annot(x = 4,
88+
y =
89+
3:
90+
Int @annot(x = 1,
91+
y =
92+
Array.apply[String](["World" : String]*)(
93+
scala.reflect.ClassTag.apply[String](classOf[String]))
94+
)
95+
) val z10: Int = 45
96+
val z11: Any @annot(annot) =
97+
f(
98+
{
99+
val y$5: Array[String] =
100+
Array.apply[String](["World" : String]*)(
101+
scala.reflect.ClassTag.apply[String](classOf[String]))
102+
new annot(x = 1, y = y$5)
103+
}
104+
)
105+
val z12: Any @annot(x = x, y = y) =
106+
f3(
107+
Array.apply[String](["World" : String]*)(
108+
scala.reflect.ClassTag.apply[String](classOf[String])),
109+
1)
110+
val z13: Any @annot(x = x, y = y) =
111+
{
112+
val y$6: Array[String] =
113+
Array.apply[String](["World" : String]*)(
114+
scala.reflect.ClassTag.apply[String](classOf[String]))
115+
f3(x = 1, y = y$6)
49116
}
50117
()
51118
}

tests/printing/dependent-annot-default-args.scala

+11-2
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,9 @@
11
class annot(x: Any, y: Any = 42) extends annotation.Annotation
22
class annot2(x: Any = -1, y: Array[Any] = Array("Hello")) extends annotation.Annotation
33

4-
def f(x: Int): Int @annot(x) = x
4+
def f(x: Any): Any @annot(x) = x
55
def f2(x: Int): Int @annot2(y = Array("Hello", x)) = x
6+
def f3(x: Any, y: Any): Any @annot(y=y, x=x) = x
67

78
def test =
89
val y: Int = ???
@@ -15,4 +16,12 @@ def test =
1516

1617
// Arguments are still lifted if the annotation class is instantiated
1718
// explicitly. See #22526.
18-
val z5 = new annot2(y = Array("World"), x = 1)
19+
val z5 = new annot(y = Array("World"), x = 1)
20+
val z6 = new annot2(y = Array("World"), x = 1)
21+
@annot(y = new annot(y = Array("World"), x = 1), x = 2) val z7 = 45
22+
@annot(y = 3: Int @annot(y = Array("World"), x = 1), x = 4) val z8 = 45
23+
val z9: Int @annot(y = new annot(y = Array("World"), x = 1), x = 2) = 46
24+
@annot(y = 3: Int @annot(y = Array("World"), x = 1), x = 4) val z10 = 45
25+
val z11 = f(new annot(y = Array("World"), x = 1))
26+
val z12 = f3(Array("World"), 1)
27+
val z13 = f3(y=Array("World"), x=1)

0 commit comments

Comments
 (0)