Skip to content

Commit d8c231b

Browse files
committed
fstring: don't assume FL_EXIVAR mean there are ivars.
1 parent a6c9b20 commit d8c231b

File tree

2 files changed

+23
-6
lines changed

2 files changed

+23
-6
lines changed

string.c

+11-2
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,7 @@
4545
#include "ruby/thread.h"
4646
#include "ruby/util.h"
4747
#include "ruby_assert.h"
48+
#include "shape.h"
4849
#include "vm_sync.h"
4950

5051
#if defined HAVE_CRYPT_R
@@ -383,7 +384,16 @@ fstring_hash(VALUE str)
383384
#define fstring_hash rb_str_hash
384385
#endif
385386

386-
#define BARE_STRING_P(str) (!FL_ANY_RAW(str, FL_EXIVAR) && RBASIC_CLASS(str) == rb_cString)
387+
static inline bool
388+
BARE_STRING_P(VALUE str)
389+
{
390+
if (RBASIC_CLASS(str) != rb_cString) return false;
391+
392+
if (FL_TEST_RAW(str, FL_EXIVAR)) {
393+
return rb_ivar_count(str) == 0;
394+
}
395+
return true;
396+
}
387397

388398
static inline st_index_t
389399
str_do_hash(VALUE str)
@@ -872,7 +882,6 @@ register_fstring(VALUE str, bool copy, bool force_precompute_hash)
872882
RUBY_ASSERT(RB_TYPE_P(result, T_STRING));
873883
RUBY_ASSERT(OBJ_FROZEN(result));
874884
RUBY_ASSERT(!FL_TEST_RAW(result, STR_FAKESTR));
875-
RUBY_ASSERT(!FL_TEST_RAW(result, FL_EXIVAR));
876885
RUBY_ASSERT(RBASIC_CLASS(result) == rb_cString);
877886

878887
return result;

variable.c

+12-4
Original file line numberDiff line numberDiff line change
@@ -2301,23 +2301,31 @@ rb_ivar_count(VALUE obj)
23012301
{
23022302
if (SPECIAL_CONST_P(obj)) return 0;
23032303

2304+
st_index_t iv_count = 0;
23042305
switch (BUILTIN_TYPE(obj)) {
23052306
case T_OBJECT:
2306-
return ROBJECT_IV_COUNT(obj);
2307+
iv_count = ROBJECT_IV_COUNT(obj);
2308+
break;
23072309
case T_CLASS:
23082310
case T_MODULE:
2309-
return RCLASS_IV_COUNT(obj);
2311+
iv_count = RCLASS_IV_COUNT(obj);
2312+
break;
23102313
default:
23112314
if (FL_TEST(obj, FL_EXIVAR)) {
23122315
struct gen_ivtbl *ivtbl;
23132316

23142317
if (rb_gen_ivtbl_get(obj, 0, &ivtbl)) {
2315-
return gen_ivtbl_count(obj, ivtbl);
2318+
iv_count = gen_ivtbl_count(obj, ivtbl);
23162319
}
23172320
}
23182321
break;
23192322
}
2320-
return 0;
2323+
2324+
if (FL_TEST_RAW(obj, FL_SEEN_OBJ_ID)) {
2325+
iv_count--;
2326+
}
2327+
2328+
return iv_count;
23212329
}
23222330

23232331
static int

0 commit comments

Comments
 (0)