go/test/fixedbugs/issue14006.go
Robert Griesemer 912a638b0c cmd/compile: check labels and branches during parse time
Instead of a separate check control flow pass (checkcfg.go)
operating on nodes, perform this check at parse time on the
new syntax tree. Permits this check to be done concurrently,
and doesn't depend on the specifics of the symbol's dclstack
implementation anymore. The remaining dclstack uses will be
removed in a follow-up change.

- added CheckBranches Mode flag (so we can turn off the check
  if we only care about syntactic correctness, e.g. for tests)

- adjusted test/goto.go error messages: the new branches
  checker only reports if a goto jumps into a block, but not
  which block (we may want to improve this again, eventually)

- also, the new branches checker reports one variable that
  is being jumped over by a goto, but it may not be the first
  one declared (this is fine either way)

- the new branches checker reports additional errors for
  fixedbugs/issue14006.go (not crucial to avoid those errors)

- the new branches checker now correctly reports only
  variable declarations being jumped over, rather than
  all declarations (issue 8042). Added respective tests.

Fixes #8042.

Change-Id: I53b6e1bda189748e1e1fb5b765a8a64337c27d40
Reviewed-on: https://go-review.googlesource.com/39998
Reviewed-by: Matthew Dempsky <mdempsky@google.com>
2017-04-19 00:36:34 +00:00

64 lines
1,023 B
Go

// errorcheck
// Copyright 2016 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.
// Literals that happen to resolve to named constants
// may be used as label names (see issue 13684). Make
// sure that other literals don't crash the compiler.
package main
const labelname = 1
func main() {
goto labelname
labelname:
}
func f() {
var x int
switch x {
case 1:
2: // ERROR "unexpected :"
case 2:
}
switch x {
case 1:
2: ; // ERROR "unexpected :"
case 2:
}
var y string
switch y {
case "foo":
"bar": // ERROR "unexpected :"
case "bar":
}
switch y {
case "foo":
"bar": ; // ERROR "unexpected :"
case "bar":
}
var z bool
switch {
case z:
labelname: // ERROR "missing statement after label"
case false:
}
switch {
case z:
labelname: // ERROR "label labelname defined and not used"
}
switch {
case z:
labelname: ; // ERROR "label labelname already defined at LINE-5"
case false:
}
}