]> Cypherpunks repositories - gostls13.git/commitdiff
internal/types/errors: add InvalidSyntaxTree error
authorRobert Griesemer <gri@golang.org>
Fri, 7 Oct 2022 00:47:13 +0000 (17:47 -0700)
committerGopher Robot <gobot@golang.org>
Mon, 10 Oct 2022 16:02:27 +0000 (16:02 +0000)
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>

src/internal/types/errors/codes.go
src/internal/types/errors/codes_test.go

index a09b5903522553f6fb6e3443e5e958a2cb1a5331..8c0273571f1663a15eff8d408ea46b2526682cb4 100644 (file)
@@ -31,6 +31,11 @@ type Code int
 // problem with types.
 
 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
 
        // Test is reserved for errors that only apply while in self-test mode.
@@ -40,6 +45,9 @@ const (
        //
        // Per the spec:
        //  "The PackageName must not be the blank identifier."
+       //
+       // Example:
+       //  package _
        BlankPkgName
 
        // MismatchedPkgName occurs when a file's package name doesn't match the
index 3bf466aec4d0d6a99ae80fd4b11aaced99410e07..6f671a94c6edc986ae4757faf3d0bf8b20399539 100644 (file)
@@ -24,7 +24,7 @@ func TestErrorCodeExamples(t *testing.T) {
                        doc := spec.Doc.Text()
                        examples := strings.Split(doc, "Example:")
                        for i := 1; i < len(examples); i++ {
-                               example := examples[i]
+                               example := strings.TrimSpace(examples[i])
                                err := checkExample(t, example)
                                if err == nil {
                                        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 {
        t.Helper()
        fset := token.NewFileSet()
-       src := fmt.Sprintf("package p\n\n%s", example)
-       file, err := parser.ParseFile(fset, "example.go", src, 0)
+       if !strings.HasPrefix(example, "package") {
+               example = "package p\n\n" + example
+       }
+       file, err := parser.ParseFile(fset, "example.go", example, 0)
        if err != nil {
                t.Fatal(err)
        }