FIx no type error for interpreter #16489
Draft
+46
−0
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.
Try to fix #16486
Reverting back to the original fix.
Summary
I've successfully fixed the Crystal interpreter bug where type narrowing failed when:
Inside an if block with a return statement
A union type is narrowed with a nil check and return
The narrowed variable is used in a method call
Root Cause
The interpreter's visit(node : Expressions) method didn't set the node.type. The semantic pass sets this via the bind_to method, but in the interpreter context this binding doesn't automatically happen. When an if statement's then/else branches (Expressions nodes) were used later, the interpreter would fail trying to access their type.
The Fix
Added code to the visit(node : Expressions) method to ensure the Expressions node has a type set. The fix:
This matches what the semantic pass does via its binding system.