Skip to content

Commit fab9114

Browse files
committed
Fix #4678: Create single Typed tree with multiple type annotations
This is mostly useful for printers as it represents what is written in the source. I may also slightly improve performance of later phases.
1 parent 615fdc9 commit fab9114

File tree

4 files changed

+36
-1
lines changed

4 files changed

+36
-1
lines changed

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

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1767,8 +1767,19 @@ class Typer extends Namer
17671767
if (ctx.mode is Mode.Type)
17681768
assignType(cpy.Annotated(tree)(arg1, annot1), arg1, annot1)
17691769
else {
1770+
val arg2 = arg1 match {
1771+
case Typed(arg2, tpt: TypeTree) =>
1772+
tpt.tpe match {
1773+
case _: AnnotatedType =>
1774+
// Avoid creating a Typed tree for each type annotation that is added.
1775+
// Drop the outer Typed tree and use its type with the addition all annotation.
1776+
arg2
1777+
case _ => arg1
1778+
}
1779+
case _ => arg1
1780+
}
17701781
val tpt = TypeTree(AnnotatedType(arg1.tpe.widenIfUnstable, Annotation(annot1)))
1771-
assignType(cpy.Typed(tree)(arg1, tpt), tpt)
1782+
assignType(cpy.Typed(tree)(arg2, tpt), tpt)
17721783
}
17731784
}
17741785

compiler/test/dotc/pos-recompilation.whitelist

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -288,6 +288,7 @@ i4526a
288288
i4526b
289289
i4586
290290
i4623
291+
i4678
291292
i4720
292293
i4773
293294
i4774a

tests/pos/i4678.decompiled

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
/** Decompiled from out/posTestFromTasty/pos/i4678/Foo.tasty */
2+
class Foo() {
3+
val x: scala.Int = (1: @annot1() @annot2() @annot3() @annot4() @annot5())
4+
}
5+
/** Decompiled from out/posTestFromTasty/pos/i4678/annot1.tasty */
6+
class annot1() extends scala.annotation.Annotation()
7+
/** Decompiled from out/posTestFromTasty/pos/i4678/annot2.tasty */
8+
class annot2() extends scala.annotation.Annotation()
9+
/** Decompiled from out/posTestFromTasty/pos/i4678/annot3.tasty */
10+
class annot3() extends scala.annotation.Annotation()
11+
/** Decompiled from out/posTestFromTasty/pos/i4678/annot4.tasty */
12+
class annot4() extends scala.annotation.Annotation()
13+
/** Decompiled from out/posTestFromTasty/pos/i4678/annot5.tasty */
14+
class annot5() extends scala.annotation.Annotation()

tests/pos/i4678.scala

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
class Foo {
2+
val x: Int = (1: @annot1 @annot2 @annot3 @annot4 @annot5)
3+
}
4+
5+
class annot1 extends scala.annotation.Annotation
6+
class annot2 extends scala.annotation.Annotation
7+
class annot3 extends scala.annotation.Annotation
8+
class annot4 extends scala.annotation.Annotation
9+
class annot5 extends scala.annotation.Annotation

0 commit comments

Comments
 (0)