Skip to content

Commit 3cb78b7

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

File tree

2 files changed

+21
-1
lines changed

2 files changed

+21
-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

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)