Skip to content

Commit 4ecbb60

Browse files
authored
Merge pull request #5714 from dotty-staging/fix-#4678
Fix #4678: Create single Typed tree with multiple type annotations
2 parents 6156757 + c676585 commit 4ecbb60

File tree

5 files changed

+50
-1
lines changed

5 files changed

+50
-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
@@ -1768,8 +1768,19 @@ class Typer extends Namer
17681768
if (ctx.mode is Mode.Type)
17691769
assignType(cpy.Annotated(tree)(arg1, annot1), arg1, annot1)
17701770
else {
1771+
val arg2 = arg1 match {
1772+
case Typed(arg2, tpt: TypeTree) =>
1773+
tpt.tpe match {
1774+
case _: AnnotatedType =>
1775+
// Avoid creating a Typed tree for each type annotation that is added.
1776+
// Drop the outer Typed tree and use its type with the addition all annotation.
1777+
arg2
1778+
case _ => arg1
1779+
}
1780+
case _ => arg1
1781+
}
17711782
val tpt = TypeTree(AnnotatedType(arg1.tpe.widenIfUnstable, Annotation(annot1)))
1772-
assignType(cpy.Typed(tree)(arg1, tpt), tpt)
1783+
assignType(cpy.Typed(tree)(arg2, tpt), tpt)
17731784
}
17741785
}
17751786

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

language-server/test/dotty/tools/languageserver/HoverTest.scala

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -174,4 +174,18 @@ class HoverTest {
174174
|}""".withSource
175175
.hover(m1 to m2, hoverContent("Int", "hello"))
176176
}
177+
178+
@Test def i4678: Unit = {
179+
code"""class Foo {
180+
| val x: Int = (${m1}1:${m2} ${m3}@annot1 @annot2 @annot3 @annot4 @annot5${m4})
181+
|}
182+
|class annot1 extends scala.annotation.Annotation
183+
|class annot2 extends scala.annotation.Annotation
184+
|class annot3 extends scala.annotation.Annotation
185+
|class annot4 extends scala.annotation.Annotation
186+
|class annot5 extends scala.annotation.Annotation
187+
|""".withSource
188+
.hover(m1 to m2, hoverContent("Int(1)"))
189+
.hover(m3 to m4, hoverContent("Int(1) @annot1 @annot2 @annot3 @annot4 @annot5"))
190+
}
177191
}

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)