-
Notifications
You must be signed in to change notification settings - Fork 130
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
NPE on exhaustive pattern matching switch expressions with sealed interface (Java 21, 3.19.500.v20240601-0610). #2521
Comments
Compiling this code with 4.31 as well as with HEAD master I don't see any NPE:
The program compiles and when run prints |
@agentgt - something appears amiss in the defect report - can you double check ?? |
@agentgt maybe you can share the project as it is - just to make sure we have the same preferences as well in the tests |
Guess what, while working on #2513 I do see this NPE. I will pursue that but it would still be useful to get a test case from here. |
This is only sporadically reproduced in that project ... Don't have a reliable test case. |
Do you have fuller logs ? Does this involve the HierarchyResolver ?? |
@srikanth-sankaran I will attach more logs soon. The project luckily is opensource: https://github.com/jstachio/rainbowgum I change that bit of code to return switch (appender) {
case InternalLogAppender ia -> ia;
}; Roughly the same eclipse settings are here: I even have the Eclipse compiler set for analysis but is broken at the moment because I can't seem to get the eclipse annotation processor to behave properly on via the Maven plugin. https://github.com/jstachio/rainbowgum/blob/main/bin/analyze.sh Which you can run with I think the setting that most likely might have an impact is I have Annotation based Null Analysis turned on. |
The logs are at this public gist: https://gist.github.com/agentgt/232e1a9a6026a992f0053af83499db7e It repeats several times. I tried to put all the exceptions that are different instead of repeating them.
EDIT it appears the common entry point with all the calls is:
and that appears to be called by lot more things than HieararchyResolver. |
Thanks. I am on leave tomorrow. If I am able to locally observe the problem with the details you have outlined, I'll work on it early next week. |
@agentgt - I have cloned that repo and imported the projects. |
How are you importing the project? I was using m2e as in the Maven pom should dictate everything with the exception of null analysis and things like formatting. |
Well, I cloned they repo in a plain eclipse SDK installation and right clicked on repo and said import project from the working tree. I am not familiar with maven. I'll take help from @jarthana and see if I am able to observe it locally. |
I will see if I can make a non maven project and check in the .project and .classpath later today if that helps. |
That would be very welcome. Thanks in advance. |
I tried, but no luck reproducing this. |
@srikanth-sankaran and @jarthana it requires turning on null analysis. Preferences > Java Compiler > Error/Warnings > Enable annotation-based null analysis I'm creating a project w/ just plain eclipse settings to show it. |
Yes, I imported the projects, enabled null analysis and made the code change you mentioned. Anything else I am missing? |
Try this project: https://github.com/agentgt/jdt-issues This project is just a plain eclipse project. It has .classpath, .settings etc checked in.
My guess is you may have not changed the code to make it break. The code I have checked in at rainbowgum purposely does not have it doing it. the project I have included above does it. |
I updated the example eclipse project with a maven pom that will use the latest ECJ.
So it is not a UI jdt bug if that helps. EDIT the pom file is not meant for import (although it will probably work) but rather easier way to kick off ecj from command line. |
Reproduced. Will dig a bit more and see what's going on.
I am positive I made the code change. It looks like this: |
Apologies @jarthana ! I misread your previous comment that you had applied the change and thought you had just pulled it. FWIW @srikanth-sankaran when I went code diving it looked like |
Also given this NPE only happens when null analysis is turned on (the irony) perhaps @stephan-herrmann might have some thoughts? As a random side note perhaps the JDT code base should be annotated for null analysis? That might be something I could help with and would allow me to become more familiar with the JDT code base. Thoughts? |
Would be great to have someone helping here. Not sure why Stephan didn't enabled that on JDT itself, probably due lot of legacy code that would require some effort. Or may be because of API compatibility? Better ask @stephan-herrmann about that. |
:) So far I simply didn't have the heart to start this, in part due to some properties of the fundamental design specifically of the compiler:
Surely developing new code with null annotations is much, much easier than retrofitting this particular code base. That said, it might still be feasible to do this in small increments. Perhaps one might actually start in a defensive way by only adding few We could try this, in small, careful steps. |
Two possible reasons:
|
That may be the case with this project but I certainly saw the same NPE in the context of #2513 where annotation based null analysis is not in the picture. I think there are some scenarios where a call of I'll study this a bit more. I am hoping I can kill off more defects than just this one, such as #2108, #2093, #1998, #1808 - let us see how lucky I get :) |
@stephan-herrmann how does the assignment to Not impacting this ticket ... |
Is it perhaps the case that we know fully well we are updating an annotated variant in that place and never the prototype - so no propagation is needed ?? |
No such luck - this turned out to be a localized defect in annotation aware mode of the compiler @stephan-herrmann alluded to in his analysis. |
…erface (eclipse-jdt#2553) * Maintain correlation between a prototype and its annotated variant * Fixes eclipse-jdt#2521
…erface (eclipse-jdt#2553) * Maintain correlation between a prototype and its annotated variant * Fixes eclipse-jdt#2521
Does it make sense to add the the annotations projects as a required project so at least for documenting purposes we can add @nonnull etc to declarations ? Even if analysis itself is not triggered ? |
Given this code:
This happens on the last RC (3.19.500.v20240601-0610) and of course 4.31 and previous versions.
EDIT Forgot to mention the work around. If you set the result of the switch to a variable then the error does not happen.
However the above has issues with null analysis as it cannot infer that
r
is NonNull which I can file as a separate bug.Thus I usually have to do:
The text was updated successfully, but these errors were encountered: