Commit ba1d1f03

ba1d1f038b3fMerged

Yukihiro "Matz" Matsumoto <[email protected]> · 6 months ago · 1 file · ~1 modified

Description

fix(analyze): widen module @ivar=nil slot from later non-nil writes (#435)

Module-body @ivar = nil was inferred as type "nil" (c_type maps to mrb_int), pinning the cst__ slot to mrb_int. A later def self.X body writing a pointer (e.g. an FFI buffer read, a class instance) failed C compile with incompatible-pointer-to- integer-conversion.

refine_module_ivar_types now also fires when cur == "nil" and the init expression is a NilNode. A new scan_module_ivar_direct_writes walks the module's cmeth bodies for @ivar = expr writes and collects expr types; pick_module_ivar_nil_widen returns the single observed non-nil type, or poly when more than one type is observed.

Mirrors the existing empty-hash and empty-array refinements.

Closes #435

Co-Authored-By: Claude Opus 4.7 (1M context) [email protected]

Details

Commit
ba1d1f038b3f
Parent
6f1a73df1902

spinel_analyze.rbmodifiedView file

@@ -8536,17 +8536,20 @@
85368536 ci = find_const_idx(cname2)
85378537 next if ci < 0
85388538 cur = @const_types[ci]
8539- # Only refine when the recorded type is the empty-hash default
8540- # (str_int_hash) or empty-array default (int_array).
8541- next unless cur == "str_int_hash" || cur == "int_array"
8539+ # Refine when the recorded type is the empty-hash default
8540+ # (str_int_hash), empty-array default (int_array), or nil
8541+ # (from `@ivar = nil` lazy-init).
8542+ next unless cur == "str_int_hash" || cur == "int_array" || cur == "nil"
85428543 expr_id = @nd_expression[sid]
85438544 next unless expr_id >= 0
85448545 next unless (cur == "str_int_hash" && is_empty_hash_literal(expr_id) == 1) ||
8545- (cur == "int_array" && is_empty_array_literal(expr_id) == 1)
8546+ (cur == "int_array" && is_empty_array_literal(expr_id) == 1) ||
8547+ (cur == "nil" && @nd_type[expr_id] == "NilNode")
85468548 # Walk all class methods in the module looking for writes to
85478549 # this ivar.
85488550 key_t_set = "".split(",")
85498551 val_t_set = "".split(",")
8552+ direct_t_set = "".split(",")
85508553 body_stmts.each { |sid2|
85518554 next unless @nd_type[sid2] == "DefNode"
85528555 bid = @nd_body[sid2]
@@ -8592,6 +8595,9 @@
85928595 @current_method_name = synth_name
85938596 if iv_write
85948597 scan_module_ivar_writes(bid, iname, key_t_set, val_t_set)
8598+ if cur == "nil"
8599+ scan_module_ivar_direct_writes(bid, iname, direct_t_set)
8600+ end
85958601 else
85968602 scan_module_const_writes(bid, iname, key_t_set, val_t_set)
85978603 end
@@ -8614,10 +8620,51 @@
86148620 @const_types[ci] = new_t
86158621 mark_array_needs(new_t)
86168622 end
8623+ elsif cur == "nil"
8624+ # `@ivar = nil` at module body → slot defaults to "nil" (mrb_int).
8625+ # Pick the widest non-nil write type observed in any cmeth body so
8626+ # `@ivar = <ptr>` later in `def self.configure` widens the slot.
8627+ new_t = pick_module_ivar_nil_widen(direct_t_set)
8628+ if new_t != "" && new_t != cur
8629+ @const_types[ci] = new_t
8630+ end
86178631 end
86188632 }
86198633 end
86208634
8635+ # Walk `nid`'s subtree collecting types of every InstanceVariableWriteNode
8636+ # whose name == iname. Used to widen module-level `@ivar = nil` slots
8637+ # when a later cmeth assigns a non-nil value (e.g. FFI pointer read).
8638+ def scan_module_ivar_direct_writes(nid, iname, type_set)
8639+ if nid < 0
8640+ return
8641+ end
8642+ if @nd_type[nid] == "InstanceVariableWriteNode" && @nd_name[nid] == iname
8643+ expr_id = @nd_expression[nid]
8644+ if expr_id >= 0
8645+ wt = infer_type(expr_id)
8646+ if wt != "" && wt != "nil"
8647+ uniq_push(type_set, wt)
8648+ end
8649+ end
8650+ end
8651+ cs = []
8652+ push_child_ids(nid, cs)
8653+ k = 0
8654+ while k < cs.length
8655+ scan_module_ivar_direct_writes(cs[k], iname, type_set)
8656+ k = k + 1
8657+ end
8658+ end
8659+
8660+ def pick_module_ivar_nil_widen(type_set)
8661+ return "" if type_set.length == 0
8662+ if type_set.length == 1
8663+ return type_set[0]
8664+ end
8665+ "poly"
8666+ end
8667+
86218668 def mark_hash_needs(t)
86228669 if t == "sym_int_hash"
86238670 @needs_sym_int_hash = 1