Skip to content

Commit d9ba132

Browse files
mbovelKacperFKorban
andcommitted
Fix betasty unpickling error with invalid annotations
Throw away erroneous trees to avoid unpickling issues in best-effort mode. Co-Authored-By: Kacper Korban <kacper.f.korban@gmail.com>
1 parent da97deb commit d9ba132

File tree

1 file changed

+33
-33
lines changed

1 file changed

+33
-33
lines changed

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

Lines changed: 33 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -1423,41 +1423,41 @@ trait Checking {
14231423
case Literal(_) => // ok
14241424
case _ =>
14251425
report.error(em"@${cls.name} needs a string literal as argument", arg.srcPos)
1426+
tree
14261427
case _ =>
1427-
if cls.isRetainsLike then () // Do not check @retain annotations
1428-
else if cls == defn.ThrowsAnnot then
1429-
// Do not check @throws annotations.
1430-
// TODO(mbovel): in tests/run/t6380.scala, an annotation tree is
1431-
// `new throws[Exception](throws.<init>[Exception])`. What is this?
1432-
()
1428+
if cls.isRetainsLike then tree
14331429
else
1434-
tpd.allTermArguments(tree).foreach(checkAnnotArg)
1435-
tree
1436-
1437-
private def checkAnnotArg(tree: Tree)(using Context): Unit =
1438-
def valid(t: Tree): Boolean =
1439-
t match
1440-
case _ if t.tpe.isEffectivelySingleton => true
1441-
case Literal(_) => true
1442-
// `_` is used as placeholder for unspecified arguments of Java
1443-
// annotations. Example: tests/run/java-ann-super-class
1444-
case Ident(nme.WILDCARD) => true
1445-
case Apply(fun, args) => valid(fun) && args.forall(valid)
1446-
case TypeApply(fun, args) => valid(fun)
1447-
case SeqLiteral(elems, _) => elems.forall(valid)
1448-
case Typed(expr, _) => valid(expr)
1449-
case NamedArg(_, arg) => valid(arg)
1450-
case Splice(_) => true
1451-
case Hole(_, _, _, _) => true
1452-
case _ => false
1453-
if !valid(tree) then
1454-
report.error(
1455-
i"""Implementation restriction: not a valid annotation argument.
1456-
|Argument: $tree
1457-
|Type: ${tree.tpe}""",
1458-
tree.srcPos
1459-
)
1460-
1430+
tpd.allTermArguments(tree).foldLeft(tree: Tree)((acc: Tree, arg: Tree) =>
1431+
if validAnnotArg(arg) then acc
1432+
else errorTree(
1433+
EmptyTree,
1434+
em"""Implementation restriction: not a valid annotation argument.
1435+
|Argument: $arg
1436+
|Type: ${arg.tpe}""",
1437+
arg.srcPos
1438+
)
1439+
)
1440+
1441+
private def validAnnotArg(t: Tree)(using Context): Boolean =
1442+
t match
1443+
case _ if t.tpe.isEffectivelySingleton => true
1444+
case Literal(_) => true
1445+
// `Ident(nme.WILDCARD)` is used as placeholder for unspecified
1446+
// arguments of Java annotations. Example: tests/run/java-ann-super-class.
1447+
case Ident(nme.WILDCARD) => true
1448+
case Apply(fun, args) => validAnnotArg(fun) && args.forall(validAnnotArg)
1449+
case TypeApply(fun, args) => validAnnotArg(fun)
1450+
case SeqLiteral(elems, _) => elems.forall(validAnnotArg)
1451+
case Typed(expr, _) => validAnnotArg(expr)
1452+
// TODO(mbovel): should probably be handled by `tpd.allTermArguments` instead.
1453+
case NamedArg(_, arg) => validAnnotArg(arg)
1454+
// TODO(mbovel): do we really want to allow `Splice` and `Hole`?
1455+
// When removing those cases, tests/pos-macros/i7519b.scala and
1456+
// tests/pos-macros/i7052.scala fail.
1457+
case Splice(_) => true
1458+
case Hole(_, _, _, _) => true
1459+
case _ => false
1460+
14611461
/** 1. Check that all case classes that extend `scala.reflect.Enum` are `enum` cases
14621462
* 2. Check that parameterised `enum` cases do not extend java.lang.Enum.
14631463
* 3. Check that only a static `enum` base class can extend java.lang.Enum.

0 commit comments

Comments
 (0)