From 39e523792e33a0bd9217161ca53c6c0cb2324a99 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Daniel=20Mart=C3=AD?= Date: Wed, 27 Sep 2017 10:51:24 +0100 Subject: [PATCH] cmd/compile: fix another invalid switch case panic MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Very similar fix to the one made in golang.org/cl/65655. This time it's for switches on interface values, as we look for duplicates in a different manner to keep types in mind. As before, add a small regression test. Updates #22001. Fixes #22063. Change-Id: I9a55d08999aeca262ad276b4649b51848a627b02 Reviewed-on: https://go-review.googlesource.com/66450 Run-TryBot: Daniel Martí TryBot-Result: Gobot Gobot Reviewed-by: Matthew Dempsky --- src/cmd/compile/internal/gc/swt.go | 5 +++++ test/fixedbugs/issue22063.go | 17 +++++++++++++++++ 2 files changed, 22 insertions(+) create mode 100644 test/fixedbugs/issue22063.go diff --git a/src/cmd/compile/internal/gc/swt.go b/src/cmd/compile/internal/gc/swt.go index dc285ae91c..08ce8c44ed 100644 --- a/src/cmd/compile/internal/gc/swt.go +++ b/src/cmd/compile/internal/gc/swt.go @@ -640,6 +640,11 @@ func checkDupExprCases(exprname *Node, clauses []*Node) { if ct := consttype(n); ct < 0 || ct == CTBOOL { continue } + // If the value has no type, we have + // already printed an error about it. + if n.Type == nil { + continue + } tv := typeVal{ typ: n.Type.LongString(), val: n.Val().Interface(), diff --git a/test/fixedbugs/issue22063.go b/test/fixedbugs/issue22063.go new file mode 100644 index 0000000000..bfdb2e0027 --- /dev/null +++ b/test/fixedbugs/issue22063.go @@ -0,0 +1,17 @@ +// errorcheck + +// Copyright 2017 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. + +// Issue 22063: panic on interface switch case with invalid name + +package p + +const X = Wrong(0) // ERROR "undefined: Wrong" + +func _() { + switch interface{}(nil) { + case X: + } +}