diff options
| author | Ian Lance Taylor <iant@golang.org> | 2018-07-11 13:51:39 -0700 |
|---|---|---|
| committer | Ian Lance Taylor <iant@golang.org> | 2018-07-11 21:34:05 +0000 |
| commit | cda1947fd12ad31060b30a0a601130bfaa26d234 (patch) | |
| tree | 247e6cfbfbf2d4685027ed9f2c845c2305881f03 /src/runtime/error.go | |
| parent | 12ed0ddec198db0eaabbaaef28bb5ffb17204e11 (diff) | |
| download | go-cda1947fd12ad31060b30a0a601130bfaa26d234.tar.xz | |
runtime: don't say "different packages" if they may not be different
Fix the panic message produced for an interface conversion error to
only say "types from different packages" if they are definitely from
different packges. If they may be from the same package, say "types
from different scopes."
Updates #18911
Fixes #26094
Change-Id: I0cea50ba31007d88e70c067b4680009ede69bab9
Reviewed-on: https://go-review.googlesource.com/123395
Reviewed-by: Austin Clements <austin@google.com>
Diffstat (limited to 'src/runtime/error.go')
| -rw-r--r-- | src/runtime/error.go | 33 |
1 files changed, 19 insertions, 14 deletions
diff --git a/src/runtime/error.go b/src/runtime/error.go index 1f77c0a0b5..9a2beaeb95 100644 --- a/src/runtime/error.go +++ b/src/runtime/error.go @@ -19,32 +19,37 @@ type Error interface { // A TypeAssertionError explains a failed type assertion. type TypeAssertionError struct { - interfaceString string - concreteString string - assertedString string - missingMethod string // one method needed by Interface, missing from Concrete + _interface *_type + concrete *_type + asserted *_type + missingMethod string // one method needed by Interface, missing from Concrete } func (*TypeAssertionError) RuntimeError() {} func (e *TypeAssertionError) Error() string { - inter := e.interfaceString - if inter == "" { - inter = "interface" + inter := "interface" + if e._interface != nil { + inter = e._interface.string() } - if e.concreteString == "" { - return "interface conversion: " + inter + " is nil, not " + e.assertedString + as := e.asserted.string() + if e.concrete == nil { + return "interface conversion: " + inter + " is nil, not " + as } + cs := e.concrete.string() if e.missingMethod == "" { - msg := "interface conversion: " + inter + " is " + e.concreteString + - ", not " + e.assertedString - if e.concreteString == e.assertedString { + msg := "interface conversion: " + inter + " is " + cs + ", not " + as + if cs == as { // provide slightly clearer error message - msg += " (types from different packages)" + if e.concrete.pkgpath() != e.asserted.pkgpath() { + msg += " (types from different packages)" + } else { + msg += " (types from different scopes)" + } } return msg } - return "interface conversion: " + e.concreteString + " is not " + e.assertedString + + return "interface conversion: " + cs + " is not " + as + ": missing method " + e.missingMethod } |
