Skip to content

Commit 0d4e18f

Browse files
committed
Fix Kernel.warn and don't call Warning#warn if a specified category is disabled
1 parent 2224072 commit 0d4e18f

File tree

4 files changed

+8
-5
lines changed

4 files changed

+8
-5
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@ Compatibility:
3939
* Fix `Thread#{thread_variable_get,thread_variable_set,thread_variable?,key?,[],[]=,fetch}` and convert a non-String/Symbol thread-local variable name to String using `#to_str` (@andrykonchin).
4040
* Fix formatting in `Exception#full_message` when `RuntimeError` is not handled and `highlight` option is specified (@andrykonchin).
4141
* Fix `String#encode` and convert fallback values into String using `#to_str` (@andrykonchin).
42+
* Fix `Kernel.warn` and don't call `Warning#warn` if a specified category is disabled (@andrykonchin).
4243

4344
Performance:
4445

spec/ruby/core/warning/warn_spec.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -121,7 +121,7 @@ def Warning.warn(msg)
121121
end
122122
end
123123

124-
ruby_bug '#19530', ''...'3.4' do
124+
ruby_bug '#20573', ''...'3.4' do
125125
it "isn't called by Kernel.warn when category is :deprecated but Warning[:deprecated] is false" do
126126
warn_deprecated = Warning[:deprecated]
127127
begin

spec/tags/core/warning/warn_tags.txt

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,5 +2,3 @@ slow:Warning.warn has Warning as the method owner
22
slow:Warning.warn can be overridden
33
slow:Warning.warn does not add a newline
44
slow:Warning.warn returns nil
5-
fails:Warning.warn isn't called by Kernel.warn when category is :deprecated but Warning[:deprecated] is false
6-
fails:Warning.warn isn't called by Kernel.warn when category is :experimental but Warning[:experimental] is false

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

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -629,6 +629,12 @@ def untrace_var(name, cmd = undefined)
629629

630630
def warn(*messages, uplevel: undefined, category: nil)
631631
if !Primitive.nil?($VERBOSE) && !messages.empty?
632+
unless Primitive.nil?(category)
633+
category = Truffle::Type.rb_convert_type(category, Symbol, :to_sym)
634+
Truffle::WarningOperations.check_category(category)
635+
return nil unless Warning[category]
636+
end
637+
632638
prefix = if Primitive.undefined?(uplevel)
633639
''
634640
else
@@ -662,15 +668,13 @@ def warn(*messages, uplevel: undefined, category: nil)
662668
unless message.encoding.ascii_compatible?
663669
raise Encoding::CompatibilityError, "ASCII incompatible encoding: #{message.encoding}"
664670
end
665-
Truffle::WarningOperations.check_category(category) unless Primitive.nil?(category)
666671

667672
$stderr.write message
668673
else
669674
warning_warn = Warning.method(:warn)
670675
if warning_warn.arity == 1
671676
warning_warn.call(message)
672677
else
673-
category = Truffle::Type.rb_convert_type(category, Symbol, :to_sym) unless Primitive.nil?(category)
674678
warning_warn.call(message, category: category)
675679
end
676680
end

0 commit comments

Comments
 (0)