]> Cypherpunks.ru repositories - gostls13.git/commitdiff
go/types, types2: use asNamed(t) instead of t.(*Named) type assertions
authorRobert Griesemer <gri@golang.org>
Thu, 24 Aug 2023 16:07:03 +0000 (09:07 -0700)
committerGopher Robot <gobot@golang.org>
Wed, 30 Aug 2023 15:45:21 +0000 (15:45 +0000)
Preparation for the introduction of alias types.
Because asNamed is not exported, existing external
tests continue to use t.(*Named).

Change-Id: I4754b406dd6b23030d3703a486d6f6620b2464fe
Reviewed-on: https://go-review.googlesource.com/c/go/+/522876
Reviewed-by: Robert Griesemer <gri@google.com>
Auto-Submit: Robert Griesemer <gri@google.com>
Run-TryBot: Robert Griesemer <gri@google.com>
Reviewed-by: Robert Findley <rfindley@google.com>
TryBot-Result: Gopher Robot <gobot@golang.org>

31 files changed:
src/cmd/compile/internal/types2/alias.go [new file with mode: 0644]
src/cmd/compile/internal/types2/builtins.go
src/cmd/compile/internal/types2/decl.go
src/cmd/compile/internal/types2/lookup.go
src/cmd/compile/internal/types2/named.go
src/cmd/compile/internal/types2/predicates.go
src/cmd/compile/internal/types2/signature.go
src/cmd/compile/internal/types2/sizes.go
src/cmd/compile/internal/types2/typeparam.go
src/cmd/compile/internal/types2/typestring.go
src/cmd/compile/internal/types2/typexpr.go
src/cmd/compile/internal/types2/under.go
src/cmd/compile/internal/types2/unify.go
src/cmd/compile/internal/types2/universe.go
src/go/types/alias.go [new file with mode: 0644]
src/go/types/builtins.go
src/go/types/decl.go
src/go/types/generate_test.go
src/go/types/interface.go
src/go/types/lookup.go
src/go/types/methodset.go
src/go/types/named.go
src/go/types/predicates.go
src/go/types/signature.go
src/go/types/sizes.go
src/go/types/typeparam.go
src/go/types/typestring.go
src/go/types/typexpr.go
src/go/types/under.go
src/go/types/unify.go
src/go/types/universe.go

