[tools] gopls/internal/util/frob: change Codec.Decode to return the value

1 view
Skip to first unread message

Robert Findley (Gerrit)

unread,
Apr 8, 2026, 2:32:08 PM (12 hours ago) Apr 8
to goph...@pubsubhelper.golang.org, golang-co...@googlegroups.com

Robert Findley has uploaded the change for review

Commit message

gopls/internal/util/frob: change Codec.Decode to return the value

Change Codec[T].Decode from Decode(data, *T) to Decode(data) T. This
reads more naturally at every call site and lets the method value be
passed directly as a filecache.Get decoder.

Updates golang/go#69523
Change-Id: I9912fb96b468a7b993174351110a89e021c74038

Change diff

diff --git a/gopls/internal/cache/analysis.go b/gopls/internal/cache/analysis.go
index 1a8829d..6bbfb79 100644
--- a/gopls/internal/cache/analysis.go
+++ b/gopls/internal/cache/analysis.go
@@ -572,7 +572,7 @@
const cacheKind = "analysis"
if data, err := filecache.Get(cacheKind, key, filecache.Bytes); err == nil {
// cache hit
- analyzeSummaryCodec.Decode(data, &summary)
+ summary = analyzeSummaryCodec.Decode(data)
} else if err != filecache.ErrNotFound {
return nil, bug.Errorf("internal error reading shared cache: %v", err)
} else {
diff --git a/gopls/internal/cache/errors.go b/gopls/internal/cache/errors.go
index 26f30c8..f7e9813 100644
--- a/gopls/internal/cache/errors.go
+++ b/gopls/internal/cache/errors.go
@@ -205,8 +205,7 @@

// decodeDiagnostics decodes the given gob-encoded diagnostics.
func decodeDiagnostics(data []byte) []*Diagnostic {
- var gobDiags []gobDiagnostic
- diagnosticsCodec.Decode(data, &gobDiags)
+ gobDiags := diagnosticsCodec.Decode(data)
var srcDiags []*Diagnostic
for _, gobDiag := range gobDiags {
var srcFixes []SuggestedFix
diff --git a/gopls/internal/cache/methodsets/methodsets.go b/gopls/internal/cache/methodsets/methodsets.go
index feb1b16..b47f4a1 100644
--- a/gopls/internal/cache/methodsets/methodsets.go
+++ b/gopls/internal/cache/methodsets/methodsets.go
@@ -67,9 +67,7 @@

// Decode decodes the given gob-encoded data as an Index.
func Decode(pkgpath metadata.PackagePath, data []byte) *Index {
- var pkg gobPackage
- packageCodec.Decode(data, &pkg)
- return &Index{pkg: pkg, PkgPath: pkgpath}
+ return &Index{pkg: packageCodec.Decode(data), PkgPath: pkgpath}
}

// Encode encodes the receiver as gob-encoded data.
diff --git a/gopls/internal/cache/symbols/symbols.go b/gopls/internal/cache/symbols/symbols.go
index 4af0240..df4f978 100644
--- a/gopls/internal/cache/symbols/symbols.go
+++ b/gopls/internal/cache/symbols/symbols.go
@@ -41,8 +41,7 @@

// Decode decodes data from [Package.Encode].
func Decode(data []byte) *Package {
- var pkg Package
- codec.Decode(data, &pkg)
+ pkg := codec.Decode(data)
return &pkg
}

diff --git a/gopls/internal/cache/testfuncs/tests.go b/gopls/internal/cache/testfuncs/tests.go
index e0e3ce1..d9d6ffe 100644
--- a/gopls/internal/cache/testfuncs/tests.go
+++ b/gopls/internal/cache/testfuncs/tests.go
@@ -25,9 +25,7 @@

// Decode decodes the given gob-encoded data as an Index.
func Decode(data []byte) *Index {
- var pkg gobPackage
- packageCodec.Decode(data, &pkg)
- return &Index{pkg}
+ return &Index{packageCodec.Decode(data)}
}

// Encode encodes the receiver as gob-encoded data.
diff --git a/gopls/internal/cache/typerefs/refs.go b/gopls/internal/cache/typerefs/refs.go
index 9b2f6a5..3b9763e 100644
--- a/gopls/internal/cache/typerefs/refs.go
+++ b/gopls/internal/cache/typerefs/refs.go
@@ -801,8 +801,7 @@
}

func decode(pkgIndex *PackageIndex, data []byte) []Class {
- var payload gobClasses
- classesCodec.Decode(data, &payload)
+ payload := classesCodec.Decode(data)

classes := make([]Class, len(payload.Classes))
for i, gobClass := range payload.Classes {
diff --git a/gopls/internal/cache/xrefs/xrefs.go b/gopls/internal/cache/xrefs/xrefs.go
index ae9fc1b..56a9ef0 100644
--- a/gopls/internal/cache/xrefs/xrefs.go
+++ b/gopls/internal/cache/xrefs/xrefs.go
@@ -148,9 +148,7 @@
// Decode decodes a serialized cross-reference index.
// It is suitable for use as a filecache.Get decoder.
func Decode(data []byte) *Index {
- var packages []*gobPackage
- packageCodec.Decode(data, &packages)
- return &Index{packages: packages}
+ return &Index{packages: packageCodec.Decode(data)}
}

// Lookup searches the index for references to any object in the target set,
diff --git a/gopls/internal/filewatcher/poll_watcher.go b/gopls/internal/filewatcher/poll_watcher.go
index 780667b..a48b8c3 100644
--- a/gopls/internal/filewatcher/poll_watcher.go
+++ b/gopls/internal/filewatcher/poll_watcher.go
@@ -348,11 +348,7 @@

func (w *pollWatcher) loadState(root string) (fileState, error) {
key := cacheKey(root)
- state, err := filecache.Get(filewatcherKind, key, func(data []byte) fileState {
- var state fileState
- codec.Decode(data, &state)
- return state
- })
+ state, err := filecache.Get(filewatcherKind, key, codec.Decode)
if err != nil && err != filecache.ErrNotFound {
bug.Reportf("internal error reading shared cache: %v", err)
}
diff --git a/gopls/internal/settings/settings.go b/gopls/internal/settings/settings.go
index 420dede..02956b8 100644
--- a/gopls/internal/settings/settings.go
+++ b/gopls/internal/settings/settings.go
@@ -1100,10 +1100,7 @@
var codec = frob.CodecFor[*Options]()

func (o *Options) Clone() *Options {
- data := codec.Encode(o)
- var clone *Options
- codec.Decode(data, &clone)
- return clone
+ return codec.Decode(codec.Encode(o))
}

// validateDirectoryFilter validates if the filter string
diff --git a/gopls/internal/util/frob/frob.go b/gopls/internal/util/frob/frob.go
index 6ef157b..357198c 100644
--- a/gopls/internal/util/frob/frob.go
+++ b/gopls/internal/util/frob/frob.go
@@ -48,8 +48,14 @@
return Codec[T]{frobFor(reflect.TypeFor[T]())}
}

-func (codec Codec[T]) Encode(v T) []byte { return codec.frob.Encode(v) }
-func (codec Codec[T]) Decode(data []byte, ptr *T) { codec.frob.Decode(data, ptr) }
+func (codec Codec[T]) Encode(v T) []byte { return codec.frob.Encode(v) }
+
+// Decode decodes data and returns the result. It is suitable for use
+// as a filecache.Get decoder.
+func (codec Codec[T]) Decode(data []byte) (v T) {
+ codec.frob.Decode(data, &v)
+ return
+}

var (
frobsMu sync.Mutex
diff --git a/gopls/internal/util/frob/frob_test.go b/gopls/internal/util/frob/frob_test.go
index 5765c96..be01385 100644
--- a/gopls/internal/util/frob/frob_test.go
+++ b/gopls/internal/util/frob/frob_test.go
@@ -34,8 +34,7 @@
E: []byte("hello"),
F: []string{s1, s2},
}
- var y Basics
- codec.Decode(codec.Encode(x), &y)
+ y := codec.Decode(codec.Encode(x))
if !reflect.DeepEqual(x, y) {
t.Fatalf("bad roundtrip: got %#v, want %#v", y, x)
}
@@ -79,8 +78,7 @@
C64: complex(math.MaxFloat32, math.MaxFloat32),
C128: complex(math.MaxFloat64, math.MaxFloat64),
}
- var max2 Ints
- codec.Decode(codec.Encode(max1), &max2)
+ max2 := codec.Decode(codec.Encode(max1))
if !reflect.DeepEqual(max1, max2) {
t.Fatalf("max: bad roundtrip: got %#v, want %#v", max2, max1)
}
@@ -97,8 +95,7 @@
C64: complex(-math.MaxFloat32, -math.MaxFloat32),
C128: complex(-math.MaxFloat64, -math.MaxFloat64),
}
- var min2 Ints
- codec.Decode(codec.Encode(min1), &min2)
+ min2 := codec.Decode(codec.Encode(min1))
if !reflect.DeepEqual(min1, min2) {
t.Fatalf("min: bad roundtrip: got %#v, want %#v", min2, min1)
}
@@ -111,8 +108,7 @@
I32: -1,
I64: -1,
}
- var neg2 Ints
- codec.Decode(codec.Encode(neg1), &neg2)
+ neg2 := codec.Decode(codec.Encode(neg1))
if !reflect.DeepEqual(neg1, neg2) {
t.Fatalf("neg: bad roundtrip: got %#v, want %#v", neg2, neg1)
}

Change information

Files:
  • M gopls/internal/cache/analysis.go
  • M gopls/internal/cache/errors.go
  • M gopls/internal/cache/methodsets/methodsets.go
  • M gopls/internal/cache/symbols/symbols.go
  • M gopls/internal/cache/testfuncs/tests.go
  • M gopls/internal/cache/typerefs/refs.go
  • M gopls/internal/cache/xrefs/xrefs.go
  • M gopls/internal/filewatcher/poll_watcher.go
  • M gopls/internal/settings/settings.go
  • M gopls/internal/util/frob/frob.go
  • M gopls/internal/util/frob/frob_test.go
Change size: M
Delta: 11 files changed, 21 insertions(+), 35 deletions(-)
Open in Gerrit

Related details

Attention set is empty
Submit Requirements:
  • requirement is not satisfiedCode-Review
  • requirement satisfiedNo-Unresolved-Comments
  • requirement is not satisfiedReview-Enforcement
  • requirement is not satisfiedTryBots-Pass
Inspect html for hidden footers to help with email filtering. To unsubscribe visit settings. DiffyGerrit
Gerrit-MessageType: newchange
Gerrit-Project: tools
Gerrit-Branch: master
Gerrit-Change-Id: I9912fb96b468a7b993174351110a89e021c74038
Gerrit-Change-Number: 764240
Gerrit-PatchSet: 1
Gerrit-Owner: Robert Findley <rfin...@golang.org>
unsatisfied_requirement
satisfied_requirement
open
diffy

Robert Findley (Gerrit)

unread,
Apr 8, 2026, 2:33:36 PM (12 hours ago) Apr 8
to goph...@pubsubhelper.golang.org, Alan Donovan, golang-co...@googlegroups.com
Attention needed from Alan Donovan

Robert Findley voted Commit-Queue+1

Commit-Queue+1
Open in Gerrit

Related details

Attention is currently required from:
  • Alan Donovan
Submit Requirements:
  • requirement is not satisfiedCode-Review
  • requirement satisfiedNo-Unresolved-Comments
  • requirement is not satisfiedReview-Enforcement
  • requirement is not satisfiedTryBots-Pass
Inspect html for hidden footers to help with email filtering. To unsubscribe visit settings. DiffyGerrit
Gerrit-MessageType: comment
Gerrit-Project: tools
Gerrit-Branch: master
Gerrit-Change-Id: I9912fb96b468a7b993174351110a89e021c74038
Gerrit-Change-Number: 764240
Gerrit-PatchSet: 1
Gerrit-Owner: Robert Findley <rfin...@golang.org>
Gerrit-Reviewer: Alan Donovan <adon...@google.com>
Gerrit-Reviewer: Robert Findley <rfin...@golang.org>
Gerrit-Attention: Alan Donovan <adon...@google.com>
Gerrit-Comment-Date: Wed, 08 Apr 2026 18:33:32 +0000
Gerrit-HasComments: No
Gerrit-Has-Labels: Yes
unsatisfied_requirement
satisfied_requirement
open
diffy

Alan Donovan (Gerrit)

unread,
Apr 8, 2026, 2:42:24 PM (11 hours ago) Apr 8
to Robert Findley, goph...@pubsubhelper.golang.org, Go LUCI, golang-co...@googlegroups.com
Attention needed from Robert Findley

Alan Donovan voted and added 2 comments

Votes added by Alan Donovan

Code-Review+2

2 comments

Patchset-level comments
File-level comment, Patchset 1 (Latest):
Alan Donovan . resolved

Very nice!

File gopls/internal/cache/symbols/symbols.go
Line 40, Patchset 1 (Latest):var codec = frob.CodecFor[Package]()
Alan Donovan . unresolved

Change to *Package, then remove the gyrations in Encode and Decode?

Open in Gerrit

Related details

Attention is currently required from:
  • Robert Findley
Submit Requirements:
  • requirement satisfiedCode-Review
  • requirement is not satisfiedNo-Unresolved-Comments
  • requirement is not satisfiedReview-Enforcement
  • requirement is not satisfiedTryBots-Pass
Inspect html for hidden footers to help with email filtering. To unsubscribe visit settings. DiffyGerrit
Gerrit-MessageType: comment
Gerrit-Project: tools
Gerrit-Branch: master
Gerrit-Change-Id: I9912fb96b468a7b993174351110a89e021c74038
Gerrit-Change-Number: 764240
Gerrit-PatchSet: 1
Gerrit-Owner: Robert Findley <rfin...@golang.org>
Gerrit-Reviewer: Alan Donovan <adon...@google.com>
Gerrit-Reviewer: Robert Findley <rfin...@golang.org>
Gerrit-Attention: Robert Findley <rfin...@golang.org>
Gerrit-Comment-Date: Wed, 08 Apr 2026 18:42:22 +0000
Gerrit-HasComments: Yes
Gerrit-Has-Labels: Yes
satisfied_requirement
unsatisfied_requirement
open
diffy

Robert Findley (Gerrit)

unread,
Apr 8, 2026, 5:47:34 PM (8 hours ago) Apr 8
to goph...@pubsubhelper.golang.org, golang-co...@googlegroups.com
Attention needed from Robert Findley

Robert Findley uploaded new patchset

Robert Findley uploaded patch set #2 to this change.
Following approvals got outdated and were removed:
  • TryBots-Pass: LUCI-TryBot-Result+1 by Go LUCI
Open in Gerrit

Related details

Attention is currently required from:
  • Robert Findley
Submit Requirements:
  • requirement satisfiedCode-Review
  • requirement is not satisfiedNo-Unresolved-Comments
  • requirement is not satisfiedReview-Enforcement
  • requirement is not satisfiedTryBots-Pass
Inspect html for hidden footers to help with email filtering. To unsubscribe visit settings. DiffyGerrit
Gerrit-MessageType: newpatchset
Gerrit-Project: tools
Gerrit-Branch: master
Gerrit-Change-Id: I9912fb96b468a7b993174351110a89e021c74038
Gerrit-Change-Number: 764240
Gerrit-PatchSet: 2
Gerrit-Owner: Robert Findley <rfin...@golang.org>
Gerrit-Reviewer: Alan Donovan <adon...@google.com>
Gerrit-Reviewer: Robert Findley <rfin...@golang.org>
Gerrit-Attention: Robert Findley <rfin...@golang.org>
satisfied_requirement
unsatisfied_requirement
open
diffy

Robert Findley (Gerrit)

unread,
Apr 8, 2026, 7:19:38 PM (7 hours ago) Apr 8
to goph...@pubsubhelper.golang.org, golang-co...@googlegroups.com
Attention needed from Robert Findley

Robert Findley uploaded new patchset

Robert Findley uploaded patch set #3 to this change.
Open in Gerrit

Related details

Attention is currently required from:
  • Robert Findley
Submit Requirements:
  • requirement satisfiedCode-Review
  • requirement is not satisfiedNo-Unresolved-Comments
  • requirement is not satisfiedReview-Enforcement
  • requirement is not satisfiedTryBots-Pass
Inspect html for hidden footers to help with email filtering. To unsubscribe visit settings. DiffyGerrit
Gerrit-MessageType: newpatchset
Gerrit-Project: tools
Gerrit-Branch: master
Gerrit-Change-Id: I9912fb96b468a7b993174351110a89e021c74038
Gerrit-Change-Number: 764240
Gerrit-PatchSet: 3
satisfied_requirement
unsatisfied_requirement
open
diffy
Reply all
Reply to author
Forward
0 new messages