Skip to content

[java] Fix #7150: type of numeric conditionals with byte, short or char operands - #7162

Merged
adangel merged 4 commits into
pmd:mainfrom
saryn17:fix-7150
Oct 26, 2026
Merged

adangel merged 4 commits into
pmd:mainfrom
saryn17:fix-7150

Conversation

@saryn17

@saryn17 saryn17 commented Oct 7, 2026 •

Copy link
Copy Markdown
Contributor

Describe the PR

PMD typed every numeric conditional with binary numeric promotion, so flag ? 'a' : 1 became int and c(Integer, char) was not applicable to it. JLS 15.25.2 gives char here, because one operand is a char and the other is an int constant that fits in a char. The same section gives short for byte or Byte with short or Short, and the primitive type for a Byte, Short or Character paired with an int constant that fits. getConditionalStandaloneType now applies these rules before the promotion. Before Java 8, PolyResolution computed 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> for new Gen<>(). CtorInvocMirror now infers it from the arguments when the expression is standalone. The Java 7 test with two such branches keeps Gen<Object>.

With the conditional typed char, the overload check still visited its branches and checked 1 against the parameter type char. An invocation context does not allow that narrowing (JLS 5.3), so c(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:

  • byte with short, primitive and boxed
  • an int constant that fits in byte, short or char, with primitive and boxed operands, and a constant variable
  • the bounds of each type, and a non-constant int operand
  • the overload from the issue, where javac selects c(Integer, char)
  • a type parameter inferred as Character from such a conditional
  • a diamond constructor call with an argument in a branch of a standalone conditional

Three existing expectations had the promoted type: true ? 1 : 'c' twice in BranchingExprsTests and true ? 1 : (short) 5 in ConversionContextTests. The rule gives char and short, and the tests now expect that.

Ran ./mvnw -B -ntp -pl pmd-java verify with Java 21. The new tests fail on main.

Related issues

Ready?

  • Added unit tests for fixed bug/feature
  • Passing all unit tests
  • Complete build ./mvnw clean verify passes (checked automatically by github actions)
  • Added (in-code) documentation (if needed)

…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.
@pmd-actions-helper

pmd-actions-helper Bot commented Oct 7, 2026 •

Copy link
Copy Markdown
Contributor

Documentation Preview

Compared to main:
This changeset changes 0 violations,
introduces 0 new violations, 0 new errors and 0 new configuration errors,
removes 0 violations, 0 errors and 0 configuration errors.
There are 0 changed duplications, 0 new duplications and 0 removed duplications.
There are 0 changed CPD errors, 0 new CPD errors and 0 removed CPD errors.

Regression Tester Report

(comment created at 2026-10-26 16:16:17+00:00 for 8e6ceb9)

@adangel adangel left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks!

@adangel adangel added this to the 7.29.0 milestone Oct 26, 2026
@adangel adangel changed the title [java] Fix the type of numeric conditionals with byte, short or char operands [java] Fix #7150: type of numeric conditionals with byte, short or char operands Oct 26, 2026
@adangel
adangel merged commit 6c892f0 into pmd:main Oct 26, 2026
2 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

in:type-resolution Affects the type resolution code

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[java] Numeric conditional with a char operand and an int constant is typed as int

3 participants