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. Skippinginthere is deliberate -- a raw int local could be either a definite literal or an uninit placeholder, and either way the matching RBS would legitimately sayintso 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
end2048320483
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 treat20487-
# as high-confidence (no transitive inference). Used by the20484+
# Returns the literal type if `nid` is an AST literal expression20485+
# or a constant whose initializer is one, else "". Used by the2048820486
# method-arbitration definite-conflict check.20487+
#20488+
# Constants are immutable in Ruby, so a `BAD = "bad"` reference20489+
# downstream is observationally equivalent to writing `"bad"`20490+
# in place. Local variables are NOT included here -- they can be20491+
# reassigned, scope-shadowed, and widened by inference, so the20492+
# check would either need per-local literal-write tracking or20493+
# risk false positives. Confirmed users of the indirection20494+
# (`x = "bad"; method(x)`) fall through silently rather than20495+
# erroring.2048920496
def rbs_literal_expr_type(nid)2049020497
if nid < 02049120498
return ""@@ -20509,6 +20516,87 @@
2050920516
if t == "NilNode"2051020517
return "nil"2051120518
end20519+
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.length20523+
eid = @const_expr_ids[ci]20524+
if eid >= 020525+
# Recurse so a `C = D` chain (constant pointing at another20526+
# literal-init constant) still resolves through to the20527+
# underlying AST literal.20528+
return rbs_literal_expr_type(eid)20529+
end20530+
end20531+
end20532+
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.length20537+
eid = @const_expr_ids[ci]20538+
if eid >= 020539+
return rbs_literal_expr_type(eid)20540+
end20541+
end20542+
end20543+
end20544+
# Local variable whose declared / inferred type is a concrete20545+
# scalar -- treat the read as "definite enough" for the conflict20546+
# check. The walker pushes the body's precomputed locals before20547+
# reaching here, so `find_var_type` resolves through to the20548+
# scope-decl type. Skip poly / int (the int could be a placeholder20549+
# from an unset attr-shaped local; the matching RBS would20550+
# 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 vt20555+
end20556+
end20557+
# Call whose body's implicit final expression is a literal20558+
# (`def make; "bad"; end` → `make()` is observably "string"20559+
# forever). The call's return is as "definite" as the literal20560+
# inside its body. Only walks the body's last statement -- a20561+
# full return-path scan would need to merge multiple return20562+
# arms and check that ALL paths yield the same literal type.20563+
# That refinement is straightforward but adds enough surface20564+
# to be worth its own pass.20565+
if t == "CallNode"20566+
mname = @nd_name[nid]20567+
recv = @nd_receiver[nid]20568+
bid = -120569+
if recv < 020570+
mi = find_method_idx(mname)20571+
if mi >= 0 && mi < @meth_body_ids.length20572+
bid = @meth_body_ids[mi]20573+
end20574+
elsif @nd_type[recv] == "ConstantReadNode"20575+
rcname = @nd_name[recv]20576+
rci = find_class_idx(rcname)20577+
if rci >= 0 && rci < @cls_cmeth_bodies.length20578+
cmnames = @cls_cmeth_names[rci].split(";")20579+
cmj = 020580+
while cmj < cmnames.length20581+
if cmnames[cmj] == mname20582+
cmbodies = @cls_cmeth_bodies[rci].split(";")20583+
if cmj < cmbodies.length20584+
bid = cmbodies[cmj].to_i20585+
end20586+
cmj = cmnames.length20587+
else20588+
cmj = cmj + 120589+
end20590+
end20591+
end20592+
end20593+
if bid >= 020594+
lit = rbs_body_implicit_return_literal(bid)20595+
if lit != ""20596+
return lit20597+
end20598+
end20599+
end2051220600
""2051320601
end2051420602