Commit 84a68794

84a68794be0aMerged

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

Description

Improve type inference for OBJECT receivers and reopened classes

Four key improvements to reduce "unknown#method" warnings for lrama:

  1. Class reopening support (codegen.c): When a class is defined across multiple files (common in Ruby gems), merge methods/ivars into the existing class_info_t instead of skipping the reopened definition. Update origin_parser when the initialize method is found. Add bounds checks and dedup guards to prevent overflow during merging.

  2. Block return type heuristic (type.c): For receiver-less calls with blocks where the method is unknown (e.g., report_duration { @grammar }), infer the return type from the block body's last expression. Restricted to non-lambda mode to avoid interfering with lambda calculus programs.

  3. Ivar name-to-class heuristic (type.c): When an ivar has unresolved VALUE type, try matching its snake_case name to a known CamelCase class (e.g., @grammar -> Grammar, @symbols_resolver -> SymbolsResolver).

  4. Method body call-site analysis (type.c): Scan class method bodies for constructor calls and method calls on typed receivers, propagating argument types to target method parameters. This complements the existing top-level and function-body call-site analysis.

All 60 tests pass. Lrama compilation no longer crashes and processes significantly more code from required files.

Co-Authored-By: Claude Opus 4.6 (1M context) [email protected]

Details

Commit
84a68794be0a
Parent
411bf93a5b3a

src/codegen.cmodifiedView file

@@ -328,7 +328,16 @@
328328
329329 static void analyze_method(codegen_ctx_t *ctx, class_info_t *cls,
330330 pm_def_node_t *def) {
331+ if (cls->method_count >= MAX_METHODS) return;
332+ /* Skip if method already registered (class reopening) */
333+ {
334+ char *dn = cstr(ctx, def->name);
335+ method_info_t *existing = find_method(cls, dn);
336+ free(dn);
337+ if (existing) return;
338+ }
331339 method_info_t *m = &cls->methods[cls->method_count++];
340+ memset(m, 0, sizeof(*m));
332341 char *name = cstr(ctx, def->name);
333342 snprintf(m->name, sizeof(m->name), "%s", name);
334343 free(name);
@@ -418,20 +427,26 @@
418427 }
419428 }
420429
430+static void analyze_module(codegen_ctx_t *ctx, pm_module_node_t *node);
431+
421432 static void analyze_class(codegen_ctx_t *ctx, pm_class_node_t *node) {
422- /* Dedup: skip if class already registered */
433+ /* Check if class already registered (reopened class) — merge if so */
434+ class_info_t *cls = NULL;
423435 if (PM_NODE_TYPE(node->constant_path) == PM_CONSTANT_READ_NODE) {
424436 pm_constant_read_node_t *cr = (pm_constant_read_node_t *)node->constant_path;
425437 char *dn = cstr(ctx, cr->name);
426- if (find_class(ctx, dn)) { free(dn); return; }
438+ cls = find_class(ctx, dn);
427439 free(dn);
428440 }
429- if (ctx->class_count >= MAX_CLASSES) return;
430-
431- class_info_t *cls = &ctx->classes[ctx->class_count++];
432- memset(cls, 0, sizeof(*cls));
433- cls->class_node = (pm_node_t *)node;
434- cls->origin_parser = ctx->parser;
441+ if (!cls) {
442+ if (ctx->class_count >= MAX_CLASSES) return;
443+ cls = &ctx->classes[ctx->class_count++];
444+ memset(cls, 0, sizeof(*cls));
445+ cls->origin_parser = ctx->parser;
446+ }
447+ /* Update class_node to the most recent definition with a body */
448+ if (node->body)
449+ cls->class_node = (pm_node_t *)node;
435450
436451 /* Get class name from constant_path */
437452 if (PM_NODE_TYPE(node->constant_path) == PM_CONSTANT_READ_NODE) {
@@ -528,7 +543,9 @@
528543
529544 /* def self.foo → class method */
530545 if (def->receiver && PM_NODE_TYPE(def->receiver) == PM_SELF_NODE) {
546+ if (cls->method_count >= MAX_METHODS) continue;
531547 method_info_t *m = &cls->methods[cls->method_count++];
548+ memset(m, 0, sizeof(*m));
532549 char *name = cstr(ctx, def->name);
533550 snprintf(m->name, sizeof(m->name), "%s", name);
534551 free(name);
@@ -559,9 +576,12 @@
559576
560577 analyze_method(ctx, cls, def);
561578
562- /* Extract ivars from initialize */
579+ /* Extract ivars from initialize — also update origin_parser
580+ * to the parser that owns the initialize method, since this is
581+ * the "main" class definition (important for reopened classes). */
563582 char *mname = cstr(ctx, def->name);
564583 if (strcmp(mname, "initialize") == 0) {
584+ cls->origin_parser = ctx->parser;
565585 analyze_ivars_from_init(ctx, cls, def->body ? (pm_node_t *)def->body : NULL);
566586 }
567587 free(mname);
@@ -590,9 +610,11 @@
590610 iv->type = vt_prim(SPINEL_TYPE_VALUE);
591611 }
592612
593- /* Generate getter */
594- if (is_accessor || is_reader) {
613+ /* Generate getter (skip if already registered) */
614+ if ((is_accessor || is_reader) &&
615+ !find_method(cls, sym_name) && cls->method_count < MAX_METHODS) {
595616 method_info_t *m = &cls->methods[cls->method_count++];
617+ memset(m, 0, sizeof(*m));
596618 snprintf(m->name, sizeof(m->name), "%s", sym_name);
597619 m->body_node = NULL;
598620 m->params_node = NULL;
@@ -604,8 +626,12 @@
604626 m->return_type = vt_prim(SPINEL_TYPE_VALUE);
605627 }
606628
607- /* Generate setter */
608- if (is_accessor || is_writer) {
629+ /* Generate setter (skip if already registered) */
630+ {
631+ char setter_name[64];
632+ snprintf(setter_name, sizeof(setter_name), "%.62s=", sym_name);
633+ if ((is_accessor || is_writer) &&
634+ !find_method(cls, setter_name) && cls->method_count < MAX_METHODS) {
609635 method_info_t *m = &cls->methods[cls->method_count++];
610636 snprintf(m->name, sizeof(m->name), "%.62s=", sym_name);
611637 m->body_node = NULL;
@@ -619,6 +645,7 @@
619645 snprintf(m->accessor_ivar, sizeof(m->accessor_ivar), "%s", sym_name);
620646 m->return_type = vt_prim(SPINEL_TYPE_VALUE);
621647 }
648+ }
622649 }
623650 }
624651 /* include ModuleName — record for mixin resolution */