Commit 58a59172
58a591724069Merged
Yukihiro "Matz" Matsumoto <[email protected]> · 5 months ago · 1 file · ~1 modified
Description
Fix class constant as value: box via sp_box_class, fix emit_box_close for TY_UNKNOWN
- ConstantReadNode and ConstantPathNode in expression context now emit sp_box_class((sp_Class){cidx}) when the name resolves to a class, instead of calling unsupported()
- emit_boxed gains a class-constant arm so boxing context also works
- emit_box_close was incorrectly emitting ')' for TY_UNKNOWN (which emit_box_open ignores), producing unbalanced parens; treat TY_UNKNOWN as no-op like TY_POLY
- case/when now accepts ConstantPathNode in when-condition checking, and handles user-object type matching
Fixes module_accessor_nil_check test.
Co-Authored-By: Claude Sonnet 4.6 [email protected]
Details
- Commit
- 58a591724069
- Parent
- c54aa12fd0f6
src/codegen.cmodifiedView file
@@ -425,7 +425,7 @@
425425
}426426
static void emit_box_close(Compiler *c, TyKind t, Buf *b) {427427
(void)c;428-
if (t == TY_POLY) return; /* no-op */428+
if (t == TY_POLY || t == TY_UNKNOWN) return; /* no-op: already sp_RbVal */429429
if (ty_is_object(t)) { buf_printf(b, "), %d)", ty_object_class(t)); return; }430430
buf_puts(b, ")");431431
}@@ -11732,11 +11732,14 @@
1173211732
if (nm && !strcmp(nm, "RUBY_REVISION")) { buf_puts(b, "SPL(\"0\")"); return; }1173311733
if (nm && !strcmp(nm, "RUBY_COPYRIGHT")) { buf_puts(b, "SPL(\"ruby - Copyright (C) 1993-2023 Yukihiro Matsumoto\")"); return; }1173411734
if (nm && !strcmp(nm, "ARGV")) { buf_puts(b, "sp_get_ARGV()"); return; }11735-
/* unregistered or untyped constant: emit a runtime NameError raise */11736-
if (nm && comp_class_index(c, nm) < 0)11737-
buf_printf(b, "(sp_raise_cls(\"NameError\", \"uninitialized constant %s\"), (const char*)NULL)", nm);11738-
else11739-
unsupported(c, id, "constant read");11735+
if (nm) {11736+
int _cidx = comp_class_index(c, nm);11737+
if (_cidx >= 0)11738+
buf_printf(b, "sp_box_class((sp_Class){%d})", _cidx); /* class as value */11739+
else11740+
buf_printf(b, "(sp_raise_cls(\"NameError\", \"uninitialized constant %s\"), (const char*)NULL)", nm);11741+
}11742+
else unsupported(c, id, "constant read");1174011743
return;1174111744
}1174211745
if (!strcmp(ty, "ConstantPathNode")) {@@ -15156,6 +15159,12 @@
1515615159
}1515715160
/* regex values can appear in poly context (multi-typed local); box as nil */1515815161
if (t == TY_REGEX) { buf_puts(b, "sp_box_nil()"); return; }15162+
/* class constant (e.g. `A` in `slot = A`): box as sp_Class value */15163+
if (t == TY_UNKNOWN && nt_type(c->nt, node) && !strcmp(nt_type(c->nt, node), "ConstantReadNode")) {15164+
const char *_cnm = nt_str(c->nt, node, "name");15165+
int _cidx = _cnm ? comp_class_index(c, _cnm) : -1;15166+
if (_cidx >= 0) { buf_printf(b, "sp_box_class((sp_Class){%d})", _cidx); return; }15167+
}1515915168
/* an empty array literal [] has TY_UNKNOWN; box it as an empty PolyArray so1516015169
it can hold any element type when stored into a poly slot */1516115170
if (t == TY_UNKNOWN && nt_type(c->nt, node) && !strcmp(nt_type(c->nt, node), "ArrayNode")) {