Compare commits

...

7 Commits

Author SHA1 Message Date
Name 8a3f91684d
Merge a4bc4a5dc5 into 49cdf0c42e 2025-06-20 15:31:52 -04:00
Damien Neil 49cdf0c42e testing, testing/synctest: handle T.Helper in synctest bubbles
Fixes #74199

Change-Id: I6a15fbd59a3a3f8c496440f56d09d695e1504e4e
Reviewed-on: https://go-review.googlesource.com/c/go/+/682576
LUCI-TryBot-Result: Go LUCI <golang-scoped@luci-project-accounts.iam.gserviceaccount.com>
Reviewed-by: Alan Donovan <adonovan@google.com>
Auto-Submit: Damien Neil <dneil@google.com>
2025-06-20 12:29:58 -07:00
cuishuang 3bf1eecbd3 runtime: fix struct comment
Change-Id: I0c33830b13c8a187ac82504c7653abb8f8cf7530
Reviewed-on: https://go-review.googlesource.com/c/go/+/681655
Reviewed-by: Sean Liao <sean@liao.dev>
Reviewed-by: Junyang Shao <shaojunyang@google.com>
Reviewed-by: Michael Knyszek <mknyszek@google.com>
LUCI-TryBot-Result: Go LUCI <golang-scoped@luci-project-accounts.iam.gserviceaccount.com>
Auto-Submit: Sean Liao <sean@liao.dev>
2025-06-20 11:28:03 -07:00
Sean Liao 8ed23a2936 crypto/cipher: fix link to crypto/aes
Fixes #74309

Change-Id: I4d97514355d825124a8d879c2590b45b039f5fd1
Reviewed-on: https://go-review.googlesource.com/c/go/+/682596
Reviewed-by: Daniel McCarney <daniel@binaryparadox.net>
Reviewed-by: Junyang Shao <shaojunyang@google.com>
Reviewed-by: Cherry Mui <cherryyz@google.com>
LUCI-TryBot-Result: Go LUCI <golang-scoped@luci-project-accounts.iam.gserviceaccount.com>
2025-06-20 11:09:26 -07:00
Adam Bender ef60769b46 go/doc: add a golden test that reproduces #62640
For #62640.
For #61394.

This is a copy of https://go-review.googlesource.com/c/go/+/528402,
which has stalled in review and the owner is no longer working on Go.

Change-Id: Ic7a1ae65c70d4857ab1061ccae1a926bf5c4ff55
Reviewed-on: https://go-review.googlesource.com/c/go/+/681235
LUCI-TryBot-Result: Go LUCI <golang-scoped@luci-project-accounts.iam.gserviceaccount.com>
Reviewed-by: Robert Griesemer <gri@google.com>
Auto-Submit: Robert Griesemer <gri@google.com>
Reviewed-by: Junyang Shao <shaojunyang@google.com>
2025-06-20 10:05:12 -07:00
1911860538 a4bc4a5dc5 cmd/cgo: restore TrimSpace removed in saveExport 2025-05-17 16:51:01 +08:00
1911860538 df05b9ea20 all: replace HasPrefix and slicing with CutPrefix 2025-05-17 16:37:04 +08:00
28 changed files with 191 additions and 59 deletions

View File

