Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 25 additions & 6 deletions core/src/main/java/org/jruby/RubyModule.java
Original file line number Diff line number Diff line change
Expand Up @@ -5624,10 +5624,7 @@ private IRubyObject setConstantCommon(ThreadContext context, String name, IRubyO
if (hiddenObj != null) hidden = hiddenObj;
boolean notAutoload = oldEntry.value != UNDEF;
if (notAutoload) {
if (warn) {
warn(context, "already initialized constant " +
(this.equals(objectClass(context)) ? name : (this + "::" + name)));
}
if (warn && context.runtime.warningsEnabled()) warnAlreadyInitialized(context, name);

storeConstant(context, name, value, hidden, file, line);
} else {
Expand Down Expand Up @@ -6126,8 +6123,30 @@ public ConstantEntry fetchConstantEntry(ThreadContext context, String name, bool

@CRuby(value = "rb_const_warn_if_deprecated", file = "variable.c", note = "warning only, callers check the deprecated flag")
private void warnDeprecatedConstant(ThreadContext context, String name) {
String parent = "Object".equals(getName(context)) ? "" : getName(context);
warnDeprecated(context, "constant " + parent + "::" + name + " is deprecated");
Ruby runtime = context.runtime;
if (!runtime.warningsEnabled() || !runtime.getWarnings().hasDeprecationWarningEnabled()) return;

RubyString quotedName = quoteConstantName(context, name);
warnDeprecated(context, this == objectClass(context) ?
str(runtime, "constant ::", quotedName, " is deprecated") :
str(runtime, "constant ", rubyName(context), "::", quotedName, " is deprecated"));
}

@CRuby(value = "const_warning_emit", file = "variable.c", note = "warning only, no previous definition line")
private void warnAlreadyInitialized(ThreadContext context, String name) {
RubyString quotedName = quoteConstantName(context, name);
warn(context, this == objectClass(context) ?
str(context.runtime, "already initialized constant ", quotedName) :
str(context.runtime, "already initialized constant ", rubyName(context), "::", quotedName));
}

// name is an id string (raw bytes), so it is read through its symbol. A warning must not
// raise Encoding::CompatibilityError, so also escape it if it can't join the module name.
private RubyString quoteConstantName(ThreadContext context, String name) {
RubyString quotedName = asSymbol(context, name).quoteUnprintable(context);
if (StringSupport.areCompatible(rubyName(context), quotedName) != null) return quotedName;

return (RubyString) RubyString.rbStrEscape(context, quotedName);
}

@Deprecated(since = "10.0.0.0")
Expand Down
6 changes: 6 additions & 0 deletions core/src/main/java/org/jruby/RubySymbol.java
Original file line number Diff line number Diff line change
Expand Up @@ -481,6 +481,12 @@ public IRubyObject inspect(ThreadContext context) {
return newString(context, result);
}

/** This symbol's name for a message, escaped if it can't be shown as is. */
@CRuby(value = "rb_id_quote_unprintable", file = "string.c")
public RubyString quoteUnprintable(ThreadContext context) {
return isSimpleName(context) ? fstring : (RubyString) RubyString.rbStrEscape(context, fstring);
}

@CRuby(value = "rb_str_symname_p", file = "string.c")
public boolean isSimpleName(ThreadContext context) {
int len;
Expand Down
43 changes: 43 additions & 0 deletions spec/ruby/core/module/const_set_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,49 @@
mod.const_get(:Foo).should == 1
end

describe "with a non-ASCII constant name" do
before :each do
@external = Encoding.default_external
@internal = Encoding.default_internal
Encoding.default_internal = nil
end

after :each do
Encoding.default_external = @external
Encoding.default_internal = @internal
end

it "includes the name in the warning" do
Encoding.default_external = Encoding::UTF_8
mod = Module.new
mod.const_set "X\u{5b9a 6570}", 42
-> {
mod.const_set "X\u{5b9a 6570}", 1
}.should complain(/already initialized constant .+::X\u{5b9a 6570}$/)
end

it "escapes the name in the warning if the default external encoding cannot show it" do
Encoding.default_external = Encoding::US_ASCII
mod = Module.new
mod.const_set "X\u{5b9a 6570}", 42
-> {
mod.const_set "X\u{5b9a 6570}", 1
}.should complain(/already initialized constant .+::X\\u5B9A\\u6570$/)
end

it "still reassigns the constant if the module and constant names have incompatible encodings" do
Encoding.default_external = Encoding::UTF_8
mod = Module.new
Module.new.const_set "M\u{5b9a}".encode(Encoding::EUC_JP), mod
mod.const_set "X\u{6570}", 42
# The text of this warning differs between implementations
-> {
mod.const_set "X\u{6570}", 1
}.should complain
mod.const_get("X\u{6570}").should == 1
end
end

it "does not warn if the previous value was an autoload" do
mod = Module.new
mod.autoload :Foo, "not-existing"
Expand Down
34 changes: 34 additions & 0 deletions spec/ruby/core/module/deprecate_constant_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,15 @@
Warning[:deprecated] = deprecated
end
end

it "names the module without calling #to_s" do
@module.deprecate_constant :PUBLIC1
def @module.to_s
raise "to_s called"
end

-> { @module::PUBLIC1 }.should complain(/warning: constant #<Module:0x\h+>::PUBLIC1 is deprecated/)
end
end

ruby_bug '#20900', ''...'3.4' do
Expand All @@ -64,6 +73,31 @@
@module.deprecate_constant(:PUBLIC1).should.equal?(@module)
end

describe "with a non-ASCII constant name" do
before :each do
@external = Encoding.default_external
@internal = Encoding.default_internal
Encoding.default_internal = nil
@module.const_set "X\u{5b9a 6570}", @value
@module.deprecate_constant "X\u{5b9a 6570}"
end

after :each do
Encoding.default_external = @external
Encoding.default_internal = @internal
end

it "includes the name in the warning" do
Encoding.default_external = Encoding::UTF_8
-> { @module.const_get "X\u{5b9a 6570}" }.should complain(/warning: constant .+::X\u{5b9a 6570} is deprecated/)
end

it "escapes the name in the warning if the default external encoding cannot show it" do
Encoding.default_external = Encoding::US_ASCII
-> { @module.const_get "X\u{5b9a 6570}" }.should complain(/warning: constant .+::X\\u5B9A\\u6570 is deprecated/)
end
end

it "raises a NameError when given an undefined name" do
-> { @module.deprecate_constant :UNDEFINED }.should.raise(NameError)
end
Expand Down
2 changes: 1 addition & 1 deletion test/mri/excludes/TestConst.rb
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
exclude :test_redefinition, "error string has badly encoded MBC and missing original location"
exclude :test_redefinition, "missing original location"
exclude :test_redefinition_memory_leak, "no working assert_no_memory_leak method"
Loading