Skip to content

Commit

Permalink
RUBY_TRY_UNUSED_BLOCK_WARNING_STRICT
Browse files Browse the repository at this point in the history
`RUBY_TRY_UNUSED_BLOCK_WARNING_STRICT=1 ruby ...` will enable
strict check for unused block warning.

This option is only for trial to compare the results so the
envname is not considered well.
Should be removed before Ruby 3.4.0 release.
  • Loading branch information
ko1 committed Apr 19, 2024
1 parent 7522d1b commit 662ce92
Show file tree
Hide file tree
Showing 4 changed files with 19 additions and 5 deletions.
7 changes: 5 additions & 2 deletions compile.c
Original file line number Diff line number Diff line change
Expand Up @@ -2006,8 +2006,11 @@ iseq_set_use_block(rb_iseq_t *iseq)
body->param.flags.use_block = 1;

rb_vm_t *vm = GET_VM();
st_data_t key = (st_data_t)rb_intern_str(body->location.label); // String -> ID
st_insert(vm->unused_block_warning_table, key, 1);

if (!vm->unused_block_warning_strict) {
st_data_t key = (st_data_t)rb_intern_str(body->location.label); // String -> ID
st_insert(vm->unused_block_warning_table, key, 1);
}
}
}

Expand Down
6 changes: 6 additions & 0 deletions vm.c
Original file line number Diff line number Diff line change
Expand Up @@ -4261,6 +4261,12 @@ Init_BareVM(void)
vm->constant_cache = rb_id_table_create(0);
vm->unused_block_warning_table = st_init_numtable();

// TODO: remove before Ruby 3.4.0 release
const char *s = getenv("RUBY_TRY_UNUSED_BLOCK_WARNING_STRICT");
if (s && strcmp(s, "1") == 0) {
vm->unused_block_warning_strict = true;
}

// setup main thread
th->nt = ZALLOC(struct rb_native_thread);
th->vm = vm;
Expand Down
1 change: 1 addition & 0 deletions vm_core.h
Original file line number Diff line number Diff line change
Expand Up @@ -774,6 +774,7 @@ typedef struct rb_vm_struct {
struct rb_id_table *negative_cme_table;
st_table *overloaded_cme_table; // cme -> overloaded_cme
st_table *unused_block_warning_table;
bool unused_block_warning_strict;

// This id table contains a mapping from ID to ICs. It does this with ID
// keys and nested st_tables as values. The nested tables have ICs as keys
Expand Down
10 changes: 7 additions & 3 deletions vm_insnhelper.c
Original file line number Diff line number Diff line change
Expand Up @@ -2978,6 +2978,7 @@ warn_unused_block(const rb_callable_method_entry_t *cme, const rb_iseq_t *iseq,
{
rb_vm_t *vm = GET_VM();
st_table *dup_check_table = vm->unused_block_warning_table;
st_data_t key;

union {
VALUE v;
Expand All @@ -2989,14 +2990,17 @@ warn_unused_block(const rb_callable_method_entry_t *cme, const rb_iseq_t *iseq,
};

// relax check
st_data_t key = (st_data_t)cme->def->original_id;
if (!vm->unused_block_warning_strict) {
key = (st_data_t)cme->def->original_id;

if (st_lookup(dup_check_table, key, NULL)) {
return;
if (st_lookup(dup_check_table, key, NULL)) {
return;
}
}

// strict check
// make unique key from pc and me->def pointer
key = 0;
for (int i=0; i<SIZEOF_VALUE; i++) {
// fprintf(stderr, "k1:%3d k2:%3d\n", k1.b[i], k2.b[SIZEOF_VALUE-1-i]);
key |= (st_data_t)(k1.b[i] ^ k2.b[SIZEOF_VALUE-1-i]) << (8 * i);
Expand Down

0 comments on commit 662ce92

Please sign in to comment.