Skip to content

Emit an error instead of crashing on incorrect Java annotation; support non-literal annot params with constant types #7495

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Nov 7, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 9 additions & 5 deletions compiler/src/dotty/tools/backend/jvm/DottyBackendInterface.scala
Original file line number Diff line number Diff line change
Expand Up @@ -240,7 +240,11 @@ class DottyBackendInterface(outputDirectory: AbstractFile, val superCallsMap: Ma
private def emitArgument(av: AnnotationVisitor,
name: String,
arg: Tree, bcodeStore: BCodeHelpers)(innerClasesStore: bcodeStore.BCInnerClassGen): Unit = {
(normalizeArgument(arg): @unchecked) match {
val narg = normalizeArgument(arg)
// Transformation phases are not run on annotation trees, so we need to run
// `constToLiteral` at this point.
val t = constToLiteral(narg)(ctx.withPhase(ctx.erasurePhase))
t match {
case Literal(const @ Constant(_)) =>
const.tag match {
case BooleanTag | ByteTag | ShortTag | CharTag | IntTag | LongTag | FloatTag | DoubleTag => av.visit(name, const.value)
Expand All @@ -257,10 +261,7 @@ class DottyBackendInterface(outputDirectory: AbstractFile, val superCallsMap: Ma
av.visit(name, t.args.head.tpe.classSymbol.denot.info.toTypeKind(bcodeStore)(innerClasesStore).toASMType)
case Ident(nme.WILDCARD) =>
// An underscore argument indicates that we want to use the default value for this parameter, so do not emit anything
case t: tpd.RefTree =>
assert(t.symbol.denot.owner.isAllOf(Flags.JavaEnumTrait),
i"not an enum: $t / ${t.symbol} / ${t.symbol.denot.owner} / ${t.symbol.denot.owner.isTerm} / ${t.symbol.denot.owner.flagsString}")

case t: tpd.RefTree if t.symbol.denot.owner.isAllOf(Flags.JavaEnumTrait) =>
val edesc = innerClasesStore.typeDescriptor(t.tpe.asInstanceOf[bcodeStore.int.Type]) // the class descriptor of the enumeration class.
val evalue = t.symbol.name.mangledString // value the actual enumeration value.
av.visitEnum(name, edesc, evalue)
Expand Down Expand Up @@ -306,6 +307,9 @@ class DottyBackendInterface(outputDirectory: AbstractFile, val superCallsMap: Ma
val desc = innerClasesStore.typeDescriptor(typ.asInstanceOf[bcodeStore.int.Type]) // the class descriptor of the nested annotation class
val nestedVisitor = av.visitAnnotation(name, desc)
emitAssocs(nestedVisitor, assocs, bcodeStore)(innerClasesStore)

case t =>
ctx.error(ex"Annotation argument is not a constant", t.sourcePos)
}
}

Expand Down
6 changes: 6 additions & 0 deletions tests/neg/java-annot/J.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
import java.lang.annotation.*;

@Retention(RetentionPolicy.RUNTIME)
public @interface J {
String value();
}
8 changes: 8 additions & 0 deletions tests/neg/java-annot/S.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
object C {
val cs: String = "cs"
}

object S {
@J(C.cs) // error: Annotation argument is not a constant
def f(): Int = 1
}
6 changes: 6 additions & 0 deletions tests/pos/java-annot/J.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
import java.lang.annotation.*;

@Retention(RetentionPolicy.RUNTIME)
public @interface J {
String value();
}
8 changes: 8 additions & 0 deletions tests/pos/java-annot/S.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
object C {
val cs: "cs" = "cs"
}

object S {
@J(C.cs) // OK: cs is a constant
def f(): Int = 1
}