1
0
mirror of https://github.com/golang/go synced 2024-11-17 20:14:45 -07:00

internal/types/errors: add InvalidSyntaxTree error

Type checkers should use InvalidSyntaxTree as error code
for invalid syntax tree errors instead of zero. This way
the zero value can be used to mark an unset error code.

Also, add an example for BlankPkgName (and adjust the
test harness slightly to make it work).

Change-Id: Ic15fa0e8e46be698e52352f2f0e4915b75e509d7
Reviewed-on: https://go-review.googlesource.com/c/go/+/439565
Reviewed-by: Robert Findley <rfindley@google.com>
Auto-Submit: Robert Griesemer <gri@google.com>
Reviewed-by: Robert Griesemer <gri@google.com>
Run-TryBot: Robert Griesemer <gri@google.com>
TryBot-Result: Gopher Robot <gobot@golang.org>
This commit is contained in:
Robert Griesemer 2022-10-06 17:47:13 -07:00 committed by Gopher Robot
parent 6688efd5df
commit 578523e4a0
2 changed files with 13 additions and 3 deletions

View File

@ -31,6 +31,11 @@ type Code int
// problem with types. // problem with types.
const ( const (
// InvalidSyntaxTree occurs if an invalid syntax tree is provided
// to the type checker. It should never happen.
InvalidSyntaxTree Code = -1
// The zero Code value indicates an unset (invalid) error code.
_ Code = iota _ Code = iota
// Test is reserved for errors that only apply while in self-test mode. // Test is reserved for errors that only apply while in self-test mode.
@ -40,6 +45,9 @@ const (
// //
// Per the spec: // Per the spec:
// "The PackageName must not be the blank identifier." // "The PackageName must not be the blank identifier."
//
// Example:
// package _
BlankPkgName BlankPkgName
// MismatchedPkgName occurs when a file's package name doesn't match the // MismatchedPkgName occurs when a file's package name doesn't match the

View File

@ -24,7 +24,7 @@ func TestErrorCodeExamples(t *testing.T) {
doc := spec.Doc.Text() doc := spec.Doc.Text()
examples := strings.Split(doc, "Example:") examples := strings.Split(doc, "Example:")
for i := 1; i < len(examples); i++ { for i := 1; i < len(examples); i++ {
example := examples[i] example := strings.TrimSpace(examples[i])
err := checkExample(t, example) err := checkExample(t, example)
if err == nil { if err == nil {
t.Fatalf("no error in example #%d", i) t.Fatalf("no error in example #%d", i)
@ -89,8 +89,10 @@ func readCode(err Error) int {
func checkExample(t *testing.T, example string) error { func checkExample(t *testing.T, example string) error {
t.Helper() t.Helper()
fset := token.NewFileSet() fset := token.NewFileSet()
src := fmt.Sprintf("package p\n\n%s", example) if !strings.HasPrefix(example, "package") {
file, err := parser.ParseFile(fset, "example.go", src, 0) example = "package p\n\n" + example
}
file, err := parser.ParseFile(fset, "example.go", example, 0)
if err != nil { if err != nil {
t.Fatal(err) t.Fatal(err)
} }