Skip statically dead branches of ?:, && and while - #327
Merged
Merged
Conversation
#324 stopped `if` from generating the branch of a `typeof x === "name"` test that folds to a constant. `?:`, `&&` and `while` still generated it, with its narrowing: in a generic specialised for an array, `typeof x === "string" ? x.length : -1` cast the array to string, and under --di the narrowed variable's debug record kept that cast alive until LLVM lowering, which hit llvm_unreachable in CastLogicHelper. - getStaticBoolean() recognises a constant condition, also through casts to boolean (a folded typeof, or an `&&` whose left side folded to false); `if` uses it too. - ?: and `false && right` evaluate the dead branch, narrowing included, in the temporary module to keep its type, and yield an undefined value of that type; the expression's type is unchanged. A boolean `false && right` is just the constant condition, so an enclosing if/while sees it too. - `while` with a false constant condition does not generate its body. `||` does not narrow and was not affected. Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
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.
Problem
#324 stopped
iffrom generating the branch of atypeof x === "name"test that folds to a constant.?:,&&andwhilestill generated that branch together with its narrowing. In a generic specialised for an array,typeof x === "string" ? x.length : -1cast the array tostring. Under--di(always passed by the test runner) the narrowed variable's debug record kept that cast alive until LLVM lowering, where a debug build hitsllvm_unreachableinCastLogicHelper.h:815. Release builds compile the check out, so CI never saw it.Fix
getStaticBoolean()recognises a condition known at compile time: a boolean literal, also through casts to boolean.ifuses it too.?:andfalse && rightdon't generate the dead branch. It is evaluated, narrowing included, in the temporary module to keep its type, and the branch yields an undefined value of that type, so the expression's type is unchanged. A booleanfalse && rightreturns the constant condition itself, so an enclosingif/whilesees that it is known.whilewith a condition known to be false doesn't generate its body.||never narrows and was not affected.Tests
New
00typeof_static_fold_conditions.ts(compile, JIT and corpus variants):?:in both orders,&&,while (typeof x === "string" && ...), each for a string and an array, plus a check that a skipped?:branch still contributes its type.Full Windows debug suite (
ctest -R "^test-"): 2739/2739 passed.Not addressed (pre-existing)
if (typeof genericFn === "function")on an uninstantiated generic function fails lowering ("llvm.mlir.addressofop must reference a global"): narrowing casts a reference to the never-emitted generic.let v = b ? 1 : "one"typesvas the literal union1 | "one"and warns "types have different sizes", with a runtime condition as well.🤖 Generated with Claude Code