diff options
-rw-r--r-- | src/compiler/scala/tools/nsc/typechecker/Typers.scala | 3 | ||||
-rw-r--r-- | test/files/neg/annot-nonconst.check | 14 | ||||
-rw-r--r-- | test/files/neg/annot-nonconst.scala | 2 | ||||
-rw-r--r-- | test/files/run/names-defaults.scala | 3 |
4 files changed, 17 insertions, 5 deletions
diff --git a/src/compiler/scala/tools/nsc/typechecker/Typers.scala b/src/compiler/scala/tools/nsc/typechecker/Typers.scala index 2d2cec697a..f576c17ad1 100644 --- a/src/compiler/scala/tools/nsc/typechecker/Typers.scala +++ b/src/compiler/scala/tools/nsc/typechecker/Typers.scala @@ -2301,7 +2301,8 @@ trait Typers { self: Analyzer => case Typed(t, _) => tree2ConstArg(t, pt) case tree => typed(tree, EXPRmode, pt) match { - case l @ Literal(c) if !l.isErroneous => + // null cannot be used as constant value for classfile annotations + case l @ Literal(c) if !(l.isErroneous || c.value == null) => Some(LiteralAnnotArg(c)) case _ => needConst(tree) diff --git a/test/files/neg/annot-nonconst.check b/test/files/neg/annot-nonconst.check index bc6a0d2c48..23429bb11a 100644 --- a/test/files/neg/annot-nonconst.check +++ b/test/files/neg/annot-nonconst.check @@ -3,8 +3,16 @@ make your annotation visible at runtime. If that is what you want, you must write the annotation class in Java. class Length(value: Int) extends ClassfileAnnotation ^ -annot-nonconst.scala:5: error: annotation argument needs to be a constant; found: n +annot-nonconst.scala:2: warning: implementation restriction: subclassing Classfile does not +make your annotation visible at runtime. If that is what +you want, you must write the annotation class in Java. +class Ann2(value: String) extends ClassfileAnnotation + ^ +annot-nonconst.scala:6: error: annotation argument needs to be a constant; found: n @Length(n) def foo = "foo" ^ -one warning found -one error found +annot-nonconst.scala:7: error: annotation argument needs to be a constant; found: null + @Ann2(null) def bar = "bar" + ^ +two warnings found +two errors found diff --git a/test/files/neg/annot-nonconst.scala b/test/files/neg/annot-nonconst.scala index f9d044a041..69bb60d34e 100644 --- a/test/files/neg/annot-nonconst.scala +++ b/test/files/neg/annot-nonconst.scala @@ -1,6 +1,8 @@ class Length(value: Int) extends ClassfileAnnotation +class Ann2(value: String) extends ClassfileAnnotation object Test { def n = 15 @Length(n) def foo = "foo" + @Ann2(null) def bar = "bar" } diff --git a/test/files/run/names-defaults.scala b/test/files/run/names-defaults.scala index 162c0bc43e..91f72e3e3c 100644 --- a/test/files/run/names-defaults.scala +++ b/test/files/run/names-defaults.scala @@ -282,10 +282,11 @@ class B1 extends A1 { } trait N { - def foo[T >: String](x: Int = -1, y: T = "jupee")(z: String): String + def foo[T >: String](x: Int = -1, y: T = "jupee")(z: String): Object } abstract class M extends N { + // also tests #2116, specialize return type when overriding. def foo[T >: String](x: Int, y: T)(z: String = "1"): String def bar(n: Int, m: Double = 1.239): Double } |