Commit ec8f1b3e
ec8f1b3ebf46Merged
Ryan Seys <[email protected]> · 5 months ago · 4 files · +2 added · ~2 modified
Description
analyze,codegen: resolve const_get with a literal name
Module.const_get(:K) was unsupported even for a literal symbol/string
name. Constants live in a flat namespace (cst_), so a literal name
resolves exactly like a ConstantRead: emit the constant value, infer the
constant's type.
A literal name that does not resolve now raises NameError at runtime, matching CRuby rather than rejecting at compile time: "uninitialized constant " for a valid constant name (qualified by a named module receiver, e.g. M::Missing, but not by Object), and "wrong constant name " for a name without a leading uppercase. The receiver is evaluated for its side effects first. A dynamic (non-literal) name cannot be resolved ahead of time and is diagnosed.
Co-Authored-By: Claude Opus 4.8 (1M context) [email protected]
Details
- Commit
- ec8f1b3ebf46
- Parent
- d4f52ea7d0bb
src/analyze_infer.cmodifiedView file
if (!strcmp(name, "!")) return TY_BOOL; if (!strcmp(name, "respond_to?") && recv >= 0) return TY_BOOL; if ((!strcmp(name, "method_defined?") || !strcmp(name, "const_defined?")) && recv >= 0) return TY_BOOL; /* const_get(:K) with a literal name resolves to the constant's type (codegen emits cst_<K>); a literal name that does not resolve raises NameError at runtime, so its value type is poly. A dynamic name is left unresolved. */ if (!strcmp(name, "const_get") && recv >= 0 && argc >= 1) { const char *cgt = nt_type(nt, argv[0]); const char *cgn = NULL; if (cgt && !strcmp(cgt, "SymbolNode")) cgn = nt_str(nt, argv[0], "value"); else if (cgt && !strcmp(cgt, "StringNode")) cgn = nt_str(nt, argv[0], "content"); if (cgn) { LocalVar *cv = comp_const(c, cgn); if (cv && cv->type != TY_UNKNOWN) return cv->type; return TY_POLY; } } if (!strcmp(name, "nil?") && recv >= 0 && argc == 0) return TY_BOOL; if (!strcmp(name, "object_id") && recv >= 0 && argc == 0) return TY_INT; if (!strcmp(name, "between?") && argc == 2 && (rt == TY_STRING || ty_is_numeric(rt))) return TY_BOOL;src/codegen_call.cmodifiedView file
Loading diff…
test/const_get_literal.rbaddedView file
Loading diff…
test/const_get_literal.rb.expectedaddedView file
Loading diff…