Skip to content

Commit

Permalink
[dev.unified] test: add switch test case for tricky nil handling
Browse files Browse the repository at this point in the history
The next CL will change Unified IR's switch statement handling to
convert values to empty interface in some tricky cases. My initial
attempt at this accidentally mishandled `case nil:` in some cases, and
this wasn't caught by any existing tests. So this CL adds one.

Change-Id: Idcfaf0e869dca91be46d665e65d4623dc52bb60f
Reviewed-on: https://go-review.googlesource.com/c/go/+/418099
Reviewed-by: Cuong Manh Le <cuong.manhle.vn@gmail.com>
Reviewed-by: Keith Randall <khr@google.com>
TryBot-Result: Gopher Robot <gobot@golang.org>
Reviewed-by: Keith Randall <khr@golang.org>
Run-TryBot: Matthew Dempsky <mdempsky@google.com>
  • Loading branch information
mdempsky committed Jul 19, 2022
1 parent 878439c commit e971b6a
Showing 1 changed file with 14 additions and 0 deletions.
14 changes: 14 additions & 0 deletions test/switch.go
Expand Up @@ -400,4 +400,18 @@ func main() {
case i > x:
os.Exit(1)
}

// Unified IR converts the tag and all case values to empty
// interface, when any of the case values aren't assignable to the
// tag value's type. Make sure that `case nil:` compares against the
// tag type's nil value (i.e., `(*int)(nil)`), not nil interface
// (i.e., `any(nil)`).
switch (*int)(nil) {
case nil:
// ok
case any(nil):
assert(false, "case any(nil) matched")
default:
assert(false, "default matched")
}
}

0 comments on commit e971b6a

Please sign in to comment.