Recognize compile-time constants in LoopConditionChecker - #6064
Open
eviaaaaa wants to merge 1 commit into
Open
Conversation
eviaaaaa
marked this pull request as ready for review
August 25, 2026 08:06
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.
LoopConditionCheckercurrently handles literal loop bounds but rejects equivalent javac compile-time constants. Local constant variables can be reported as if they were mutable condition variables, while static constants, member selects, casts, and conditional constant expressions can stop analysis entirely. This causes the checker to miss genuine loops whose only changing-looking operand is actually constant.Use javac's attributed constant value through
ASTHelpers.constValueinstead of inferring constants from modifiers or initializer syntax. Constant identifiers are ignored as condition variables, and otherwise unsupported expression kinds are accepted only when javac has assigned them a compile-time constant value.This intentionally does not treat boxed constants, method-call initializers, mutable fields, or other runtime values as constants.
Fixes #5826
The tests were first applied without the production change.
LoopConditionCheckerTestran 12 tests with 2 failures: local/static constant bounds were not treated like literals, and member-select/cast/conditional constant expressions produced no diagnostic.The regression tests cover local, static, and externally declared primitive constants; constant initializer, cast, and conditional expressions; and negative controls for boxed, runtime, and mutable values. They also verify that only the genuinely mutable variable is reported.
LoopConditionCheckerTestand the fullcore -amtest suite pass. A full reactorclean install -DskipTestsalso succeeds.