Commit 46f138f2 authored by Russ Cox's avatar Russ Cox
Browse files

cmd/compile, go/types: fix checking of bad type switch

Consider the following program:

	package p

	func f() {
		x := 1
		v := 2
		switch v.(type) {
		case int:
			println(x)
			println(x / 0)
		case 1:
		}
	}

Before this CL, the compiler prints:

	x.go:4:2: x declared but not used
	x.go:6:9: v (variable of type int) is not an interface

x is in fact used, and other errors in the switch go undiagnosed.

This commit fixes that problem by processing the switch statement
even when the 'not an interface' error is reported.

Now the compiler drops the spurious 'declared but not used'
and adds two previously undiagnosed problems:

	x.go:6:9: v (variable of type int) is not an interface
	x.go:9:15: invalid operation: division by zero
	x.go:10:7: 1 is not a type

go/types was printing roughly the same thing the compiler did before,
and now still prints roughly the same thing the compiler does after.
(The only differences are in the exact reported columns.)

Fixes #50493.

Change-Id: I317883f29077b1b4bbd0e8793617fd3bb31aa0f8
Reviewed-on: https://go-review.googlesource.com/c/go/+/379117


Trust: Russ Cox <rsc@golang.org>
Run-TryBot: Russ Cox <rsc@golang.org>
Reviewed-by: default avatarRobert Griesemer <gri@golang.org>
TryBot-Result: Gopher Robot <gobot@golang.org>
parent 75bcdd59
Loading
Loading
Loading
Loading
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please to comment