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

@@ -2013,6 +2013,14 @@
20132013 if t == "RangeNode"
20142014 return "range"
20152015 end
2016+ if t == "DefinedNode"
2017+ # `defined?(expr)` returns a tag string ("local-variable",
2018+ # "instance-variable", "constant", "method", "expression") or
2019+ # nil. spinel emits as `const char*` with NULL for the nil case;
2020+ # `puts` already handles a NULL string by printing an empty
2021+ # line, matching CRuby's `puts nil`. Issue #711.
2022+ return "string"
2023+ end
20162024 if t == "RescueModifierNode"
20172025 # `expr rescue fallback` — unify the types of the two branches.
20182026 # The fallback always runs on error, so prefer its type when the
@@ -6582,7 +6590,13 @@
65826590 end
65836591 if t == "InstanceVariableWriteNode"
65846592 register_toplevel_ivar(@nd_name[nid], infer_ivar_init_type(@nd_expression[nid]))
6585- elsif t == "InstanceVariableReadNode" || t == "InstanceVariableTargetNode" || t == "InstanceVariableOperatorWriteNode" || t == "InstanceVariableAndWriteNode" || t == "InstanceVariableOrWriteNode"
6593+ elsif t == "InstanceVariableReadNode"
6594+ # Read-only at toplevel: register with type "nil" (read-only at
6595+ # class scope already uses "nil" via scan_ivars). The widening
6596+ # in register_toplevel_ivar later upgrades it to a concrete type
6597+ # on encountering a write. Issue #711.
6598+ register_toplevel_ivar(@nd_name[nid], "nil")
6599+ elsif t == "InstanceVariableTargetNode" || t == "InstanceVariableOperatorWriteNode" || t == "InstanceVariableAndWriteNode" || t == "InstanceVariableOrWriteNode"
65866600 register_toplevel_ivar(@nd_name[nid], "int")
65876601 end
65886602 cs = []