]> Cypherpunks repositories - gostls13.git/commitdiff
go/types, types2: factor out check for updated type arguments (cleanup)
authorRobert Griesemer <gri@golang.org>
Thu, 23 May 2024 02:36:05 +0000 (19:36 -0700)
committerGopher Robot <gobot@golang.org>
Thu, 23 May 2024 03:04:07 +0000 (03:04 +0000)
Change-Id: I3e2668e4a24c145f121199a5f7f4278ff5d5f1da
Reviewed-on: https://go-review.googlesource.com/c/go/+/587676
Reviewed-by: Robert Griesemer <gri@google.com>
Reviewed-by: Robert Findley <rfindley@google.com>
Auto-Submit: Robert Griesemer <gri@google.com>
LUCI-TryBot-Result: Go LUCI <golang-scoped@luci-project-accounts.iam.gserviceaccount.com>

src/cmd/compile/internal/types2/subst.go
src/go/types/subst.go

index 2690ef689cbac6f1ff4f21b2ed6cf947d6473f61..650ae846a61e85dd62a40be52991a9f14e7d6948 100644 (file)
@@ -105,32 +105,19 @@ func (subst *subster) typ(typ Type) Type {
                }
 
                // TODO(gri) do we need this for Alias types?
-               var newTArgs []Type
                if t.TypeArgs().Len() != n {
                        return Typ[Invalid] // error reported elsewhere
                }
 
                // already instantiated
-               // For each (existing) type argument targ, determine if it needs
+               // For each (existing) type argument determine if it needs
                // to be substituted; i.e., if it is or contains a type parameter
                // that has a type argument for it.
