Skip to content

Commit c81360d

Browse files
committed
Kernel#warn: don't call Warning.warn unless the category is enabled
[Bug #20573] Followup: #10960 I believe `Kernel#warn` should behave in the same way than internal `rb_warning_* APIs
1 parent ce06924 commit c81360d

File tree

2 files changed

+31
-1
lines changed

2 files changed

+31
-1
lines changed

spec/ruby/core/warning/warn_spec.rb

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -121,6 +121,30 @@ def Warning.warn(msg)
121121
end
122122
end
123123

124+
ruby_bug '#19530', ''...'3.4' do
125+
it "isn't called by Kernel.warn when category is :deprecated but Warning[:deprecated] is false" do
126+
warn_deprecated = Warning[:deprecated]
127+
begin
128+
Warning[:deprecated] = false
129+
Warning.should_not_receive(:warn)
130+
Kernel.warn("foo", category: :deprecated)
131+
ensure
132+
Warning[:deprecated] = warn_deprecated
133+
end
134+
end
135+
136+
it "isn't called by Kernel.warn when category is :experimental but Warning[:experimental] is false" do
137+
warn_experimental = Warning[:experimental]
138+
begin
139+
Warning[:experimental] = false
140+
Warning.should_not_receive(:warn)
141+
Kernel.warn("foo", category: :experimental)
142+
ensure
143+
Warning[:experimental] = warn_experimental
144+
end
145+
end
146+
end
147+
124148
it "prints the message when VERBOSE is false" do
125149
-> { Warning.warn("foo") }.should complain("foo")
126150
end

warning.rb

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,13 @@ module Kernel
4747
# be removed in the future.
4848
# :experimental :: Used for experimental features that may change in
4949
# future releases.
50+
# :performance :: Used for warning about APIs or pattern that have
51+
# negative performance impact
5052
def warn(*msgs, uplevel: nil, category: nil)
51-
Primitive.rb_warn_m(msgs, uplevel, category)
53+
if Primitive.cexpr!("NIL_P(category)")
54+
Primitive.rb_warn_m(msgs, uplevel, nil)
55+
elsif Warning[category = Primitive.cexpr!("rb_to_symbol_type(category)")]
56+
Primitive.rb_warn_m(msgs, uplevel, category)
57+
end
5258
end
5359
end

0 commit comments

Comments
 (0)