Skip to content
Merged
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
51 changes: 51 additions & 0 deletions internal/vm/prelude.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2412,3 +2412,54 @@ def method_missing(name, *args, &block)
end
end
end

# Warning is the module Ruby routes warnings through. Warning.warn writes a
# message to $stderr and can be overridden; Warning.[] / Warning.[]= read and
# toggle whether each warning category is enabled.
module Warning
@warning_categories = {
deprecated: false,
experimental: true,
performance: false,
strict_unused_block: false,
}

class << self
# Warning.[](category) reports whether the category's warnings are enabled. A
# non-Symbol raises TypeError; an unknown category raises ArgumentError.
def [](category)
unless category.is_a?(Symbol)
raise TypeError, "wrong argument type #{category.class} (expected Symbol)"
end
unless @warning_categories.key?(category)
raise ArgumentError, "unknown category: #{category}"
end
@warning_categories[category]
end

# Warning.[]=(category, flag) enables or disables the category, with the same
# validation as Warning.[].
def []=(category, flag)
unless category.is_a?(Symbol)
raise TypeError, "wrong argument type #{category.class} (expected Symbol)"
end
unless @warning_categories.key?(category)
raise ArgumentError, "unknown category: #{category}"
end
@warning_categories[category] = flag
end

# Warning.categories lists the known warning categories.
def categories
@warning_categories.keys
end
end

# Warning.warn writes the message to $stderr unchanged and returns nil. It is a
# module function (via extend self) so it can be overridden by redefining it.
def warn(message, category: nil)
$stderr.write(message)
nil
end
extend self
end
104 changes: 102 additions & 2 deletions internal/vm/prelude_frozen_gen.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

44 changes: 44 additions & 0 deletions internal/vm/warning_module_internal_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
// Copyright (c) the go-embedded-ruby/ruby authors
//
// SPDX-License-Identifier: BSD-3-Clause

package vm

import "testing"

// TestWarningModule covers the Warning module: the per-category enable flags read
// and written through Warning.[] / Warning.[]= (with TypeError for a non-Symbol
// and ArgumentError for an unknown category), Warning.categories, and that
// Warning.warn returns nil and is an overridable module function owned by
// Warning (extend self). Verified against ruby 4.0.6.
func TestWarningModule(t *testing.T) {
cases := []struct{ src, want string }{
// Default category flags.
{`p Warning[:deprecated]`, `false`},
{`p Warning[:experimental]`, `true`},
{`p Warning[:performance]`, `false`},
{`p Warning[:strict_unused_block]`, `false`},
// []= toggles a category and [] reads it back.
{`Warning[:performance] = true; p Warning[:performance]`, `true`},
{`Warning[:deprecated] = true; p Warning[:deprecated]`, `true`},
// categories lists the known categories.
{`p Warning.categories.include?(:deprecated) && Warning.categories.include?(:experimental) && Warning.categories.include?(:performance)`, `true`},
// An unknown category raises ArgumentError.
{`begin; Warning[:noop]; rescue ArgumentError => e; p e.message; end`, `"unknown category: noop"`},
{`begin; Warning[:noop] = false; rescue ArgumentError => e; p e.message; end`, `"unknown category: noop"`},
// A non-Symbol category raises TypeError.
{`begin; Warning[42]; rescue TypeError => e; p e.class; end`, `TypeError`},
{`begin; Warning["deprecated"]; rescue TypeError => e; p e.class; end`, `TypeError`},
{`begin; Warning[false] = true; rescue TypeError => e; p e.class; end`, `TypeError`},
// warn is owned by Warning, which extends itself.
{`p Warning.method(:warn).owner`, `Warning`},
{`p Warning.singleton_class.ancestors.include?(Warning)`, `true`},
// warn is overridable.
{`def Warning.warn(m); "got: #{m}"; end; p Warning.warn("hi")`, `"got: hi"`},
}
for _, c := range cases {
if got := eval(t, c.src); got != c.want+"\n" {
t.Errorf("src=%q got=%q want=%q", c.src, got, c.want+"\n")
}
}
}