Skip to content

Commit eeadce2

Browse files
committed
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 <[email protected]> Run-TryBot: Russ Cox <[email protected]> TryBot-Result: Go Bot <[email protected]> Reviewed-by: Jay Conrod <[email protected]>
1 parent 6d2ef2e commit eeadce2

File tree

2 files changed

+21
-14
lines changed

2 files changed

+21
-14
lines changed

src/go/build/constraint/expr.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -426,6 +426,9 @@ func parsePlusBuildExpr(text string) Expr {
426426
x = or(x, y)
427427
}
428428
}
429+
if x == nil {
430+
x = tag("ignore")
431+
}
429432
return x
430433
}
431434

src/go/build/constraint/expr_test.go

Lines changed: 18 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -216,6 +216,7 @@ var parsePlusBuildExprTests = []struct {
216216
{"!!x", tag("ignore")},
217217
{"!x", not(tag("x"))},
218218
{"!", tag("ignore")},
219+
{"", tag("ignore")},
219220
}
220221

221222
func TestParsePlusBuildExpr(t *testing.T) {
@@ -232,34 +233,37 @@ func TestParsePlusBuildExpr(t *testing.T) {
232233
var constraintTests = []struct {
233234
in string
234235
x Expr
235-
err error
236+
err string
236237
}{
237-
{"//+build x y", or(tag("x"), tag("y")), nil},
238-
{"// +build x y \n", or(tag("x"), tag("y")), nil},
239-
{"// +build x y \n ", nil, errNotConstraint},
240-
{"// +build x y \nmore", nil, errNotConstraint},
241-
{" //+build x y", nil, errNotConstraint},
238+
{"//+build !", tag("ignore"), ""},
239+
{"//+build", tag("ignore"), ""},
240+
{"//+build x y", or(tag("x"), tag("y")), ""},
241+
{"// +build x y \n", or(tag("x"), tag("y")), ""},
242+
{"// +build x y \n ", nil, "not a build constraint"},
243+
{"// +build x y \nmore", nil, "not a build constraint"},
244+
{" //+build x y", nil, "not a build constraint"},
242245

243-
{"//go:build x && y", and(tag("x"), tag("y")), nil},
244-
{"//go:build x && y\n", and(tag("x"), tag("y")), nil},
245-
{"//go:build x && y\n ", nil, errNotConstraint},
246-
{"//go:build x && y\nmore", nil, errNotConstraint},
247-
{" //go:build x && y", nil, errNotConstraint},
246+
{"//go:build x && y", and(tag("x"), tag("y")), ""},
247+
{"//go:build x && y\n", and(tag("x"), tag("y")), ""},
248+
{"//go:build x && y\n ", nil, "not a build constraint"},
249+
{"//go:build x && y\nmore", nil, "not a build constraint"},
250+
{" //go:build x && y", nil, "not a build constraint"},
251+
{"//go:build\n", nil, "unexpected end of expression"},
248252
}
249253

250254
func TestParse(t *testing.T) {
251255
for i, tt := range constraintTests {
252256
t.Run(fmt.Sprint(i), func(t *testing.T) {
253257
x, err := Parse(tt.in)
254258
if err != nil {
255-
if tt.err == nil {
259+
if tt.err == "" {
256260
t.Errorf("Constraint(%q): unexpected error: %v", tt.in, err)
257-
} else if tt.err != err {
261+
} else if !strings.Contains(err.Error(), tt.err) {
258262
t.Errorf("Constraint(%q): error %v, want %v", tt.in, err, tt.err)
259263
}
260264
return
261265
}
262-
if tt.err != nil {
266+
if tt.err != "" {
263267
t.Errorf("Constraint(%q) = %v, want error %v", tt.in, x, tt.err)
264268
return
265269
}

0 commit comments

Comments
 (0)