You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Sema: Fix compilation timeout in analyzeAs with nested cast builtins
Resolves a circular dependency issue where `@as` coercion with nested
cast builtins (`@intCast`, `@floatCast`, `@ptrCast`, `@truncate`) would
cause infinite recursion in complex control flow contexts.
The bug occurs when the pattern `@as(DestType, @intcast(value))` appears
in code with:
- Loop constructs
- Short-circuit boolean operations (OR/AND)
- Optional unwrapping
Example problematic pattern:
```zig
while (condition) {
if (opt == null or (opt.?)[@as(usize, @intcast(pid))] == false) {
break;
}
}
```
Root cause: The original code would resolve the operand before the
destination type, causing the inner cast builtin to recursively analyze
without type context, leading to circular dependencies in the type
resolution system.
Fix: When the operand is a cast builtin, resolve the destination type
FIRST, then analyze the inner cast with proper type context. This breaks
the circular dependency while maintaining correct type coercion semantics.
The fix adds an optimization path that:
1. Detects when operand is a type-directed cast builtin
2. Resolves destination type before analyzing the operand
3. Skips redundant outer coercion if types already match
4. Preserves existing behavior for non-cast operands
A helper function `validateCastDestType` was extracted to eliminate
code duplication and improve maintainability.
Tested with Bun codebase which previously timed out during compilation.
The pattern appears in src/install/updatePackageJSONAndInstall.zig:722.
Related: oven-sh/bun
0 commit comments