fix: collapse ternary with the same constant in both branches#2908
Open
obus-globus wants to merge 1 commit into
Open
fix: collapse ternary with the same constant in both branches#2908obus-globus wants to merge 1 commit into
obus-globus wants to merge 1 commit into
Conversation
obus-globus
force-pushed
the
fix/ternary-same-const
branch
from
July 4, 2026 02:59
9f4d4a9 to
e60c4dd
Compare
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.
NOTE: This is what one may call "AI slop". Feel free to close this PR if it's not up to your quality standards or even if you just aren't interested in reviewing it. I use these changes downstream on an APK and thought I might as well make a PR offering to upstream them, as your CONTRIBUTING.md doesn't say anything discouraging this.
Description
if (c) return x; else return x;(and the assignment form) decompiled to a redundantc ? x : x, a ternary with the same constant in both branches, e.g.return apiException != null ? null : null;. It shows on every input path (dex and java/class alike):TernaryModforms the ternary without noticing the two results are equal, and nothing removes it.SimplifyVisitor.simplifyTernary(which already normalizes a ternary's condition) now also collapsesc ? x : xtoxwhen both value args are the same constant (InsnArg.isSameConst, covering int/null/string/class constants), for both the return-wrapped and assign-result forms.Dropping the condition is only safe when evaluating it has no observable effect, so
canDropConditioncollapses only when every condition instruction is non-throwing and every operand is a parameter or a plain field read (IGET/SGET).foo() ? x : xanda[0] > 0 ? x : xare left as-is, and a cast operand is rejected so the erasedCHECK_CASTbehindString s = list.get(0)(and itsClassCastException) is never dropped.This is scoped to same-constant branches. A same-value ternary (
c ? v : vwith the same variable) is left as-is.Includes an integration test.