aboutsummaryrefslogtreecommitdiff
path: root/src/runtime/error.go
diff options
context:
space:
mode:
Diffstat (limited to 'src/runtime/error.go')
-rw-r--r--src/runtime/error.go35
1 files changed, 29 insertions, 6 deletions
diff --git a/src/runtime/error.go b/src/runtime/error.go
index fe95f31005..406f36ca5f 100644
--- a/src/runtime/error.go
+++ b/src/runtime/error.go
@@ -211,11 +211,16 @@ type stringer interface {
String() string
}
-// printany prints an argument passed to panic.
+// printpanicval prints an argument passed to panic.
// If panic is called with a value that has a String or Error method,
// it has already been converted into a string by preprintpanics.
-func printany(i any) {
- switch v := i.(type) {
+//
+// To ensure that the traceback can be unambiguously parsed even when
+// the panic value contains "\ngoroutine" and other stack-like
+// strings, newlines in the string representation of v are replaced by
+// "\n\t".
+func printpanicval(v any) {
+ switch v := v.(type) {
case nil:
print("nil")
case bool:
@@ -251,19 +256,22 @@ func printany(i any) {
case complex128:
print(v)
case string:
- print(v)
+ printindented(v)
default:
- printanycustomtype(i)
+ printanycustomtype(v)
}
}
+// Invariant: each newline in the string representation is followed by a tab.
func printanycustomtype(i any) {
eface := efaceOf(&i)
typestring := toRType(eface._type).string()
switch eface._type.Kind_ {
case abi.String:
- print(typestring, `("`, *(*string)(eface.data), `")`)
+ print(typestring, `("`)
+ printindented(*(*string)(eface.data))
+ print(`")`)
case abi.Bool:
print(typestring, "(", *(*bool)(eface.data), ")")
case abi.Int:
@@ -301,6 +309,21 @@ func printanycustomtype(i any) {
}
}
+// printindented prints s, replacing "\n" with "\n\t".
+func printindented(s string) {
+ for {
+ i := bytealg.IndexByteString(s, '\n')
+ if i < 0 {
+ break
+ }
+ i += len("\n")
+ print(s[:i])
+ print("\t")
+ s = s[i:]
+ }
+ print(s)
+}
+
// panicwrap generates a panic for a call to a wrapped value method
// with a nil pointer receiver.
//