]> Cypherpunks repositories - gostls13.git/commitdiff
go/build/constraint: fix parsing of "// +build" (with no args)
authorRuss Cox <rsc@golang.org>
Tue, 18 May 2021 15:16:38 +0000 (11:16 -0400)
committerRuss Cox <rsc@golang.org>
Tue, 18 May 2021 22:36:55 +0000 (22:36 +0000)
"// +build" by itself was like "// +build !" - unsatisfiable.
Make it so again (right now it panics).

Fixes #44487.

Change-Id: Iacbc1398af6f988ef011f9f438e792eb62f8f434
Reviewed-on: https://go-review.googlesource.com/c/go/+/320829
Trust: Russ Cox <rsc@golang.org>
Run-TryBot: Russ Cox <rsc@golang.org>
TryBot-Result: Go Bot <gobot@golang.org>
Reviewed-by: Jay Conrod <jayconrod@google.com>
src/go/build/constraint/expr.go
src/go/build/constraint/expr_test.go

index 1ef707ceacffbb8a9fcb64ee74d88147b091a806..957eb9b5279b4f173dec48661322944fe1c23891 100644 (file)
@@ -426,6 +426,9 @@ func parsePlusBuildExpr(text string) Expr {
                        x = or(x, y)
                }
        }
+       if x == nil {
+               x = tag("ignore")
+       }
        return x
 }
 
index 4979f8b5f28978e8e891b818e5df709b3eccf17a..15d189012efb7d91bf4c7b8bf9e9d93910a2e282 100644 (file)
@@ -216,6 +216,7 @@ var parsePlusBuildExprTests = []struct {
        {"!!x", tag("ignore")},
        {"!x", not(tag("x"))},
        {"!", tag("ignore")},
+       {"", tag("ignore")},
 }
 
 func TestParsePlusBuildExpr(t *testing.T) {
@@ -232,19 +233,22 @@ func TestParsePlusBuildExpr(t *testing.T) {
 var constraintTests = []struct {
        in  string
        x   Expr
-       err error
+       err string
 }{
-       {"//+build x y", or(tag("x"), tag("y")), nil},
-       {"// +build x y \n", or(tag("x"), tag("y")), nil},
-       {"// +build x y \n ", nil, errNotConstraint},
-       {"// +build x y \nmore", nil, errNotConstraint},
-       {" //+build x y", nil, errNotConstraint},
+       {"//+build !", tag("ignore"), ""},
+       {"//+build", tag("ignore"), ""},
+       {"//+build x y", or(tag("x"), tag("y")), ""},
+       {"// +build x y \n", or(tag("x"), tag("y")), ""},
+       {"// +build x y \n ", nil, "not a build constraint"},
+       {"// +build x y \nmore", nil, "not a build constraint"},
+       {" //+build x y", nil, "not a build constraint"},
 
-       {"//go:build x && y", and(tag("x"), tag("y")), nil},
-       {"//go:build x && y\n", and(tag("x"), tag("y")), nil},
-       {"//go:build x && y\n ", nil, errNotConstraint},
-       {"//go:build x && y\nmore", nil, errNotConstraint},
-       {" //go:build x && y", nil, errNotConstraint},
+       {"//go:build x && y", and(tag("x"), tag("y")), ""},
+       {"//go:build x && y\n", and(tag("x"), tag("y")), ""},
+       {"//go:build x && y\n ", nil, "not a build constraint"},
+       {"//go:build x && y\nmore", nil, "not a build constraint"},
+       {" //go:build x && y", nil, "not a build constraint"},
+       {"//go:build\n", nil, "unexpected end of expression"},
 }
 
 func TestParse(t *testing.T) {
@@ -252,14 +256,14 @@ func TestParse(t *testing.T) {
                t.Run(fmt.Sprint(i), func(t *testing.T) {
                        x, err := Parse(tt.in)
                        if err != nil {
-                               if tt.err == nil {
+                               if tt.err == "" {
                                        t.Errorf("Constraint(%q): unexpected error: %v", tt.in, err)
-                               } else if tt.err != err {
+                               } else if !strings.Contains(err.Error(), tt.err) {
                                        t.Errorf("Constraint(%q): error %v, want %v", tt.in, err, tt.err)
                                }
                                return
                        }
-                       if tt.err != nil {
+                       if tt.err != "" {
                                t.Errorf("Constraint(%q) = %v, want error %v", tt.in, x, tt.err)
                                return
                        }