From 75c1aed3457238295d761b5a6f3adcf776c578e7 Mon Sep 17 00:00:00 2001 From: Robert Griesemer Date: Mon, 4 Jun 2018 16:32:07 -0700 Subject: [PATCH] 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 TryBot-Result: Gobot Gobot Reviewed-by: Keith Randall --- src/runtime/error.go | 7 ++++++- test/fixedbugs/issue18911.dir/a.go | 7 +++++++ test/fixedbugs/issue18911.dir/b.go | 21 +++++++++++++++++++++ test/fixedbugs/issue18911.go | 7 +++++++ 4 files changed, 41 insertions(+), 1 deletion(-) create mode 100644 test/fixedbugs/issue18911.dir/a.go create mode 100644 test/fixedbugs/issue18911.dir/b.go create mode 100644 test/fixedbugs/issue18911.go diff --git a/src/runtime/error.go b/src/runtime/error.go index 4b6fb32b78f..1f77c0a0b56 100644 --- a/src/runtime/error.go +++ b/src/runtime/error.go @@ -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 diff --git a/test/fixedbugs/issue18911.dir/a.go b/test/fixedbugs/issue18911.dir/a.go new file mode 100644 index 00000000000..d2221e76128 --- /dev/null +++ b/test/fixedbugs/issue18911.dir/a.go @@ -0,0 +1,7 @@ +// 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 }{} diff --git a/test/fixedbugs/issue18911.dir/b.go b/test/fixedbugs/issue18911.dir/b.go new file mode 100644 index 00000000000..da2388b88d1 --- /dev/null +++ b/test/fixedbugs/issue18911.dir/b.go @@ -0,0 +1,21 @@ +// 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 }) +} diff --git a/test/fixedbugs/issue18911.go b/test/fixedbugs/issue18911.go new file mode 100644 index 00000000000..8bf34a382af --- /dev/null +++ b/test/fixedbugs/issue18911.go @@ -0,0 +1,7 @@ +// 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