@ -796,8 +796,8 @@ func toValidName(name string) string {
p = strings.TrimPrefix(p, "/")
for strings.HasPrefix(p, "../") {
p = p[len("../"):]
for cut := true; cut; {
p, cut = strings.CutPrefix(p, "../")
}
return p

View File

@ -288,11 +288,12 @@ func (f *File) saveExport(x interface{}, context astContext) {
return
}
for _, c := range n.Doc.List {
if !strings.HasPrefix(c.Text, "//export ") {
name, cut := strings.CutPrefix(c.Text, "//export ")
if !cut {
continue
}
name := strings.TrimSpace(c.Text[9:])
name = strings.TrimSpace(name)
if name == "" {
error_(c.Pos(), "export missing name")
}

View File

@ -255,10 +255,10 @@ func bootstrapBuildTools() {
// Copy binaries into tool binary directory.
for _, name := range bootstrapDirs {
if !strings.HasPrefix(name, "cmd/") {
name, cut := strings.CutPrefix(name, "cmd/")
if !cut {
continue
}
name = name[len("cmd/"):]
if !strings.Contains(name, "/") {
copyfile(pathf("%s/%s%s", tooldir, name, exe), pathf("%s/bin/%s%s", workspace, name, exe), writeExec)
}

View File

@ -31,10 +31,10 @@ func GoModLookup(gomod []byte, key string) string {
}
func parseKey(line []byte, key string) (string, bool) {
if !strings.HasPrefix(string(line), key) {
s, cut := strings.CutPrefix(string(line), key)
if !cut {
return "", false
}
s := strings.TrimPrefix(string(line), key)
if len(s) == 0 || (s[0] != ' ' && s[0] != '\t') {
return "", false
}

View File

@ -132,10 +132,11 @@ func godebugForGoVersion(v string) map[string]string {
v = v[:j]
}
if !strings.HasPrefix(v, "1.") {
nv, cut := strings.CutPrefix(v, "1.")
if !cut {
return nil
}
n, err := strconv.Atoi(v[len("1."):])
n, err := strconv.Atoi(nv)
if err != nil {
return nil
}

View File

@ -308,10 +308,10 @@ func (r *gitRepo) Tags(ctx context.Context, prefix string) (*Tags, error) {
List: []Tag{},
}
for ref, hash := range refs {
if !strings.HasPrefix(ref, "refs/tags/") {
tag, cut := strings.CutPrefix(ref, "refs/tags/")
if !cut {
continue
}
tag := ref[len("refs/tags/"):]
if !strings.HasPrefix(tag, prefix) {
continue
}
@ -766,19 +766,19 @@ func (r *gitRepo) RecentTag(ctx context.Context, rev, prefix string, allowed fun
line = strings.TrimSpace(line)
// git do support lstrip in for-each-ref format, but it was added in v2.13.0. Stripping here
// instead gives support for git v2.7.0.
if !strings.HasPrefix(line, "refs/tags/") {
line, cut := strings.CutPrefix(line, "refs/tags/")
if !cut {
continue
}
line = line[len("refs/tags/"):]
if !strings.HasPrefix(line, prefix) {
semtag, cut := strings.CutPrefix(line, prefix)
if !cut {
continue
}
if !allowed(line) {
continue
}
semtag := line[len(prefix):]
if semver.Compare(semtag, highest) > 0 {
highest = semtag
}

View File

@ -565,10 +565,10 @@ func (r *codeRepo) convert(ctx context.Context, info *codehost.RevInfo, statVers
// If the tag is invalid, retracted, or a pseudo-version, tagToVersion returns
// an empty version.
tagToVersion := func(tag string) (v string, tagIsCanonical bool) {
if !strings.HasPrefix(tag, tagPrefix) {
trimmed, cut := strings.CutPrefix(tag, tagPrefix)
if !cut {
return "", false
}
trimmed := tag[len(tagPrefix):]
// Tags that look like pseudo-versions would be confusing. Ignore them.
if module.IsPseudoVersion(tag) {
return "", false

View File

@ -171,10 +171,11 @@ func (ctxt *Context) hasSubdir(root, dir string) (rel string, ok bool) {
func hasSubdir(root, dir string) (rel string, ok bool) {
root = str.WithFilePathSeparator(filepath.Clean(root))
dir = filepath.Clean(dir)
if !strings.HasPrefix(dir, root) {
path, cut := strings.CutPrefix(dir, root)
if !cut {
return "", false
}
return filepath.ToSlash(dir[len(root):]), true
return filepath.ToSlash(path), true
}
// gopath returns the list of Go path directories.

View File

@ -65,10 +65,10 @@ func indexModule(modroot string) ([]byte, error) {
if !d.IsDir() {
return nil
}
if !strings.HasPrefix(path, root) {
rel, cut := strings.CutPrefix(path, root)
if !cut {
panic(fmt.Errorf("path %v in walk doesn't have modroot %v as prefix", path, modroot))
}
rel := path[len(root):]
packages = append(packages, importRaw(modroot, rel))
return nil
})

View File

@ -75,10 +75,10 @@ func RunToolScriptTest(t *testing.T, repls []ToolReplacement, scriptsdir string,
found := false
for k := range env {
ev := env[k]
if !strings.HasPrefix(ev, "PATH=") {
oldpath, cut := strings.CutPrefix(ev, "PATH=")
if !cut {
continue
}
oldpath := ev[5:]
env[k] = "PATH=" + dir + string(filepath.ListSeparator) + oldpath
found = true
break

View File

@ -807,10 +807,10 @@ func (state *peLoaderState) preprocessSymbols() error {
// "__imp_XYZ" but no XYZ can be found.
func LookupBaseFromImport(s loader.Sym, ldr *loader.Loader, arch *sys.Arch) (loader.Sym, error) {
sname := ldr.SymName(s)
if !strings.HasPrefix(sname, "__imp_") {
basename, cut := strings.CutPrefix(sname, "__imp_")
if !cut {
return 0, nil
}
basename := sname[len("__imp_"):]
if arch.Family == sys.I386 && basename[0] == '_' {
basename = basename[1:] // _Name => Name
}

View File

@ -82,7 +82,7 @@ func newGCM(cipher Block, nonceSize, tagSize int) (AEAD, error) {
// NewGCMWithRandomNonce returns the given cipher wrapped in Galois Counter
// Mode, with randomly-generated nonces. The cipher must have been created by
// [aes.NewCipher].
// [crypto/aes.NewCipher].
//
// It generates a random 96-bit nonce, which is prepended to the ciphertext by Seal,
// and is extracted from the ciphertext by Open. The NonceSize of the AEAD is zero,

View File

@ -493,11 +493,11 @@ func parseFieldOptions(sf reflect.StructField) (out fieldOptions, ignored bool,
}
switch opt {
case "case":
if !strings.HasPrefix(tag, ":") {
tag, cut := strings.CutPrefix(tag, ":")
if !cut {
err = cmp.Or(err, fmt.Errorf("Go struct field %s is missing value for `case` tag option; specify `case:ignore` or `case:strict` instead", sf.Name))
break
}
tag = tag[len(":"):]
opt, n, err2 := consumeTagOption(tag)
if err2 != nil {
err = cmp.Or(err, fmt.Errorf("Go struct field %s has malformed value for `case` tag option: %v", sf.Name, err2))
@ -527,11 +527,11 @@ func parseFieldOptions(sf reflect.StructField) (out fieldOptions, ignored bool,
case "string":
out.string = true
case "format":
if !strings.HasPrefix(tag, ":") {
tag, cut := strings.CutPrefix(tag, ":")
if !cut {
err = cmp.Or(err, fmt.Errorf("Go struct field %s is missing value for `format` tag option", sf.Name))
break
}
tag = tag[len(":"):]
opt, n, err2 := consumeTagOption(tag)
if err2 != nil {
err = cmp.Or(err, fmt.Errorf("Go struct field %s has malformed value for `format` tag option: %v", sf.Name, err2))

View File

@ -178,12 +178,12 @@ func splitGoBuild(line string) (expr string, ok bool) {
return "", false
}
if !strings.HasPrefix(line, "//go:build") {
line, cut := strings.CutPrefix(line, "//go:build")
if !cut {
return "", false
}
line = strings.TrimSpace(line)
line = line[len("//go:build"):]
// If strings.TrimSpace finds more to trim after removing the //go:build prefix,
// it means that the prefix was followed by a space, making this a //go:build line
@ -373,17 +373,17 @@ func splitPlusBuild(line string) (expr string, ok bool) {
return "", false
}
if !strings.HasPrefix(line, "//") {
line, cut := strings.CutPrefix(line, "//")
if !cut {
return "", false
}
line = line[len("//"):]
// Note the space is optional; "//+build" is recognized too.
line = strings.TrimSpace(line)
if !strings.HasPrefix(line, "+build") {
line, cut = strings.CutPrefix(line, "+build")
if !cut {
return "", false
}
line = line[len("+build"):]
// If strings.TrimSpace finds more to trim after removing the +build prefix,
// it means that the prefix was followed by a space, making this a +build line

22
src/go/doc/testdata/issue62640.0.golden vendored Normal file
View File

@ -0,0 +1,22 @@
//
PACKAGE issue62640
IMPORTPATH
testdata/issue62640
FILENAMES
testdata/issue62640.go
TYPES
//
type E struct{}
// F should be hidden within S because of the S.F field.
func (E) F()
//
type S struct {
E
F int
}

22
src/go/doc/testdata/issue62640.1.golden vendored Normal file
View File

@ -0,0 +1,22 @@
//
PACKAGE issue62640
IMPORTPATH
testdata/issue62640
FILENAMES
testdata/issue62640.go
TYPES
//
type E struct{}
// F should be hidden within S because of the S.F field.
func (E) F()
//
type S struct {
E
F int
}

25
src/go/doc/testdata/issue62640.2.golden vendored Normal file
View File

@ -0,0 +1,25 @@
//
PACKAGE issue62640
IMPORTPATH
testdata/issue62640
FILENAMES
testdata/issue62640.go
TYPES
//
type E struct{}
// F should be hidden within S because of the S.F field.
func (E) F()
//
type S struct {
E
F int
}
// F should be hidden within S because of the S.F field.
func (S) F()

15
src/go/doc/testdata/issue62640.go vendored Normal file
View File

@ -0,0 +1,15 @@
// Copyright 2025 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 issue62640
type E struct{}
// F should be hidden within S because of the S.F field.
func (E) F() {}
type S struct {
E
F int
}

View File

@ -87,18 +87,27 @@ func gofips140() string {
// isFIPSVersion reports whether v is a valid FIPS version,
// of the form vX.Y.Z.
func isFIPSVersion(v string) bool {
if !strings.HasPrefix(v, "v") {
v, cut := strings.CutPrefix(v, "v")
if !cut {
return false
}
v, ok := skipNum(v[len("v"):])
if !ok || !strings.HasPrefix(v, ".") {
v, ok := skipNum(v)
if !ok {
return false
}
v, ok = skipNum(v[len("."):])
if !ok || !strings.HasPrefix(v, ".") {
v, cut = strings.CutPrefix(v, ".")
if !cut {
return false
}
v, ok = skipNum(v[len("."):])
v, ok = skipNum(v)
if !ok {
return false
}
v, cut = strings.CutPrefix(v, ".")
if !cut {
return false
}
v, ok = skipNum(v)
return ok && v == ""
}

View File

@ -37,10 +37,11 @@ func NewTextReader(r io.Reader) (*TextReader, error) {
return nil, fmt.Errorf("failed to parse header")
}
gover, line := readToken(line)
if !strings.HasPrefix(gover, "Go1.") {
ver, cut := strings.CutPrefix(gover, "Go1.")
if !cut {
return nil, fmt.Errorf("failed to parse header Go version")
}
rawv, err := strconv.ParseUint(gover[len("Go1."):], 10, 64)
rawv, err := strconv.ParseUint(ver, 10, 64)
if err != nil {
return nil, fmt.Errorf("failed to parse header Go version: %v", err)
}

View File

@ -103,10 +103,11 @@ func checkMediaTypeDisposition(s string) error {
if rest == "" {
return nil
}
if !strings.HasPrefix(rest, "/") {
rest, cut := strings.CutPrefix(rest, "/")
if !cut {
return errors.New("mime: expected slash after first token")
}
subtype, rest := consumeToken(rest[1:])
subtype, rest := consumeToken(rest)
if subtype == "" {
return errors.New("mime: expected token after slash")
}
@ -309,11 +310,11 @@ func consumeValue(v string) (value, rest string) {
func consumeMediaParam(v string) (param, value, rest string) {
rest = strings.TrimLeftFunc(v, unicode.IsSpace)
if !strings.HasPrefix(rest, ";") {
rest, cut := strings.CutPrefix(rest, ";")
if !cut {
return "", "", v
}
rest = rest[1:] // consume semicolon
rest = strings.TrimLeftFunc(rest, unicode.IsSpace)
param, rest = consumeToken(rest)
param = strings.ToLower(param)
@ -322,10 +323,10 @@ func consumeMediaParam(v string) (param, value, rest string) {
}
rest = strings.TrimLeftFunc(rest, unicode.IsSpace)
if !strings.HasPrefix(rest, "=") {
rest, cut = strings.CutPrefix(rest, "=")
if !cut {
return "", "", v
}
rest = rest[1:] // consume equals sign
rest = strings.TrimLeftFunc(rest, unicode.IsSpace)
value, rest2 := consumeValue(rest)
if value == "" && rest2 == rest {

View File

@ -1017,12 +1017,13 @@ func parseRange(s string, size int64) ([]httpRange, error) {
return nil, nil // header not present
}
const b = "bytes="
if !strings.HasPrefix(s, b) {
s, cut := strings.CutPrefix(s, b)
if !cut {
return nil, errors.New("invalid range")
}
var ranges []httpRange
noOverlap := false
for ra := range strings.SplitSeq(s[len(b):], ",") {
for ra := range strings.SplitSeq(s, ",") {
ra = textproto.TrimString(ra)
if ra == "" {
continue

View File

@ -6760,10 +6760,10 @@ const http2TrailerPrefix = "Trailer:"
// fields to be trailers.
func (rws *http2responseWriterState) promoteUndeclaredTrailers() {
for k, vv := range rws.handlerHeader {
if !strings.HasPrefix(k, http2TrailerPrefix) {
trailerKey, cut := strings.CutPrefix(k, http2TrailerPrefix)
if !cut {
continue
}
trailerKey := strings.TrimPrefix(k, http2TrailerPrefix)
rws.declareTrailer(trailerKey)
rws.handlerHeader[CanonicalHeaderKey(trailerKey)] = vv
}

View File

@ -224,14 +224,15 @@ func (rw *ResponseRecorder) Result() *http.Response {
}
}
for k, vv := range rw.HeaderMap {
if !strings.HasPrefix(k, http.TrailerPrefix) {
k, cut := strings.CutPrefix(k, http.TrailerPrefix)
if !cut {
continue
}
if res.Trailer == nil {
res.Trailer = make(http.Header)
}
for _, v := range vv {
res.Trailer.Add(strings.TrimPrefix(k, http.TrailerPrefix), v)
res.Trailer.Add(k, v)
}
}
return res

View File

@ -312,8 +312,10 @@ type heapArena struct {
// during marking.
pageSpecials [pagesPerArena / 8]uint8
// pageUseSpanDartboard is a bitmap that indicates which spans are
// heap spans and also gcUsesSpanDartboard.
// pageUseSpanInlineMarkBits is a bitmap where each bit corresponds
// to a span, as only spans one page in size can have inline mark bits.
// The bit indicates that the span has a spanInlineMarkBits struct
// stored directly at the top end of the span's memory.
pageUseSpanInlineMarkBits [pagesPerArena / 8]uint8
// checkmarks stores the debug.gccheckmark state. It is only

View File

@ -0,0 +1,15 @@
// Copyright 2025 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 synctest_test
import "testing"
// helperLog is a t.Helper which logs.
// Since it is a helper, the log prefix should contain
// the caller's file, not helper_test.go.
func helperLog(t *testing.T, s string) {
t.Helper()
t.Log(s)
}

View File

@ -140,6 +140,18 @@ func TestRun(t *testing.T) {
})
}
func TestHelper(t *testing.T) {
runTest(t, []string{"-test.v"}, func() {
synctest.Test(t, func(t *testing.T) {
helperLog(t, "log in helper")
})
}, `^=== RUN TestHelper
synctest_test.go:.* log in helper
--- PASS: TestHelper.*
PASS
$`)
}
func wantPanic(t *testing.T, want string) {
if e := recover(); e != nil {
if got := fmt.Sprint(e); got != want {

View File

@ -1261,6 +1261,9 @@ func (c *common) Skipped() bool {
// When printing file and line information, that function will be skipped.
// Helper may be called simultaneously from multiple goroutines.
func (c *common) Helper() {
if c.isSynctest {
c = c.parent
}
c.mu.Lock()
defer c.mu.Unlock()
if c.helperPCs == nil {