cmd/dist: inline matchexpr into its only caller

Now that the implementation of matchexpr is trivial (because it reuses
the go/build/constraint package), inline it into its only caller. This
leaves two fewer .go files in cmd/dist: no need for TestBuildParser as
the go/build/constraint package has its own tests.

For #79185.

Change-Id: Ib3312527e14b8e826493558594a607fa0f06fd18
Reviewed-on: https://go-review.googlesource.com/c/go/+/773823
Reviewed-by: Alan Donovan <adonovan@google.com>
Reviewed-by: Dmitri Shuralyov <dmitshur@google.com>
Auto-Submit: Dmitri Shuralyov <dmitshur@golang.org>
LUCI-TryBot-Result: golang-scoped@luci-project-accounts.iam.gserviceaccount.com <golang-scoped@luci-project-accounts.iam.gserviceaccount.com>
This commit is contained in:
Dmitri Shuralyov 2026-05-04 11:51:57 -04:00 committed by Gopher Robot
parent e1dff0e0b9
commit f6664a0a60
3 changed files with 5 additions and 66 deletions

View file

@ -9,6 +9,7 @@ import (
"encoding/json"
"flag"
"fmt"
"go/build/constraint"
"io"
"io/fs"
"log"
@ -1166,11 +1167,12 @@ func shouldbuild(file, pkg string) bool {
break
}
if strings.HasPrefix(p, "//go:build ") {
matched, err := matchexpr(p[len("//go:build "):])
c, err := constraint.Parse(p)
if err != nil {
errprintf("%s: %v", file, err)
errprintf("%s: parsing //go:build line: %v", file, err)
return false
}
return matched
return c.Eval(matchtag)
}
}

View file

@ -1,19 +0,0 @@
// Copyright 2021 The Go Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
package main
import (
"fmt"
"go/build/constraint"
)
// matchexpr parses and evaluates the //go:build expression x.
func matchexpr(x string) (matched bool, _ error) {
c, err := constraint.Parse("//go:build " + x)
if err != nil {
return false, fmt.Errorf("parsing //go:build line: %v", err)
}
return c.Eval(matchtag), nil
}

View file

@ -1,44 +0,0 @@
// Copyright 2021 The Go Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
package main
import (
"fmt"
"reflect"
"testing"
)
var buildParserTests = []struct {
x string
matched bool
err error
}{
{"gc", true, nil},
{"gccgo", false, nil},
{"!gc", false, nil},
{"gc && gccgo", false, nil},
{"gc || gccgo", true, nil},
{"gc || (gccgo && !gccgo)", true, nil},
{"gc && (gccgo || !gccgo)", true, nil},
{"gccgo && gc || gc", true, nil},
{"!(gc && (gccgo || !gccgo))", false, nil},
{"gccgo || gc", true, nil},
{"!(!(!(gccgo || gc)))", false, nil},
{"compiler_bootstrap", false, nil},
{"cmd_go_bootstrap", true, nil},
{"syntax(error", false, fmt.Errorf("parsing //go:build line: unexpected token (")},
{"(gc", false, fmt.Errorf("parsing //go:build line: missing close paren")},
{"gc gc", false, fmt.Errorf("parsing //go:build line: unexpected token gc")},
{"(gc))", false, fmt.Errorf("parsing //go:build line: unexpected token )")},
}
func TestBuildParser(t *testing.T) {
for _, tt := range buildParserTests {
matched, err := matchexpr(tt.x)
if matched != tt.matched || !reflect.DeepEqual(err, tt.err) {
t.Errorf("matchexpr(%q) = %v, %v; want %v, %v", tt.x, matched, err, tt.matched, tt.err)
}
}
}