Commit bb638286
bb63828682c8Merged
Yukihiro "Matz" Matsumoto <[email protected]> · 6 months ago · 4 files · +2 added · ~2 modified
Description
fix(codegen,analyze): defined? resolves to a tag string or NULL (#711)
puts defined?(x) used to print a giant integer -- the codegen's
DefinedNode arm returned the C string literal "expression" and
infer_type defaulted to int, so puts printed the pointer's address
instead of the string.
Fix:
- infer_type returns "string" for DefinedNode.
- compile_expr inspects the inner node and resolves at compile time:
- LocalVariableReadNode -> "local-variable" if in scope else NULL
- InstanceVariableReadNode -> "instance-variable" if the slot's recorded type is anything other than "nil" (the read-only register-stub type), else NULL
- ConstantReadNode -> "constant" if registered as a const or a user/builtin class, else NULL
- bare CallNode -> "method" if find_method_idx finds it, else NULL
- NilNode / TrueNode / FalseNode / SelfNode -> "expression"
- Everything else -> "expression"
- scan_toplevel_ivars registers read-only @ivars with type "nil" (matching the class-method side from #712), so the new defined? ivar check can distinguish "scan-stub" from "actually written".
puts on a NULL const char* already prints an empty line, matching
CRuby's puts nil. So the nil cases print empty and the tag cases
print the tag.
Test: test/defined_op.rb covers all seven shapes plus a written- ivar positive case.
460 tests pass; optcarrot checksum 59662.
Closes #711
Co-Authored-By: Claude Opus 4.7 [email protected]
Details
- Commit
- bb63828682c8
- Parent
- 2163485fa3f9
spinel_analyze.rbmodifiedView file
if t == "RangeNode" return "range" end if t == "DefinedNode" # `defined?(expr)` returns a tag string ("local-variable", # "instance-variable", "constant", "method", "expression") or # nil. spinel emits as `const char*` with NULL for the nil case; # `puts` already handles a NULL string by printing an empty # line, matching CRuby's `puts nil`. Issue #711. return "string" end if t == "RescueModifierNode" # `expr rescue fallback` — unify the types of the two branches. # The fallback always runs on error, so prefer its type when the end if t == "InstanceVariableWriteNode" register_toplevel_ivar(@nd_name[nid], infer_ivar_init_type(@nd_expression[nid])) elsif t == "InstanceVariableReadNode" || t == "InstanceVariableTargetNode" || t == "InstanceVariableOperatorWriteNode" || t == "InstanceVariableAndWriteNode" || t == "InstanceVariableOrWriteNode" elsif t == "InstanceVariableReadNode" # Read-only at toplevel: register with type "nil" (read-only at # class scope already uses "nil" via scan_ivars). The widening # in register_toplevel_ivar later upgrades it to a concrete type # on encountering a write. Issue #711. register_toplevel_ivar(@nd_name[nid], "nil") elsif t == "InstanceVariableTargetNode" || t == "InstanceVariableOperatorWriteNode" || t == "InstanceVariableAndWriteNode" || t == "InstanceVariableOrWriteNode" register_toplevel_ivar(@nd_name[nid], "int") end cs = []spinel_codegen.rbmodifiedView file
Loading diff…
test/defined_op.rbaddedView file
Loading diff…
test/defined_op.rb.expectedaddedView file
Loading diff…