From b307d6bbc40d52623ab2added84a8f49ff0bb32d Mon Sep 17 00:00:00 2001 From: tannevaled Date: Wed, 26 Aug 2026 13:51:35 +0200 Subject: [PATCH] Kernel#warn: route through Warning.warn with category filtering MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Kernel#warn wrote its arguments straight to $stderr and ignored the category: and uplevel: keywords. It now builds one message — each argument on its own line, a newline appended only when the argument does not already end with one — and routes it through Warning.warn(message, category:), so a program that overrides Warning.warn (or reads the category) sees it, as in MRI. - A String category is converted to a Symbol; any other non-nil, non-Symbol value raises TypeError ("no implicit conversion of X into Symbol"). - A category whose warnings are disabled (Warning[category] is false) drops the message without calling Warning.warn; an unknown category raises the ArgumentError Warning.[] raises. - The uplevel: keyword is validated (a negative value raises ArgumentError "negative level (n)", a non-Integer a TypeError) but not acted on, as rbgo has no caller line to prepend. - No message does nothing. Verified against ruby 4.0.6. Full package coverage stays at 100.0%. Zero- regression rubyspec sweep; core/kernel/warn_spec 3 -> 8 and core/warning/warn_spec 3 -> 4. (The residual examples need parser-emitted warnings to route through Warning.warn, Warning.warn arity introspection, or the -w default the harness does not set — separate concerns.) Co-Authored-By: Claude Opus 4.8 --- internal/vm/io.go | 57 +++++++++++++++++++++--- internal/vm/kernel_warn_internal_test.go | 43 ++++++++++++++++++ 2 files changed, 95 insertions(+), 5 deletions(-) create mode 100644 internal/vm/kernel_warn_internal_test.go diff --git a/internal/vm/io.go b/internal/vm/io.go index f6e595d6..b57a388b 100644 --- a/internal/vm/io.go +++ b/internal/vm/io.go @@ -148,13 +148,60 @@ func (vm *VM) registerIO() { vm.consts["STDOUT"], vm.consts["STDERR"], vm.consts["STDIN"] = stdout, stderr, stdin vm.globals["$stdout"], vm.globals["$stderr"], vm.globals["$stdin"] = stdout, stderr, stdin - // Kernel#warn writes each message (newline-terminated) to the current $stderr. + // Kernel#warn builds one message — each argument on its own line, a newline + // appended only when it does not already end with one — and routes it through + // Warning.warn(message, category:) so a program that overrides Warning.warn (or + // reads the category) sees it, as in MRI. A String category is converted to a + // Symbol (anything else is a TypeError); a category whose warnings are disabled + // (Warning[category] is false) is dropped, and an unknown category raises. The + // uplevel: keyword is validated (a negative or non-Integer value raises) but not + // acted on, as rbgo has no caller line to prepend. With no message it does + // nothing. vm.cObject.define("warn", func(vm *VM, _ object.Value, args []object.Value, _ *Proc) object.Value { - o := vm.curStderr() - for _, a := range args { - vm.ioPutsValue(o, a) + var category object.Value = object.NilV + pos := args + if kw := trailingKwHash(args); kw != nil { + pos = args[:len(args)-1] + if v, ok := kw.Get(object.SymVal("category")); ok { + category = v + } + if v, ok := kw.Get(object.SymVal("uplevel")); ok && !object.IsNil(v) { + n, isInt := v.(object.Integer) + if !isInt { + raise("TypeError", "no implicit conversion of %s into Integer", vm.classOf(v).name) + } + if int64(n) < 0 { + raise("ArgumentError", "negative level (%d)", int64(n)) + } + } } - return object.NilV + var b strings.Builder + for _, a := range pos { + s := vm.displayStr(a) + b.WriteString(s) + if !strings.HasSuffix(s, "\n") { + b.WriteByte('\n') + } + } + if b.Len() == 0 { + return object.NilV + } + if !object.IsNil(category) { + switch c := category.(type) { + case object.Symbol: + case *object.String: + category = object.Symbol(c.Str()) + default: + raise("TypeError", "no implicit conversion of %s into Symbol", vm.classOf(category).name) + } + // Warning[category] filters the message (and raises for an unknown one). + if !vm.send(vm.consts["Warning"], "[]", []object.Value{category}, nil).Truthy() { + return object.NilV + } + } + kw := object.NewHash() + kw.Set(object.SymVal("category"), category) + return vm.send(vm.consts["Warning"], "warn", []object.Value{object.NewString(b.String()), kw}, nil) }) // File streams: File.open returns a buffered, file-backed IO carrying the diff --git a/internal/vm/kernel_warn_internal_test.go b/internal/vm/kernel_warn_internal_test.go new file mode 100644 index 00000000..88f92b55 --- /dev/null +++ b/internal/vm/kernel_warn_internal_test.go @@ -0,0 +1,43 @@ +// Copyright (c) the go-embedded-ruby/ruby authors +// +// SPDX-License-Identifier: BSD-3-Clause + +package vm + +import "testing" + +// TestKernelWarn covers Kernel#warn routing through Warning.warn: it builds one +// message (each argument newline-terminated, no doubled newline), passes a +// nil-or-converted category keyword, drops a message whose category is disabled +// and raises for an unknown one, validates the uplevel keyword, and does nothing +// with no message. Verified against ruby 4.0.6. +func TestKernelWarn(t *testing.T) { + // Capture what Warning.warn receives by overriding it. + const cap = `$c = nil; Warning.singleton_class.send(:define_method, :warn) { |m, category: nil| $c = [m, category] }; ` + cases := []struct{ src, want string }{ + // Each argument on its own line; an existing newline is not doubled. + {cap + `warn("a", "b\n", "c"); p $c`, `["a\nb\nc\n", nil]`}, + {cap + `warn("hello"); p $c`, `["hello\n", nil]`}, + // A String category converts to a Symbol; nil stays nil. + {cap + `Warning[:deprecated] = true; warn("x", category: "deprecated"); p $c`, `["x\n", :deprecated]`}, + // A disabled category drops the message (Warning.warn is not called). + {cap + `Warning[:deprecated] = false; warn("x", category: :deprecated); p $c`, `nil`}, + // An enabled category passes through. + {cap + `Warning[:experimental] = true; warn("x", category: :experimental); p $c`, `["x\n", :experimental]`}, + // No message does nothing. + {cap + `warn; p $c`, `nil`}, + // An unknown category raises ArgumentError. + {`begin; warn("x", category: :bogus); rescue ArgumentError => e; p e.message; end`, `"unknown category: bogus"`}, + // A non-Symbol/String category is a TypeError. + {`begin; warn("x", category: 5); rescue TypeError => e; p e.message; end`, `"no implicit conversion of Integer into Symbol"`}, + // uplevel is validated: negative raises ArgumentError, non-Integer TypeError. + {`begin; warn("x", uplevel: -1); rescue ArgumentError => e; p e.message; end`, `"negative level (-1)"`}, + {`begin; warn("x", uplevel: "a"); rescue TypeError => e; p e.message; end`, `"no implicit conversion of String into Integer"`}, + {cap + `warn("x", uplevel: 0); p $c`, `["x\n", nil]`}, + } + 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") + } + } +}