From 35af869d7d309d09509970d688cdedfd60dad0c8 Mon Sep 17 00:00:00 2001 From: "Yukihiro \"Matz\" Matsumoto" Date: Thu, 25 Dec 2025 01:24:58 +0900 Subject: [PATCH] vm.c: allow Hash subclasses to override []; close #6675 The OP_GETIDX optimization now only applies to instances of the Hash class itself. Subclasses fall back to method dispatch, allowing them to override the [] method. This fixes compatibility with libraries like mruby-hashie that rely on aliasing/overriding [] in subclasses. Trade-off: Hash#[] cannot be overridden on the Hash class itself (only on subclasses). This is a reasonable semantic for mruby since subclassing is the proper pattern for customization. Co-authored-by: Claude --- src/vm.c | 2 ++ 1 file changed, 2 insertions(+) diff --git a/src/vm.c b/src/vm.c index 9ea39ddc9..b74b70405 100644 --- a/src/vm.c +++ b/src/vm.c @@ -1892,6 +1892,8 @@ RETRY_TRY_BLOCK: } break; case MRB_TT_HASH: + /* optimize only for Hash class; subclasses may override [] */ + if (mrb_obj_class(mrb, va) != mrb->hash_class) goto getidx_fallback; va = mrb_hash_get(mrb, va, vb); ci = mrb->c->ci; regs[a] = va;