Repository navigation
[java] Fix #7313: Fold casts to boolean and boolean equality - #7314
Merged
Merged
Conversation
JLS 15.29 allows casts to primitive types and the operators == and != in constant expressions. ConstantFolder folded a cast only to a numeric type or String. It folded == and != only for numeric and char operands. So (boolean) true and true == false had no constant value. A cast to boolean now keeps a boolean operand value, and == and != compare two boolean values. A cast to Boolean still has no constant value.
Contributor
|
Compared to main: (comment created at 2026-11-14 02:48:17+00:00 for 5a8917a) |
3 of 4 tasks
UncleOwen
reviewed
Nov 13, 2026
A comparison or a cast of final Boolean locals has a folded value, but it is not a compile-time constant. A comparison of final boolean locals is a compile-time constant.
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
ConstantFoldernow folds a cast tobooleanand the operators==and!=with twobooleanoperands, which JLS 15.29 allows in constant expressions. A cast toBooleanstill has no constant value. This changesgetConstFoldingResult(),getConstValue(),isCompileTimeConstant()and the XPath attribute@CompileTimeConstantfor such expressions.With this change, the block lambda bodies in the sample of #7313 are value-compatible, and the three
pool.submit(..)calls selectsubmit(Callable), as with javac.New cases are in
testCastsandtestEqualityofConstantExpressionsTestsand inConstValuesKotlinTest, and one block lambda body is added inLambdaInferenceTest. Onmain(4a1d37d), the newisConstcases, the lambda body and theConstValuesKotlinTestcase fail. ThenotConstcases pass. The disabled case ofUnnecessaryBoxing.xmlin #7136 that waits for #7313 passes with this change.Outside the PR, I ran all Java rules except the documentation category before and after this change, without auxclasspath. The input was 2,145 files of the JDK 21 sources (
java.base,java.desktop,java.xml) and the 914 main source files of the PMD modules other than pmd-core and pmd-java. Four ANTLR-generated files among them have@SuppressWarnings("all"), so PMD reports nothing for them. The reports are the same. A second rule prints the method selected at each method call, constructor call, explicit constructor call and method reference. Its output is also the same, for 162,950 sites in the JDK files and 12,712 in the PMD modules. Without auxclasspath, the overload resolution fails at 67 and 5,242 of them.I also ran the rules on a small sample with these expressions in field initializers, conditions, a
returnand conditional expressions. The only difference is a new FinalFieldCouldBeStatic report forprivate final boolean castField = (boolean) true;. javac gives that field aConstantValueattribute.Ran
mvnw.cmd -B -ntp -pl :pmd-java verifywith Java 21 on Windows 11. Of 10,799 tests, none failed or had errors, and 21 were skipped.Related issues
Ready?
pmd-java./mvnw clean verifypasses (checked automatically by github actions)