Skip to content

Commit 924ea08

Browse files
committed
Reject all trees that define symbols in annotations
1 parent dd3b668 commit 924ea08

File tree

14 files changed

+88
-80
lines changed

14 files changed

+88
-80
lines changed

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

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1452,16 +1452,11 @@ trait Checking {
14521452
| NamedArg
14531453
| Assign
14541454
| Block
1455-
| If
1456-
| Closure
1457-
| Return
14581455
| SeqLiteral
14591456
| Inlined
14601457
| Quote
14611458
| Splice
14621459
| Hole
1463-
| ValDef
1464-
| DefDef
14651460
| Annotated
14661461
]
14671462

tests/neg/annot-invalid.check

Lines changed: 60 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -10,15 +10,63 @@
1010
| Implementation restriction: expression cannot be used inside an annotation argument.
1111
| Tree: class C() extends Object() {}
1212
| Type: C
13-
-- Error: tests/neg/annot-invalid.scala:7:9 ----------------------------------------------------------------------------
14-
7 | @annot(new Object {}) val y1: Int = 0 // error
15-
| ^^^^^^^^^^^^^
16-
| Implementation restriction: expression cannot be used inside an annotation argument.
17-
| Tree: final class $anon() extends Object() {}
18-
| Type: Object {...}
19-
-- Error: tests/neg/annot-invalid.scala:8:16 ---------------------------------------------------------------------------
20-
8 | @annot({class C}) val y2: Int = 0 // error
21-
| ^^^^^^^
22-
| Implementation restriction: expression cannot be used inside an annotation argument.
23-
| Tree: class C() extends Object() {}
24-
| Type: C
13+
-- Error: tests/neg/annot-invalid.scala:6:27 ---------------------------------------------------------------------------
14+
6 | val x16: Int @annot({val y: Int = 2}) = 0 // error
15+
| ^^^^^^^^^^^^^^
16+
| Implementation restriction: expression cannot be used inside an annotation argument.
17+
| Tree: val y: Int = 2
18+
| Type: (y : Int)
19+
-- Error: tests/neg/annot-invalid.scala:7:27 ---------------------------------------------------------------------------
20+
7 | val x17: Int @annot({def f = 2}) = 0 // error
21+
| ^^^^^^^^^
22+
| Implementation restriction: expression cannot be used inside an annotation argument.
23+
| Tree: def f: Int = 2
24+
| Type: (f : => Int)
25+
-- Error: tests/neg/annot-invalid.scala:8:31 ---------------------------------------------------------------------------
26+
8 | val x18: Int @annot((x: Int) => x) = 0 // error
27+
| ^^^^^^^^^^^^^
28+
| Implementation restriction: expression cannot be used inside an annotation argument.
29+
| Tree: closure($anonfun)
30+
| Type: Int => Int
31+
-- [E006] Not Found Error: tests/neg/annot-invalid.scala:9:22 ----------------------------------------------------------
32+
9 | val x19: Int @annot(O.g) = 0 // error
33+
| ^
34+
| Not found: O
35+
|
36+
| longer explanation available when compiling with `-explain`
37+
-- Error: tests/neg/annot-invalid.scala:11:9 ---------------------------------------------------------------------------
38+
11 | @annot(new Object {}) val y1: Int = 0 // error
39+
| ^^^^^^^^^^^^^
40+
| Implementation restriction: expression cannot be used inside an annotation argument.
41+
| Tree: final class $anon() extends Object() {}
42+
| Type: Object {...}
43+
-- Error: tests/neg/annot-invalid.scala:12:16 --------------------------------------------------------------------------
44+
12 | @annot({class C}) val y2: Int = 0 // error
45+
| ^^^^^^^
46+
| Implementation restriction: expression cannot be used inside an annotation argument.
47+
| Tree: class C() extends Object() {}
48+
| Type: C
49+
-- Error: tests/neg/annot-invalid.scala:13:14 --------------------------------------------------------------------------
50+
13 | @annot({val y: Int = 2}) val y16: Int = 0 // error
51+
| ^^^^^^^^^^^^^^
52+
| Implementation restriction: expression cannot be used inside an annotation argument.
53+
| Tree: val y: Int = 2
54+
| Type: (y : Int)
55+
-- Error: tests/neg/annot-invalid.scala:14:14 --------------------------------------------------------------------------
56+
14 | @annot({def f = 2}) val y17: Int = 0 // error
57+
| ^^^^^^^^^
58+
| Implementation restriction: expression cannot be used inside an annotation argument.
59+
| Tree: def f: Int = 2
60+
| Type: (f : => Int)
61+
-- Error: tests/neg/annot-invalid.scala:15:18 --------------------------------------------------------------------------
62+
15 | @annot((x: Int) => x) val y18: Int = 0 // error
63+
| ^^^^^^^^^^^^^
64+
| Implementation restriction: expression cannot be used inside an annotation argument.
65+
| Tree: closure($anonfun)
66+
| Type: Int => Int
67+
-- [E006] Not Found Error: tests/neg/annot-invalid.scala:16:9 ----------------------------------------------------------
68+
16 | @annot(O.g) val y19: Int = 0 // error
69+
| ^
70+
| Not found: O
71+
|
72+
| longer explanation available when compiling with `-explain`

