Commit 9e5a124a
9e5a124ac71bMerged
Yukihiro "Matz" Matsumoto <[email protected]> · 5 months ago · 4 files · +2 added · ~2 modified
Description
Merge pull request #1496 from ryanseys/rs-const-get-foldable
Resolve const_get with a literal name
Details
- Commit
- 9e5a124ac71b
- Parent
- d4f52ea7d0bb
- Merge parent
- ec8f1b3ebf46
src/analyze_infer.cmodifiedView file
@@ -2194,6 +2194,16 @@
21942194
if (!strcmp(name, "!")) return TY_BOOL;21952195
if (!strcmp(name, "respond_to?") && recv >= 0) return TY_BOOL;21962196
if ((!strcmp(name, "method_defined?") || !strcmp(name, "const_defined?")) && recv >= 0) return TY_BOOL;2197+
/* const_get(:K) with a literal name resolves to the constant's type (codegen2198+
emits cst_<K>); a literal name that does not resolve raises NameError at2199+
runtime, so its value type is poly. A dynamic name is left unresolved. */2200+
if (!strcmp(name, "const_get") && recv >= 0 && argc >= 1) {2201+
const char *cgt = nt_type(nt, argv[0]);2202+
const char *cgn = NULL;2203+
if (cgt && !strcmp(cgt, "SymbolNode")) cgn = nt_str(nt, argv[0], "value");2204+
else if (cgt && !strcmp(cgt, "StringNode")) cgn = nt_str(nt, argv[0], "content");2205+
if (cgn) { LocalVar *cv = comp_const(c, cgn); if (cv && cv->type != TY_UNKNOWN) return cv->type; return TY_POLY; }2206+
}21972207
if (!strcmp(name, "nil?") && recv >= 0 && argc == 0) return TY_BOOL;21982208
if (!strcmp(name, "object_id") && recv >= 0 && argc == 0) return TY_INT;21992209
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…