Skip to content

Commit

Permalink
Handle nested explicitly constructed annotations
Browse files Browse the repository at this point in the history
  • Loading branch information
mbovel committed Feb 10, 2025
1 parent 5f8ae12 commit 7d1b62a
Show file tree
Hide file tree
Showing 3 changed files with 85 additions and 9 deletions.
4 changes: 2 additions & 2 deletions compiler/src/dotty/tools/dotc/typer/ProtoTypes.scala
Original file line number Diff line number Diff line change
Expand Up @@ -536,8 +536,8 @@ object ProtoTypes {
def typedArg(arg: untpd.Tree, formal: Type)(using Context): Tree = {
val wideFormal = formal.widenExpr
val argCtx =
if wideFormal eq formal then ctx
else ctx.withNotNullInfos(ctx.notNullInfos.retractMutables)
if wideFormal eq formal then ctx.retractMode(Mode.InAnnotation)
else ctx.retractMode(Mode.InAnnotation).withNotNullInfos(ctx.notNullInfos.retractMutables)
val locked = ctx.typerState.ownedVars
val targ = cacheTypedArg(arg,
typer.typedUnadapted(_, wideFormal, locked)(using argCtx),
Expand Down
77 changes: 72 additions & 5 deletions tests/printing/dependent-annot-default-args.check
Original file line number Diff line number Diff line change
Expand Up @@ -23,15 +23,16 @@ package <empty> {
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 f(x: Any): Any @annot(x) = x
def f2(x: Int):
Int @annot2(
y = Array.apply[Any](["Hello",x : Any]*)(scala.reflect.ClassTag.Any))
= x
def f3(x: Any, y: Any): Any @annot(x = x, y = y) = x
def test: Unit =
{
val y: Int = ???
val z: Int @annot(y) = f(y)
val z: Any @annot(y) = f(y)
val z2:
Int @annot2(
y = Array.apply[Any](["Hello",y : Any]*)(scala.reflect.ClassTag.Any)
Expand All @@ -41,11 +42,77 @@ package <empty> {
@annot2(
y = Array.apply[Any](["Hello",y : Any]*)(scala.reflect.ClassTag.Any))
val z4: Int = 45
val z5: annot2 =
val z5: annot =
{
val y$1: Array[Any] =
val y$1: Array[String] =
Array.apply[String](["World" : String]*)(
scala.reflect.ClassTag.apply[String](classOf[String]))
new annot(x = 1, y = y$1)
}
val z6: annot2 =
{
val y$2: Array[Any] =
Array.apply[Any](["World" : Any]*)(scala.reflect.ClassTag.Any)
new annot2(x = 1, y = y$1)
new annot2(x = 1, y = y$2)
}
@annot(x = 2,
y =
{
val y$3: Array[String] =
Array.apply[String](["World" : String]*)(
scala.reflect.ClassTag.apply[String](classOf[String]))
new annot(x = 1, y = y$3)
}
) val z7: Int = 45
@annot(x = 4,
y =
3:
Int @annot(x = 1,
y =
Array.apply[String](["World" : String]*)(
scala.reflect.ClassTag.apply[String](classOf[String]))
)
) val z8: Int = 45
val z9:
Int @annot(x = 2,
y =
{
val y$4: Array[String] =
Array.apply[String](["World" : String]*)(
scala.reflect.ClassTag.apply[String](classOf[String]))
new annot(x = 1, y = y$4)
}
)
= 46
@annot(x = 4,
y =
3:
Int @annot(x = 1,
y =
Array.apply[String](["World" : String]*)(
scala.reflect.ClassTag.apply[String](classOf[String]))
)
) val z10: Int = 45
val z11: Any @annot(annot) =
f(
{
val y$5: Array[String] =
Array.apply[String](["World" : String]*)(
scala.reflect.ClassTag.apply[String](classOf[String]))
new annot(x = 1, y = y$5)
}
)
val z12: Any @annot(x = x, y = y) =
f3(
Array.apply[String](["World" : String]*)(
scala.reflect.ClassTag.apply[String](classOf[String])),
1)
val z13: Any @annot(x = x, y = y) =
{
val y$6: Array[String] =
Array.apply[String](["World" : String]*)(
scala.reflect.ClassTag.apply[String](classOf[String]))
f3(x = 1, y = y$6)
}
()
}
Expand Down
13 changes: 11 additions & 2 deletions tests/printing/dependent-annot-default-args.scala
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
class annot(x: Any, y: Any = 42) extends annotation.Annotation
class annot2(x: Any = -1, y: Array[Any] = Array("Hello")) extends annotation.Annotation

def f(x: Int): Int @annot(x) = x
def f(x: Any): Any @annot(x) = x
def f2(x: Int): Int @annot2(y = Array("Hello", x)) = x
def f3(x: Any, y: Any): Any @annot(y=y, x=x) = x

def test =
val y: Int = ???
Expand All @@ -15,4 +16,12 @@ def test =

// Arguments are still lifted if the annotation class is instantiated
// explicitly. See #22526.
val z5 = new annot2(y = Array("World"), x = 1)
val z5 = new annot(y = Array("World"), x = 1)
val z6 = new annot2(y = Array("World"), x = 1)
@annot(y = new annot(y = Array("World"), x = 1), x = 2) val z7 = 45
@annot(y = 3: Int @annot(y = Array("World"), x = 1), x = 4) val z8 = 45
val z9: Int @annot(y = new annot(y = Array("World"), x = 1), x = 2) = 46
@annot(y = 3: Int @annot(y = Array("World"), x = 1), x = 4) val z10 = 45
val z11 = f(new annot(y = Array("World"), x = 1))
val z12 = f3(Array("World"), 1)
val z13 = f3(y=Array("World"), x=1)

0 comments on commit 7d1b62a

Please sign in to comment.