tests/neg/annot-invalid.scala

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,16 @@ class annot[T](arg: T) extends scala.annotation.Annotation
33
def main =
44
val x1: Int @annot(new Object {}) = 0 // error
55
val x2: Int @annot({class C}) = 0 // error
6+
val x16: Int @annot({val y: Int = 2}) = 0 // error
7+
val x17: Int @annot({def f = 2}) = 0 // error
8+
val x18: Int @annot((x: Int) => x) = 0 // error
9+
val x19: Int @annot(O.g) = 0 // error
610

711
@annot(new Object {}) val y1: Int = 0 // error
812
@annot({class C}) val y2: Int = 0 // error
13+
@annot({val y: Int = 2}) val y16: Int = 0 // error
14+
@annot({def f = 2}) val y17: Int = 0 // error
15+
@annot((x: Int) => x) val y18: Int = 0 // error
16+
@annot(O.g) val y19: Int = 0 // error
917

1018
()

tests/neg/i15054.scala

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
import scala.annotation.Annotation
2+
3+
class AnAnnotation(function: Int => String) extends Annotation
4+
5+
@AnAnnotation(_.toString) // error: not a valid annotation
6+
val a = 1
7+
@AnAnnotation(_.toString.length.toString) // error: not a valid annotation
8+
val b = 2
9+
10+
def test =
11+
@AnAnnotation(_.toString) // error: not a valid annotation
12+
val a = 1
13+
@AnAnnotation(_.toString.length.toString) // error: not a valid annotation
14+
val b = 2
15+
a + b

tests/neg/i7740a.scala

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
class A(a: Any) extends annotation.StaticAnnotation
2+
@A({val x = 0}) trait B // error: not a valid annotation

tests/neg/i7740b.scala

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
class A(a: Any) extends annotation.StaticAnnotation
2+
@A({def x = 0}) trait B // error: not a valid annotation

tests/pos/annot-17939b.scala

Lines changed: 0 additions & 10 deletions
This file was deleted.

tests/pos/annot-boundtype.scala

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,3 +13,4 @@ def f2(t: Tuple) =
1313
t match
1414
case _: (t *: ts) => ()
1515
case _ => ()
16+

tests/pos/annot-valid.scala

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -22,10 +22,6 @@ def main =
2222
val x13: Int @annot(throw new Error()) = 0
2323
val x14: Int @annot(42: Double) = 0
2424
val x15: Int @annot(O.g(2)) = 0
25-
val x16: Int @annot({val y: Int = 2}) = 0
26-
val x17: Int @annot({def f = 2}) = 0
27-
val x18: Int @annot((x: Int) => x) = 0
28-
val x19: Int @annot(O.g) = 0
2925

3026
@annot(42) val y1: Int = 0
3127
@annot("hello") val y2: Int = 0
@@ -42,9 +38,5 @@ def main =
4238
@annot(throw new Error()) val y13: Int = 0
4339
@annot(42: Double) val y14: Int = 0
4440
@annot(O.g(2)) val y15: Int = 0
45-
@annot({val y: Int = 2}) val y16: Int = 0
46-
@annot({def f = 2}) val y17: Int = 0
47-
@annot((x: Int) => x) val y18: Int = 0
48-
@annot(O.g) val y19: Int = 0
4941

5042
()

tests/pos/i15054.scala

Lines changed: 0 additions & 15 deletions
This file was deleted.

0 commit comments

Comments
 (0)