Skip to content

Commit dd3b668

Browse files
committed
Discard the full annotation tree if a sub-tree is invalid
1 parent 040ec39 commit dd3b668

File tree

2 files changed

+40
-42
lines changed

2 files changed

+40
-42
lines changed

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

Lines changed: 36 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -1425,48 +1425,46 @@ trait Checking {
14251425
report.error(em"@${cls.name} needs a string literal as argument", arg.srcPos)
14261426
tree
14271427
case _ =>
1428-
checkAnnotTreeMap.transform(tree)
1429-
1430-
private def checkAnnotTreeMap(using Context) =
1431-
new TreeMap:
1432-
override def transform(tree: Tree)(using Context): Tree =
1433-
tree match
1434-
case _ if tree.isType =>
1435-
super.transform(tree)
1436-
case _: ( EmptyTree.type
1437-
| Ident
1438-
| Select
1439-
| This
1440-
| Super
1441-
| Apply
1442-
| TypeApply
1443-
| Literal
1444-
| New
1445-
| Typed
1446-
| NamedArg
1447-
| Assign
1448-
| Block
1449-
| If
1450-
| Closure
1451-
| Return
1452-
| SeqLiteral
1453-
| Inlined
1454-
| Quote
1455-
| Splice
1456-
| Hole
1457-
| ValDef
1458-
| DefDef
1459-
| Annotated) =>
1460-
super.transform(tree)
1461-
case _ =>
1428+
tree.find(!isValidAnnotSubtree(_)) match
1429+
case None => tree
1430+
case Some(invalidSubTree) =>
14621431
errorTree(
14631432
EmptyTree,
1464-
em"""Implementation restriction: this tree cannot be used in an annotation.
1465-
|Tree: ${tree}
1466-
|Type: ${tree.tpe}""",
1467-
tree.srcPos
1433+
em"""Implementation restriction: expression cannot be used inside an annotation argument.
1434+
|Tree: ${invalidSubTree}
1435+
|Type: ${invalidSubTree.tpe}""",
1436+
invalidSubTree.srcPos
14681437
)
14691438

1439+
/** Returns `true` if this tree can appear inside an annotation argument. */
1440+
private def isValidAnnotSubtree(subTree: Tree) =
1441+
subTree.isType || subTree.isInstanceOf[
1442+
EmptyTree.type
1443+
| Ident
1444+
| Select
1445+
| This
1446+
| Super
1447+
| Apply
1448+
| TypeApply
1449+
| Literal
1450+
| New
1451+
| Typed
1452+
| NamedArg
1453+
| Assign
1454+
| Block
1455+
| If
1456+
| Closure
1457+
| Return
1458+
| SeqLiteral
1459+
| Inlined
1460+
| Quote
1461+
| Splice
1462+
| Hole
1463+
| ValDef
1464+
| DefDef
1465+
| Annotated
1466+
]
1467+
14701468
/** 1. Check that all case classes that extend `scala.reflect.Enum` are `enum` cases
14711469
* 2. Check that parameterised `enum` cases do not extend java.lang.Enum.
14721470
* 3. Check that only a static `enum` base class can extend java.lang.Enum.

tests/neg/annot-invalid.check

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,24 @@
11
-- Error: tests/neg/annot-invalid.scala:4:21 ---------------------------------------------------------------------------
22
4 | val x1: Int @annot(new Object {}) = 0 // error
33
| ^^^^^^^^^^^^^
4-
| Implementation restriction: this tree cannot be used in an annotation.
4+
| Implementation restriction: expression cannot be used inside an annotation argument.
55
| Tree: final class $anon() extends Object() {}
66
| Type: Object {...}
77
-- Error: tests/neg/annot-invalid.scala:5:28 ---------------------------------------------------------------------------
88
5 | val x2: Int @annot({class C}) = 0 // error
99
| ^^^^^^^
10-
| Implementation restriction: this tree cannot be used in an annotation.
10+
| Implementation restriction: expression cannot be used inside an annotation argument.
1111
| Tree: class C() extends Object() {}
1212
| Type: C
1313
-- Error: tests/neg/annot-invalid.scala:7:9 ----------------------------------------------------------------------------
1414
7 | @annot(new Object {}) val y1: Int = 0 // error
1515
| ^^^^^^^^^^^^^
16-
| Implementation restriction: this tree cannot be used in an annotation.
16+
| Implementation restriction: expression cannot be used inside an annotation argument.
1717
| Tree: final class $anon() extends Object() {}
1818
| Type: Object {...}
1919
-- Error: tests/neg/annot-invalid.scala:8:16 ---------------------------------------------------------------------------
2020
8 | @annot({class C}) val y2: Int = 0 // error
2121
| ^^^^^^^
22-
| Implementation restriction: this tree cannot be used in an annotation.
22+
| Implementation restriction: expression cannot be used inside an annotation argument.
2323
| Tree: class C() extends Object() {}
2424
| Type: C

0 commit comments

Comments
 (0)