-
Notifications
You must be signed in to change notification settings - Fork 917
Fix possible CCE during auto completion of invalid code #9173
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
Merged
+1
−1
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
midElandmidTMare only used innetbeans/java/java.completion/src/org/netbeans/modules/java/completion/JavaCompletionTask.java
Line 6427 in 2fba977
which is ok with any combination of null.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Using
instanceofonElements is formally speaking not correct (getKind()should be used), as one implementation type can implement multiple API interfaces. Although I think use ofinstanceofwill be accepted more and more. And it is not so bad forElement, as I don't recall implementation classes where this would be a real problem. (It is definitely a problem for certainTypeMirrors - the implementation typeClassTypeimplements bothDeclaredTypeandErrorTypeAPI interfaces, and this effectively cannot be changed while keeping all other things working reasonably. There are also some rareTrees where this is true.)There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Wouldn't mind seeing an example of how this should be written like this. I think I understand what you're getting at but it seems very similar to what was there already.
We don't have a utility that does this check somewhere? Take TreePath, Kind and expected output Class?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
tbh 70% of the reason i wrote it that way is because the new
instanceofget-check-and-assign pattern can be put into a one-liner which is the common style throughout the whole class. You could do something similar withOptionalbut it is less elegant IMO. (but of course this shouldn't be an excuse to write broken code ;))I did have the
Kindvsimplementsissue in the back of my head since @lahodaj explained it to me once, but I believe in this particular case it probably wouldn't matter the way the two variables are used.Having
TypeMirrorof kindExecutablebut theElementof a type other thanExecutableElementsounds to me like inconsistent state? I suppose thats why the original code casted it without kind check?Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes, I assumed the point was that this should be checking the kind from the element being casted? Although like you say the current situation seems inconsistent. I've looked through a few reports and stack traces before we've had with CCE, and I just get the feeling it might be better to verify types as well as kind before casting. I wondered if we had a utility somewhere already that does that. Bonus if it also checks for
nullfirst. Could simplify a few bits of this while keeping the logic in one place.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I don't think we have a utility to fuse kind check and cast (although we could have - esp. for trees, as there's Kind.asInterface). Given we don't really support arbitrary Java Language Model (javax.lang.model), but only the one produced by javac, I think using instanceof for ExecutableElement is OKish.
FTR, the lines above show the kind check:
as Michael says, it is not easy to this without the auxiliary variable, esp. not without duplicating the
getTypeMirrorcall. But, with source level >= 21, we could do:As for the inconsistency, the attribution of erroneous code sometimes produces "funny" results - in particular, javac has no real
ExecutableElementinstance for really erroneous methods, its all eitherClassSymbolor specialized subclases ofSymbol, but notMethodSymbol/ExecutableElement, I think. I think I had some brief periods where I looked at possibilities to change this, but there I never found a viable solution.