Commit 75c1aed3 authored by Robert Griesemer's avatar Robert Griesemer

runtime: slightly better error message for assertion panics with identical looking types

Fixes #18911.

Change-Id: Ice10f37460a4f0a66cddeacfe26c28045f5e60fe
Reviewed-on: https://go-review.googlesource.com/116255
Run-TryBot: Robert Griesemer <gri@golang.org>
TryBot-Result: Gobot Gobot <gobot@golang.org>
Reviewed-by: 's avatarKeith Randall <khr@golang.org>
parent b219a68a
......@@ -36,8 +36,13 @@ func (e *TypeAssertionError) Error() string {
return "interface conversion: " + inter + " is nil, not " + e.assertedString
}
if e.missingMethod == "" {
return "interface conversion: " + inter + " is " + e.concreteString +
msg := "interface conversion: " + inter + " is " + e.concreteString +
", not " + e.assertedString
if e.concreteString == e.assertedString {
// provide slightly clearer error message
msg += " (types from different packages)"
}
return msg
}
return "interface conversion: " + e.concreteString + " is not " + e.assertedString +
": missing method " + e.missingMethod
......
// Copyright 2018 The Go Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
package a
var X interface{} = struct{ x int }{}
// Copyright 2018 The Go Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
package main
import "./a"
import "strings"
func main() {
defer func() {
p, ok := recover().(error)
if ok && strings.Contains(p.Error(), "different packages") {
return
}
panic(p)
}()
// expected to fail and report two identical looking (but different) types
_ = a.X.(struct{ x int })
}
// rundir
// Copyright 2018 The Go Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
package ignore
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment