diff --git a/internal/vm/prelude.rb b/internal/vm/prelude.rb index 0fc96240..3fa47cb0 100644 --- a/internal/vm/prelude.rb +++ b/internal/vm/prelude.rb @@ -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 diff --git a/internal/vm/prelude_frozen_gen.go b/internal/vm/prelude_frozen_gen.go index 65eaff63..b90883fb 100644 --- a/internal/vm/prelude_frozen_gen.go +++ b/internal/vm/prelude_frozen_gen.go @@ -10,8 +10,8 @@ import ( func embeddedPrelude() *bytecode.ISeq { return &bytecode.ISeq{ Name: "
", - Insns: []bytecode.Instr{{Op: 43}, {Op: 9}, {Op: 43, A: 1, B: 1}, {Op: 9}, {Op: 42, A: 2, B: 2}, {Op: 9}, {Op: 42, A: 3, B: 3}, {Op: 9}, {Op: 42, A: 4, B: 4}, {Op: 9}, {Op: 42, A: 5, B: 5}, {Op: 9}, {Op: 42, A: 6, B: 6}, {Op: 9}, {Op: 42, A: 7, B: 7}, {Op: 9}, {Op: 42, A: 8, B: 8}, {Op: 9}, {Op: 42, A: 9, B: 9}, {Op: 9}, {Op: 42, A: 10, B: 10}, {Op: 9}, {Op: 43, A: 11, B: 11}, {Op: 9}, {Op: 42, A: 12, B: 12}, {Op: 9}, {Op: 43, A: 13, B: 13}, {Op: 9}, {Op: 43, A: 14, B: 14}, {Op: 9}, {Op: 42, A: 15, B: 15}, {Op: 9}, {Op: 42, A: 16, B: 16}, {Op: 9}, {Op: 44, A: 17, B: 17}, {Op: 9}, {Op: 42, A: 18, B: 18}, {Op: 9}, {Op: 43, A: 19, B: 19}, {Op: 9}, {Op: 42, A: 20, B: 20}, {Op: 9}, {Op: 43, A: 21, B: 21}, {Op: 9}, {Op: 43, A: 22, B: 22}, {Op: 9}, {Op: 42, A: 23, B: 23}, {Op: 9}, {Op: 33, A: 23}, {Op: 41, A: 24}, {Op: 9}, {Op: 42, A: 25, B: 24}, {Op: 53}}, - Names: []string{"Comparable", "Enumerable", "Numeric", "Integer", "Float", "String", "Symbol", "Array", "Range", "Hash", "Set", "Gem", "OpenStruct", "Forwardable", "SingleForwardable", "Delegator", "SimpleDelegator", "DelegateClass", "Pathname", "Singleton", "ERB", "Slim", "Haml", "OptionParser", "OptParse", "Tempfile"}, + Insns: []bytecode.Instr{{Op: 43}, {Op: 9}, {Op: 43, A: 1, B: 1}, {Op: 9}, {Op: 42, A: 2, B: 2}, {Op: 9}, {Op: 42, A: 3, B: 3}, {Op: 9}, {Op: 42, A: 4, B: 4}, {Op: 9}, {Op: 42, A: 5, B: 5}, {Op: 9}, {Op: 42, A: 6, B: 6}, {Op: 9}, {Op: 42, A: 7, B: 7}, {Op: 9}, {Op: 42, A: 8, B: 8}, {Op: 9}, {Op: 42, A: 9, B: 9}, {Op: 9}, {Op: 42, A: 10, B: 10}, {Op: 9}, {Op: 43, A: 11, B: 11}, {Op: 9}, {Op: 42, A: 12, B: 12}, {Op: 9}, {Op: 43, A: 13, B: 13}, {Op: 9}, {Op: 43, A: 14, B: 14}, {Op: 9}, {Op: 42, A: 15, B: 15}, {Op: 9}, {Op: 42, A: 16, B: 16}, {Op: 9}, {Op: 44, A: 17, B: 17}, {Op: 9}, {Op: 42, A: 18, B: 18}, {Op: 9}, {Op: 43, A: 19, B: 19}, {Op: 9}, {Op: 42, A: 20, B: 20}, {Op: 9}, {Op: 43, A: 21, B: 21}, {Op: 9}, {Op: 43, A: 22, B: 22}, {Op: 9}, {Op: 42, A: 23, B: 23}, {Op: 9}, {Op: 33, A: 23}, {Op: 41, A: 24}, {Op: 9}, {Op: 42, A: 25, B: 24}, {Op: 9}, {Op: 43, A: 26, B: 25}, {Op: 53}}, + Names: []string{"Comparable", "Enumerable", "Numeric", "Integer", "Float", "String", "Symbol", "Array", "Range", "Hash", "Set", "Gem", "OpenStruct", "Forwardable", "SingleForwardable", "Delegator", "SimpleDelegator", "DelegateClass", "Pathname", "Singleton", "ERB", "Slim", "Haml", "OptionParser", "OptParse", "Tempfile", "Warning"}, NumRequired: 0, SplatIndex: -1, KwRestSlot: -1, @@ -6336,6 +6336,106 @@ func embeddedPrelude() *bytecode.ISeq { }, Super: "", }, + &bytecode.ISeq{ + Name: "", + Insns: []bytecode.Instr{{Op: 1}, {Op: 4}, {Op: 1, A: 1}, {Op: 3}, {Op: 1, A: 2}, {Op: 4}, {Op: 1, A: 3}, {Op: 4}, {Op: 7, A: 4}, {Op: 32}, {Op: 9}, {Op: 5}, {Op: 77}, {Op: 9}, {Op: 44, A: 1, B: 1}, {Op: 9}, {Op: 5}, {Op: 5}, {Op: 30, A: 2, B: 1}, {Op: 53}}, + Consts: []object.Value{ + object.Symbol("deprecated"), + object.Symbol("experimental"), + object.Symbol("performance"), + object.Symbol("strict_unused_block"), + }, + Names: []string{"@warning_categories", "warn", "extend"}, + NumRequired: 0, + SplatIndex: -1, + KwRestSlot: -1, + BlockSlot: -1, + NumLocals: 0, + Children: []*bytecode.ISeq{ + &bytecode.ISeq{ + Name: "", + Insns: []bytecode.Instr{{Op: 44}, {Op: 9}, {Op: 44, A: 1, B: 1}, {Op: 9}, {Op: 44, A: 2, B: 2}, {Op: 53}}, + Names: []string{"[]", "[]=", "categories"}, + NumRequired: 0, + SplatIndex: -1, + KwRestSlot: -1, + BlockSlot: -1, + NumLocals: 0, + Children: []*bytecode.ISeq{ + &bytecode.ISeq{ + Name: "[]", + Insns: []bytecode.Instr{{Op: 11}, {Op: 33}, {Op: 30, A: 1, B: 1, Flags: 1}, {Op: 25}, {Op: 28, A: 20}, {Op: 5}, {Op: 33, A: 2}, {Op: 1}, {Op: 1, A: 1}, {Op: 30, A: 3}, {Op: 13}, {Op: 11}, {Op: 30, A: 4, Flags: 1}, {Op: 30, A: 3}, {Op: 13}, {Op: 1, A: 2}, {Op: 30, A: 3}, {Op: 13}, {Op: 30, A: 5, B: 2}, {Op: 26, A: 21}, {Op: 2}, {Op: 9}, {Op: 31, A: 6}, {Op: 11}, {Op: 30, A: 7, B: 1, Flags: 1}, {Op: 25}, {Op: 28, A: 41}, {Op: 5}, {Op: 33, A: 8}, {Op: 1, A: 3}, {Op: 1, A: 4}, {Op: 30, A: 3}, {Op: 13}, {Op: 11}, {Op: 30, A: 3}, {Op: 13}, {Op: 1, A: 5}, {Op: 30, A: 3}, {Op: 13}, {Op: 30, A: 5, B: 2}, {Op: 26, A: 42}, {Op: 2}, {Op: 9}, {Op: 31, A: 6}, {Op: 11}, {Op: 30, A: 9, B: 1, Flags: 1}, {Op: 53}}, + Consts: []object.Value{ + object.NewString(""), + object.NewString("wrong argument type "), + object.NewString(" (expected Symbol)"), + object.NewString(""), + object.NewString("unknown category: "), + object.NewString(""), + }, + Names: []string{"Symbol", "is_a?", "TypeError", "to_s", "class", "raise", "@warning_categories", "key?", "ArgumentError", "[]"}, + Params: []string{"category"}, + NumRequired: 1, + SplatIndex: -1, + KwRestSlot: -1, + BlockSlot: -1, + NumLocals: 1, + Locals: []string{"category"}, + Super: "", + }, + &bytecode.ISeq{ + Name: "[]=", + Insns: []bytecode.Instr{{Op: 11}, {Op: 33}, {Op: 30, A: 1, B: 1, Flags: 1}, {Op: 25}, {Op: 28, A: 20}, {Op: 5}, {Op: 33, A: 2}, {Op: 1}, {Op: 1, A: 1}, {Op: 30, A: 3}, {Op: 13}, {Op: 11}, {Op: 30, A: 4, Flags: 1}, {Op: 30, A: 3}, {Op: 13}, {Op: 1, A: 2}, {Op: 30, A: 3}, {Op: 13}, {Op: 30, A: 5, B: 2}, {Op: 26, A: 21}, {Op: 2}, {Op: 9}, {Op: 31, A: 6}, {Op: 11}, {Op: 30, A: 7, B: 1, Flags: 1}, {Op: 25}, {Op: 28, A: 41}, {Op: 5}, {Op: 33, A: 8}, {Op: 1, A: 3}, {Op: 1, A: 4}, {Op: 30, A: 3}, {Op: 13}, {Op: 11}, {Op: 30, A: 3}, {Op: 13}, {Op: 1, A: 5}, {Op: 30, A: 3}, {Op: 13}, {Op: 30, A: 5, B: 2}, {Op: 26, A: 42}, {Op: 2}, {Op: 9}, {Op: 31, A: 6}, {Op: 11}, {Op: 11, A: 1}, {Op: 12, A: 2}, {Op: 30, A: 9, B: 2, Flags: 1}, {Op: 9}, {Op: 11, A: 2}, {Op: 53}}, + Consts: []object.Value{ + object.NewString(""), + object.NewString("wrong argument type "), + object.NewString(" (expected Symbol)"), + object.NewString(""), + object.NewString("unknown category: "), + object.NewString(""), + }, + Names: []string{"Symbol", "is_a?", "TypeError", "to_s", "class", "raise", "@warning_categories", "key?", "ArgumentError", "[]="}, + Params: []string{"category", "flag"}, + NumRequired: 2, + SplatIndex: -1, + KwRestSlot: -1, + BlockSlot: -1, + NumLocals: 3, + Locals: []string{"category", "flag", ""}, + Super: "", + }, + &bytecode.ISeq{ + Name: "categories", + Insns: []bytecode.Instr{{Op: 31}, {Op: 30, A: 1, Flags: 1}, {Op: 53}}, + Names: []string{"@warning_categories", "keys"}, + NumRequired: 0, + SplatIndex: -1, + KwRestSlot: -1, + BlockSlot: -1, + NumLocals: 0, + Super: "", + }, + }, + Super: "", + }, + &bytecode.ISeq{ + Name: "warn", + Insns: []bytecode.Instr{{Op: 63}, {Op: 27, A: 5}, {Op: 2}, {Op: 12, A: 1}, {Op: 9}, {Op: 36}, {Op: 11}, {Op: 30, A: 1, B: 1, Flags: 1}, {Op: 9}, {Op: 2}, {Op: 53}}, + Names: []string{"$stderr", "write"}, + Params: []string{"message"}, + NumRequired: 1, + SplatIndex: -1, + KwNames: []string{"category"}, + KwRequired: []bool{false}, + KwRestSlot: -1, + BlockSlot: -1, + NumLocals: 2, + Locals: []string{"message", "category"}, + Super: "", + }, + }, + Super: "", + }, }, Super: "", } diff --git a/internal/vm/warning_module_internal_test.go b/internal/vm/warning_module_internal_test.go new file mode 100644 index 00000000..cf09b7be --- /dev/null +++ b/internal/vm/warning_module_internal_test.go @@ -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") + } + } +}