-               for i, targ := range t.TypeArgs().list() {
-                       new_targ := subst.typ(targ)
-                       if new_targ != targ {
-                               if newTArgs == nil {
-                                       newTArgs = make([]Type, n)
-                                       copy(newTArgs, t.TypeArgs().list())
-                               }
-                               newTArgs[i] = new_targ
-                       }
-               }
-
-               if newTArgs == nil {
-                       return t // nothing to substitute
+               targs, updated := subst.typeList(t.TypeArgs().list())
+               if updated {
+                       return subst.check.newAliasInstance(subst.pos, t.orig, targs, subst.ctxt)
                }
 
-               return subst.check.newAliasInstance(subst.pos, t.orig, newTArgs, subst.ctxt)
-
        case *Array:
                elem := subst.typOrNil(t.elem)
                if elem != t.elem {
@@ -245,18 +232,6 @@ func (subst *subster) typ(typ Type) Type {
                }
 
        case *Named:
-               // dump is for debugging
-               dump := func(string, ...interface{}) {}
-               if subst.check != nil && subst.check.conf.Trace {
-                       subst.check.indent++
-                       defer func() {
-                               subst.check.indent--
-                       }()
-                       dump = func(format string, args ...interface{}) {
-                               subst.check.trace(subst.pos, format, args...)
-                       }
-               }
-
                // subst is called during expansion, so in this function we need to be
                // careful not to call any methods that would cause t to be expanded: doing
                // so would result in deadlock.
@@ -265,44 +240,26 @@ func (subst *subster) typ(typ Type) Type {
                orig := t.Origin()
                n := orig.TypeParams().Len()
                if n == 0 {
-                       dump(">>> %s is not parameterized", t)
                        return t // type is not parameterized
                }
 
-               var newTArgs []Type
                if t.TypeArgs().Len() != n {
                        return Typ[Invalid] // error reported elsewhere
                }
 
                // already instantiated
-               dump(">>> %s already instantiated", t)
-               // For each (existing) type argument targ, determine if it needs
+               // For each (existing) type argument determine if it needs
                // to be substituted; i.e., if it is or contains a type parameter
                // that has a type argument for it.
-               for i, targ := range t.TypeArgs().list() {
-                       dump(">>> %d targ = %s", i, targ)
-                       new_targ := subst.typ(targ)
-                       if new_targ != targ {
-                               dump(">>> substituted %d targ %s => %s", i, targ, new_targ)
-                               if newTArgs == nil {
-                                       newTArgs = make([]Type, n)
-                                       copy(newTArgs, t.TypeArgs().list())
-                               }
-                               newTArgs[i] = new_targ
-                       }
+               targs, updated := subst.typeList(t.TypeArgs().list())
+               if updated {
+                       // Create a new instance and populate the context to avoid endless
+                       // recursion. The position used here is irrelevant because validation only
+                       // occurs on t (we don't call validType on named), but we use subst.pos to
+                       // help with debugging.
+                       return subst.check.instance(subst.pos, orig, targs, subst.expanding, subst.ctxt)
                }
 
-               if newTArgs == nil {
-                       dump(">>> nothing to substitute in %s", t)
-                       return t // nothing to substitute
-               }
-
-               // Create a new instance and populate the context to avoid endless
-               // recursion. The position used here is irrelevant because validation only
-               // occurs on t (we don't call validType on named), but we use subst.pos to
-               // help with debugging.
-               return subst.check.instance(subst.pos, orig, newTArgs, subst.expanding, subst.ctxt)
-
        case *TypeParam:
                return subst.smap.lookup(t)
 
index 42e0c5ea2a33a591bfaa421d7b15a5da60b25da7..5ad2ff61eb1d30ea9f0cc62c4dea37abf321826e 100644 (file)
@@ -108,32 +108,19 @@ func (subst *subster) typ(typ Type) Type {
                }
 
                // TODO(gri) do we need this for Alias types?
-               var newTArgs []Type
                if t.TypeArgs().Len() != n {
                        return Typ[Invalid] // error reported elsewhere
                }
 
                // already instantiated
-               // For each (existing) type argument targ, determine if it needs
+               // For each (existing) type argument determine if it needs
                // to be substituted; i.e., if it is or contains a type parameter
                // that has a type argument for it.
-               for i, targ := range t.TypeArgs().list() {
-                       new_targ := subst.typ(targ)
-                       if new_targ != targ {
-                               if newTArgs == nil {
-                                       newTArgs = make([]Type, n)
-                                       copy(newTArgs, t.TypeArgs().list())
-                               }
-                               newTArgs[i] = new_targ
-                       }
-               }
-
-               if newTArgs == nil {
-                       return t // nothing to substitute
+               targs, updated := subst.typeList(t.TypeArgs().list())
+               if updated {
+                       return subst.check.newAliasInstance(subst.pos, t.orig, targs, subst.ctxt)
                }
 
-               return subst.check.newAliasInstance(subst.pos, t.orig, newTArgs, subst.ctxt)
-
        case *Array:
                elem := subst.typOrNil(t.elem)
                if elem != t.elem {
@@ -248,18 +235,6 @@ func (subst *subster) typ(typ Type) Type {
                }
 
        case *Named:
-               // dump is for debugging
-               dump := func(string, ...interface{}) {}
-               if subst.check != nil && subst.check.conf._Trace {
-                       subst.check.indent++
-                       defer func() {
-                               subst.check.indent--
-                       }()
-                       dump = func(format string, args ...interface{}) {
-                               subst.check.trace(subst.pos, format, args...)
-                       }
-               }
-
                // subst is called during expansion, so in this function we need to be
                // careful not to call any methods that would cause t to be expanded: doing
                // so would result in deadlock.
@@ -268,44 +243,26 @@ func (subst *subster) typ(typ Type) Type {
                orig := t.Origin()
                n := orig.TypeParams().Len()
                if n == 0 {
-                       dump(">>> %s is not parameterized", t)
                        return t // type is not parameterized
                }
 
-               var newTArgs []Type
                if t.TypeArgs().Len() != n {
                        return Typ[Invalid] // error reported elsewhere
                }
 
                // already instantiated
-               dump(">>> %s already instantiated", t)
-               // For each (existing) type argument targ, determine if it needs
+               // For each (existing) type argument determine if it needs
                // to be substituted; i.e., if it is or contains a type parameter
                // that has a type argument for it.
-               for i, targ := range t.TypeArgs().list() {
-                       dump(">>> %d targ = %s", i, targ)
-                       new_targ := subst.typ(targ)
-                       if new_targ != targ {
-                               dump(">>> substituted %d targ %s => %s", i, targ, new_targ)
-                               if newTArgs == nil {
-                                       newTArgs = make([]Type, n)
-                                       copy(newTArgs, t.TypeArgs().list())
-                               }
-                               newTArgs[i] = new_targ
-                       }
+               targs, updated := subst.typeList(t.TypeArgs().list())
+               if updated {
+                       // Create a new instance and populate the context to avoid endless
+                       // recursion. The position used here is irrelevant because validation only
+                       // occurs on t (we don't call validType on named), but we use subst.pos to
+                       // help with debugging.
+                       return subst.check.instance(subst.pos, orig, targs, subst.expanding, subst.ctxt)
                }
 
-               if newTArgs == nil {
-                       dump(">>> nothing to substitute in %s", t)
-                       return t // nothing to substitute
-               }
-
-               // Create a new instance and populate the context to avoid endless
-               // recursion. The position used here is irrelevant because validation only
-               // occurs on t (we don't call validType on named), but we use subst.pos to
-               // help with debugging.
-               return subst.check.instance(subst.pos, orig, newTArgs, subst.expanding, subst.ctxt)
-
        case *TypeParam:
                return subst.smap.lookup(t)