Skip to content
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

Scalac fails to recognize static final fields as constants when parsing Java sources #5333

Closed
scabug opened this issue Dec 22, 2011 · 5 comments

Comments

@scabug
Copy link

scabug commented Dec 22, 2011

Attempting to compile attached files with scalac results in an error:

scalac Test.scala IntAnnotation.java Const.java 
Test.scala:1: error: annotation argument needs to be a constant; found: Const.CONST
@IntAnnotation(Const.CONST)
                     ^
one error found

If Const.java is compiled with javac first, scalac does not report any errors:

javac Const.java
scalac Test.scala IntAnnotation.java

Another example: https://gist.github.com/lrytz/838709adcfb0f08eb2c5. SBT runs the scala compiler only once.

@scabug
Copy link
Author

scabug commented Dec 22, 2011

Imported From: https://issues.scala-lang.org/browse/SI-5333?orig=1
Reporter: Marek Wiącek (marekw)
Assignee: @jrudolph
Affected Versions: 2.9.0-1, 2.10.0, 2.11.0-M8
See #5699
Attachments:

  • Const.java (created on Dec 22, 2011 6:46:41 PM UTC, 61 bytes)
  • IntAnnotation.java (created on Dec 22, 2011 6:46:41 PM UTC, 51 bytes)
  • Test.scala (created on Dec 22, 2011 6:46:41 PM UTC, 44 bytes)

@scabug
Copy link
Author

scabug commented May 12, 2012

@soc said:
I can verify this with Scala version 2.10.0-20120511-235304-d55ea54589 (OpenJDK 64-Bit Server VM, Java 1.6.0_24), which contains the fix for #2764.

Looks like another issue related to the classfile parser like #5699.

@scabug
Copy link
Author

scabug commented Sep 21, 2012

@vigdorchik said:
I believe this one is different from #5699:
the former is about incorrect parsing of annotation definition, while this one is about the parser not parsing java field initializer, and thus confusing scala constant evaluator.

@scabug
Copy link
Author

scabug commented Oct 20, 2014

@lrytz said (edited on Oct 20, 2014 12:56:08 PM UTC):
Problem is: it's an intrusive change trying to fix this one, because we don't compile java files (just parse signatures). in Scalac, constants folding happens thanks to the type system, (1 + 1) has ConstantType(2). To make the following work, we need to push expressions through the typer:

public class A {
  public static final int N = 0;
  public static final int K = A.N + A.N; // needs to have ConstantType
}

Also, the way things are set up in Scalac, a compile-time constant (in Scala) needs to have an inferred type. In Java, there's always an explicit type. So we need to be able to decide somehow if the type (for K) should be inferred or not.

@scabug
Copy link
Author

scabug commented Dec 15, 2016

@adriaanm said:
A good initial implementation was done by @jrudolph in scala/scala#5487. A full implementation would move this logic into the type checker, but I think this could be tracked as a new issue when it comes up.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

1 participant