summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
author卜部昌平 <shyouhei@ruby-lang.org>2020-08-14 14:45:23 +0900
committer卜部昌平 <shyouhei@ruby-lang.org>2020-08-15 12:09:26 +0900
commitff30358d13d24d8202f2717c43700be70bdd49d3 (patch)
tree32757f4895a67fefcfb4830e40e2206a7ac388f7
parent72d0f2f0e0546a4c7c3b9ec85d3f67a36e9c5038 (diff)
RARRAY_AREF: convert into an inline function
RARRAY_AREF has been a macro for reasons. We might not be able to change that for public APIs, but why not relax the situation internally to make it an inline function.
Notes
Notes: Merged: https://github.com/ruby/ruby/pull/3419
-rw-r--r--array.c2
-rw-r--r--complex.c1
-rw-r--r--dir.c1
-rw-r--r--enumerator.c3
-rw-r--r--include/ruby/internal/core/rarray.h25
-rw-r--r--internal.h3
-rw-r--r--internal/array.h11
-rw-r--r--marshal.c1
-rw-r--r--pack.c1
-rw-r--r--random.c1
-rw-r--r--rational.c1
-rw-r--r--transcode.c1
12 files changed, 34 insertions, 17 deletions
diff --git a/array.c b/array.c
index d3e1985f55..ae2a4fd8b1 100644
--- a/array.c
+++ b/array.c
@@ -7221,7 +7221,7 @@ rb_ary_sample(rb_execution_context_t *ec, VALUE ary, VALUE randgen, VALUE nv, VA
return rb_ary_new_capa(0);
case 1:
i = rnds[0];
- return rb_ary_new_from_values(1, &RARRAY_AREF(ary, i));
+ return rb_ary_new_from_args(1, RARRAY_AREF(ary, i));
case 2:
i = rnds[0];
j = rnds[1];
diff --git a/complex.c b/complex.c
index 3e118677a3..b8dd6169ae 100644
--- a/complex.c
+++ b/complex.c
@@ -19,6 +19,7 @@
#define NDEBUG
#include "id.h"
#include "internal.h"
+#include "internal/array.h"
#include "internal/class.h"
#include "internal/complex.h"
#include "internal/math.h"
diff --git a/dir.c b/dir.c
index 957110823b..e5b6705b32 100644
--- a/dir.c
+++ b/dir.c
@@ -105,6 +105,7 @@ char *strchr(char*,char);
#include "encindex.h"
#include "id.h"
#include "internal.h"
+#include "internal/array.h"
#include "internal/dir.h"
#include "internal/encoding.h"
#include "internal/error.h"
diff --git a/enumerator.c b/enumerator.c
index 77cf565eec..3ea308a7cd 100644
--- a/enumerator.c
+++ b/enumerator.c
@@ -2087,7 +2087,8 @@ lazy_flat_map_proc(VALUE proc_entry, struct MEMO *result, VALUE memos, long memo
long i;
LAZY_MEMO_RESET_BREAK(result);
for (i = 0; i + 1 < RARRAY_LEN(ary); i++) {
- lazy_yielder_yield(result, proc_index, 1, &RARRAY_AREF(ary, i));
+ const VALUE argv = RARRAY_AREF(ary, i);
+ lazy_yielder_yield(result, proc_index, 1, &argv);
}
if (break_p) LAZY_MEMO_SET_BREAK(result);
if (i >= RARRAY_LEN(ary)) return 0;
diff --git a/include/ruby/internal/core/rarray.h b/include/ruby/internal/core/rarray.h
index a21500e1d2..938e2dc897 100644
--- a/include/ruby/internal/core/rarray.h
+++ b/include/ruby/internal/core/rarray.h
@@ -256,20 +256,15 @@ RARRAY_ASET(VALUE ary, long i, VALUE v)
RB_OBJ_WRITE(ary, &ptr[i], v));
}
-/* RARRAY_AREF is used as a lvalue. Cannot be a function. */
-#if 0
-RBIMPL_ATTR_PURE_UNLESS_DEBUG()
-RBIMPL_ATTR_ARTIFICIAL()
-static inline VALUE
-RARRAY_AREF(VALUE ary, long i)
-{
- RBIMPL_ASSERT_TYPE(ary, RUBY_T_ARRAY);
-
- return RARRAY_CONST_PTR_TRANSIENT(ary)[i];
-}
-#else
-# undef RARRAY_AREF
-# define RARRAY_AREF(a, i) RARRAY_CONST_PTR_TRANSIENT(a)[i]
-#endif
+/*
+ * :FIXME: we want to convert RARRAY_AREF into an inline function (to add rooms
+ * for more sanity checks). However there were situations where the address of
+ * this macro is taken i.e. &RARRAY_AREF(...). They cannot be possible if this
+ * is not a macro. Such usages are abuse, and we eliminated them internally.
+ * However we are afraid of similar things to remain in the wild. This macro
+ * remains as it is due to that. If we could warn such usages we can set a
+ * transition path, but currently no way is found to do so.
+ */
+#define RARRAY_AREF(a, i) RARRAY_CONST_PTR_TRANSIENT(a)[i]
#endif /* RBIMPL_RARRAY_H */
diff --git a/internal.h b/internal.h
index da0dab5e7e..303029ac60 100644
--- a/internal.h
+++ b/internal.h
@@ -31,6 +31,9 @@
/* Following macros were formerly defined in this header but moved to somewhere
* else. In order to detect them we undef here. */
+/* internal/array.h */
+#undef RARRAY_AREF
+
/* internal/class.h */
#undef RClass
#undef RCLASS_SUPER
diff --git a/internal/array.h b/internal/array.h
index 2e53d43c7d..c9f2b47145 100644
--- a/internal/array.h
+++ b/internal/array.h
@@ -100,4 +100,15 @@ RARY_TRANSIENT_UNSET(VALUE ary)
})
#endif
+#undef RARRAY_AREF
+RBIMPL_ATTR_PURE_UNLESS_DEBUG()
+RBIMPL_ATTR_ARTIFICIAL()
+static inline VALUE
+RARRAY_AREF(VALUE ary, long i)
+{
+ RBIMPL_ASSERT_TYPE(ary, RUBY_T_ARRAY);
+
+ return RARRAY_CONST_PTR_TRANSIENT(ary)[i];
+}
+
#endif /* INTERNAL_ARRAY_H */
diff --git a/marshal.c b/marshal.c
index 480ae599a2..2c6ed7a1dd 100644
--- a/marshal.c
+++ b/marshal.c
@@ -22,6 +22,7 @@
#include "encindex.h"
#include "id_table.h"
#include "internal.h"
+#include "internal/array.h"
#include "internal/bignum.h"
#include "internal/class.h"
#include "internal/encoding.h"
diff --git a/pack.c b/pack.c
index b6d252aa35..9fecc31a34 100644
--- a/pack.c
+++ b/pack.c
@@ -17,6 +17,7 @@
#include <sys/types.h>
#include "internal.h"
+#include "internal/array.h"
#include "internal/bits.h"
#include "internal/string.h"
#include "internal/symbol.h"
diff --git a/random.c b/random.c
index 8a59d1c0f9..d213f69bf9 100644
--- a/random.c
+++ b/random.c
@@ -56,6 +56,7 @@
#endif
#include "internal.h"
+#include "internal/array.h"
#include "internal/compilers.h"
#include "internal/numeric.h"
#include "internal/random.h"
diff --git a/rational.c b/rational.c
index 967cdce9c1..aa90342027 100644
--- a/rational.c
+++ b/rational.c
@@ -24,6 +24,7 @@
#define NDEBUG
#include "id.h"
#include "internal.h"
+#include "internal/array.h"
#include "internal/complex.h"
#include "internal/gc.h"
#include "internal/numeric.h"
diff --git a/transcode.c b/transcode.c
index fdc3514d78..3a37c63987 100644
--- a/transcode.c
+++ b/transcode.c
@@ -14,6 +14,7 @@
#include <ctype.h>
#include "internal.h"
+#include "internal/array.h"
#include "internal/inits.h"
#include "internal/object.h"
#include "internal/string.h"