Commit 7b74e197

7b74e1972df2Merged

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

Description

feat(analyze): extend RBS conflict detection to indirect literal args (#626)

The method-param conflict check from fa12337 / fa6d555 only fired when the call site passed an AST literal directly. Followup broadens the "definite literal" recognizer to three indirections the user might reach for between writing a literal and reaching the call:

  • Constant references (BAD = "bad"; c.add(1, BAD)): walk @const_expr_ids to the literal's defining AST node. Constants are immutable, so the indirection is observationally equivalent to writing the literal in place.
  • Call returns whose body's implicit final expression is a literal (def make; "bad"; end; c.add(1, make)): inspect the callee's body, fall through to the literal's type. Conservative: only the single implicit-return path; multi- arm returns still fall through silently.
  • Local variables whose scope-decl type pins to a concrete scalar (x = "bad"; c.add(1, x)): the walker pushes the body's precomputed locals before reaching the check, so find_var_type resolves the LocalVariableReadNode to its converged type. Skipping int here is deliberate -- a raw int local could be either a definite literal or an uninit placeholder, and either way the matching RBS would legitimately say int so the comparison resolves equal without a false alarm.

Verified all four shapes (direct literal / constant / method return / local) all error; conforming code still builds clean.

Refs #626 (sub-issue 2).

Co-Authored-By: Claude Opus 4.7 [email protected]

Details

Commit
7b74e1972df2
Parent
216bee0d3f7e

spinel_analyze.rbmodifiedView file

@@ -20481,11 +20481,18 @@
2048120481 }
2048220482 end
2048320483
20484- # Returns the literal type if `nid` is an AST literal expression,
20485- # else "". Mirrors the relevant arms of `infer_ivar_init_type`
20486- # but stays focused on the literal-node shapes we want to treat
20487- # as high-confidence (no transitive inference). Used by the
20484+ # Returns the literal type if `nid` is an AST literal expression
20485+ # or a constant whose initializer is one, else "". Used by the
2048820486 # method-arbitration definite-conflict check.
20487+ #
20488+ # Constants are immutable in Ruby, so a `BAD = "bad"` reference
20489+ # downstream is observationally equivalent to writing `"bad"`
20490+ # in place. Local variables are NOT included here -- they can be
20491+ # reassigned, scope-shadowed, and widened by inference, so the
20492+ # check would either need per-local literal-write tracking or
20493+ # risk false positives. Confirmed users of the indirection
20494+ # (`x = "bad"; method(x)`) fall through silently rather than
20495+ # erroring.
2048920496 def rbs_literal_expr_type(nid)
2049020497 if nid < 0
2049120498 return ""
@@ -20509,6 +20516,87 @@
2050920516 if t == "NilNode"
2051020517 return "nil"
2051120518 end
20519+ if t == "ConstantReadNode"
20520+ cname = resolve_const_read_name(@nd_name[nid])
20521+ ci = find_const_idx(cname)
20522+ if ci >= 0 && ci < @const_expr_ids.length
20523+ eid = @const_expr_ids[ci]
20524+ if eid >= 0
20525+ # Recurse so a `C = D` chain (constant pointing at another
20526+ # literal-init constant) still resolves through to the
20527+ # underlying AST literal.
20528+ return rbs_literal_expr_type(eid)
20529+ end
20530+ end
20531+ end
20532+ if t == "ConstantPathNode"
20533+ cpname = resolve_const_ref_name(nid)
20534+ if cpname != ""
20535+ ci = find_const_idx(cpname)
20536+ if ci >= 0 && ci < @const_expr_ids.length
20537+ eid = @const_expr_ids[ci]
20538+ if eid >= 0
20539+ return rbs_literal_expr_type(eid)
20540+ end
20541+ end
20542+ end
20543+ end
20544+ # Local variable whose declared / inferred type is a concrete
20545+ # scalar -- treat the read as "definite enough" for the conflict
20546+ # check. The walker pushes the body's precomputed locals before
20547+ # reaching here, so `find_var_type` resolves through to the
20548+ # scope-decl type. Skip poly / int (the int could be a placeholder
20549+ # from an unset attr-shaped local; the matching RBS would
20550+ # legitimately say int so the comparison resolves equal anyway).
20551+ if t == "LocalVariableReadNode"
20552+ vt = find_var_type(@nd_name[nid])
20553+ if vt == "string" || vt == "float" || vt == "bool" || vt == "symbol"
20554+ return vt
20555+ end
20556+ end
20557+ # Call whose body's implicit final expression is a literal
20558+ # (`def make; "bad"; end` → `make()` is observably "string"
20559+ # forever). The call's return is as "definite" as the literal
20560+ # inside its body. Only walks the body's last statement -- a
20561+ # full return-path scan would need to merge multiple return
20562+ # arms and check that ALL paths yield the same literal type.
20563+ # That refinement is straightforward but adds enough surface
20564+ # to be worth its own pass.
20565+ if t == "CallNode"
20566+ mname = @nd_name[nid]
20567+ recv = @nd_receiver[nid]
20568+ bid = -1
20569+ if recv < 0
20570+ mi = find_method_idx(mname)
20571+ if mi >= 0 && mi < @meth_body_ids.length
20572+ bid = @meth_body_ids[mi]
20573+ end
20574+ elsif @nd_type[recv] == "ConstantReadNode"
20575+ rcname = @nd_name[recv]
20576+ rci = find_class_idx(rcname)
20577+ if rci >= 0 && rci < @cls_cmeth_bodies.length
20578+ cmnames = @cls_cmeth_names[rci].split(";")
20579+ cmj = 0
20580+ while cmj < cmnames.length
20581+ if cmnames[cmj] == mname
20582+ cmbodies = @cls_cmeth_bodies[rci].split(";")
20583+ if cmj < cmbodies.length
20584+ bid = cmbodies[cmj].to_i
20585+ end
20586+ cmj = cmnames.length
20587+ else
20588+ cmj = cmj + 1
20589+ end
20590+ end
20591+ end
20592+ end
20593+ if bid >= 0
20594+ lit = rbs_body_implicit_return_literal(bid)
20595+ if lit != ""
20596+ return lit
20597+ end
20598+ end
20599+ end
2051220600 ""
2051320601 end
2051420602