[Bug #21764] Propagate the encoding of ID to warning

This commit is contained in:
Nobuyoshi Nakada 2025-12-05 00:27:45 +09:00
parent 465a86c341
commit cf12aff059
No known key found for this signature in database
GPG Key ID: 3582D74E1FEE4465
Notes: git 2025-12-04 16:02:02 +00:00
4 changed files with 47 additions and 20 deletions

20
io.c
View File

@ -8641,31 +8641,19 @@ rb_f_printf(int argc, VALUE *argv, VALUE _)
return Qnil;
}
static void
deprecated_str_setter(VALUE val, ID id, VALUE *var)
{
rb_str_setter(val, id, &val);
if (!NIL_P(val)) {
rb_warn_deprecated("'%s'", NULL, rb_id2name(id));
}
*var = val;
}
extern void rb_deprecated_str_setter(VALUE val, ID id, VALUE *var);
static void
deprecated_rs_setter(VALUE val, ID id, VALUE *var)
{
rb_deprecated_str_setter(val, id, &val);
if (!NIL_P(val)) {
if (!RB_TYPE_P(val, T_STRING)) {
rb_raise(rb_eTypeError, "value of %"PRIsVALUE" must be String", rb_id2str(id));
}
if (rb_str_equal(val, rb_default_rs)) {
val = rb_default_rs;
}
else {
val = rb_str_frozen_bare_string(val);
}
rb_enc_str_coderange(val);
rb_warn_deprecated("'%s'", NULL, rb_id2name(id));
}
*var = val;
}
@ -15730,7 +15718,7 @@ Init_IO(void)
rb_define_method(rb_cIO, "initialize", rb_io_initialize, -1);
rb_output_fs = Qnil;
rb_define_hooked_variable("$,", &rb_output_fs, 0, deprecated_str_setter);
rb_define_hooked_variable("$,", &rb_output_fs, 0, rb_deprecated_str_setter);
rb_default_rs = rb_fstring_lit("\n"); /* avoid modifying RS_default */
rb_vm_register_global_object(rb_default_rs);
@ -15740,7 +15728,7 @@ Init_IO(void)
rb_gvar_ractor_local("$/"); // not local but ractor safe
rb_define_hooked_variable("$-0", &rb_rs, 0, deprecated_rs_setter);
rb_gvar_ractor_local("$-0"); // not local but ractor safe
rb_define_hooked_variable("$\\", &rb_output_rs, 0, deprecated_str_setter);
rb_define_hooked_variable("$\\", &rb_output_rs, 0, rb_deprecated_str_setter);
rb_define_virtual_variable("$_", get_LAST_READ_LINE, set_LAST_READ_LINE);
rb_gvar_ractor_local("$_");

View File

@ -748,6 +748,10 @@ describe "Predefined global $/" do
it "raises a TypeError if assigned a boolean" do
-> { $/ = true }.should raise_error(TypeError, 'value of $/ must be String')
end
it "warns if assigned non-nil" do
-> { $/ = "_" }.should complain(/warning: (?:non-nil )?[`']\$\/' is deprecated/)
end
end
describe "Predefined global $-0" do
@ -825,6 +829,10 @@ describe "Predefined global $-0" do
it "raises a TypeError if assigned a boolean" do
-> { $-0 = true }.should raise_error(TypeError, 'value of $-0 must be String')
end
it "warns if assigned non-nil" do
-> { $-0 = "_" }.should complain(/warning: (?:non-nil )?[`']\$-0' is deprecated/)
end
end
describe "Predefined global $\\" do
@ -864,6 +872,10 @@ describe "Predefined global $\\" do
-> { $\ = 1 }.should raise_error(TypeError, 'value of $\ must be String')
-> { $\ = true }.should raise_error(TypeError, 'value of $\ must be String')
end
it "warns if assigned non-nil" do
-> { $\ = "_" }.should complain(/warning: (?:non-nil )?[`']\$\\' is deprecated/)
end
end
describe "Predefined global $," do
@ -880,7 +892,7 @@ describe "Predefined global $," do
end
it "warns if assigned non-nil" do
-> { $, = "_" }.should complain(/warning: [`']\$,' is deprecated/)
-> { $, = "_" }.should complain(/warning: (?:non-nil )?[`']\$,' is deprecated/)
end
end
@ -917,7 +929,7 @@ describe "Predefined global $;" do
end
it "warns if assigned non-nil" do
-> { $; = "_" }.should complain(/warning: [`']\$;' is deprecated/)
-> { $; = "_" }.should complain(/warning: (?:non-nil )?[`']\$;' is deprecated/)
end
end

View File

@ -11375,6 +11375,21 @@ rb_str_setter(VALUE val, ID id, VALUE *var)
*var = val;
}
static void
nil_setter_warning(ID id)
{
rb_warn_deprecated("'%"PRIsVALUE"'", NULL, rb_id2str(id));
}
void
rb_deprecated_str_setter(VALUE val, ID id, VALUE *var)
{
rb_str_setter(val, id, var);
if (!NIL_P(*var)) {
nil_setter_warning(id);
}
}
static void
rb_fs_setter(VALUE val, ID id, VALUE *var)
{
@ -11385,7 +11400,7 @@ rb_fs_setter(VALUE val, ID id, VALUE *var)
rb_id2str(id));
}
if (!NIL_P(val)) {
rb_warn_deprecated("'$;'", NULL);
nil_setter_warning(id);
}
*var = val;
}

View File

@ -1886,6 +1886,17 @@ CODE
assert_raise_with_message(TypeError, /\$;/) {
$; = []
}
name = "\u{5206 5217}"
assert_separately([], "#{<<~"do;"}\n#{<<~"end;"}")
do;
alias $#{name} $;
assert_deprecated_warning(/\\$#{name}/) { $#{name} = "" }
assert_raise_with_message(TypeError, /\\$#{name}/) { $#{name} = 1 }
end;
end
def test_fs_gc
return unless @cls == String
assert_separately(%W[-W0], "#{<<~"begin;"}\n#{<<~'end;'}")
bug = '[ruby-core:79582] $; must not be GCed'
@ -2761,7 +2772,7 @@ CODE
assert_equal([S("abcdb"), S("c"), S("e")], S("abcdbce").rpartition(/b\Kc/))
end
def test_fs_setter
def test_rs
return unless @cls == String
assert_raise(TypeError) { $/ = 1 }
@ -2769,6 +2780,7 @@ CODE
assert_separately([], "#{<<~"do;"}\n#{<<~"end;"}")
do;
alias $#{name} $/
assert_deprecated_warning(/\\$#{name}/) { $#{name} = "" }
assert_raise_with_message(TypeError, /\\$#{name}/) { $#{name} = 1 }
end;
end