Skip to content

Commit

Permalink
[dev.unified] test: relax panic message expectations
Browse files Browse the repository at this point in the history
In this test, traditionally the comparison `*l == r[0]` was left as a
comparison between `*l` (type `any`) and `r[0]` (type `*int`), and the
rest of the compiler needed to handle mixed-typed comparisons.
However, this means more complexity for wiring up explicit rtypes.

To simplify rtype handling, the next CL will change unified IR to
instead handle the expression as `*l == any(r[0])`. However, a
consequence of this currently is that walk will now sequence the
`any(r[0])` expression first, because it involves a
concrete-to-interface conversion. And in turn, this means the `r[0]`
panic ("index out of bounds") will take priority over the `*l`
panic ("nil pointer dereference").

This is a change in user-visible semantics in some cases, but the Go
spec leaves this unspecified, so it shouldn't be an issue. Note also:
gccgo has the same behavior (i.e., panicking on index out of bounds,
not nil pointer dereference), and cmd/compile also already has the
same behavior when the interface conversion is explicit (as in the
added "nil pointer dereference #3" test case).

Updates #23735.
Updates #32187.

Change-Id: I49e5dcca85b4680f9c8780ef0013e64254d38fe5
Reviewed-on: https://go-review.googlesource.com/c/go/+/418097
Run-TryBot: Matthew Dempsky <mdempsky@google.com>
TryBot-Result: Gopher Robot <gobot@golang.org>
Reviewed-by: Cuong Manh Le <cuong.manhle.vn@gmail.com>
Reviewed-by: David Chase <drchase@google.com>
  • Loading branch information
mdempsky committed Jul 19, 2022
1 parent 76a82f0 commit ebd34e3
Showing 1 changed file with 8 additions and 6 deletions.
14 changes: 8 additions & 6 deletions test/fixedbugs/issue32187.go
Expand Up @@ -36,24 +36,26 @@ func main() {
{"type assertion", "", func() { _ = x == x.(*int) }},
{"out of bounds", "", func() { _ = x == s[1] }},
{"nil pointer dereference #1", "", func() { _ = x == *p }},
{"nil pointer dereference #2", "nil pointer dereference", func() { _ = *l == r[0] }},
// TODO(mdempsky): Restore "nil pointer dereference" check. The Go
// spec doesn't mandate an order for panics (or even panic
// messages), but left-to-right is less confusing to users.
{"nil pointer dereference #2", "", func() { _ = *l == r[0] }},
{"nil pointer dereference #3", "", func() { _ = *l == any(r[0]) }},
}

for _, tc := range tests {
testFuncShouldPanic(tc.name, tc.errStr, tc.f)
}
}

func testFuncShouldPanic(name, errStr string, f func()) {
func testFuncShouldPanic(name, want string, f func()) {
defer func() {
e := recover()
if e == nil {
log.Fatalf("%s: comparison did not panic\n", name)
}
if errStr != "" {
if !strings.Contains(e.(error).Error(), errStr) {
log.Fatalf("%s: wrong panic message\n", name)
}
if have := e.(error).Error(); !strings.Contains(have, want) {
log.Fatalf("%s: wrong panic message: have %q, want %q\n", name, have, want)
}
}()
f()
Expand Down

0 comments on commit ebd34e3

Please sign in to comment.