Skip to content

Commit

Permalink
Fix constant name of Ractor::IsolationError message
Browse files Browse the repository at this point in the history
`dest` of `const_decl_path` is `NODE_COLON2` or `NODE_COLON3` in some cases.
For example, `B::C ||= [“Not ” + “shareable”]` passes `NODE_COLON2`
and `::C ||= [“Not ” + “shareable”]` passes `NODE_COLON3`.
This commit fixes `Ractor::IsolationError` message for such case.

```
# shareable_constant_value: literal
::C ||= ["Not " + "shareable"]

# Before
# => cannot assign unshareable object to C (Ractor::IsolationError)

# After
# => cannot assign unshareable object to ::C (Ractor::IsolationError)
```
  • Loading branch information
yui-knk committed Jan 6, 2024
1 parent d5d038f commit e6ff28c
Show file tree
Hide file tree
Showing 2 changed files with 59 additions and 6 deletions.
31 changes: 25 additions & 6 deletions parse.y
Expand Up @@ -13639,12 +13639,30 @@ const_decl_path(struct parser_params *p, NODE **dest)
if (!nd_type_p(n, NODE_CALL)) {
const YYLTYPE *loc = &n->nd_loc;
VALUE path;
if (RNODE_CDECL(n)->nd_vid) {
path = rb_id2str(RNODE_CDECL(n)->nd_vid);
switch (nd_type(n)) {
case NODE_CDECL:
if (RNODE_CDECL(n)->nd_vid) {
path = rb_id2str(RNODE_CDECL(n)->nd_vid);
goto end;
}
else {
n = RNODE_CDECL(n)->nd_else;
}
break;
case NODE_COLON2:
break;
case NODE_COLON3:
// ::Const
path = rb_str_new_cstr("::");
rb_str_append(path, rb_id2str(RNODE_COLON3(n)->nd_mid));
goto end;
default:
rb_bug("unexpected node: %s", ruby_node_name(nd_type(n)));
UNREACHABLE_RETURN(0);
}
else {
n = RNODE_CDECL(n)->nd_else;
path = rb_ary_new();

path = rb_ary_new();
if (n) {
for (; n && nd_type_p(n, NODE_COLON2); n = RNODE_COLON2(n)->nd_head) {
rb_ary_push(path, rb_id2str(RNODE_COLON2(n)->nd_mid));
}
Expand All @@ -13662,8 +13680,9 @@ const_decl_path(struct parser_params *p, NODE **dest)
rb_ary_push(path, rb_str_new_cstr("..."));
}
path = rb_ary_join(rb_ary_reverse(path), rb_str_new_cstr("::"));
path = rb_fstring(path);
}
end:
path = rb_fstring(path);
*dest = n = NEW_LIT(path, loc);
RB_OBJ_WRITTEN(p->ast, Qnil, RNODE_LIT(n)->nd_lit);
}
Expand Down
34 changes: 34 additions & 0 deletions test/ruby/test_parse.rb
Expand Up @@ -1525,6 +1525,40 @@ def test_shareable_constant_value_unshareable_literal
::B::C = ["Not " + "shareable"]
end
end;
assert_separately([], "#{<<~"begin;"}\n#{<<~'end;'}")
begin;
assert_raise_with_message(Ractor::IsolationError, /unshareable object to ::C/) do
# shareable_constant_value: literal
::C ||= ["Not " + "shareable"]
end
end;
assert_raise_separately(Ractor::IsolationError, /unshareable object to B::C/,
"#{<<~"begin;"}\n#{<<~'end;'}")
begin;
# shareable_constant_value: literal
B = Class.new
B::C ||= ["Not " + "shareable"]
end;
assert_separately([], "#{<<~"begin;"}\n#{<<~'end;'}")
begin;
assert_raise_with_message(Ractor::IsolationError, /unshareable object to ::B::C/) do
# shareable_constant_value: literal
::B = Class.new
::B::C ||= ["Not " + "shareable"]
end
end;
assert_raise_separately(Ractor::IsolationError, /unshareable object to ...::C/,
"#{<<~"begin;"}\n#{<<~'end;'}")
begin;
# shareable_constant_value: literal
B = Class.new
def self.expr; B; end
expr::C ||= ["Not " + "shareable"]
end;
end
def test_shareable_constant_value_nonliteral
Expand Down

0 comments on commit e6ff28c

Please sign in to comment.