1
0
mirror of https://github.com/golang/go synced 2024-09-29 13:24:28 -06:00
go/test/fixedbugs/issue33460.go
Matthew Dempsky c302785df9 cmd/compile: fix "previous" position info for duplicate switch cases
Because the Node AST represents references to declared objects (e.g.,
variables, packages, types, constants) by directly pointing to the
referred object, we don't have use-position info for these objects.

For switch statements with duplicate cases, we report back where the
first duplicate value appeared. However, due to the AST
representation, if the value was a declared constant, we mistakenly
reported the constant declaration position as the previous case
position.

This CL reports back against the 'case' keyword's position instead, if
there's no more precise information available to us.

It also refactors code to emit the same "previous at" error message
for duplicate values in map literals.

Thanks to Emmanuel Odeke for the test case.

Fixes #33460.

Change-Id: Iec69542ccd4aad594dde8df02d1b880a422c5622
Reviewed-on: https://go-review.googlesource.com/c/go/+/188901
Reviewed-by: Robert Griesemer <gri@golang.org>
Reviewed-by: Emmanuel Odeke <emm.odeke@gmail.com>
Run-TryBot: Emmanuel Odeke <emm.odeke@gmail.com>
TryBot-Result: Gobot Gobot <gobot@golang.org>
2019-08-27 19:53:05 +00:00

38 lines
642 B
Go

// errorcheck
// Copyright 2019 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.
package p
const (
zero = iota
one
two
three
)
const iii int = 0x3
func f(v int) {
switch v {
case zero, one:
case two, one: // ERROR "previous case at LINE-1"
case three:
case 3: // ERROR "previous case at LINE-1"
case iii: // ERROR "previous case at LINE-2"
}
}
const b = "b"
var _ = map[string]int{
"a": 0,
b: 1,
"a": 2, // ERROR "previous key at LINE-2"
"b": 3, // ERROR "previous key at LINE-2"
"b": 4, // ERROR "previous key at LINE-3"
}