go/test/fixedbugs/issue22662b.go
Ian Lance Taylor f1778c28a9 test: recognize and use gc build tag
Change the run.go driver to recognize the "gc" build tag.

Change existing tests to use the "gc" build tag if they use some
feature that seems specific to the gc compiler, such as passing specific
options to or expecting specific behavior from "go tool compile".
Change tests to use the "!gccgo" build tag if they use "go build" or
"go run", as while those might work with compilers other than gc, they
won't work with the way that gccgo runs its testsuite (which happens
independently of the go command).

For #43252

Change-Id: I666e04b6d7255a77dfc256ee304094e3a6bb15ad
Reviewed-on: https://go-review.googlesource.com/c/go/+/279052
Trust: Ian Lance Taylor <iant@golang.org>
Reviewed-by: Cherry Zhang <cherryyz@google.com>
2020-12-18 00:10:44 +00:00

61 lines
1.8 KiB
Go

// +build !js,gc
// run
// Copyright 2018 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.
// Verify the impact of line directives on error positions and position formatting.
package main
import (
"io/ioutil"
"log"
"os"
"os/exec"
"strings"
)
// Each of these tests is expected to fail (missing package clause)
// at the position determined by the preceding line directive.
var tests = []struct {
src, pos string
}{
{"//line :10\n", ":10:"}, // no filename means no filename
{"//line :10:4\n", "filename:10:4"}, // no filename means use existing filename
{"//line foo.go:10\n", "foo.go:10:"}, // no column means don't print a column
{"//line foo.go:10:4\n", "foo.go:10:4:"}, // column means print a column
{"//line foo.go:10:4\n\n", "foo.go:11:1:"}, // relative columns start at 1 after newline
{"/*line :10*/", ":10:"},
{"/*line :10:4*/", "filename:10:4"},
{"/*line foo.go:10*/", "foo.go:10:"},
{"/*line foo.go:10:4*/", "foo.go:10:4:"},
{"/*line foo.go:10:4*/\n", "foo.go:11:1:"},
}
func main() {
f, err := ioutil.TempFile("", "issue22662b.go")
if err != nil {
log.Fatal(err)
}
f.Close()
defer os.Remove(f.Name())
for _, test := range tests {
if err := ioutil.WriteFile(f.Name(), []byte(test.src), 0660); err != nil {
log.Fatal(err)
}
out, err := exec.Command("go", "tool", "compile", f.Name()).CombinedOutput()
if err == nil {
log.Fatalf("expected compiling\n---\n%s\n---\nto fail", test.src)
}
errmsg := strings.Replace(string(out), f.Name(), "filename", -1) // use "filename" instead of actual (long) filename
if !strings.HasPrefix(errmsg, test.pos) {
log.Fatalf("%q: got %q; want position %q", test.src, errmsg, test.pos)
}
}
}