diff --git a/src/cmd/compile/internal/types2/alias.go b/src/cmd/compile/internal/types2/alias.go
new file mode 100644 (file)
index 0000000..375046b
--- /dev/null
@@ -0,0 +1,17 @@
+// Copyright 2023 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 types2
+
+// This file will eventually define an Alias type.
+// For now it declares asNamed. Once Alias types
+// exist, asNamed will need to indirect through
+// them as needed.
+
+// asNamed returns t as *Named if that is t's
+// actual type. It returns nil otherwise.
+func asNamed(t Type) *Named {
+       n, _ := t.(*Named)
+       return n
+}
index 53be480f54d5b7e6082551a7e5103930a86c4094..41e60f118de3c5e33441b889890f785159268b5f 100644 (file)
@@ -923,7 +923,7 @@ func hasVarSize(t Type, seen map[*Named]bool) (varSized bool) {
        // Cycles are only possible through *Named types.
        // The seen map is used to detect cycles and track
        // the results of previously seen types.
-       if named, _ := t.(*Named); named != nil {
+       if named := asNamed(t); named != nil {
                if v, ok := seen[named]; ok {
                        return v
                }
index 2914b496f4843f1d5ebea9cfd3da4e669456694e..8c6fb45ac0c01331f006be593bfb70f6df5392cd 100644 (file)
@@ -475,7 +475,7 @@ func (check *Checker) varDecl(obj *Var, lhs []*Var, typ, init syntax.Expr) {
 
 // isImportedConstraint reports whether typ is an imported type constraint.
 func (check *Checker) isImportedConstraint(typ Type) bool {
-       named, _ := typ.(*Named)
+       named := asNamed(typ)
        if named == nil || named.obj.pkg == check.pkg || named.obj.pkg == nil {
                return false
        }
@@ -488,7 +488,7 @@ func (check *Checker) typeDecl(obj *TypeName, tdecl *syntax.TypeDecl, def *Named
 
        var rhs Type
        check.later(func() {
-               if t, _ := obj.typ.(*Named); t != nil { // type may be invalid
+               if t := asNamed(obj.typ); t != nil { // type may be invalid
                        check.validType(t)
                }
                // If typ is local, an error was already reported where typ is specified/defined.
@@ -638,7 +638,7 @@ func (check *Checker) collectMethods(obj *TypeName) {
 
        // spec: "If the base type is a struct type, the non-blank method
        // and field names must be distinct."
-       base, _ := obj.typ.(*Named) // shouldn't fail but be conservative
+       base := asNamed(obj.typ) // shouldn't fail but be conservative
        if base != nil {
                assert(base.TypeArgs().Len() == 0) // collectMethods should not be called on an instantiated type
 
index b7370ca38d01231044208661c1c90767c89f1087..620ad1a70c80b2a9e07368af8febde3d9a2960ce 100644 (file)
@@ -54,7 +54,7 @@ func LookupFieldOrMethod(T Type, addressable bool, pkg *Package, name string) (o
        // Thus, if we have a named pointer type, proceed with the underlying
        // pointer type but discard the result if it is a method since we would
        // not have found it for T (see also go.dev/issue/8590).
-       if t, _ := T.(*Named); t != nil {
+       if t := asNamed(T); t != nil {
                if p, _ := t.Underlying().(*Pointer); p != nil {
                        obj, index, indirect = lookupFieldOrMethodImpl(p, false, pkg, name, false)
                        if _, ok := obj.(*Func); ok {
@@ -138,7 +138,7 @@ func lookupFieldOrMethodImpl(T Type, addressable bool, pkg *Package, name string
 
                        // If we have a named type, we may have associated methods.
                        // Look for those first.
-                       if named, _ := typ.(*Named); named != nil {
+                       if named := asNamed(typ); named != nil {
                                if alt := seen.lookup(named); alt != nil {
                                        // We have seen this type before, at a more shallow depth
                                        // (note that multiples of this type at the current depth
index 7c9a46f2315afb342b2945d3f996f6fa537c2829..82c2cb3a5bf43fa0648323b1d4c9013fab31d42e 100644 (file)
@@ -141,7 +141,7 @@ const (
 // If the given type name obj doesn't have a type yet, its type is set to the returned named type.
 // The underlying type must not be a *Named.
 func NewNamed(obj *TypeName, underlying Type, methods []*Func) *Named {
-       if _, ok := underlying.(*Named); ok {
+       if asNamed(underlying) != nil {
                panic("underlying type must not be *Named")
        }
        return (*Checker)(nil).newNamed(obj, underlying, methods)
@@ -434,7 +434,7 @@ func (t *Named) SetUnderlying(underlying Type) {
        if underlying == nil {
                panic("underlying type must not be nil")
        }
-       if _, ok := underlying.(*Named); ok {
+       if asNamed(underlying) != nil {
                panic("underlying type must not be *Named")
        }
        t.resolve().underlying = underlying
@@ -598,7 +598,7 @@ func (n *Named) expandUnderlying() Type {
        orig := n.inst.orig
        targs := n.inst.targs
 
-       if _, unexpanded := orig.underlying.(*Named); unexpanded {
+       if asNamed(orig.underlying) != nil {
                // We should only get a Named underlying type here during type checking
                // (for example, in recursive type declarations).
                assert(check != nil)
@@ -656,7 +656,7 @@ func (n *Named) expandUnderlying() Type {
 //
 // TODO(rfindley): eliminate this function or give it a better name.
 func safeUnderlying(typ Type) Type {
-       if t, _ := typ.(*Named); t != nil {
+       if t := asNamed(typ); t != nil {
                return t.underlying
        }
        return typ.Underlying()
index 075bd97d0f77f1209172ace23dad808a1cf81c23..872b874ecb4991487d892598ef06550f7c23532d 100644 (file)
@@ -124,7 +124,7 @@ func hasEmptyTypeset(t Type) bool {
 // TODO(gri) should we include signatures or assert that they are not present?
 func isGeneric(t Type) bool {
        // A parameterized type is only generic if it doesn't have an instantiation already.
-       named, _ := t.(*Named)
+       named := asNamed(t)
        return named != nil && named.obj != nil && named.inst == nil && named.TypeParams().Len() > 0
 }
 
@@ -435,7 +435,7 @@ func (c *comparer) identical(x, y Type, p *ifacePair) bool {
                // Two named types are identical if their type names originate
                // in the same type declaration; if they are instantiated they
                // must have identical type argument lists.
-               if y, ok := y.(*Named); ok {
+               if y := asNamed(y); y != nil {
                        // check type arguments before origins to match unifier
                        // (for correct source code we need to do all checks so
                        // order doesn't matter)
index 8e0dfe2881aa158fe2e8348df8455f8ca10b5904..7eeb7340f81ccfaa7f81abd59281ec4b434890c1 100644 (file)
@@ -136,7 +136,7 @@ func (check *Checker) funcType(sig *Signature, recvPar *syntax.Field, tparams []
                                // Also: Don't report an error via genericType since it will be reported
                                //       again when we type-check the signature.
                                // TODO(gri) maybe the receiver should be marked as invalid instead?
-                               if recv, _ := check.genericType(rname, nil).(*Named); recv != nil {
+                               if recv := asNamed(check.genericType(rname, nil)); recv != nil {
                                        recvTParams = recv.TypeParams().list()
                                }
                        }
index cc0288da4d1b538566d4e598f3a424801f5ac356..64da072fbf53bc38a8d16e1c0e567483c52f3258 100644 (file)
@@ -112,8 +112,8 @@ func (s *StdSizes) Alignof(T Type) (result int64) {
 }
 
 func IsSyncAtomicAlign64(T Type) bool {
-       named, ok := T.(*Named)
-       if !ok {
+       named := asNamed(T)
+       if named == nil {
                return false
        }
        obj := named.Obj()
index aebbec27a8b7e3b54f56ab93f8c9c20f7af81b8d..46c2101c47513e5d23407dabff1283a9af03ba24 100644 (file)
@@ -132,7 +132,7 @@ func (t *TypeParam) iface() *Interface {
                // pos is used for tracing output; start with the type parameter position.
                pos := t.obj.pos
                // use the (original or possibly instantiated) type bound position if we have one
-               if n, _ := bound.(*Named); n != nil {
+               if n := asNamed(bound); n != nil {
                        pos = n.obj.pos
                }
                computeInterfaceTypeSet(t.check, pos, ityp)
index 2f4fb5220dca3f86fcc5b58df8cf63e2027db26a..dfa929476c4b073f22d8617afdf132b570d4ef9c 100644 (file)
@@ -218,7 +218,7 @@ func (w *typeWriter) typ(typ Type) {
                                w.string("any")
                                break
                        }
-                       if t == universeComparable.Type().(*Named).underlying {
+                       if t == asNamed(universeComparable.Type()).underlying {
                                w.string("interface{comparable}")
                                break
                        }
index 5a59db023a0e3ef1e25723b6004ecf5f8e8ff39c..bf353427ab5ce1101690fe443bcdc7db4feea855 100644 (file)
@@ -420,7 +420,7 @@ func (check *Checker) instantiatedType(x syntax.Expr, xlist []syntax.Expr, def *
                return gtyp // error already reported
        }
 
-       orig, _ := gtyp.(*Named)
+       orig := asNamed(gtyp)
        if orig == nil {
                panic(fmt.Sprintf("%v: cannot instantiate %v", x.Pos(), gtyp))
        }
@@ -433,7 +433,7 @@ func (check *Checker) instantiatedType(x syntax.Expr, xlist []syntax.Expr, def *
        }
 
        // create the instance
-       inst := check.instance(x.Pos(), orig, targs, nil, check.context()).(*Named)
+       inst := asNamed(check.instance(x.Pos(), orig, targs, nil, check.context()))
        def.setUnderlying(inst)
 
        // orig.tparams may not be set up, so we need to do expansion later.
index 887f7816ba1a474af157e489572f51aad93a8c1c..6b24399de43f0ec49d911653991fa55a6586b009 100644 (file)
@@ -9,7 +9,7 @@ package types2
 // under must only be called when a type is known
 // to be fully set up.
 func under(t Type) Type {
-       if t, _ := t.(*Named); t != nil {
+       if t := asNamed(t); t != nil {
                return t.under()
        }
        return t.Underlying()
index 5d58e2da13e50c34265d27fdd524084a78c1c5d0..e0340a59071af331e44f1fe931e8f22a81349891 100644 (file)
@@ -311,7 +311,7 @@ func (u *unifier) nify(x, y Type, mode unifyMode, p *ifacePair) (result bool) {
        // Ensure that if we have at least one
        // - defined type, make sure one is in y
        // - type parameter recorded with u, make sure one is in x
-       if _, ok := x.(*Named); ok || u.asTypeParam(y) != nil {
+       if asNamed(x) != nil || u.asTypeParam(y) != nil {
                if traceInference {
                        u.tracef("%s ≡ %s\t// swap", y, x)
                }
@@ -335,7 +335,7 @@ func (u *unifier) nify(x, y Type, mode unifyMode, p *ifacePair) (result bool) {
        // we will fail at function instantiation or argument assignment time.
        //
        // If we have at least one defined type, there is one in y.
-       if ny, _ := y.(*Named); mode&exact == 0 && ny != nil && isTypeLit(x) && !(u.enableInterfaceInference && IsInterface(x)) {
+       if ny := asNamed(y); mode&exact == 0 && ny != nil && isTypeLit(x) && !(u.enableInterfaceInference && IsInterface(x)) {
                if traceInference {
                        u.tracef("%s ≡ under %s", x, ny)
                }
@@ -372,8 +372,8 @@ func (u *unifier) nify(x, y Type, mode unifyMode, p *ifacePair) (result bool) {
                                // We have a match, possibly through underlying types.
                                xi := asInterface(x)
                                yi := asInterface(y)
-                               _, xn := x.(*Named)
-                               _, yn := y.(*Named)
+                               xn := asNamed(x) != nil
+                               yn := asNamed(y) != nil
                                // If we have two interfaces, what to do depends on
                                // whether they are named and their method sets.
                                if xi != nil && yi != nil {
@@ -728,7 +728,7 @@ func (u *unifier) nify(x, y Type, mode unifyMode, p *ifacePair) (result bool) {
        case *Named:
                // Two named types unify if their type names originate in the same type declaration.
                // If they are instantiated, their type argument lists must unify.
-               if y, ok := y.(*Named); ok {
+               if y := asNamed(y); y != nil {
                        // Check type arguments before origins so they unify
                        // even if the origins don't match; for better error
                        // messages (see go.dev/issue/53692).
index 79cd8cbf0ace6ee66c51d85639d6cff5aa6af685..c8be81b9ebe708889b8a594ca225695dcf5f0544 100644 (file)
@@ -265,7 +265,7 @@ func def(obj Object) {
                return // nothing to do
        }
        // fix Obj link for named types
-       if typ, _ := obj.Type().(*Named); typ != nil {
+       if typ := asNamed(obj.Type()); typ != nil {
                typ.obj = obj.(*TypeName)
        }
        // exported identifiers go into package unsafe
diff --git a/src/go/types/alias.go b/src/go/types/alias.go
new file mode 100644 (file)
index 0000000..7dc7fe9
--- /dev/null
@@ -0,0 +1,19 @@
+// Code generated by "go test -run=Generate -write=all"; DO NOT EDIT.
+
+// Copyright 2023 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 types
+
+// This file will eventually define an Alias type.
+// For now it declares asNamed. Once Alias types
+// exist, asNamed will need to indirect through
+// them as needed.
+
+// asNamed returns t as *Named if that is t's
+// actual type. It returns nil otherwise.
+func asNamed(t Type) *Named {
+       n, _ := t.(*Named)
+       return n
+}
index 35b8755a91556cc903ea66ab481368bd8671fb48..0f054e35aee20a3e0bc86952e8ec35641673550f 100644 (file)
@@ -922,7 +922,7 @@ func hasVarSize(t Type, seen map[*Named]bool) (varSized bool) {
        // Cycles are only possible through *Named types.
        // The seen map is used to detect cycles and track
        // the results of previously seen types.
-       if named, _ := t.(*Named); named != nil {
+       if named := asNamed(t); named != nil {
                if v, ok := seen[named]; ok {
                        return v
                }
index af8ec8435e85f22c5c1829b44380c5888e759aa7..642d2604f9e93fbf4cbe8b791ab871ec742b6dfd 100644 (file)
@@ -544,7 +544,7 @@ func (check *Checker) varDecl(obj *Var, lhs []*Var, typ, init ast.Expr) {
 
 // isImportedConstraint reports whether typ is an imported type constraint.
 func (check *Checker) isImportedConstraint(typ Type) bool {
-       named, _ := typ.(*Named)
+       named := asNamed(typ)
        if named == nil || named.obj.pkg == check.pkg || named.obj.pkg == nil {
                return false
        }
@@ -557,7 +557,7 @@ func (check *Checker) typeDecl(obj *TypeName, tdecl *ast.TypeSpec, def *Named) {
 
        var rhs Type
        check.later(func() {
-               if t, _ := obj.typ.(*Named); t != nil { // type may be invalid
+               if t := asNamed(obj.typ); t != nil { // type may be invalid
                        check.validType(t)
                }
                // If typ is local, an error was already reported where typ is specified/defined.
@@ -726,7 +726,7 @@ func (check *Checker) collectMethods(obj *TypeName) {
 
        // spec: "If the base type is a struct type, the non-blank method
        // and field names must be distinct."
-       base, _ := obj.typ.(*Named) // shouldn't fail but be conservative
+       base := asNamed(obj.typ) // shouldn't fail but be conservative
        if base != nil {
                assert(base.TypeArgs().Len() == 0) // collectMethods should not be called on an instantiated type
 
index f38206a4968f334e5118732260f7cdb8b73637bd..6af3715f87d3064589fb09397291909ba8201df2 100644 (file)
@@ -95,6 +95,7 @@ func generate(t *testing.T, filename string, write bool) {
 type action func(in *ast.File)
 
 var filemap = map[string]action{
+       "alias.go":        nil,
        "array.go":        nil,
        "basic.go":        nil,
        "chan.go":         nil,
index 5fe9b57c3f0d5767e9e002b796d964b8cbb012f4..74562d8a893db1f7dedf67f08d5ceb219c25094e 100644 (file)
@@ -104,7 +104,7 @@ func (t *Interface) NumEmbeddeds() int { return len(t.embeddeds) }
 // The result is nil if the i'th embedded type is not a defined type.
 //
 // Deprecated: Use EmbeddedType which is not restricted to defined (*Named) types.
-func (t *Interface) Embedded(i int) *Named { tname, _ := t.embeddeds[i].(*Named); return tname }
+func (t *Interface) Embedded(i int) *Named { return asNamed(t.embeddeds[i]) }
 
 // EmbeddedType returns the i'th embedded type of interface t for 0 <= i < t.NumEmbeddeds().
 func (t *Interface) EmbeddedType(i int) Type { return t.embeddeds[i] }
index d96dd86e5eb0d866658ca0e7b7fb521c5507303a..4fcae994f906339c2d230a00c6dc0875a18f042f 100644 (file)
@@ -56,7 +56,7 @@ func LookupFieldOrMethod(T Type, addressable bool, pkg *Package, name string) (o
        // Thus, if we have a named pointer type, proceed with the underlying
        // pointer type but discard the result if it is a method since we would
        // not have found it for T (see also go.dev/issue/8590).
-       if t, _ := T.(*Named); t != nil {
+       if t := asNamed(T); t != nil {
                if p, _ := t.Underlying().(*Pointer); p != nil {
                        obj, index, indirect = lookupFieldOrMethodImpl(p, false, pkg, name, false)
                        if _, ok := obj.(*Func); ok {
@@ -140,7 +140,7 @@ func lookupFieldOrMethodImpl(T Type, addressable bool, pkg *Package, name string
 
                        // If we have a named type, we may have associated methods.
                        // Look for those first.
-                       if named, _ := typ.(*Named); named != nil {
+                       if named := asNamed(typ); named != nil {
                                if alt := seen.lookup(named); alt != nil {
                                        // We have seen this type before, at a more shallow depth
                                        // (note that multiples of this type at the current depth
index 0d9d9b481771af6f7976079e072c36d0efd39990..7d272df5f3ae48264df7897d30161d12fc0c3b2d 100644 (file)
@@ -80,7 +80,7 @@ func NewMethodSet(T Type) *MethodSet {
        // (spec: "The type denoted by T is called the receiver base type;
        // it must not be a pointer or interface type and it must be declared
        // in the same package as the method.").
-       if t, _ := T.(*Named); t != nil && isPointer(t) {
+       if t := asNamed(T); t != nil && isPointer(t) {
                return &emptyMethodSet
        }
 
@@ -117,7 +117,7 @@ func NewMethodSet(T Type) *MethodSet {
 
                        // If we have a named type, we may have associated methods.
                        // Look for those first.
-                       if named, _ := typ.(*Named); named != nil {
+                       if named := asNamed(typ); named != nil {
                                if alt := seen.lookup(named); alt != nil {
                                        // We have seen this type before, at a more shallow depth
                                        // (note that multiples of this type at the current depth
index fae73412342af02569b4b94a1266259ff00d8d60..e57cbbaa6168632f95cc102b13f29ba085c057df 100644 (file)
@@ -143,7 +143,7 @@ const (
 // If the given type name obj doesn't have a type yet, its type is set to the returned named type.
 // The underlying type must not be a *Named.
 func NewNamed(obj *TypeName, underlying Type, methods []*Func) *Named {
-       if _, ok := underlying.(*Named); ok {
+       if asNamed(underlying) != nil {
                panic("underlying type must not be *Named")
        }
        return (*Checker)(nil).newNamed(obj, underlying, methods)
@@ -436,7 +436,7 @@ func (t *Named) SetUnderlying(underlying Type) {
        if underlying == nil {
                panic("underlying type must not be nil")
        }
-       if _, ok := underlying.(*Named); ok {
+       if asNamed(underlying) != nil {
                panic("underlying type must not be *Named")
        }
        t.resolve().underlying = underlying
@@ -600,7 +600,7 @@ func (n *Named) expandUnderlying() Type {
        orig := n.inst.orig
        targs := n.inst.targs
 
-       if _, unexpanded := orig.underlying.(*Named); unexpanded {
+       if asNamed(orig.underlying) != nil {
                // We should only get a Named underlying type here during type checking
                // (for example, in recursive type declarations).
                assert(check != nil)
@@ -658,7 +658,7 @@ func (n *Named) expandUnderlying() Type {
 //
 // TODO(rfindley): eliminate this function or give it a better name.
 func safeUnderlying(typ Type) Type {
-       if t, _ := typ.(*Named); t != nil {
+       if t := asNamed(typ); t != nil {
                return t.underlying
        }
        return typ.Underlying()
index 752d1a76c2c7ca8453607db259e562c1036c3705..dcbf30a556bab92a5cfa36c1df9f34cdb81c5d43 100644 (file)
@@ -126,7 +126,7 @@ func hasEmptyTypeset(t Type) bool {
 // TODO(gri) should we include signatures or assert that they are not present?
 func isGeneric(t Type) bool {
        // A parameterized type is only generic if it doesn't have an instantiation already.
-       named, _ := t.(*Named)
+       named := asNamed(t)
        return named != nil && named.obj != nil && named.inst == nil && named.TypeParams().Len() > 0
 }
 
@@ -437,7 +437,7 @@ func (c *comparer) identical(x, y Type, p *ifacePair) bool {
                // Two named types are identical if their type names originate
                // in the same type declaration; if they are instantiated they
                // must have identical type argument lists.
-               if y, ok := y.(*Named); ok {
+               if y := asNamed(y); y != nil {
                        // check type arguments before origins to match unifier
                        // (for correct source code we need to do all checks so
                        // order doesn't matter)
index 8285f1b3d4f1a54da8e61e3a81f0cf7a86a2d501..a366b9dd0d703dbd47d26c61649dd7841da3fc33 100644 (file)
@@ -140,7 +140,7 @@ func (check *Checker) funcType(sig *Signature, recvPar *ast.FieldList, ftyp *ast
                                // Also: Don't report an error via genericType since it will be reported
                                //       again when we type-check the signature.
                                // TODO(gri) maybe the receiver should be marked as invalid instead?
-                               if recv, _ := check.genericType(rname, nil).(*Named); recv != nil {
+                               if recv := asNamed(check.genericType(rname, nil)); recv != nil {
                                        recvTParams = recv.TypeParams().list()
                                }
                        }
index c329752b3a034d0f9ed931f87b32d60c139269c9..5e40614f39122c52c2b8cdd8a51ad18d2147556f 100644 (file)
@@ -114,8 +114,8 @@ func (s *StdSizes) Alignof(T Type) (result int64) {
 }
 
 func _IsSyncAtomicAlign64(T Type) bool {
-       named, ok := T.(*Named)
-       if !ok {
+       named := asNamed(T)
+       if named == nil {
                return false
        }
        obj := named.Obj()
index 763fcc61f0d65f55b5993e7f33843b763ec0c34b..787926a367865de0c0fa4415bb36bdf551dea17c 100644 (file)
@@ -134,7 +134,7 @@ func (t *TypeParam) iface() *Interface {
                // pos is used for tracing output; start with the type parameter position.
                pos := t.obj.pos
                // use the (original or possibly instantiated) type bound position if we have one
-               if n, _ := bound.(*Named); n != nil {
+               if n := asNamed(bound); n != nil {
                        pos = n.obj.pos
                }
                computeInterfaceTypeSet(t.check, pos, ityp)
index 9615e24157a5a62d23fcdbe6275f1f9e9e55675b..cb735f2b49c9ca8952df5d9583a1731f06271f4d 100644 (file)
@@ -219,7 +219,7 @@ func (w *typeWriter) typ(typ Type) {
                                w.string("any")
                                break
                        }
-                       if t == universeComparable.Type().(*Named).underlying {
+                       if t == asNamed(universeComparable.Type()).underlying {
                                w.string("interface{comparable}")
                                break
                        }
index ca390ab9223ba23326feb7e202e4c3f3a9279d62..d92ac9cabdd0aeebc0f8013d1337f7464b841dc9 100644 (file)
@@ -411,7 +411,7 @@ func (check *Checker) instantiatedType(ix *typeparams.IndexExpr, def *Named) (re
                return gtyp // error already reported
        }
 
-       orig, _ := gtyp.(*Named)
+       orig := asNamed(gtyp)
        if orig == nil {
                panic(fmt.Sprintf("%v: cannot instantiate %v", ix.Pos(), gtyp))
        }
@@ -424,7 +424,7 @@ func (check *Checker) instantiatedType(ix *typeparams.IndexExpr, def *Named) (re
        }
 
        // create the instance
-       inst := check.instance(ix.Pos(), orig, targs, nil, check.context()).(*Named)
+       inst := asNamed(check.instance(ix.Pos(), orig, targs, nil, check.context()))
        def.setUnderlying(inst)
 
        // orig.tparams may not be set up, so we need to do expansion later.
index f17d3bcda4c85319eda6c63769b3278cfb2c6342..3838528b531751c2229f8ca997b9670e587349f0 100644 (file)
@@ -11,7 +11,7 @@ package types
 // under must only be called when a type is known
 // to be fully set up.
 func under(t Type) Type {
-       if t, _ := t.(*Named); t != nil {
+       if t := asNamed(t); t != nil {
                return t.under()
        }
        return t.Underlying()
index d8d5cd6f1a696f0505d225ce9c419d964c8aff1c..3c7b782b5a2d460f62e640cc9f870a169e5e8b32 100644 (file)
@@ -313,7 +313,7 @@ func (u *unifier) nify(x, y Type, mode unifyMode, p *ifacePair) (result bool) {
        // Ensure that if we have at least one
        // - defined type, make sure one is in y
        // - type parameter recorded with u, make sure one is in x
-       if _, ok := x.(*Named); ok || u.asTypeParam(y) != nil {
+       if asNamed(x) != nil || u.asTypeParam(y) != nil {
                if traceInference {
                        u.tracef("%s ≡ %s\t// swap", y, x)
                }
@@ -337,7 +337,7 @@ func (u *unifier) nify(x, y Type, mode unifyMode, p *ifacePair) (result bool) {
        // we will fail at function instantiation or argument assignment time.
        //
        // If we have at least one defined type, there is one in y.
-       if ny, _ := y.(*Named); mode&exact == 0 && ny != nil && isTypeLit(x) && !(u.enableInterfaceInference && IsInterface(x)) {
+       if ny := asNamed(y); mode&exact == 0 && ny != nil && isTypeLit(x) && !(u.enableInterfaceInference && IsInterface(x)) {
                if traceInference {
                        u.tracef("%s ≡ under %s", x, ny)
                }
@@ -374,8 +374,8 @@ func (u *unifier) nify(x, y Type, mode unifyMode, p *ifacePair) (result bool) {
                                // We have a match, possibly through underlying types.
                                xi := asInterface(x)
                                yi := asInterface(y)
-                               _, xn := x.(*Named)
-                               _, yn := y.(*Named)
+                               xn := asNamed(x) != nil
+                               yn := asNamed(y) != nil
                                // If we have two interfaces, what to do depends on
                                // whether they are named and their method sets.
                                if xi != nil && yi != nil {
@@ -730,7 +730,7 @@ func (u *unifier) nify(x, y Type, mode unifyMode, p *ifacePair) (result bool) {
        case *Named:
                // Two named types unify if their type names originate in the same type declaration.
                // If they are instantiated, their type argument lists must unify.
-               if y, ok := y.(*Named); ok {
+               if y := asNamed(y); y != nil {
                        // Check type arguments before origins so they unify
                        // even if the origins don't match; for better error
                        // messages (see go.dev/issue/53692).
index cc4d42d98c022e43cd619599b578b47ec76e159f..bde0293527f2681e09d592f1092aa3d36b9b756d 100644 (file)
@@ -267,7 +267,7 @@ func def(obj Object) {
                return // nothing to do
        }
        // fix Obj link for named types
-       if typ, _ := obj.Type().(*Named); typ != nil {
+       if typ := asNamed(obj.Type()); typ != nil {
                typ.obj = obj.(*TypeName)
        }
        // exported identifiers go into package unsafe