| 1 |
// +build !nacl,!js |
| 2 |
// run |
| 3 |
|
| 4 |
// Copyright 2018 The Go Authors. All rights reserved. |
| 5 |
// Use of this source code is governed by a BSD-style |
| 6 |
// license that can be found in the LICENSE file. |
| 7 |
|
| 8 |
// Ensure that label redefinition errors print out |
| 9 |
// a column number that matches the start of the current label's |
| 10 |
// definition instead of the label delimiting token ":" |
| 11 |
|
| 12 |
package main |
| 13 |
|
| 14 |
import ( |
| 15 |
"bytes" |
| 16 |
"fmt" |
| 17 |
"io/ioutil" |
| 18 |
"log" |
| 19 |
"os" |
| 20 |
"os/exec" |
| 21 |
"path/filepath" |
| 22 |
"regexp" |
| 23 |
) |
| 24 |
|
| 25 |
func main() { |
| 26 |
tmpdir, err := ioutil.TempDir("", "issue26411") |
| 27 |
if err != nil { |
| 28 |
log.Fatalf("Failed to create temporary directory: %v", err) |
| 29 |
} |
| 30 |
defer os.RemoveAll(tmpdir) |
| 31 |
|
| 32 |
tests := []struct { |
| 33 |
code string |
| 34 |
errors []string |
| 35 |
}{ |
| 36 |
{ |
| 37 |
code: ` |
| 38 |
package main |
| 39 |
|
| 40 |
func main() { |
| 41 |
foo: |
| 42 |
foo: |
| 43 |
} |
| 44 |
`, |
| 45 |
errors: []string{ |
| 46 |
"^.+:5:1: label foo defined and not used\n", |
| 47 |
".+:6:1: label foo already defined at .+:5:1\n$", |
| 48 |
}, |
| 49 |
}, |
| 50 |
{ |
| 51 |
code: ` |
| 52 |
package main |
| 53 |
|
| 54 |
func main() { |
| 55 |
|
| 56 |
bar: |
| 57 |
bar: |
| 58 |
bar: |
| 59 |
bar : |
| 60 |
} |
| 61 |
`, |
| 62 |
|
| 63 |
errors: []string{ |
| 64 |
"^.+:6:13: label bar defined and not used\n", |
| 65 |
".+:7:4: label bar already defined at .+:6:13\n", |
| 66 |
".+:8:1: label bar already defined at .+:6:13\n", |
| 67 |
".+:9:1: label bar already defined at .+:6:13\n$", |
| 68 |
}, |
| 69 |
}, |
| 70 |
} |
| 71 |
|
| 72 |
for i, test := range tests { |
| 73 |
filename := filepath.Join(tmpdir, fmt.Sprintf("%d.go", i)) |
| 74 |
if err := ioutil.WriteFile(filename, []byte(test.code), 0644); err != nil { |
| 75 |
log.Printf("#%d: failed to create file %s", i, filename) |
| 76 |
continue |
| 77 |
} |
| 78 |
output, _ := exec.Command("go", "tool", "compile", filename).CombinedOutput() |
| 79 |
|
| 80 |
// remove each matching error from the output |
| 81 |
for _, err := range test.errors { |
| 82 |
rx := regexp.MustCompile(err) |
| 83 |
match := rx.Find(output) |
| 84 |
output = bytes.Replace(output, match, nil, 1) // remove match (which might be nil) from output |
| 85 |
} |
| 86 |
|
| 87 |
// at this point all output should have been consumed |
| 88 |
if len(output) != 0 { |
| 89 |
log.Printf("Test case %d has unmatched errors:\n%s", i, output) |
| 90 |
} |
| 91 |
} |
| 92 |
} |