Repository navigation
Open
Labels
devdevelopment tasks, build issues...
Milestone
Description
NullAway branch is here:
This is the crash (see here), which comes when running ./gradlew :test-java-lib:compileJava on a JDK 8 JVM:
warning: unknown enum constant java.lang.annotation.ElementType.MODULE
warning: unknown enum constant java.lang.annotation.ElementType.MODULE
An exception has occurred in the compiler (1.8.0_345). Please file a bug against the Java compiler via the Java bug reporting page (http://bugreport.java.com) after checking the Bug Database (http://bugs.java.com) for duplicates. Include your program and the following diagnostic in your report. Thank you.
java.lang.AssertionError: annotationType(): unrecognized Attribute name MODULE (class com.sun.tools.javac.util.UnsharedNameTable$NameImpl)
at com.sun.tools.javac.util.Assert.error(Assert.java:133)
at com.sun.tools.javac.code.TypeAnnotations.annotationType(TypeAnnotations.java:231)
at com.sun.tools.javac.code.TypeAnnotations$TypeAnnotationPositions.separateAnnotationsKinds(TypeAnnotations.java:294)
at com.sun.tools.javac.code.TypeAnnotations$TypeAnnotationPositions.visitMethodDef(TypeAnnotations.java:1066)
at com.sun.tools.javac.tree.JCTree$JCMethodDecl.accept(JCTree.java:778)
at com.sun.tools.javac.tree.TreeScanner.scan(TreeScanner.java:49)
at com.sun.tools.javac.code.TypeAnnotations$TypeAnnotationPositions.scan(TypeAnnotations.java:275)
at com.sun.tools.javac.tree.TreeScanner.scan(TreeScanner.java:57)
From #34 I can't tell if this was intended or not. Do we not support using JSpecify annotations when building on JDK 8? Building on JDK 8 is not a requirement for NullAway (we're planning to remove JDK 8 support soon); I am just wondering if this was intentional.
Metadata
Metadata
Assignees
Labels
devdevelopment tasks, build issues...
Activity
msridhar commentedon Oct 8, 2022
FWIW a possible difference between this and previous test cases is that the code being compiled uses
@NullMarkedand@NullUnmarkedon methods:https://github.com/uber/NullAway/blob/e1ea7f62e8c82fe578e1c18446827849dad76c39/test-java-lib/src/main/java/com/example/jspecify/unannotatedpackage/Methods.java
msridhar commentedon Oct 10, 2022
After further thought I realized we probably want to have NullAway support JDK 8 for some time longer, to support more open-source projects that haven't updated. So having JSpecify support building with JDK 8 would be helpful to us.
cpovirk commentedon Oct 10, 2022
Thanks very much for testing this and for the repro instructions.
This is new: We'd known that building with Java 8 could produce a warning (which becomes an error under
-Werror), and we'd known that reflection under Java 8 could lead to exceptions at runtime. But I don't think we'd known of build-time crashes.Brain dump follows.
Testing notes
It does appear that the code compiles fine if the usages of
@NullMarkedand@NullUnmarkedappear only on classes, at least in the testing I've done so far.(I would say "And the code compiles fine with the 0.2.0 release," but: In order to test with 0.2.0, I have to change the test code such that... the usages of
@NullMarkedand@NullUnmarkedappear only on classes :) And we already know that that's enough to eliminate the error.)One annotation of either type on a method is enough to produce the error. That's true even if the annotation is redundant on account of a matching annotation on the enclosing class.
Removing
MODULEfrom the@Targetof the all annotations that are used in the file being compiled eliminates the problem. Removing it from any annotations that aren't used doesn't, though it eliminates the relevant "unknown enum constant ElementType.MODULE" warning. (I didn't test this all exhaustively, but that appears to be what's going on.) MovingMODULEto come afterPACKAGEinNullUnmarked.java(as it already was inNullMarked.java) does not help.It also appears that the code compiles fine if I build with JDK11 but with
--release 8(options.compilerArgs += ["--release", "8"]intest-java-lib/build.gradle)... well, aside from the usual "unknown enum constant" warning, the one that becomes an error under-Werroruntil I turn that off inbuild.gradle.Finally, it appears that NullAway is not necessary for the crash: A build of your
Methods.javawith plain javac8 likewise crashes.Why this is happening
Part of the story here is almost certainly #245: We used to have the
MODULEtarget on@NullMarkedonly in a Java-9+ copy in the jar. But that turned out to not always let people actually use the annotation on a module. So 0.3.0-alpha-1 switched us to having only a single copy of@NullMaked, which naturally includes theMODULEtarget.It's still a bit of a mystery as to why this error appears only with certain test code. [edit: I believe msridhar suggested that it has something to do with which cases javac needs to ask "Is this a type-use annotation, an annotation applicable to this element, or both?] But it's not surprising in general that a "missing" internal class could cause problems for javac, and it's not surprising that that could cause crashes that appear and disappear for mysterious reasons.
How to avoid it
In an ideal world, everyone would build with a newer JDK, even if they target Java 8, typically by using
--release. (See, for example, #271 (comment).) But using--releasecan be kind of a pain. (See, for example, google/guava#3990.) And beyond that, upgrading javac is often easy but can sometimes require some work.A theoretical way for a project to continue to be able to build with javac8 to use a
@ModulelessNullMarkedalias that@Implies(NullMarked.class)but does not includeMODULEin its@Target. (My naive reading of javac is that it might be OK for dependencies of that project to continue using plain@NullMarked: The specific crash here is in code that I don't think runs on binary deps, only on source code. However, it's possible that there's similar code that does run when looking at binary deps. We'd have to test. If binary deps produce a similar issue, then all the JSpecify-using libraries that that project depends on would also need to use a@ModulelessNullMarkedalias. And "unfortunately," those libraries' developers wouldn't notice that they need to use the special annotation that unless they built with javac8 themselves. If we did end up wanting to go this direction, then we could consider providing a standard "@ModulelessNullMarked" annotation.) Note that this alias might also help with runtime reflection, but I wouldn't say that confidently until we'd actually implemented it :) For that matter, I'm not even sure if the build-time crash would necessarily go away. Ah, we could probably make most (all?) of the problems go away if tools had a special case for this standard "@ModulelessNullMarked" annotation (as opposed to relying on normal@Implieslogic). But that's a little gross.Another spin on that possibility is for the main
@NullMarkedto be the one to not containMODULEin its@Target. We'd then probably want to supply a standard alias that does target modules. (Or maybe the aliasing would run in the reverse direction, as in my previous option: Then the canonical annotation would be@NullMarked.NullMarkedModule, and@NullMarkedwould imply it. That way, we avoid having an annotation with@Target(MODULE)imply one without@Target(MODULE), which I think we've been assuming would be unrecognized (1, 2)? But maybe giving@NullMarked.NullMarkedModule@Target(MODULE)reintroduces many of the problems I was trying to avoid?) The hope then would be that eventually we could addMODULEto the@Targetof@NullMarkedonce javac8 use is really dead. This has the advantage that the negative experience is mostly limited to people who try to put@NullMarkedon a module, get a clear error, and hopefully read enough of our docs to see that@NullMarked.NullMarkedModuleis available as a workaround.A different approach is to omit
@Targetentirely. But that has the obvious downsides. Plus, it sounds like it still doesn't actually work how it's intended to: #34 (comment).This continues to need more thought
Yuck. But thanks again.
cpovirk commentedon Oct 10, 2022
Oh, this is a good place to recall that
MODULEmakes annotations usable elsewhere under some versions of javac. That is a small and gross but practical reason to avoid it on the main annotation. It would also be a reason for tools to more carefully validate the locations on which annotations appear... which in turn could make the no-@Targetapproach more appealing, if only that approach, you know, worked :)msridhar commentedon Oct 10, 2022
Thanks so much for investigating, @cpovirk. I have one question. Based on the stack trace and error message, I don't think this crash in JDK 8 javac is intended behavior. Also it looks like one can at least still backport fixes to JDK 8 for future patch releases. Here is the line of code throwing the assertion error:
https://github.com/openjdk/jdk8u-dev/blob/d285382278fcb76ceddfb61cf8bb047ff269de44/langtools/src/share/classes/com/sun/tools/javac/code/TypeAnnotations.java#L231
I wonder if we could submit a PR that changes this assertion to something less crash-y? Like maybe emit another warning instead for an unknown enum constant? Then at least this issue could be fixed in a future JDK 8 release, which gives us more options (like leaving things the way they are and telling users to update their JDK 8 release). Of course this doesn't help with working around the current crash.
It looks like someone submitted an issue on this before:
https://bugs.openjdk.org/browse/JDK-8279557
They didn't have a reproducer so it got closed, but we have one now. If we think it's worthwhile, I can try to re-open that issue or submit a new one, to see if we get feedback on the possibility of a change. Let me know if we think that's worth a try.
cpovirk commentedon Oct 10, 2022
Thank you! That is a better thought than any of the thoughts in my post :)
It does seem likely that upstream would like the compiler to not crash, regardless of how strange the code we feed it is.
It's possible that they could respond by changing the crash into a more normal but still fatal error, or it's possible that they don't deem this fix worth backporting. But maybe they'll see the value in making it easier for libraries to use the
MODULEtarget. (As noted in #245, we tried solving this problem with multi-release jars instead. But it's not clear if multi-release jars are supposed to solve this problem, and regardless, they don't under some major tools today.)Reproducing does look easy. If you're up for taking things to upstream, that would be terrific.
msridhar commentedon Oct 10, 2022
I will report this upstream and then report back. Let's hope for a positive response :-)
msridhar commentedon Oct 14, 2022
The bug report got created:
https://bugs.openjdk.org/browse/JDK-8295314
However, it got erroneously closed as "Cannot reproduce" because the repro instructions were not followed correctly (JDK 17 was used to compile
B.javainstead of JDK 8).I will try to use a link they provided to "submit additional info" to clarify. But @cpovirk @kevinb9n if we know someone with a JDK bug system account who could comment directly, that might be more effective.
cpovirk commentedon Oct 14, 2022
Thanks. I think @eamonnmcmanus said that he had The Power? Éamonn, are you up for it?
eamonnmcmanus commentedon Oct 14, 2022
I do indeed have an openjdk.org account. Let me know if "submit additional info" doesn't produce the desired results and I can comment on the bug.
msridhar commentedon Oct 15, 2022
Update: it looks like "submit additional info" worked as the bug has been re-opened. Not sure who would look at this / prioritize it. I'll wait a bit and see what happens.
msridhar commentedon Oct 20, 2022
For those familiar with the JDK bug reporting process: is there any way to know if JDK-8295314 has or will be triaged? I am wondering how we can get the right eyes on it. I think we have a good case that it's worth fixing, but probably we need someone involved with JDK 8 maintenance to see it for it to get noticed, and I'm not sure if that will happen automatically.
We're about to add a one-off
@NullUnmarkedannotation to a project because of this issue 😐msridhar commentedon Oct 27, 2022
Update: thanks to help from @cushon and @cpovirk, we discovered this bug still exists in the latest version of
javacif you create a.classfile for an annotation with an invalidElementTypein its@Targetmeta-annotation. This has been filed as a new bug against JDK 19:https://bugs.openjdk.org/browse/JDK-8296010
Our hope is that the new bug gets fixed in the latest Java version, and that eventually we can backport the fix all the way to JDK 8.
12 remaining items
cpovirk commentedon Mar 18, 2024
Sorry, I lost track of this thread... among others recently... :(
We had tried a multi-release jar for this purpose a couple years ago, and it turns out that it doesn't achieve that purpose. The idea of a multi-release jar is that the API of the classes is supposed to match across versions. The definition of "API" was left a little fuzzy AFAICT, but the idea seems to (mostly?) be that a compiler should be able to look at whichever version it wants. That means that we can't count on it to look at the version that contains
MODULEin its@Target, and indeed multiple tools currently don't do that.Or maybe the tools are buggy. Even if so, we probably don't want to take an approach that will cause problems for those tools in order to work around a problem with a tool (javac) that is probably at least easier to migrate off of (by upgrading to javac 9+, even while still targeting older versions of Java).
cpovirk commentedon Mar 18, 2024
(But thank you for raising that suggestion! I wish that it had worked out....)
cpovirk commentedon Jul 12, 2024
I should write down a clarification here, since I was missing it myself:
There are two distinct problems that the
@Target(MODULE, ...)onNullMarkedcan cause:java.lang.AssertionError: annotationType(): unrecognized Attribute name MODULEwarning: unknown enum constant ElementType.MODULE(OK, users could also have trouble reading the
@Targetvalues at runtime under Java 8, but that seems less likely to come up.)This issue is nominally about (1) but for
NullUnmarked. (It's true that the output in the first post of this issue also contains the second warning, but the two problems are separable.) We have solved the problem for that annotation by removingMODULEfrom the target of that annotation. (That change doesn't reduce functionality: "Unmarked" is already the "default default." Thus, setting it explicitly on the module would always be a no-op.)Problem (1) still exists for
NullMarked, but note that it can arise only when you apply the annotation at the method level, not the class or package level. We don't expect it to be applied at the method level as often as the other two levels, but occasionally it will be.Also note that problem (1) exists only when building with a JDK 8 javac. Building with a newer JDK, even when targeting an older version of Java (as with
--release 8), does not trigger this problem.(We were able to get new versions of javac changed to produce a better error for unrecognized
@Targetvalues, rather than a crash. But that doesn't help people who build with JDK 8 at all—unless that change is someday backported, in which case their builds will still fail but in a slightly nicer way :))Then there's problem (2). That problem exists not only under the JDK 8 javac but also under newer versions of javac but only with
--release 8and then only sometimes, notably with Error Prone. (The Error Prone problem began with a change I made that was released in Error Prone 2.20.0, but a fix will be coming in the next release, whether that's numbered 2.28.1 or 2.29.0. In the meantime, you can work around it by disabling the offending check with-Xep:NullArgumentForNonNullParameter:OFF.) I think we have also seen it without Error Prone, as demonstrated in a simplified example in #50 (comment) (and maybe seen other times, like in #245 (comment) or in a report by @SimY4 or others), but I'm not sure of the exact circumstances required to trigger it. My guess would be that:@NullMarkedin the code you're compiling with--release 8.SimY4 commentedon Jul 13, 2024
@cpovirk TBH, this problem could be solve in two ways.
a. you can roll with multi-release jars (because as I said before, you'll face this issue EVERY time when Java will introduce new annotation target)
b. you can roll new artefact release for jspecify specific for Java 8. (smth like
jspecify-jdk8For my personal self I'm thinking of forking jspecify and roll my own version for only JDK 8 annotation targets specifically (b).
sdavids commentedon Aug 26, 2024
This still can be reproduced:
cpovirk commentedon Aug 26, 2024
Thanks for the easy repro instructions! I should really get around to testing all my guesses from July....
It looks like the problem reproduces even without Error Prone, so I think this one falls under:
We haven't found any way to avoid that error without breaking support for using
@NullMarkedon modules (even if we use multi-release jars), so all we've got is workarounds. You can drop-Werrorfor the build that containspackage-info. Or, if you can compilepackage-infoseparately from the rest of your files, you can keep-Werrorbut use-source 8 -target 8instead of--release 8for it. (That is very likely to be safe in practice, but it's not fully safe, at least not without additional enforcement like that from Animal Sniffer.) For a--release 8/-source 8build, it might be possible to substitute in a Java 9+ copy ofjavax.lang.model.element.ElementKindduring the build, but I haven't tried that. (Or you could make your own copy of the JSpecifyNullMarkedannotation withoutMODULEin its@Targetlist if you have that level of control over your build....)The long-term hope is of course that Java 8 usage will continue to shrink. Given the choice between better Java 8 support and better Java 9+ support, we went with the latter, but it's been frustrating that we had to make that choice.
sdavids commentedon Aug 26, 2024
Would a separate
jspecify-jdk8artifact not suffice?—This has been mentioned several times…JetBrains - Java Programming - The State of Developer Ecosystem in 2023
cpovirk commentedon Aug 26, 2024
There unfortunately is no particularly user-friendly way to ensure that the artifact that users want is the one that ends up appearing first on the classpath in general. And even if the right one ends up first, tools sometimes report errors when different versions of a class exists in multiple jars on the classpath.
Given that, the approach could work well for users who exclusively use the Java 8 version, but it would introduce those potential problems for users who use a mix of that and the Java 9+ version.
sdavids commentedon Aug 26, 2024
Would it not be a similar situation to google/guava#3683
cpovirk commentedon Aug 26, 2024
Yes, but based on how that's gone, it's not a situation that I'm overly comfortable with: We've been kind of lost in helping users who have run into problems. Additionally, that helps only users of Gradle.
cushon commentedon Aug 26, 2024
I was curious what the jetbrains numbers for Java 8 usage looked like historically, according to their survey it peaked in 2018 and the decrease in usage has been accelerating.
I am curious how common using
-Werroris in general, but my sense is that there are enough javac diagnostics that can't be selectively disabled that using-Werroris challenging for many projects.scordio commentedon Jun 7, 2025
I encountered the same issue when adopting JSpecify and NullAway in
jimfs-junit-jupiter, a JUnit extension that targets Java 8 and has a module descriptor. I achieve this by having two compile executions, as Maven suggests.Eventually, I dropped
-Werrorfrom the compile execution targeting Java 8.cpovirk commentedon Jun 9, 2025
Thanks for letting us know. It sounds like you were seeing the "warning: unknown enum constant ElementType.MODULE" message (which appears with
--release 8), which then failed your build because of-Werror, and not the outright crash ("unrecognized Attribute name MODULE") that happens when trying to build with a javac binary from JDK 8, right?(I hope that we end up seeing enough usage of
@NullMarkedat the module level to justify this annoyance for people who support Java 8 :\)scordio commentedon Jun 9, 2025
Yes, exactly. I only now realize how well documented this was; sorry for the noise 🙂
cpovirk commentedon Jun 9, 2025
I'm happy to have the report: It's a useful reminder that we need to eventually promote things out of the wiki and onto jspecify.dev itself.