]> Cypherpunks repositories - gostls13.git/commitdiff
go/types: implement NewTypeList and use it instead of composite literals
authorRobert Griesemer <gri@golang.org>
Tue, 24 Aug 2021 16:28:43 +0000 (09:28 -0700)
committerRobert Griesemer <gri@golang.org>
Wed, 25 Aug 2021 23:43:40 +0000 (23:43 +0000)
Also, simplify a bit of code in predicates.go.

This is a backport of changes in CL 344615 that were made in addition
to the changes of the original CL 343933; it brings go/types in sync
with types2.

Change-Id: I14cd4d4704d29894d0fbb8d129744d65e332ad22
Reviewed-on: https://go-review.googlesource.com/c/go/+/344570
Trust: Robert Griesemer <gri@golang.org>
Trust: Dan Scales <danscales@google.com>
Run-TryBot: Robert Griesemer <gri@golang.org>
TryBot-Result: Go Bot <gobot@golang.org>
Reviewed-by: Dan Scales <danscales@google.com>
src/go/types/check.go
src/go/types/instantiate.go
src/go/types/predicates.go
src/go/types/subst.go
src/go/types/typelists.go

index 909bf8d52d07182d43d823f5bddd57ef70497a5e..ab3a388e9f3f5805226926aa1ec81bd8294f492b 100644 (file)
@@ -407,7 +407,7 @@ func (check *Checker) recordInferred(call ast.Expr, targs []Type, sig *Signature
        assert(call != nil)
        assert(sig != nil)
        if m := check.Inferred; m != nil {
-               m[call] = Inferred{&TypeList{targs}, sig}
+               m[call] = Inferred{NewTypeList(targs), sig}
        }
 }
 
index 3ee09b7e8425ea2508162bea7038ac7ffacc5f25..5f691d5246b3f515e61ea958c5f1f0ff4a9e2d7d 100644 (file)
@@ -131,7 +131,7 @@ func (check *Checker) instance(pos token.Pos, typ Type, targs []Type) (res Type)
 
                tname := NewTypeName(pos, t.obj.pkg, t.obj.name, nil)
                named := check.newNamed(tname, t, nil, nil, nil) // methods and tparams are set when named is loaded
-               named.targs = &TypeList{targs}
+               named.targs = NewTypeList(targs)
                named.instance = &instance{pos}
                if check != nil {
                        check.typMap[h] = named
index 2f4ef9dace0d743583095e162c7abc10532b3e5b..d4055bb0cc934deb84a5d6ef42dcf756efd7e093 100644 (file)
@@ -312,16 +312,14 @@ func identical(x, y Type, cmpTags bool, p *ifacePair) bool {
                                return false
                        }
 
-                       if nargs := len(xargs); nargs > 0 {
+                       if len(xargs) > 0 {
                                // Instances are identical if their original type and type arguments
                                // are identical.
                                if !Identical(x.orig, y.orig) {
                                        return false
                                }
-                               for i := 0; i < nargs; i++ {
-                                       xa := xargs[i]
-                                       ya := yargs[i]
-                                       if !Identical(xa, ya) {
+                               for i, xa := range xargs {
+                                       if !Identical(xa, yargs[i]) {
                                                return false
                                        }
                                }
index 8b8d6fb82a783dae578b78f701c1856b80442a47..1c53cdaf2ca7bb369633df228fa283047db09250 100644 (file)
@@ -233,7 +233,7 @@ func (subst *subster) typ(typ Type) Type {
                // It's ok to provide a nil *Checker because the newly created type
                // doesn't need to be (lazily) expanded; it's expanded below.
                named := (*Checker)(nil).newNamed(tname, t.orig, nil, t.tparams, t.methods) // t is loaded, so tparams and methods are available
-               named.targs = &TypeList{newTArgs}
+               named.targs = NewTypeList(newTArgs)
                subst.typMap[h] = named
                t.expand(subst.typMap) // must happen after typMap update to avoid infinite recursion
 
index a8181404bf732256293a4c2458d941d17231be1a..ef8ea1f32b69063b5070f2601d8e65eace957936 100644 (file)
@@ -27,6 +27,14 @@ func (l *TParamList) list() []*TypeParam {
 // TypeList holds a list of types.
 type TypeList struct{ types []Type }
 
+// NewTypeList returns a new TypeList with the types in list.
+func NewTypeList(list []Type) *TypeList {
+       if len(list) == 0 {
+               return nil
+       }
+       return &TypeList{list}
+}
+
 // Len returns the number of types in the list.
 // It is safe to call on a nil receiver.
 func (l *TypeList) Len() int { return len(l.list()) }