summaryrefslogtreecommitdiff
path: root/internal.h
diff options
context:
space:
mode:
authorJeremy Evans <code@jeremyevans.net>2019-08-21 13:59:36 -0700
committerNobuyoshi Nakada <nobu@ruby-lang.org>2019-12-12 15:50:19 +0900
commit55b7ba368696033f2e89b77cbcd4a05dec97b139 (patch)
tree797c7c459c25bd585cb56a3eb5b159181594ac5e /internal.h
parent1a4a9bdb5da973f8a89e699ce6d0fb1ca21090bd (diff)
downloadruby-55b7ba368696033f2e89b77cbcd4a05dec97b139.tar.gz
Make super in instance_eval in method in module raise TypeError
This makes behavior the same as super in instance_eval in method in class. The reason this wasn't implemented before is that there is a check to determine if the self in the current context is of the expected class, and a module itself can be included in multiple classes, so it doesn't have an expected class. Implementing this requires giving iclasses knowledge of which class created them, so that super call in the module method knows the expected class for super calls. This reference is called includer, and should only be set for iclasses. Note that the approach Ruby uses in this check is not robust. If you instance_eval another object of the same class and call super, instead of an TypeError, you get super called with the instance_eval receiver instead of the method receiver. Truly fixing super would require keeping a reference to the super object (method receiver) in each frame where scope has changed, and using that instead of current self when calling super. Fixes [Bug #11636]
Diffstat (limited to 'internal.h')
-rw-r--r--internal.h8
1 files changed, 8 insertions, 0 deletions
diff --git a/internal.h b/internal.h
index 374db5c0a4..3bd95c2c25 100644
--- a/internal.h
+++ b/internal.h
@@ -1039,6 +1039,7 @@ struct rb_classext_struct {
const VALUE origin_;
const VALUE refined_class;
rb_alloc_func_t allocator;
+ const VALUE includer;
};
typedef struct rb_classext_struct rb_classext_t;
@@ -1078,6 +1079,7 @@ int rb_singleton_class_internal_p(VALUE sklass);
#else
# define RCLASS_SERIAL(c) (RCLASS_EXT(c)->class_serial)
#endif
+#define RCLASS_INCLUDER(c) (RCLASS_EXT(c)->includer)
#define RCLASS_CLONED FL_USER6
#define RICLASS_IS_ORIGIN FL_USER5
@@ -1090,6 +1092,12 @@ RCLASS_SET_ORIGIN(VALUE klass, VALUE origin)
if (klass != origin) FL_SET(origin, RICLASS_IS_ORIGIN);
}
+static inline void
+RCLASS_SET_INCLUDER(VALUE iclass, VALUE klass)
+{
+ RB_OBJ_WRITE(iclass, &RCLASS_INCLUDER(iclass), klass);
+}
+
#undef RCLASS_SUPER
static inline VALUE
RCLASS_SUPER(VALUE klass)