Make constant assignments more conforming to JIS X 3017:2013 11.4.2.2.3

compile.c (NODE_CDECL): Evaluate the module before the value
test/ruby/test_const.rb (test_evaluation_order): added a test case
This commit is contained in:
Yuki Yugui Sonoda 2019-06-16 22:28:34 +09:00
parent 2fb1564c02
commit 44caca11cf
2 changed files with 21 additions and 8 deletions

View File

@ -6855,21 +6855,26 @@ iseq_compile_each0(rb_iseq_t *iseq, LINK_ANCHOR *const ret, const NODE *node, in
break; break;
} }
case NODE_CDECL:{ case NODE_CDECL:{
CHECK(COMPILE(ret, "lvalue", node->nd_value)); ID base_id;
if (!popped) {
ADD_INSN(ret, line, dup);
}
if (node->nd_vid) { if (node->nd_vid) {
ADD_INSN1(ret, line, putspecialobject, ADD_INSN1(ret, line, putspecialobject,
INT2FIX(VM_SPECIAL_OBJECT_CONST_BASE)); INT2FIX(VM_SPECIAL_OBJECT_CONST_BASE));
ADD_INSN1(ret, line, setconstant, ID2SYM(node->nd_vid)); base_id = node->nd_vid;
} }
else { else {
compile_cpath(ret, iseq, node->nd_else); compile_cpath(ret, iseq, node->nd_else);
ADD_INSN1(ret, line, setconstant, ID2SYM(node->nd_else->nd_mid)); base_id = node->nd_else->nd_mid;
} }
CHECK(COMPILE(ret, "lvalue", node->nd_value));
if (popped) {
ADD_INSN(ret, line, swap);
} else {
ADD_INSN(ret, line, dup);
ADD_INSN1(ret, line, reverse, INT2FIX(3));
}
ADD_INSN1(ret, line, setconstant, ID2SYM(base_id));
break; break;
} }
case NODE_CVASGN:{ case NODE_CVASGN:{

View File

@ -69,4 +69,12 @@ PRE
def test_toplevel_lookup def test_toplevel_lookup
assert_raise(NameError, '[Feature #11547]') {TestConst::Object} assert_raise(NameError, '[Feature #11547]') {TestConst::Object}
end end
def test_evaluation_order
assert_raise_with_message(RuntimeError, "recv", 'JIS X 3017:2013 11.4.2.2.3') {
eval <<~EOS
raise('recv')::C = raise('value')
EOS
}
end
end end