From 8fafdad41e075fd42877ca5086094cf8ba6ed6e0 Mon Sep 17 00:00:00 2001 From: Nicolas Stucki Date: Mon, 1 Nov 2021 16:22:57 +0100 Subject: [PATCH] Keep annotation of @main methods in `def main` Co-authored-by: Guillaume Martres --- compiler/src/dotty/tools/dotc/ast/MainProxies.scala | 12 ++++++++++++ tests/neg-custom-args/no-experimental/i13848.scala | 7 +++++++ tests/pos-custom-args/no-experimental/i13848.scala | 8 ++++++++ 3 files changed, 27 insertions(+) create mode 100644 tests/neg-custom-args/no-experimental/i13848.scala create mode 100644 tests/pos-custom-args/no-experimental/i13848.scala diff --git a/compiler/src/dotty/tools/dotc/ast/MainProxies.scala b/compiler/src/dotty/tools/dotc/ast/MainProxies.scala index 9178478c399b..2eafeca16e39 100644 --- a/compiler/src/dotty/tools/dotc/ast/MainProxies.scala +++ b/compiler/src/dotty/tools/dotc/ast/MainProxies.scala @@ -94,8 +94,20 @@ object MainProxies { val body = Try(call, handler :: Nil, EmptyTree) val mainArg = ValDef(nme.args, TypeTree(defn.ArrayType.appliedTo(defn.StringType)), EmptyTree) .withFlags(Param) + /** Replace typed `Ident`s that have been typed with a TypeSplice with the reference to the symbol. + * The annotations will be retype-checked in another scope that may not have the same imports. + */ + def insertTypeSplices = new TreeMap { + override def transform(tree: Tree)(using Context): Tree = tree match + case tree: tpd.Ident @unchecked => TypedSplice(tree) + case tree => super.transform(tree) + } + val annots = mainFun.annotations + .filterNot(_.matches(defn.MainAnnot)) + .map(annot => insertTypeSplices.transform(annot.tree)) val mainMeth = DefDef(nme.main, (mainArg :: Nil) :: Nil, TypeTree(defn.UnitType), body) .withFlags(JavaStatic) + .withAnnotations(annots) val mainTempl = Template(emptyConstructor, Nil, Nil, EmptyValDef, mainMeth :: Nil) val mainCls = TypeDef(mainFun.name.toTypeName, mainTempl) .withFlags(Final | Invisible) diff --git a/tests/neg-custom-args/no-experimental/i13848.scala b/tests/neg-custom-args/no-experimental/i13848.scala new file mode 100644 index 000000000000..886ab1e85d67 --- /dev/null +++ b/tests/neg-custom-args/no-experimental/i13848.scala @@ -0,0 +1,7 @@ +import annotation.experimental + +@main +def run(): Unit = f // error + +@experimental +def f = 2 diff --git a/tests/pos-custom-args/no-experimental/i13848.scala b/tests/pos-custom-args/no-experimental/i13848.scala new file mode 100644 index 000000000000..8b65ccb078e1 --- /dev/null +++ b/tests/pos-custom-args/no-experimental/i13848.scala @@ -0,0 +1,8 @@ +import annotation.experimental + +@main +@experimental +def run(): Unit = f + +@experimental +def f = 2