From a42f0e2da638996f313ef6dbbe3a4435533fbd97 Mon Sep 17 00:00:00 2001 From: cuishuang Date: Fri, 11 Apr 2025 18:27:20 +0800 Subject: [PATCH 1/3] all: use built-in max/min to simplify the code Change-Id: Ic195ff4df3749050079c85739f61897279933268 Reviewed-on: https://go-review.googlesource.com/c/text/+/664816 Auto-Submit: Sean Liao Reviewed-by: Sean Liao Reviewed-by: Michael Knyszek LUCI-TryBot-Result: Go LUCI Reviewed-by: Mark Freeman --- secure/precis/enforce_test.go | 7 ------- unicode/bidi/core.go | 11 ++--------- 2 files changed, 2 insertions(+), 16 deletions(-) diff --git a/secure/precis/enforce_test.go b/secure/precis/enforce_test.go index 0ced110a..d86a42f8 100644 --- a/secure/precis/enforce_test.go +++ b/secure/precis/enforce_test.go @@ -92,13 +92,6 @@ func TestTransformMallocs(t *testing.T) { } } -func min(a, b int) int { - if a < b { - return a - } - return b -} - // TestTransformerShortBuffers tests that the precis.Transformer implements the // spirit, not just the letter (the method signatures), of the // transform.Transformer interface. diff --git a/unicode/bidi/core.go b/unicode/bidi/core.go index 9d2ae547..fb827323 100644 --- a/unicode/bidi/core.go +++ b/unicode/bidi/core.go @@ -427,13 +427,6 @@ type isolatingRunSequence struct { func (i *isolatingRunSequence) Len() int { return len(i.indexes) } -func maxLevel(a, b level) level { - if a > b { - return a - } - return b -} - // Rule X10, second bullet: Determine the start-of-sequence (sos) and end-of-sequence (eos) types, // either L or R, for each isolating run sequence. func (p *paragraph) isolatingRunSequence(indexes []int) *isolatingRunSequence { @@ -474,8 +467,8 @@ func (p *paragraph) isolatingRunSequence(indexes []int) *isolatingRunSequence { indexes: indexes, types: types, level: level, - sos: typeForLevel(maxLevel(prevLevel, level)), - eos: typeForLevel(maxLevel(succLevel, level)), + sos: typeForLevel(max(prevLevel, level)), + eos: typeForLevel(max(succLevel, level)), } } From 42f038dad6d204bacd83c23ca0f312d8866039ce Mon Sep 17 00:00:00 2001 From: Nicola Murino Date: Mon, 27 Nov 2023 11:38:36 +0100 Subject: [PATCH 2/3] x/text: fix nil dereference in gotext extract Ident.Obj may be nil if the referenced declaration is in another file. Fixes golang/go#60555 Change-Id: I730af89c7f52540b305b3da8c19448d089e9744c Reviewed-on: https://go-review.googlesource.com/c/text/+/545055 Reviewed-by: Emmanuel Odeke Reviewed-by: Junyang Shao LUCI-TryBot-Result: Go LUCI Reviewed-by: Sean Liao Auto-Submit: Emmanuel Odeke Reviewed-by: Michael Knyszek --- message/pipeline/extract.go | 10 +++-- .../testdata/test60555/catalog_gen.go | 38 +++++++++++++++++++ .../test60555/extracted.gotext.json.want | 15 ++++++++ message/pipeline/testdata/test60555/main.go | 18 +++++++++ .../pipeline/testdata/test60555/message.go | 7 ++++ 5 files changed, 85 insertions(+), 3 deletions(-) create mode 100644 message/pipeline/testdata/test60555/catalog_gen.go create mode 100644 message/pipeline/testdata/test60555/extracted.gotext.json.want create mode 100644 message/pipeline/testdata/test60555/main.go create mode 100644 message/pipeline/testdata/test60555/message.go diff --git a/message/pipeline/extract.go b/message/pipeline/extract.go index 23e58f02..7d8385d9 100644 --- a/message/pipeline/extract.go +++ b/message/pipeline/extract.go @@ -597,9 +597,13 @@ func (px packageExtracter) handleCall(call *ast.CallExpr) bool { key := []string{} if ident, ok := format.(*ast.Ident); ok { key = append(key, ident.Name) - if v, ok := ident.Obj.Decl.(*ast.ValueSpec); ok && v.Comment != nil { - // TODO: get comment above ValueSpec as well - comment = v.Comment.Text() + // Ident.Obj may be nil if the referenced declaration is in another + // file. + if ident.Obj != nil { + if v, ok := ident.Obj.Decl.(*ast.ValueSpec); ok && v.Comment != nil { + // TODO: get comment above ValueSpec as well + comment = v.Comment.Text() + } } } if c := px.getComment(call.Args[0]); c != "" { diff --git a/message/pipeline/testdata/test60555/catalog_gen.go b/message/pipeline/testdata/test60555/catalog_gen.go new file mode 100644 index 00000000..dce54afd --- /dev/null +++ b/message/pipeline/testdata/test60555/catalog_gen.go @@ -0,0 +1,38 @@ +// Code generated by running "go generate" in golang.org/x/text. DO NOT EDIT. + +package main + +import ( + "golang.org/x/text/language" + "golang.org/x/text/message" + "golang.org/x/text/message/catalog" +) + +type dictionary struct { + index []uint32 + data string +} + +func (d *dictionary) Lookup(key string) (data string, ok bool) { + p, ok := messageKeyToIndex[key] + if !ok { + return "", false + } + start, end := d.index[p], d.index[p+1] + if start == end { + return "", false + } + return d.data[start:end], true +} + +func init() { + dict := map[string]catalog.Dictionary{} + fallback := language.MustParse("en-US") + cat, err := catalog.NewFromMap(dict, catalog.Fallback(fallback)) + if err != nil { + panic(err) + } + message.DefaultCatalog = cat +} + +var messageKeyToIndex = map[string]int{} diff --git a/message/pipeline/testdata/test60555/extracted.gotext.json.want b/message/pipeline/testdata/test60555/extracted.gotext.json.want new file mode 100644 index 00000000..2abf7b7a --- /dev/null +++ b/message/pipeline/testdata/test60555/extracted.gotext.json.want @@ -0,0 +1,15 @@ +{ + "language": "en-US", + "messages": [ + { + "id": [ + "testMessage", + "Test message" + ], + "key": "Test message", + "message": "Test message", + "translation": "", + "position": "testdata/test60555/main.go:17:10" + } + ] +} \ No newline at end of file diff --git a/message/pipeline/testdata/test60555/main.go b/message/pipeline/testdata/test60555/main.go new file mode 100644 index 00000000..69b7529c --- /dev/null +++ b/message/pipeline/testdata/test60555/main.go @@ -0,0 +1,18 @@ +// 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 main + +import ( + "golang.org/x/text/language" + "golang.org/x/text/message" +) + +// Ensures that we can catch any regressions with nil dereferences +// from const declarations in other files within the same package. +// See issue https://golang.org/issues/60555 +func main() { + p := message.NewPrinter(language.English) + p.Printf(testMessage) +} diff --git a/message/pipeline/testdata/test60555/message.go b/message/pipeline/testdata/test60555/message.go new file mode 100644 index 00000000..ea2a98c1 --- /dev/null +++ b/message/pipeline/testdata/test60555/message.go @@ -0,0 +1,7 @@ +// 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 main + +const testMessage = "Test message" From c6abd0305e90ada9293824462268d0ec20d02e5e Mon Sep 17 00:00:00 2001 From: Gopher Robot Date: Wed, 8 Oct 2025 05:01:53 -0700 Subject: [PATCH 3/3] go.mod: update golang.org/x dependencies Update golang.org/x dependencies to their latest tagged versions. Change-Id: I325df01d59743d807da87a0ae9bf8e224af211c4 Reviewed-on: https://go-review.googlesource.com/c/text/+/710076 Reviewed-by: Dmitri Shuralyov Reviewed-by: David Chase Auto-Submit: Gopher Robot LUCI-TryBot-Result: Go LUCI --- go.mod | 4 ++-- go.sum | 8 ++++---- 2 files changed, 6 insertions(+), 6 deletions(-) diff --git a/go.mod b/go.mod index a36aaa16..1d56ac36 100644 --- a/go.mod +++ b/go.mod @@ -2,9 +2,9 @@ module golang.org/x/text go 1.24.0 -require golang.org/x/tools v0.36.0 // tagx:ignore +require golang.org/x/tools v0.37.0 // tagx:ignore require ( - golang.org/x/mod v0.27.0 // indirect; tagx:ignore + golang.org/x/mod v0.28.0 // indirect; tagx:ignore golang.org/x/sync v0.17.0 // indirect ) diff --git a/go.sum b/go.sum index d0c3f53e..1fa55e6d 100644 --- a/go.sum +++ b/go.sum @@ -1,8 +1,8 @@ github.com/google/go-cmp v0.6.0 h1:ofyhxvXcZhMsU5ulbFiLKl/XBFqE1GSq7atu8tAmTRI= github.com/google/go-cmp v0.6.0/go.mod h1:17dUlkBOakJ0+DkrSSNjCkIjxS6bF9zb3elmeNGIjoY= -golang.org/x/mod v0.27.0 h1:kb+q2PyFnEADO2IEF935ehFUXlWiNjJWtRNgBLSfbxQ= -golang.org/x/mod v0.27.0/go.mod h1:rWI627Fq0DEoudcK+MBkNkCe0EetEaDSwJJkCcjpazc= +golang.org/x/mod v0.28.0 h1:gQBtGhjxykdjY9YhZpSlZIsbnaE2+PgjfLWUQTnoZ1U= +golang.org/x/mod v0.28.0/go.mod h1:yfB/L0NOf/kmEbXjzCPOx1iK1fRutOydrCMsqRhEBxI= golang.org/x/sync v0.17.0 h1:l60nONMj9l5drqw6jlhIELNv9I0A4OFgRsG9k2oT9Ug= golang.org/x/sync v0.17.0/go.mod h1:9KTHXmSnoGruLpwFjVSX0lNNA75CykiMECbovNTZqGI= -golang.org/x/tools v0.36.0 h1:kWS0uv/zsvHEle1LbV5LE8QujrxB3wfQyxHfhOk0Qkg= -golang.org/x/tools v0.36.0/go.mod h1:WBDiHKJK8YgLHlcQPYQzNCkUxUypCaa5ZegCVutKm+s= +golang.org/x/tools v0.37.0 h1:DVSRzp7FwePZW356yEAChSdNcQo6Nsp+fex1SUW09lE= +golang.org/x/tools v0.37.0/go.mod h1:MBN5QPQtLMHVdvsbtarmTNukZDdgwdwlO5qGacAzF0w=