From b2ca0efb2d5e20dd7c77fedc16590164c78ba603 Mon Sep 17 00:00:00 2001 From: Paul Phillips Date: Thu, 31 Mar 2011 03:59:42 +0000 Subject: Modified some typer logic to allow annotation a... Modified some typer logic to allow annotation arguments for constants which don't have the form Literal(_). The current logic seems to be avoided most of the time, but scaladoc breaks when it runs into it. This closes #4301. I can't figure out from the ticket what the deal is with #2764 and what is presently happening, but it seems like this patch could only improve the situation. Review by rytz. --- .../scala/tools/nsc/typechecker/Typers.scala | 25 +++++++++++++++------- 1 file changed, 17 insertions(+), 8 deletions(-) (limited to 'src') diff --git a/src/compiler/scala/tools/nsc/typechecker/Typers.scala b/src/compiler/scala/tools/nsc/typechecker/Typers.scala index 08d5f0e12d..e797acc2c5 100644 --- a/src/compiler/scala/tools/nsc/typechecker/Typers.scala +++ b/src/compiler/scala/tools/nsc/typechecker/Typers.scala @@ -2523,13 +2523,22 @@ trait Typers extends Modes { /** Calling constfold right here is necessary because some trees (negated * floats and literals in particular) are not yet folded. */ - def tryConst(tr: Tree, pt: Type) = typed(constfold(tr), EXPRmode, pt) match { - // null cannot be used as constant value for classfile annotations - case l @ Literal(c) if !(l.isErroneous || c.value == null) => - Some(LiteralAnnotArg(c)) - case _ => - error(tr.pos, "annotation argument needs to be a constant; found: "+tr) - None + def tryConst(tr: Tree, pt: Type): Option[LiteralAnnotArg] = { + val const: Constant = typed(constfold(tr), EXPRmode, pt) match { + case l @ Literal(c) if !l.isErroneous => c + case tree => tree.tpe match { + case ConstantType(c) => c + case tpe => null + } + } + def fail(msg: String) = { error(tr.pos, msg) ; None } + + if (const == null) + fail("annotation argument needs to be a constant; found: " + tr) + else if (const.value == null) + fail("annotation argument cannot be null") + else + Some(LiteralAnnotArg(const)) } /** Converts an untyped tree to a ClassfileAnnotArg. If the conversion fails, @@ -2575,7 +2584,7 @@ trait Typers extends Modes { def trees2ConstArg(trees: List[Tree], pt: Type): Option[ArrayAnnotArg] = { val args = trees.map(tree2ConstArg(_, pt)) if (args.exists(_.isEmpty)) None - else Some(ArrayAnnotArg(args.map(_.get).toArray)) + else Some(ArrayAnnotArg(args.flatten.toArray)) } // begin typedAnnotation -- cgit v1.2.3