Commit 4ce2dd66

4ce2dd66d634Merged

Yukihiro "Matz" Matsumoto <[email protected]> · 5 months ago · 2 files · ~2 modified

Description

Infer hash/array param types from body usage (post-fixpoint)

When a method parameter has no concretely-typed callers, the body's subscript pattern (param["key"] / param[:key]) and array-specific methods now widen the param to the appropriate hash/array type.

Runs as a post-fixpoint step (not inside the fixpoint loop) so that caller-side types always take priority — running inside the loop caused inferred types to conflict with caller-propagated types, widening to TY_POLY instead.

Add a codegen cast arm in emit_arg_or_default so callers that pass poly/nil can still reach a hash-typed param.

Fixes param_body_hash_inference, param_body_array_hash_inference.

Co-Authored-By: Claude Sonnet 4.6 [email protected]

Details

Commit
4ce2dd66d634
Parent
d45fae1a24a7

src/analyze.cmodifiedView file

@@ -3047,6 +3047,85 @@
30473047 return 0;
30483048 }
30493049
3050+/* Infer a still-unknown parameter as a typed hash when the body indexes
3051+ it with a literal key: `param["key"]` → str_poly_hash,
3052+ `param[:sym]` → sym_poly_hash. Runs in the fixpoint alongside
3053+ infer_string_params so methods with no concrete-typed caller still
3054+ resolve their hash param type from body usage. */
3055+static int infer_hash_params(Compiler *c) {
3056+ const NodeTable *nt = c->nt;
3057+ int changed = 0;
3058+ static const char *const hash_only_meths[] = {
3059+ "keys","values","each_pair","merge","has_key?","key?","fetch","store",
3060+ "delete","transform_values","transform_keys","to_h","each_with_object",NULL
3061+ };
3062+ for (int id = 0; id < nt->count; id++) {
3063+ const char *ty = nt_type(nt, id);
3064+ if (!ty || strcmp(ty, "CallNode")) continue;
3065+ const char *name = nt_str(nt, id, "name");
3066+ if (!name) continue;
3067+ int recv = nt_ref(nt, id, "receiver");
3068+ if (recv < 0) continue;
3069+ const char *rty = nt_type(nt, recv);
3070+ if (!rty || strcmp(rty, "LocalVariableReadNode")) continue;
3071+ Scope *s = comp_scope_of(c, id);
3072+ LocalVar *lv = scope_local(s, nt_str(nt, recv, "name"));
3073+ if (!lv || !lv->is_param || lv->type != TY_UNKNOWN) continue;
3074+ /* Literal-key [] / fetch: infer specific variant */
3075+ if (!strcmp(name, "[]") || !strcmp(name, "fetch")) {
3076+ int args = nt_ref(nt, id, "arguments");
3077+ int an = 0;
3078+ const int *argv = args >= 0 ? nt_arr(nt, args, "arguments", &an) : NULL;
3079+ if (an < 1) continue;
3080+ const char *kty = argv ? nt_type(nt, argv[0]) : NULL;
3081+ if (!kty) continue;
3082+ TyKind want = TY_UNKNOWN;
3083+ if (!strcmp(kty, "StringNode") || !strcmp(kty, "InterpolatedStringNode"))
3084+ want = TY_STR_POLY_HASH;
3085+ else if (!strcmp(kty, "SymbolNode"))
3086+ want = TY_SYM_POLY_HASH;
3087+ if (want == TY_UNKNOWN) continue;
3088+ lv->type = want; changed = 1;
3089+ continue;
3090+ }
3091+ /* Hash-only methods: widen to str_poly_hash (most common variant) */
3092+ for (int k = 0; hash_only_meths[k]; k++) {
3093+ if (!strcmp(name, hash_only_meths[k])) { lv->type = TY_STR_POLY_HASH; changed = 1; break; }
3094+ }
3095+ }
3096+ return changed;
3097+}
3098+
3099+/* Infer a still-unknown parameter as poly_array when the body calls an
3100+ array-only method on it: push/pop/shift/unshift/concat/length/size/empty?.
3101+ Does NOT fire on << (overlaps with Integer/String) or arithmetic ops.
3102+ Runs inside the fixpoint so array params without typed callers still resolve. */
3103+static int infer_array_params(Compiler *c) {
3104+ const NodeTable *nt = c->nt;
3105+ static const char *const arr_meths[] = {
3106+ "push","pop","shift","unshift","concat","flatten","compact","transpose",
3107+ "each_with_index","each_with_object","zip","combination","permutation",NULL
3108+ };
3109+ int changed = 0;
3110+ for (int id = 0; id < nt->count; id++) {
3111+ const char *ty = nt_type(nt, id);
3112+ if (!ty || strcmp(ty, "CallNode")) continue;
3113+ const char *name = nt_str(nt, id, "name");
3114+ if (!name) continue;
3115+ int is_arr = 0;
3116+ for (int k = 0; arr_meths[k]; k++) if (!strcmp(name, arr_meths[k])) { is_arr = 1; break; }
3117+ if (!is_arr) continue;
3118+ int recv = nt_ref(nt, id, "receiver");
3119+ if (recv < 0) continue;
3120+ const char *rty = nt_type(nt, recv);
3121+ if (!rty || strcmp(rty, "LocalVariableReadNode")) continue;
3122+ Scope *s = comp_scope_of(c, id);
3123+ LocalVar *lv = scope_local(s, nt_str(nt, recv, "name"));
3124+ if (lv && lv->is_param && lv->type == TY_UNKNOWN) { lv->type = TY_POLY_ARRAY; changed = 1; }
3125+ }
3126+ return changed;
3127+}
3128+
30503129 /* Infer a still-unknown parameter as String when the body calls a
30513130 String-only method on it (a param with no concrete-typed caller). */
30523131 static int infer_string_params(Compiler *c) {
@@ -4253,6 +4332,19 @@
42534332 /* recompute returns: a method returning such a param is now poly */
42544333 for (int iter = 0; iter < 8; iter++) if (!infer_return_types(c)) break;
42554334
4335+ /* Post-fixpoint body-usage inference: type any param still TY_UNKNOWN
4336+ from how it is used inside the method body (hash subscript patterns,
4337+ array-specific calls). Runs after the main fixpoint so caller-side
4338+ types always win; the mini-loop below propagates the new types. */
4339+ if (infer_hash_params(c) | infer_array_params(c)) {
4340+ for (int iter = 0; iter < 16; iter++) {
4341+ int ch = 0;
4342+ ch |= infer_param_types(c);
4343+ ch |= infer_return_types(c);
4344+ if (!ch) break;
4345+ }
4346+ }
4347+
42564348 /* Post-fixpoint: unify param types across method override families.
42574349 When an override widens a param to TY_POLY but the parent (or
42584350 sibling) keeps it scalar, the generated C signatures disagree and