scala / bug

Scala 2 bug reports only. Please, no questions — proper bug reports only.
https://scala-lang.org
232 stars 21 forks source link

annotation argument needs to be a constant; found: Bar.constant #10426

Closed spockz closed 5 years ago

spockz commented 7 years ago

When generating scaladoc for the trait below scaladoc will choke on the constant. sbt compile works just fine. Stackoverflow suggests setting canAdaptConstantTypeToLiteral to true.

Regardless, the scaladoc compiler should probably use the same settings as the normal compiler.

import javax.annotation.Priority
import javax.ws.rs.Priorities

@Priority(Bar.constant)
trait A {
  def x(): String
}

object Bar {
  final val constant = Priorities.USER + 42
}
jvican commented 7 years ago

Hey @spockz, thanks for filing this issue.

So the thing here is, it's unclear whether this option has to be false for Scaladoc. Does anyone in the Scala team know why this is the case?

If no one thinks this is a bad idea, I think the best way to move this forward is trying to flip the boolean at hand for scaladocs and see what it breaks. If nothing breaks, the option can be configured via a compiler scaladoc setting or whichever method is used to configure scaladoc. When this is done, we can make this option configurable from sbt so that this error doesn't happen again.

lrytz commented 7 years ago

Maybe Scaladoc wants to retain the original trees for printing default arguments.

trait T {
  def x(i: Int = Bar.constant): Unit
}
object Bar {
  final val constant = Integer.MAX_VALUE + 42
}

gives

image

lrytz commented 7 years ago

If it's just about that, we could achieve the same effect by adding an OriginalTree attachment added to the Literal https://github.com/scala/scala/blob/v2.12.2/src/compiler/scala/tools/nsc/typechecker/Typers.scala#L998

(I haven't checked / debugged any of this, just speculating)

spockz commented 7 years ago

How can we proceed on this? If this would be fixed would it require a release of 2.11 in order to be able to generate docs in 2.11 or can we use the same scaladoc when cross compiling?

jvican commented 7 years ago

2.11.x is EOL'ed, so that can't happen @spockz. This could happen in the next 2.12.x release if I'm not mistaken.

If it's just about that, we could achieve the same effect by adding an OriginalTree attachment added to the Literal https://github.com/scala/scala/blob/v2.12.2/src/compiler/scala/tools/nsc/typechecker/Typers.scala#L998

BTW, this fix would help me get Zinc working for final constants (which would be enormous). If you confirm me this attachment is ok @lrytz, I'm more than happy to submit a PR with that fix. I proposed a more ambitious approach to this problem, but that didn't seem to work out: https://github.com/scala/scala-dev/issues/340.

hrhino commented 6 years ago

There's now an OriginalTreeAttachment which should allow threading of the needle here.

som-snytt commented 6 years ago

I thought typelevel constant folding was going to be the solution.

som-snytt commented 5 years ago

Duplicates https://github.com/scala/bug/issues/9532

qingwei91 commented 1 year ago

Hi, may I know if this is fixed?

I am bumping into this issue on scala 2.12.18

som-snytt commented 1 year ago

Looks like the linked ticket was 2.13.