diff options
author | 卜部昌平 <shyouhei@ruby-lang.org> | 2020-08-14 14:45:23 +0900 |
---|---|---|
committer | 卜部昌平 <shyouhei@ruby-lang.org> | 2020-08-15 12:09:26 +0900 |
commit | ff30358d13d24d8202f2717c43700be70bdd49d3 (patch) | |
tree | 32757f4895a67fefcfb4830e40e2206a7ac388f7 | |
parent | 72d0f2f0e0546a4c7c3b9ec85d3f67a36e9c5038 (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.c | 2 | ||||
-rw-r--r-- | complex.c | 1 | ||||
-rw-r--r-- | dir.c | 1 | ||||
-rw-r--r-- | enumerator.c | 3 | ||||
-rw-r--r-- | include/ruby/internal/core/rarray.h | 25 | ||||
-rw-r--r-- | internal.h | 3 | ||||
-rw-r--r-- | internal/array.h | 11 | ||||
-rw-r--r-- | marshal.c | 1 | ||||
-rw-r--r-- | pack.c | 1 | ||||
-rw-r--r-- | random.c | 1 | ||||
-rw-r--r-- | rational.c | 1 | ||||
-rw-r--r-- | transcode.c | 1 |
12 files changed, 34 insertions, 17 deletions
@@ -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]; @@ -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" @@ -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 */ @@ -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" @@ -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" @@ -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" |