go/build/constraint: fix parsing of "// +build" (with no args)

"// +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>
This commit is contained in:
Russ Cox 2021-05-18 11:16:38 -04:00
parent 6d2ef2ef2a
commit eeadce2d87
2 changed files with 21 additions and 14 deletions

View file

@ -426,6 +426,9 @@ func parsePlusBuildExpr(text string) Expr {
x = or(x, y)
}
}
if x == nil {
x = tag("ignore")
}
return x
}

View 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
}