diff --git a/compiler/src/dotty/tools/dotc/typer/Typer.scala b/compiler/src/dotty/tools/dotc/typer/Typer.scala index e35b0c9bba68..4bbb8f472a0f 100644 --- a/compiler/src/dotty/tools/dotc/typer/Typer.scala +++ b/compiler/src/dotty/tools/dotc/typer/Typer.scala @@ -1767,8 +1767,19 @@ class Typer extends Namer if (ctx.mode is Mode.Type) assignType(cpy.Annotated(tree)(arg1, annot1), arg1, annot1) else { + val arg2 = arg1 match { + case Typed(arg2, tpt: TypeTree) => + tpt.tpe match { + case _: AnnotatedType => + // Avoid creating a Typed tree for each type annotation that is added. + // Drop the outer Typed tree and use its type with the addition all annotation. + arg2 + case _ => arg1 + } + case _ => arg1 + } val tpt = TypeTree(AnnotatedType(arg1.tpe.widenIfUnstable, Annotation(annot1))) - assignType(cpy.Typed(tree)(arg1, tpt), tpt) + assignType(cpy.Typed(tree)(arg2, tpt), tpt) } } diff --git a/compiler/test/dotc/pos-recompilation.whitelist b/compiler/test/dotc/pos-recompilation.whitelist index 6a2d586579f5..90b0cfb3314b 100644 --- a/compiler/test/dotc/pos-recompilation.whitelist +++ b/compiler/test/dotc/pos-recompilation.whitelist @@ -288,6 +288,7 @@ i4526a i4526b i4586 i4623 +i4678 i4720 i4773 i4774a diff --git a/language-server/test/dotty/tools/languageserver/HoverTest.scala b/language-server/test/dotty/tools/languageserver/HoverTest.scala index b78ea0e3163a..f7de9d1e7da9 100644 --- a/language-server/test/dotty/tools/languageserver/HoverTest.scala +++ b/language-server/test/dotty/tools/languageserver/HoverTest.scala @@ -174,4 +174,18 @@ class HoverTest { |}""".withSource .hover(m1 to m2, hoverContent("Int", "hello")) } + + @Test def i4678: Unit = { + code"""class Foo { + | val x: Int = (${m1}1:${m2} ${m3}@annot1 @annot2 @annot3 @annot4 @annot5${m4}) + |} + |class annot1 extends scala.annotation.Annotation + |class annot2 extends scala.annotation.Annotation + |class annot3 extends scala.annotation.Annotation + |class annot4 extends scala.annotation.Annotation + |class annot5 extends scala.annotation.Annotation + |""".withSource + .hover(m1 to m2, hoverContent("Int(1)")) + .hover(m3 to m4, hoverContent("Int(1) @annot1 @annot2 @annot3 @annot4 @annot5")) + } } diff --git a/tests/pos/i4678.decompiled b/tests/pos/i4678.decompiled new file mode 100644 index 000000000000..3d85df1ff562 --- /dev/null +++ b/tests/pos/i4678.decompiled @@ -0,0 +1,14 @@ +/** Decompiled from out/posTestFromTasty/pos/i4678/Foo.tasty */ +class Foo() { + val x: scala.Int = (1: @annot1() @annot2() @annot3() @annot4() @annot5()) +} +/** Decompiled from out/posTestFromTasty/pos/i4678/annot1.tasty */ +class annot1() extends scala.annotation.Annotation() +/** Decompiled from out/posTestFromTasty/pos/i4678/annot2.tasty */ +class annot2() extends scala.annotation.Annotation() +/** Decompiled from out/posTestFromTasty/pos/i4678/annot3.tasty */ +class annot3() extends scala.annotation.Annotation() +/** Decompiled from out/posTestFromTasty/pos/i4678/annot4.tasty */ +class annot4() extends scala.annotation.Annotation() +/** Decompiled from out/posTestFromTasty/pos/i4678/annot5.tasty */ +class annot5() extends scala.annotation.Annotation() \ No newline at end of file diff --git a/tests/pos/i4678.scala b/tests/pos/i4678.scala new file mode 100644 index 000000000000..51b6785dda83 --- /dev/null +++ b/tests/pos/i4678.scala @@ -0,0 +1,9 @@ +class Foo { + val x: Int = (1: @annot1 @annot2 @annot3 @annot4 @annot5) +} + +class annot1 extends scala.annotation.Annotation +class annot2 extends scala.annotation.Annotation +class annot3 extends scala.annotation.Annotation +class annot4 extends scala.annotation.Annotation +class annot5 extends scala.annotation.Annotation