Commit 463acf8b

463acf8b5d9aMerged

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

Description

Fix class-level instance variable ||= memoization pattern

  • init civ_* statics to nil sentinels (SP_INT_NIL / NULL / {SP_TAG_NIL}) instead of zero/default_value so unset class ivars read as Ruby nil
  • add TY_INT / TY_STRING branches to InstanceVariableOrWriteNode codegen (both expr and stmt contexts) that check the nil sentinel and assign
  • remove InstanceVariableOrWriteNode/AndWriteNode from the tail-position "no tail value" list so the last @v ||= x expression returns @v
  • extend infer_ivar_types to recognise ||=/&&=/op= ivar write nodes

Fixes class_ivar_in_class_method test (Foo.cache ||= 42, Baz.memo, etc.)

Co-Authored-By: Claude Sonnet 4.6 [email protected]

Details

Commit
463acf8b5d9a
Parent
211c332e7682

src/analyze.cmodifiedView file

@@ -3277,7 +3277,10 @@
32773277 for (int id = 0; id < nt->count; id++) {
32783278 const char *ty = nt_type(nt, id);
32793279 if (!ty) continue;
3280- if (!strcmp(ty, "InstanceVariableWriteNode")) {
3280+ if (!strcmp(ty, "InstanceVariableWriteNode") ||
3281+ !strcmp(ty, "InstanceVariableOrWriteNode") ||
3282+ !strcmp(ty, "InstanceVariableAndWriteNode") ||
3283+ !strcmp(ty, "InstanceVariableOperatorWriteNode")) {
32813284 const char *nm = nt_str(nt, id, "name");
32823285 TyKind vt = infer_type(c, nt_ref(nt, id, "value"));
32833286 if (vt == TY_NIL) continue; /* nil write doesn't pin the ivar type */