Commit 91406e8e
91406e8e7e3cMerged
Yukihiro "Matz" Matsumoto <[email protected]> · 6 months ago · 2 files · +1 added · ~1 modified
Description
Merge pull request #270 from mame/pr/chained-attr-setter
fix(codegen): infer_call_type for attr-writer returns rhs type
Details
- Commit
- 91406e8e7e3c
- Parent
- 6207dbdf55b1
- Merge parent
- a332dd6e56d8
spinel_codegen.rbmodifiedView file
@@ -2247,6 +2247,34 @@
22472247
end22482248
end22492249
2250+
# `obj.attr = val` (attr-writer call) — Ruby semantics: the2251+
# assignment expression evaluates to the rhs value, so the2252+
# inferred type is the rhs argument's type. Without this, a2253+
# chain like `@a = obj.attr = val` mistypes the outer `@a`2254+
# as int because the inner call falls through to the int2255+
# default.2256+
if mname.length > 1 && mname[mname.length - 1] == "=" &&2257+
mname != "==" && mname != "!=" && mname != "<=" && mname != ">="2258+
if recv >= 02259+
rt_w = infer_type(recv)2260+
if is_obj_type(rt_w) == 12261+
bname_w = mname[0, mname.length - 1]2262+
bt_w = base_type(rt_w)2263+
cname_w = bt_w[4, bt_w.length - 4]2264+
ci_w = find_class_idx(cname_w)2265+
if ci_w >= 0 && cls_has_attr_writer(ci_w, bname_w) == 12266+
args_id_w = @nd_arguments[nid]2267+
if args_id_w >= 02268+
arg_ids_w = get_args(args_id_w)2269+
if arg_ids_w.length > 02270+
return infer_type(arg_ids_w[0])2271+
end2272+
end2273+
end2274+
end2275+
end2276+
end2277+
22502278
# User-defined top-level method (bare call): take precedence over22512279
# name-based builtin inference so `def minmax(a,b); ... end; minmax(1,2)`22522280
# binds to the user def instead of Array#minmax's tuple return.test/chained_attr_setter.rbaddedView file
Loading diff…