Commit 6207dbdf

6207dbdf55b1Merged

Yukihiro "Matz" Matsumoto <[email protected]> · 6 months ago · 3 files · +2 added · ~1 modified

Description

Merge pull request #269 from OriPekelman/fix/266-def-length-poisons-inference

fix(codegen): #266 — don't commit param to obj_ on container-only body usage

Details

Commit
6207dbdf55b1
Parent
9c5dfd1d89ff
Merge parent
ed9cbf2de750

spinel_codegen.rbmodifiedView file

@@ -9008,7 +9008,7 @@
90089008 # them — issue #35.
90099009 called = "".split(",")
90109010 collect_param_methods(bid, pnames[pk], called)
9011- if called.length > 0
9011+ if called.length > 0 && called_methods_only_on_container_builtins(called) == 0
90129012 ci2 = 0
90139013 best = -1
90149014 while ci2 < @cls_names.length
@@ -9047,7 +9047,7 @@
90479047 if ptypes[pk] == "int"
90489048 called = "".split(",")
90499049 collect_param_methods(bid, pnames[pk], called)
9050- if called.length > 0
9050+ if called.length > 0 && called_methods_only_on_container_builtins(called) == 0
90519051 ci2 = 0
90529052 best = -1
90539053 while ci2 < @cls_names.length
@@ -9479,6 +9479,53 @@
94799479 return 1
94809480 end
94819481
9482+ # Issue #266: when picking a user class for a parameter from "what
9483+ # methods does the body call on it", reject sets that consist only
9484+ # of methods also available on built-in container types
9485+ # (int_array / float_array / str_array / sym_array / ptr_array).
9486+ # Otherwise a user class that happens to define `def length` (with
9487+ # nothing else) gets picked for any param the body calls `.length`
9488+ # on — even when the actual call site passes an IntArray. The body
9489+ # signal is too weak to commit; leave the param at "int" so call-
9490+ # site type unification (scan_new_calls) decides.
9491+ def called_methods_only_on_container_builtins(called)
9492+ k = 0
9493+ while k < called.length
9494+ m = called[k]
9495+ if m != "length" && m != "size" && m != "[]" && m != "[]=" &&
9496+ m != "<<" && m != "push" && m != "pop" &&
9497+ m != "shift" && m != "unshift" &&
9498+ m != "first" && m != "last" &&
9499+ m != "each" && m != "each_with_index" && m != "each_index" &&
9500+ m != "map" && m != "map!" && m != "collect" &&
9501+ m != "select" && m != "reject" && m != "filter" &&
9502+ m != "count" && m != "include?" &&
9503+ m != "empty?" && m != "any?" && m != "all?" && m != "none?" &&
9504+ m != "sort" && m != "sort!" && m != "reverse" && m != "reverse!" &&
9505+ m != "join" && m != "to_a" && m != "to_s" && m != "inspect" &&
9506+ m != "find" && m != "find_index" &&
9507+ m != "sum" && m != "min" && m != "max" && m != "minmax" &&
9508+ m != "concat" && m != "flatten" && m != "uniq" && m != "compact" &&
9509+ m != "slice" && m != "fetch" && m != "dig" &&
9510+ # Universal Object methods
9511+ m != "freeze" && m != "frozen?" && m != "dup" && m != "clone" &&
9512+ m != "hash" && m != "class" && m != "tap" &&
9513+ m != "==" && m != "!=" && m != "eql?" && m != "equal?" &&
9514+ m != "nil?" && m != "is_a?" && m != "kind_of?" && m != "respond_to?" &&
9515+ # Generic operators that exist on builtin numeric/container types
9516+ # too — `+` is concat on Array/String, arithmetic on Numeric;
9517+ # a param using just `+` is no more "user-classy" than `length`.
9518+ m != "+" && m != "-" && m != "*" && m != "/" && m != "%" &&
9519+ m != "&" && m != "|" && m != "^" && m != "~" &&
9520+ m != "<" && m != ">" && m != "<=" && m != ">=" && m != "<=>" &&
9521+ m != "===" && m != "!"
9522+ return 0
9523+ end
9524+ k = k + 1
9525+ end
9526+ return 1
9527+ end
9528+
94829529 def infer_ivar_types_from_writers
94839530 # Set up main scope for type inference
94849531 push_scope