From 24ca86f3083e8dd4ad5ea61e26acf02440d0ad35 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Daniel=20Mart=C3=AD?= Date: Sat, 23 Sep 2017 23:04:31 +0100 Subject: [PATCH] cmd/compile: fix invalid switch case value panic MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit This is a regression introduced by myself in golang.org/cl/41852, confirmed by the program that reproduces the crash that can be seen in the added test. Fixes #21988. Change-Id: I18d5b2b3de63ced84db705b18490b00b16b59e02 Reviewed-on: https://go-review.googlesource.com/65655 Run-TryBot: Daniel Martí TryBot-Result: Gobot Gobot Reviewed-by: Keith Randall --- src/cmd/compile/internal/gc/swt.go | 5 +++++ test/fixedbugs/issue21988.go | 17 +++++++++++++++++ 2 files changed, 22 insertions(+) create mode 100644 test/fixedbugs/issue21988.go diff --git a/src/cmd/compile/internal/gc/swt.go b/src/cmd/compile/internal/gc/swt.go index 469af86aa6..dc285ae91c 100644 --- a/src/cmd/compile/internal/gc/swt.go +++ b/src/cmd/compile/internal/gc/swt.go @@ -610,6 +610,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 + } val := n.Val().Interface() prev, dup := seen[val] diff --git a/test/fixedbugs/issue21988.go b/test/fixedbugs/issue21988.go new file mode 100644 index 0000000000..850e0398d6 --- /dev/null +++ b/test/fixedbugs/issue21988.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 21988: panic on switch case with invalid value + +package p + +const X = Wrong(0) // ERROR "undefined: Wrong" + +func _() { + switch 0 { + case X: + } +}