Skip to content

runtime: add missing runtime error prefix to PanicNilError #63816

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 5 commits into
base: master
Choose a base branch
from
Open
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
2 changes: 1 addition & 1 deletion src/runtime/panic.go
Original file line number Diff line number Diff line change
Expand Up @@ -700,7 +700,7 @@ type PanicNilError struct {
_ [0]*PanicNilError
}

func (*PanicNilError) Error() string { return "panic called with nil argument" }
func (*PanicNilError) Error() string { return "runtime error: panic called with nil argument" }
func (*PanicNilError) RuntimeError() {}

var panicnil = &godebugInc{name: "panicnil"}
Expand Down
54 changes: 54 additions & 0 deletions src/runtime/panic_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
package runtime_test

import (
"runtime"
"strings"
"testing"
)
Expand Down Expand Up @@ -46,3 +47,56 @@ func TestPanicWithDirectlyPrintableCustomTypes(t *testing.T) {
})
}
}

func TestPanicNilErrorPrefix(t *testing.T) {
tests := []struct {
name string
wantPanic string
fn func()
}{
{
name: "panic(nil)",
fn: func() {
panic(nil)
},
},
{
name: "panic((any)(nil))",
fn: func() {
var foo any = nil
panic(foo)
},
},
{
name: "panic((error)(nil))",
fn: func() {
var err error
panic(err)
},
},
}

for _, tt := range tests {
tt := tt
t.Run(tt.name, func(t *testing.T) {
defer func() {
r := recover()
if r == nil {
t.Fatal("expected a panic")
}

re, ok := r.(runtime.Error)
if !ok {
t.Fatalf("wrong panic type: got %T, want runtime.Error", r)
}
got := re.Error()
want := "runtime error: panic called with nil argument"
if !strings.Contains(got, want) {
t.Fatalf("got: %s, want: %s", got, want)
}
}()

tt.fn()
})
}
}
2 changes: 1 addition & 1 deletion test/fixedbugs/issue19658.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ func main() {
Input string
Expect string
}{
{"", "nil", "panic: panic called with nil argument"},
{"", "nil", "panic: runtime error: panic called with nil argument"},
{"errors.New", `"test"`, "panic: test"},
{"S", "S{}", "panic: s-stringer"},
{"byte", "8", "panic: 8"},
Expand Down