summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorJean Boussier <[email protected]>2024-06-12 12:24:38 +0200
committerJean Boussier <[email protected]>2024-06-12 20:57:31 +0200
commitc81360db757bf1c1efbd36bb160e71de94bed8ed (patch)
tree2309a0c439ef1cb5ba7e9680f9dca4554eaa922b
parentce06924a17176d18816d968867858f97401d7c82 (diff)
Kernel#warn: don't call `Warning.warn` unless the category is enabled
[Bug #20573] Followup: https://github.com/ruby/ruby/pull/10960 I believe `Kernel#warn` should behave in the same way than internal `rb_warning_* APIs
-rw-r--r--spec/ruby/core/warning/warn_spec.rb24
-rw-r--r--warning.rb8
2 files changed, 31 insertions, 1 deletions
diff --git a/spec/ruby/core/warning/warn_spec.rb b/spec/ruby/core/warning/warn_spec.rb
index 8f96fe9287..1c21fe29e0 100644
--- a/spec/ruby/core/warning/warn_spec.rb
+++ b/spec/ruby/core/warning/warn_spec.rb
@@ -121,6 +121,30 @@ describe "Warning.warn" do
end
end
+ ruby_bug '#19530', ''...'3.4' do
+ it "isn't called by Kernel.warn when category is :deprecated but Warning[:deprecated] is false" do
+ warn_deprecated = Warning[:deprecated]
+ begin
+ Warning[:deprecated] = false
+ Warning.should_not_receive(:warn)
+ Kernel.warn("foo", category: :deprecated)
+ ensure
+ Warning[:deprecated] = warn_deprecated
+ end
+ end
+
+ it "isn't called by Kernel.warn when category is :experimental but Warning[:experimental] is false" do
+ warn_experimental = Warning[:experimental]
+ begin
+ Warning[:experimental] = false
+ Warning.should_not_receive(:warn)
+ Kernel.warn("foo", category: :experimental)
+ ensure
+ Warning[:experimental] = warn_experimental
+ end
+ end
+ end
+
it "prints the message when VERBOSE is false" do
-> { Warning.warn("foo") }.should complain("foo")
end
diff --git a/warning.rb b/warning.rb
index aab5e7c2c6..625dd9506b 100644
--- a/warning.rb
+++ b/warning.rb
@@ -47,7 +47,13 @@ module Kernel
# be removed in the future.
# :experimental :: Used for experimental features that may change in
# future releases.
+ # :performance :: Used for warning about APIs or pattern that have
+ # negative performance impact
def warn(*msgs, uplevel: nil, category: nil)
- Primitive.rb_warn_m(msgs, uplevel, category)
+ if Primitive.cexpr!("NIL_P(category)")
+ Primitive.rb_warn_m(msgs, uplevel, nil)
+ elsif Warning[category = Primitive.cexpr!("rb_to_symbol_type(category)")]
+ Primitive.rb_warn_m(msgs, uplevel, category)
+ end
end
end