Commit 8b1d6cc1
8b1d6cc1af09Merged
Yukihiro "Matz" Matsumoto <[email protected]> · 5 months ago · 4 files · +2 added · ~2 modified
Description
Merge pull request #1495 from ryanseys/rs-ivar-get-set
Lower instance_variable_get/set on a literal name
Details
- Commit
- 8b1d6cc1af09
- Parent
- 02f605f199f0
- Merge parent
- ffd7802c32b8
src/analyze_infer.cmodifiedView file
@@ -1134,6 +1134,21 @@
11341134
if (!strcmp(name, "is_a?") || !strcmp(name, "kind_of?") || !strcmp(name, "instance_of?") ||11351135
!strcmp(name, "respond_to?") || !strcmp(name, "==") || !strcmp(name, "!=") ||11361136
!strcmp(name, "nil?") || !strcmp(name, "equal?") || !strcmp(name, "frozen?")) return TY_BOOL;1137+
/* instance_variable_get(:@x) yields @x's declared type; instance_variable_set1138+
yields the field type too (C `lvalue = v` evaluates to the lvalue). The1139+
codegen lowers both to a direct iv_ field access on the known layout. */1140+
if ((!strcmp(name, "instance_variable_get") || !strcmp(name, "instance_variable_set")) && argc >= 1) {1141+
const char *a0ty = nt_type(nt, argv[0]);1142+
if (a0ty && (!strcmp(a0ty, "SymbolNode") || !strcmp(a0ty, "StringNode"))) {1143+
const char *sym = !strcmp(a0ty, "SymbolNode")1144+
? nt_str(nt, argv[0], "value") : nt_str(nt, argv[0], "content");1145+
/* A name in the layout yields its declared type; an undefined-but-valid1146+
`@`-name reads as nil and a bad name (no `@`) raises NameError -- both poly. */1147+
int iv = (sym && sym[0] == '@') ? comp_ivar_index(cls, sym) : -1;1148+
if (iv >= 0) return cls->ivar_types[iv];1149+
return TY_POLY;1150+
}1151+
}11371152
/* attr reader (resolve alias so `alias v access_token` returns @access_token type) */11381153
{ int rdcls = -1;11391154
if (comp_reader_in_chain(c, cid, name, &rdcls)) {src/codegen_call.cmodifiedView file
Loading diff…
test/ivar_get_set_literal.rbaddedView file
Loading diff…
test/ivar_get_set_literal.rb.expectedaddedView file
Loading diff…