aboutsummaryrefslogtreecommitdiff
path: root/src/internal
diff options
context:
space:
mode:
authorRobert Griesemer <gri@golang.org>2022-10-06 17:47:13 -0700
committerGopher Robot <gobot@golang.org>2022-10-10 16:02:27 +0000
commit578523e4a0f68e9b35984f017bb3471b0bd313b4 (patch)
treefa1ea60e8a10fce29d0906d6880b7a339c4b99a1 /src/internal
parent6688efd5df1ef82dbe5d1f47b4c9bf4321a44126 (diff)
downloadgo-578523e4a0f68e9b35984f017bb3471b0bd313b4.tar.xz
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>
Diffstat (limited to 'src/internal')
-rw-r--r--src/internal/types/errors/codes.go8
-rw-r--r--src/internal/types/errors/codes_test.go8
2 files changed, 13 insertions, 3 deletions
diff --git a/src/internal/types/errors/codes.go b/src/internal/types/errors/codes.go
index a09b590352..8c0273571f 100644
--- a/src/internal/types/errors/codes.go
+++ b/src/internal/types/errors/codes.go
@@ -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
diff --git a/src/internal/types/errors/codes_test.go b/src/internal/types/errors/codes_test.go
index 3bf466aec4..6f671a94c6 100644
--- a/src/internal/types/errors/codes_test.go
+++ b/src/internal/types/errors/codes_test.go
@@ -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)
}