]> Cypherpunks repositories - gostls13.git/commitdiff
cmd/go/internal/modload: make State.modfetchState a pointer
authorIan Alexander <jitsu@google.com>
Sun, 16 Nov 2025 04:08:02 +0000 (23:08 -0500)
committerIan Alexander <jitsu@google.com>
Tue, 25 Nov 2025 04:43:56 +0000 (20:43 -0800)
This change aligns modfetch.State with modload.State by using pointer
parameters and receivers.

[git-generate]
cd src/cmd/go/internal/modload
sed -i '
  s/oldState/old/
  s/old := State{/old = \&State{/
  s/func (s \*State) setState(new State) State {/func (s *State) setState(new *State) (old *State) {/
  s/setState(State{})/setState(NewState())/
' init.go
cd ../modfetch
sed -i '
  s/oldState = State{/oldState = \&State{/
  s/func SetState(newState State) (oldState State) {/func SetState(newState *State) (oldState *State) {/
  s/SetState(State{})/SetState(NewState())/
' fetch.go
cd ../modload
sed -i '
  s/old.modfetchState = modfetch.SetState(new.modfetchState)/_ = modfetch.SetState(\&new.modfetchState)/
' init.go
rf '
  #
  # Prepare to swap the existing modfetchState field for a pointer type
  #
  mv State.modfetchState State.modfetchState_
  add State:/modfetchState_/+0 modfetchState *modfetch.State
  #
  # Update State.setState to set & restore additional values
  #
  add State.setState:/s\.requirements,/+0 workFilePath: s.workFilePath,
  add State.setState:/s\.workFilePath,/+0 modfetchState: s.modfetchState,
  #
  # Swap the existing modfetchState field for a pointer type
  #
  add init.go:/.* = modfetch.SetState\(.*\)/-0 s.modfetchState = new.modfetchState
  add init.go:/.* = modfetch.SetState\(.*\)/-0 old.modfetchState = modfetch.SetState(s.modfetchState) // TODO(jitsu): remove after completing global state elimination
  rm init.go:/_ = modfetch.SetState\(.*\)/
  rm State.modfetchState_
'
sed -i '
  s/return &State{}/s := new(State)\ns.modfetchState = modfetch.NewState()\nreturn s/
' init.go
go fmt

Change-Id: I0602ecf976fd3ee93844e77989291d729ad71595
Reviewed-on: https://go-review.googlesource.com/c/go/+/720900
Reviewed-by: Michael Matloob <matloob@golang.org>
LUCI-TryBot-Result: Go LUCI <golang-scoped@luci-project-accounts.iam.gserviceaccount.com>
Reviewed-by: Mark Freeman <markfreeman@google.com>
src/cmd/go/internal/modfetch/fetch.go
src/cmd/go/internal/modload/init.go

index 87ea829180fb6eb74dab798b2597b28e740b4179..8a17a374d0f75471fb364295d854acb2866f05cc 100644 (file)
@@ -503,14 +503,14 @@ func (s *State) AddWorkspaceGoSumFile(file string) {
 // Reset resets globals in the modfetch package, so previous loads don't affect
 // contents of go.sum files.
 func Reset() {
-       SetState(State{})
+       SetState(NewState())
 }
 
 // SetState sets the global state of the modfetch package to the newState, and returns the previous
 // global state. newState should have been returned by SetState, or be an empty State.
 // There should be no concurrent calls to any of the exported functions of this package with
 // a call to SetState because it will modify the global state in a non-thread-safe way.
-func SetState(newState State) (oldState State) {
+func SetState(newState *State) (oldState *State) {
        if newState.lookupCache == nil {
                newState.lookupCache = new(par.Cache[lookupCacheKey, Repo])
        }
@@ -521,7 +521,7 @@ func SetState(newState State) (oldState State) {
        goSum.mu.Lock()
        defer goSum.mu.Unlock()
 
-       oldState = State{
+       oldState = &State{
                goSumFile:           ModuleFetchState.goSumFile,
                workspaceGoSumFiles: ModuleFetchState.workspaceGoSumFiles,
                lookupCache:         ModuleFetchState.lookupCache,
index ad7b08e062af2c8cc0c7617a105a3b086fd7b8ff..4680ea427e9b7d2420656172e405ee956337e1c7 100644 (file)
@@ -80,7 +80,7 @@ func EnterWorkspace(loaderstate *State, ctx context.Context) (exit func(), err e
        }
 
        // Reset the state to a clean state.
-       oldstate := loaderstate.setState(State{})
+       oldstate := loaderstate.setState(NewState())
        loaderstate.ForceUseModules = true
 
        // Load in workspace mode.
@@ -385,11 +385,11 @@ func WorkFilePath(loaderstate *State) string {
 // Reset clears all the initialized, cached state about the use of modules,
 // so that we can start over.
 func (s *State) Reset() {
-       s.setState(State{})
+       s.setState(NewState())
 }
 
-func (s *State) setState(new State) State {
-       oldState := State{
+func (s *State) setState(new *State) (old *State) {
+       old = &State{
                initialized:     s.initialized,
                ForceUseModules: s.ForceUseModules,
                RootMode:        s.RootMode,
@@ -397,6 +397,8 @@ func (s *State) setState(new State) State {
                modulesEnabled:  cfg.ModulesEnabled,
                MainModules:     s.MainModules,
                requirements:    s.requirements,
+               workFilePath:    s.workFilePath,
+               modfetchState:   s.modfetchState,
        }
        s.initialized = new.initialized
        s.ForceUseModules = new.ForceUseModules
@@ -409,8 +411,10 @@ func (s *State) setState(new State) State {
        // The modfetch package's global state is used to compute
        // the go.sum file, so save and restore it along with the
        // modload state.
-       oldState.modfetchState = modfetch.SetState(new.modfetchState)
-       return oldState
+       s.modfetchState = new.modfetchState
+       old.modfetchState = modfetch.SetState(s.modfetchState) // TODO(jitsu): remove after completing global state elimination
+
+       return old
 }
 
 type State struct {
@@ -448,10 +452,14 @@ type State struct {
        // Set to the path to the go.work file, or "" if workspace mode is
        // disabled
        workFilePath  string
-       modfetchState modfetch.State
+       modfetchState *modfetch.State
 }
 
-func NewState() *State { return &State{} }
+func NewState() *State {
+       s := new(State)
+       s.modfetchState = modfetch.NewState()
+       return s
+}
 
 // Init determines whether module mode is enabled, locates the root of the
 // current module (if any), sets environment variables for Git subprocesses, and