Sema: Fix compilation timeout in analyzeAs with nested cast builtins #25687
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.
Summary
Fixes a circular dependency issue in
Sema.analyzeAs()where@ascoercion with nested cast builtins (@intCast,@floatCast,@ptrCast,@truncate) causes infinite recursion/timeout during compilation in specific control flow patterns.Problematic Pattern
The bug manifests when the following elements are combined:
@as(DestType, @intCast(value))patternExample from the wild (from Bun's codebase):
This pattern causes compilation to timeout indefinitely.
Minimal reproduction:
Root Cause
The original implementation in
Sema.analyzeAs()resolves the operand before the destination type:When the operand is a type-directed cast builtin like
@intCast, this creates a circular dependency:@astries to resolve@intCast(pid)without knowing the destination type@intCastrecursively analyzes itself without type contextFix
The fix adds an optimization path that detects when the operand is a cast builtin and resolves the destination type FIRST:
This breaks the circular dependency while preserving correct type coercion semantics.
Testing
Verified the fix by:
updatePackageJSONAndInstall.zig:722Related