Repository navigation
[java] Fix #7150: type of numeric conditionals with byte, short or char operands - #7162
Merged
Merged
Conversation
…operands A conditional like flag ? 'a' : 1 was typed int by binary numeric promotion. JLS 15.25.2 gives char when one operand is a char and the other is an int constant that fits, short for byte with short, and the unboxed type for a Byte, Short or Character operand with such a constant. getConditionalStandaloneType now applies these rules before the promotion. Conditionals before Java 8 take the same standalone path instead of a computation from the branch types alone. On that path a diamond constructor call is inferred from its arguments instead of being typed with the generic type declaration. The branches of a standalone conditional are checked against its type, not against the parameter type of the enclosing call.
Contributor
|
Compared to main: (comment created at 2026-10-26 16:16:17+00:00 for 8e6ceb9) |
3 of 4 tasks
adangel
approved these changes
Oct 26, 2026
…ernal/infer/ast/ConditionalMirrorImpl.java
UncleOwen
reviewed
Oct 26, 2026
adangel
reviewed
Oct 26, 2026
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
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.
Describe the PR
PMD typed every numeric conditional with binary numeric promotion, so
flag ? 'a' : 1becameintandc(Integer, char)was not applicable to it. JLS 15.25.2 givescharhere, because one operand is acharand the other is an int constant that fits in achar. The same section givesshortforbyteorBytewithshortorShort, and the primitive type for aByte,ShortorCharacterpaired with an int constant that fits.getConditionalStandaloneTypenow applies these rules before the promotion. Before Java 8,PolyResolutioncomputed the type of a conditional from the types of its branches alone and could not see the constant. Conditionals are standalone in those versions, so they now take the same path as a conditional without a target type. On that path, a diamond constructor call in a branch was typed with its generic type declaration,Gen<T>fornew Gen<>().CtorInvocMirrornow infers it from the arguments when the expression is standalone. The Java 7 test with two such branches keepsGen<Object>.With the conditional typed
char, the overload check still visited its branches and checked1against the parameter typechar. An invocation context does not allow that narrowing (JLS 5.3), soc(Integer, char)was rejected. The compatibility of a standalone conditional is decided by its own type, and the branch walk only finishes the inference of poly branches. The branches of a standalone conditional are now checked against their own standalone types.New cases in
BranchingExprsTests, run from Java 7 to the latest version:bytewithshort, primitive and boxedbyte,shortorchar, with primitive and boxed operands, and a constant variableintoperandc(Integer, char)Characterfrom such a conditionalThree existing expectations had the promoted type:
true ? 1 : 'c'twice inBranchingExprsTestsandtrue ? 1 : (short) 5inConversionContextTests. The rule givescharandshort, and the tests now expect that.Ran
./mvnw -B -ntp -pl pmd-java verifywith Java 21. The new tests fail onmain.Related issues
Ready?
./mvnw clean verifypasses (checked automatically by github actions)