Skip to content

Commit 5c8d013

Browse files
committed
fixing Warning#warn printing bug
1 parent 96e7f36 commit 5c8d013

File tree

4 files changed

+59
-0
lines changed

4 files changed

+59
-0
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -115,6 +115,7 @@ Bug fixes:
115115
* Fix `Kernel#sleep` and `Mutex#sleep` for durations smaller than 1 millisecond (#2716, @eregon).
116116
* Fix `IO#{wait,wait_readable,wait_writable}` with a timeout > INT_MAX seconds (@eregon).
117117
* Use the compatible encoding for `String#{sub,gsub,index,rindex}` (#2749, @eregon).
118+
* Fix `Warning#warn` called with category specified is no longer throwing exception (#20446, @horakivo).
118119

119120
Compatibility:
120121

spec/ruby/core/warning/warn_spec.rb

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,54 @@ def Warning.warn(msg)
7373
$VERBOSE = verbose
7474
end
7575
end
76+
77+
it "should print message when category is :deprecated and Warning[:deprecated] is true" do
78+
warn_deprecated = Warning[:deprecated]
79+
Warning[:deprecated] = true
80+
begin
81+
-> {
82+
warn("foo", category: :deprecated)
83+
}.should complain("foo\n")
84+
ensure
85+
Warning[:deprecated] = warn_deprecated
86+
end
87+
end
88+
89+
it "should print message when category is :experimental and Warning[:experimental] is true" do
90+
warn_experimental = Warning[:experimental]
91+
Warning[:experimental] = true
92+
begin
93+
-> {
94+
warn("foo", category: :experimental)
95+
}.should complain("foo\n")
96+
ensure
97+
Warning[:experimental] = warn_experimental
98+
end
99+
end
100+
101+
it "should not print message when category is :deprecated but Warning[:deprecated] is false" do
102+
warn_deprecated = Warning[:deprecated]
103+
Warning[:deprecated] = false
104+
begin
105+
-> {
106+
warn("foo", category: :deprecated)
107+
}.should_not complain
108+
ensure
109+
Warning[:deprecated] = warn_deprecated
110+
end
111+
end
112+
113+
it "should not print message when category is :experimental but Warning[:experimental] is false" do
114+
warn_experimental = Warning[:experimental]
115+
Warning[:experimental] = false
116+
begin
117+
-> {
118+
warn("foo", category: :experimental)
119+
}.should_not complain
120+
ensure
121+
Warning[:experimental] = warn_experimental
122+
end
123+
end
76124
end
77125

78126
ruby_version_is ''...'3.0' do

src/main/java/org/truffleruby/core/CoreLibrary.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1048,6 +1048,7 @@ public boolean isTruffleBootMainMethod(SharedMethodInfo info) {
10481048
"/core/truffle/method_operations.rb",
10491049
"/core/method.rb",
10501050
"/core/unbound_method.rb",
1051+
"/core/truffle/warning_operations.rb",
10511052
"/core/warning.rb",
10521053
"/core/weakmap.rb",
10531054
"/core/tracepoint.rb",

src/main/ruby/truffleruby/core/warning.rb

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,9 +16,18 @@ def warn(message, category: nil)
1616
unless message.encoding.ascii_compatible?
1717
raise Encoding::CompatibilityError, "ASCII incompatible encoding: #{message.encoding}"
1818
end
19+
1920
unless Primitive.nil?(category)
2021
Truffle::Type.rb_check_type(category, Symbol)
2122
Truffle::WarningOperations.check_category(category)
23+
24+
if category == :deprecated && !Warning[:deprecated]
25+
return nil
26+
end
27+
28+
if category == :experimental && !Warning[:experimental]
29+
return nil
30+
end
2231
end
2332

2433
$stderr.write message

0 commit comments

Comments
 (0)