-
-
Notifications
You must be signed in to change notification settings - Fork 1.7k
Fix no type error for interpreter #16489
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
cyangle
wants to merge
5
commits into
crystal-lang:master
Choose a base branch
from
cyangle:fix_no_type_error
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Conversation
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
Similar to compiler's implementation in Crystal::CodeGenVisitor#codegen_if_branch
Contributor
Author
|
I have no idea. Maybe the first approach is the correct fix: diff --git a/src/compiler/crystal/interpreter/compiler.cr b/src/compiler/crystal/interpreter/compiler.cr
index bba9d5093..da3b875a8 100644
--- a/src/compiler/crystal/interpreter/compiler.cr
+++ b/src/compiler/crystal/interpreter/compiler.cr
@@ -591,6 +591,17 @@ class Crystal::Repl::Compiler < Crystal::Visitor
@wants_value = old_wants_value
+ # Ensure the Expressions node has a type. The semantic pass sets this via
+ # bind_to, but in some interpreter contexts it might not be set yet.
+ # Only set if not already set to avoid overwriting.
+ unless node.type?
+ if node.expressions.empty?
+ node.type = @context.program.nil_type
+ else
+ node.type = node.expressions.last.type?
+ end
+ end
+
false
end
There's a TODO comment acknowledging that there's something wrong with semantic pass: crystal/src/compiler/crystal/interpreter/compiler.cr Lines 1303 to 1306 in f6a1c53
|
This reverts commit 6367225.
Contributor
Author
|
Alternative fix is not robust. It still fails for the non-reduced code. The first fix works for both reduced and non-reduced code. |
Sija
reviewed
Dec 13, 2025
Co-authored-by: Sijawusz Pur Rahnama <[email protected]>
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.
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.