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 end
22482248 end
22492249
2250+ # `obj.attr = val` (attr-writer call) — Ruby semantics: the
2251+ # assignment expression evaluates to the rhs value, so the
2252+ # inferred type is the rhs argument's type. Without this, a
2253+ # chain like `@a = obj.attr = val` mistypes the outer `@a`
2254+ # as int because the inner call falls through to the int
2255+ # default.
2256+ if mname.length > 1 && mname[mname.length - 1] == "=" &&
2257+ mname != "==" && mname != "!=" && mname != "<=" && mname != ">="
2258+ if recv >= 0
2259+ rt_w = infer_type(recv)
2260+ if is_obj_type(rt_w) == 1
2261+ 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) == 1
2266+ args_id_w = @nd_arguments[nid]
2267+ if args_id_w >= 0
2268+ arg_ids_w = get_args(args_id_w)
2269+ if arg_ids_w.length > 0
2270+ return infer_type(arg_ids_w[0])
2271+ end
2272+ end
2273+ end
2274+ end
2275+ end
2276+ end
2277+
22502278 # User-defined top-level method (bare call): take precedence over
22512279 # 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.