From 8c37715240b04e8150c66c42b41ee7068e90e1d4 Mon Sep 17 00:00:00 2001 From: Som Snytt Date: Wed, 23 Apr 2025 07:49:28 -0700 Subject: [PATCH] Keep unused annot on params --- .../src/dotty/tools/dotc/transform/PostTyper.scala | 6 ++++-- tests/warn/i23033.scala | 13 +++++++++++++ 2 files changed, 17 insertions(+), 2 deletions(-) create mode 100644 tests/warn/i23033.scala diff --git a/compiler/src/dotty/tools/dotc/transform/PostTyper.scala b/compiler/src/dotty/tools/dotc/transform/PostTyper.scala index 3ca67c5798fe..a7d1e70ea90e 100644 --- a/compiler/src/dotty/tools/dotc/transform/PostTyper.scala +++ b/compiler/src/dotty/tools/dotc/transform/PostTyper.scala @@ -247,8 +247,10 @@ class PostTyper extends MacroTransform with InfoTransformer { thisPhase => if sym.is(Param) then registerIfUnrolledParam(sym) // @unused is getter/setter but we want it on ordinary method params - if !sym.owner.is(Method) || sym.owner.isConstructor then - sym.keepAnnotationsCarrying(thisPhase, Set(defn.ParamMetaAnnot), orNoneOf = defn.NonBeanMetaAnnots) + // @param should be consulted only for fields + val unusing = sym.getAnnotation(defn.UnusedAnnot) + sym.keepAnnotationsCarrying(thisPhase, Set(defn.ParamMetaAnnot), orNoneOf = defn.NonBeanMetaAnnots) + unusing.foreach(sym.addAnnotation) else if sym.is(ParamAccessor) then // @publicInBinary is not a meta-annotation and therefore not kept by `keepAnnotationsCarrying` val publicInBinaryAnnotOpt = sym.getAnnotation(defn.PublicInBinaryAnnot) diff --git a/tests/warn/i23033.scala b/tests/warn/i23033.scala new file mode 100644 index 000000000000..6170664e05e2 --- /dev/null +++ b/tests/warn/i23033.scala @@ -0,0 +1,13 @@ +//> using options -Werror -Wunused:all + +import scala.annotation.unused +import scala.concurrent.ExecutionContext +import scala.util.NotGiven + +object Test { + given [T](using @unused ev: NotGiven[T <:< Int]): AnyRef with {} +} +object Useful: + given [T](using @unused ec: ExecutionContext): AnyRef with {} +object Syntax: + given [T] => (@unused ec: ExecutionContext) => AnyRef