cmd/compile/internal/types: remove TTYPEPARAM and TUNION types

These were used by the nounified frontend for representing
uninstantiated generic types; however, the unified frontend only needs
types1 to represent instantiated types.

Updates #57410.

Change-Id: Iac417fbf2b86f4e08bd7fdd26ae8ed17395ce833
Reviewed-on: https://go-review.googlesource.com/c/go/+/458621
Run-TryBot: Matthew Dempsky <mdempsky@google.com>
Reviewed-by: Keith Randall <khr@golang.org>
Reviewed-by: Keith Randall <khr@google.com>
TryBot-Result: Gopher Robot <gobot@golang.org>
diff --git a/src/cmd/compile/internal/noder/helpers.go b/src/cmd/compile/internal/noder/helpers.go
index 4ef46a4..ff2d50f 100644
--- a/src/cmd/compile/internal/noder/helpers.go
+++ b/src/cmd/compile/internal/noder/helpers.go
@@ -61,9 +61,7 @@
 	if !typ.IsUntyped() {
 		val = typecheck.DefaultLit(ir.NewBasicLit(src.NoXPos, val), typ).Val()
 	}
-	if !typ.IsTypeParam() {
-		ir.AssertValidTypeForConst(typ, val)
-	}
+	ir.AssertValidTypeForConst(typ, val)
 	return val
 }
 
@@ -211,15 +209,7 @@
 func IncDec(pos src.XPos, op ir.Op, x ir.Node) *ir.AssignOpStmt {
 	assert(x.Type() != nil)
 	bl := ir.NewBasicLit(pos, one)
-	if x.Type().HasTParam() {
-		// If the operand is generic, then types2 will have proved it must be
-		// a type that fits with increment/decrement, so just set the type of
-		// "one" to n.Type(). This works even for types that are eventually
-		// float or complex.
-		typed(x.Type(), bl)
-	} else {
-		bl = typecheck.DefaultLit(bl, x.Type())
-	}
+	bl = typecheck.DefaultLit(bl, x.Type())
 	return ir.NewAssignOpStmt(pos, op, x, bl)
 }
 
diff --git a/src/cmd/compile/internal/reflectdata/reflect.go b/src/cmd/compile/internal/reflectdata/reflect.go
index d0b237d..9d71bf6 100644
--- a/src/cmd/compile/internal/reflectdata/reflect.go
+++ b/src/cmd/compile/internal/reflectdata/reflect.go
@@ -959,7 +959,7 @@
 
 func writeType(t *types.Type) *obj.LSym {
 	t = formalType(t)
-	if t.IsUntyped() || t.HasTParam() {
+	if t.IsUntyped() {
 		base.Fatalf("writeType %v", t)
 	}
 
@@ -1260,11 +1260,6 @@
 
 // NeedRuntimeType ensures that a runtime type descriptor is emitted for t.
 func NeedRuntimeType(t *types.Type) {
-	if t.HasTParam() {
-		// Generic types don't really exist at run-time and have no runtime
-		// type descriptor.  But we do write out shape types.
-		return
-	}
 	if _, ok := signatset[t]; !ok {
 		signatset[t] = struct{}{}
 		signatslice = append(signatslice, typeAndStr{t: t, short: types.TypeSymName(t), regular: t.String()})
@@ -1781,9 +1776,6 @@
 		if s.Pkg.Name != "main" {
 			continue
 		}
-		if n.Type().HasTParam() {
-			continue // skip generic functions (#52937)
-		}
 		ptabs = append(ptabs, n)
 	}
 }
diff --git a/src/cmd/compile/internal/typecheck/dcl.go b/src/cmd/compile/internal/typecheck/dcl.go
index fce7d3d..5d69506 100644
--- a/src/cmd/compile/internal/typecheck/dcl.go
+++ b/src/cmd/compile/internal/typecheck/dcl.go
@@ -305,12 +305,6 @@
 // f is method type, with receiver.
 // return function type, receiver as first argument (or not).
 func NewMethodType(sig *types.Type, recv *types.Type) *types.Type {
-	if sig.HasTParam() {
-		base.Fatalf("NewMethodType with type parameters in signature %+v", sig)
-	}
-	if recv != nil && recv.HasTParam() {
-		base.Fatalf("NewMethodType with type parameters in receiver %+v", recv)
-	}
 	nrecvs := 0
 	if recv != nil {
 		nrecvs++
diff --git a/src/cmd/compile/internal/typecheck/subr.go b/src/cmd/compile/internal/typecheck/subr.go
index a5387b2..ad8e801 100644
--- a/src/cmd/compile/internal/typecheck/subr.go
+++ b/src/cmd/compile/internal/typecheck/subr.go
@@ -730,19 +730,7 @@
 		return false
 	}
 
-	if t.IsInterface() || t.IsTypeParam() {
-		if t.IsTypeParam() {
-			// If t is a simple type parameter T, its type and underlying is the same.
-			// If t is a type definition:'type P[T any] T', its type is P[T] and its
-			// underlying is T. Therefore we use 't.Underlying() != t' to distinguish them.
-			if t.Underlying() != t {
-				CalcMethods(t)
-			} else {
-				// A typeparam satisfies an interface if its type bound
-				// has all the methods of that interface.
-				t = t.Bound()
-			}
-		}
+	if t.IsInterface() {
 		i := 0
 		tms := t.AllMethods().Slice()
 		for _, im := range iface.AllMethods().Slice() {
diff --git a/src/cmd/compile/internal/types/fmt.go b/src/cmd/compile/internal/types/fmt.go
index 990f2e5..108283c 100644
--- a/src/cmd/compile/internal/types/fmt.go
+++ b/src/cmd/compile/internal/types/fmt.go
@@ -571,27 +571,6 @@
 	case TUNSAFEPTR:
 		b.WriteString("unsafe.Pointer")
 
-	case TTYPEPARAM:
-		if t.Sym() != nil {
-			sconv2(b, t.Sym(), 'v', mode)
-		} else {
-			b.WriteString("tp")
-			// Print out the pointer value for now to disambiguate type params
-			fmt.Fprintf(b, "%p", t)
-		}
-
-	case TUNION:
-		for i := 0; i < t.NumTerms(); i++ {
-			if i > 0 {
-				b.WriteString("|")
-			}
-			elem, tilde := t.Term(i)
-			if tilde {
-				b.WriteString("~")
-			}
-			tconv2(b, elem, 0, mode, visited)
-		}
-
 	case Txxx:
 		b.WriteString("Txxx")
 
diff --git a/src/cmd/compile/internal/types/kind_string.go b/src/cmd/compile/internal/types/kind_string.go
index 3e6a8bc..1e1e846 100644
--- a/src/cmd/compile/internal/types/kind_string.go
+++ b/src/cmd/compile/internal/types/kind_string.go
@@ -37,22 +37,20 @@
 	_ = x[TANY-26]
 	_ = x[TSTRING-27]
 	_ = x[TUNSAFEPTR-28]
-	_ = x[TTYPEPARAM-29]
-	_ = x[TUNION-30]
-	_ = x[TIDEAL-31]
-	_ = x[TNIL-32]
-	_ = x[TBLANK-33]
-	_ = x[TFUNCARGS-34]
-	_ = x[TCHANARGS-35]
-	_ = x[TSSA-36]
-	_ = x[TTUPLE-37]
-	_ = x[TRESULTS-38]
-	_ = x[NTYPE-39]
+	_ = x[TIDEAL-29]
+	_ = x[TNIL-30]
+	_ = x[TBLANK-31]
+	_ = x[TFUNCARGS-32]
+	_ = x[TCHANARGS-33]
+	_ = x[TSSA-34]
+	_ = x[TTUPLE-35]
+	_ = x[TRESULTS-36]
+	_ = x[NTYPE-37]
 }
 
-const _Kind_name = "xxxINT8UINT8INT16UINT16INT32UINT32INT64UINT64INTUINTUINTPTRCOMPLEX64COMPLEX128FLOAT32FLOAT64BOOLPTRFUNCSLICEARRAYSTRUCTCHANMAPINTERFORWANYSTRINGUNSAFEPTRTYPEPARAMUNIONIDEALNILBLANKFUNCARGSCHANARGSSSATUPLERESULTSNTYPE"
+const _Kind_name = "xxxINT8UINT8INT16UINT16INT32UINT32INT64UINT64INTUINTUINTPTRCOMPLEX64COMPLEX128FLOAT32FLOAT64BOOLPTRFUNCSLICEARRAYSTRUCTCHANMAPINTERFORWANYSTRINGUNSAFEPTRIDEALNILBLANKFUNCARGSCHANARGSSSATUPLERESULTSNTYPE"
 
-var _Kind_index = [...]uint8{0, 3, 7, 12, 17, 23, 28, 34, 39, 45, 48, 52, 59, 68, 78, 85, 92, 96, 99, 103, 108, 113, 119, 123, 126, 131, 135, 138, 144, 153, 162, 167, 172, 175, 180, 188, 196, 199, 204, 211, 216}
+var _Kind_index = [...]uint8{0, 3, 7, 12, 17, 23, 28, 34, 39, 45, 48, 52, 59, 68, 78, 85, 92, 96, 99, 103, 108, 113, 119, 123, 126, 131, 135, 138, 144, 153, 158, 161, 166, 174, 182, 185, 190, 197, 202}
 
 func (i Kind) String() string {
 	if i >= Kind(len(_Kind_index)-1) {
diff --git a/src/cmd/compile/internal/types/size.go b/src/cmd/compile/internal/types/size.go
index e655a36..b8b90b2 100644
--- a/src/cmd/compile/internal/types/size.go
+++ b/src/cmd/compile/internal/types/size.go
@@ -123,10 +123,6 @@
 			continue
 		}
 
-		if m.Type.IsUnion() {
-			continue
-		}
-
 		// In 1.18, embedded types can be anything. In Go 1.17, we disallow
 		// embedding anything other than interfaces. This requirement was caught
 		// by types2 already, so allow non-interface here.
@@ -343,12 +339,6 @@
 		t.align = uint8(PtrSize)
 		expandiface(t)
 
-	case TUNION:
-		// Always part of an interface for now, so size/align don't matter.
-		// Pretend a union is represented like an interface.
-		w = 2 * int64(PtrSize)
-		t.align = uint8(PtrSize)
-
 	case TCHAN: // implemented as pointer
 		w = int64(PtrSize)
 
@@ -445,11 +435,6 @@
 			base.Warn("bad type %v %d\n", t1, w)
 		}
 		t.align = 1
-
-	case TTYPEPARAM:
-		// TODO(danscales) - remove when we eliminate the need
-		// to do CalcSize in noder2 (which shouldn't be needed in the noder)
-		w = int64(PtrSize)
 	}
 
 	if PtrSize == 4 && w != int64(int32(w)) {
diff --git a/src/cmd/compile/internal/types/type.go b/src/cmd/compile/internal/types/type.go
index 3198bb6..5e4c1b9 100644
--- a/src/cmd/compile/internal/types/type.go
+++ b/src/cmd/compile/internal/types/type.go
@@ -60,8 +60,6 @@
 	TANY
 	TSTRING
 	TUNSAFEPTR
-	TTYPEPARAM
-	TUNION
 
 	// pseudo-types for literals
 	TIDEAL // untyped numeric constants
@@ -154,8 +152,6 @@
 	// TARRAY: *Array
 	// TSLICE: Slice
 	// TSSA: string
-	// TTYPEPARAM:  *Typeparam
-	// TUNION: *Union
 	extra interface{}
 
 	// width is the width of this Type in bytes.
@@ -207,16 +203,14 @@
 	typeNoalg                  // suppress hash and eq algorithm generation
 	typeDeferwidth             // width computation has been deferred and type is on deferredTypeStack
 	typeRecur
-	typeHasTParam // there is a typeparam somewhere in the type (generic function or type)
-	typeIsShape   // represents a set of closely related types, for generics
-	typeHasShape  // there is a shape somewhere in the type
+	typeIsShape  // represents a set of closely related types, for generics
+	typeHasShape // there is a shape somewhere in the type
 )
 
 func (t *Type) NotInHeap() bool  { return t.flags&typeNotInHeap != 0 }
 func (t *Type) Noalg() bool      { return t.flags&typeNoalg != 0 }
 func (t *Type) Deferwidth() bool { return t.flags&typeDeferwidth != 0 }
 func (t *Type) Recur() bool      { return t.flags&typeRecur != 0 }
-func (t *Type) HasTParam() bool  { return t.flags&typeHasTParam != 0 }
 func (t *Type) IsShape() bool    { return t.flags&typeIsShape != 0 }
 func (t *Type) HasShape() bool   { return t.flags&typeHasShape != 0 }
 
@@ -225,9 +219,6 @@
 func (t *Type) SetDeferwidth(b bool) { t.flags.set(typeDeferwidth, b) }
 func (t *Type) SetRecur(b bool)      { t.flags.set(typeRecur, b) }
 
-// Generic types should never have alg functions.
-func (t *Type) SetHasTParam(b bool) { t.flags.set(typeHasTParam, b); t.flags.set(typeNoalg, b) }
-
 // Should always do SetHasShape(true) when doing SetIsShape(true).
 func (t *Type) SetIsShape(b bool)  { t.flags.set(typeIsShape, b) }
 func (t *Type) SetHasShape(b bool) { t.flags.set(typeHasShape, b) }
@@ -272,14 +263,8 @@
 		base.Fatalf("Setting nil or zero-length rparams")
 	}
 	t.rparams = &rparams
-	// HasTParam should be set if any rparam is or has a type param. This is
-	// to handle the case of a generic type which doesn't reference any of its
-	// type params (e.g. most commonly, an empty struct).
+	// HasShape should be set if any type argument is or has a shape type.
 	for _, rparam := range rparams {
-		if rparam.HasTParam() {
-			t.SetHasTParam(true)
-			break
-		}
 		if rparam.HasShape() {
 			t.SetHasShape(true)
 			break
@@ -291,7 +276,7 @@
 // instantiated generic type where all type arguments are non-generic or fully
 // instantiated generic types.
 func (t *Type) IsFullyInstantiated() bool {
-	return len(t.RParams()) > 0 && !t.HasTParam()
+	return len(t.RParams()) > 0
 }
 
 // NoPkg is a nil *Pkg value for clarity.
@@ -406,18 +391,6 @@
 	implicit bool
 }
 
-// Typeparam contains Type fields specific to typeparam types.
-type Typeparam struct {
-	index int // type parameter index in source order, starting at 0
-	bound *Type
-}
-
-// Union contains Type fields specific to union types.
-type Union struct {
-	terms  []*Type
-	tildes []bool // whether terms[i] is of form ~T
-}
-
 // Ptr contains Type fields specific to pointer types.
 type Ptr struct {
 	Elem *Type // element type
@@ -599,10 +572,6 @@
 		t.extra = new(Tuple)
 	case TRESULTS:
 		t.extra = new(Results)
-	case TTYPEPARAM:
-		t.extra = new(Typeparam)
-	case TUNION:
-		t.extra = new(Union)
 	}
 	return t
 }
@@ -614,9 +583,6 @@
 	}
 	t := newType(TARRAY)
 	t.extra = &Array{Elem: elem, Bound: bound}
-	if elem.HasTParam() {
-		t.SetHasTParam(true)
-	}
 	if elem.HasShape() {
 		t.SetHasShape(true)
 	}
@@ -629,8 +595,8 @@
 		if t.Elem() != elem {
 			base.Fatalf("elem mismatch")
 		}
-		if elem.HasTParam() != t.HasTParam() || elem.HasShape() != t.HasShape() {
-			base.Fatalf("Incorrect HasTParam/HasShape flag for cached slice type")
+		if elem.HasShape() != t.HasShape() {
+			base.Fatalf("Incorrect HasShape flag for cached slice type")
 		}
 		return t
 	}
@@ -638,9 +604,6 @@
 	t := newType(TSLICE)
 	t.extra = Slice{Elem: elem}
 	elem.cache.slice = t
-	if elem.HasTParam() {
-		t.SetHasTParam(true)
-	}
 	if elem.HasShape() {
 		t.SetHasShape(true)
 	}
@@ -653,9 +616,6 @@
 	ct := t.ChanType()
 	ct.Elem = elem
 	ct.Dir = dir
-	if elem.HasTParam() {
-		t.SetHasTParam(true)
-	}
 	if elem.HasShape() {
 		t.SetHasShape(true)
 	}
@@ -666,9 +626,6 @@
 	t := newType(TTUPLE)
 	t.extra.(*Tuple).first = t1
 	t.extra.(*Tuple).second = t2
-	if t1.HasTParam() || t2.HasTParam() {
-		t.SetHasTParam(true)
-	}
 	if t1.HasShape() || t2.HasShape() {
 		t.SetHasShape(true)
 	}
@@ -700,9 +657,6 @@
 	mt := t.MapType()
 	mt.Key = k
 	mt.Elem = v
-	if k.HasTParam() || v.HasTParam() {
-		t.SetHasTParam(true)
-	}
 	if k.HasShape() || v.HasShape() {
 		t.SetHasShape(true)
 	}
@@ -724,8 +678,8 @@
 		if t.Elem() != elem {
 			base.Fatalf("NewPtr: elem mismatch")
 		}
-		if elem.HasTParam() != t.HasTParam() || elem.HasShape() != t.HasShape() {
-			base.Fatalf("Incorrect HasTParam/HasShape flag for cached pointer type")
+		if elem.HasShape() != t.HasShape() {
+			base.Fatalf("Incorrect HasShape flag for cached pointer type")
 		}
 		return t
 	}
@@ -737,9 +691,6 @@
 	if NewPtrCacheEnabled {
 		elem.cache.ptr = t
 	}
-	if elem.HasTParam() {
-		t.SetHasTParam(true)
-	}
 	if elem.HasShape() {
 		t.SetHasShape(true)
 	}
@@ -886,8 +837,6 @@
 	case TARRAY:
 		x := *t.extra.(*Array)
 		nt.extra = &x
-	case TTYPEPARAM:
-		base.Fatalf("typeparam types cannot be copied")
 	case TTUPLE, TSSA, TRESULTS:
 		base.Fatalf("ssa types cannot be copied")
 	}
@@ -1493,14 +1442,6 @@
 	return t.kind == TINTER
 }
 
-func (t *Type) IsUnion() bool {
-	return t.kind == TUNION
-}
-
-func (t *Type) IsTypeParam() bool {
-	return t.kind == TTYPEPARAM
-}
-
 // IsEmptyInterface reports whether t is an empty interface type.
 func (t *Type) IsEmptyInterface() bool {
 	return t.IsInterface() && t.AllMethods().Len() == 0
@@ -1748,9 +1689,6 @@
 	if underlying.NotInHeap() {
 		t.SetNotInHeap(true)
 	}
-	if underlying.HasTParam() {
-		t.SetHasTParam(true)
-	}
 	if underlying.HasShape() {
 		t.SetHasShape(true)
 	}
@@ -1776,15 +1714,6 @@
 	}
 }
 
-func fieldsHasTParam(fields []*Field) bool {
-	for _, f := range fields {
-		if f.Type != nil && f.Type.HasTParam() {
-			return true
-		}
-	}
-	return false
-}
-
 func fieldsHasShape(fields []*Field) bool {
 	for _, f := range fields {
 		if f.Type != nil && f.Type.HasShape() {
@@ -1808,10 +1737,6 @@
 	t.SetInterface(methods)
 	for _, f := range methods {
 		// f.Type could be nil for a broken interface declaration
-		if f.Type != nil && f.Type.HasTParam() {
-			t.SetHasTParam(true)
-			break
-		}
 		if f.Type != nil && f.Type.HasShape() {
 			t.SetHasShape(true)
 			break
@@ -1822,40 +1747,6 @@
 	return t
 }
 
-// NewTypeParam returns a new type param with the specified sym (package and name)
-// and specified index within the typeparam list.
-func NewTypeParam(obj Object, index int) *Type {
-	t := newType(TTYPEPARAM)
-	t.obj = obj
-	t.extra.(*Typeparam).index = index
-	t.SetHasTParam(true)
-	return t
-}
-
-// Index returns the index of the type param within its param list.
-func (t *Type) Index() int {
-	t.wantEtype(TTYPEPARAM)
-	return t.extra.(*Typeparam).index
-}
-
-// SetIndex sets the index of the type param within its param list.
-func (t *Type) SetIndex(i int) {
-	t.wantEtype(TTYPEPARAM)
-	t.extra.(*Typeparam).index = i
-}
-
-// SetBound sets the bound of a typeparam.
-func (t *Type) SetBound(bound *Type) {
-	t.wantEtype(TTYPEPARAM)
-	t.extra.(*Typeparam).bound = bound
-}
-
-// Bound returns the bound of a typeparam.
-func (t *Type) Bound() *Type {
-	t.wantEtype(TTYPEPARAM)
-	return t.extra.(*Typeparam).bound
-}
-
 // IsImplicit reports whether an interface is implicit (i.e. elided from a type
 // parameter constraint).
 func (t *Type) IsImplicit() bool {
@@ -1869,41 +1760,6 @@
 	t.extra.(*Interface).implicit = true
 }
 
-// NewUnion returns a new union with the specified set of terms (types). If
-// tildes[i] is true, then terms[i] represents ~T, rather than just T.
-func NewUnion(terms []*Type, tildes []bool) *Type {
-	t := newType(TUNION)
-	if len(terms) != len(tildes) {
-		base.Fatalf("Mismatched terms and tildes for NewUnion")
-	}
-	t.extra.(*Union).terms = terms
-	t.extra.(*Union).tildes = tildes
-	nt := len(terms)
-	for i := 0; i < nt; i++ {
-		if terms[i].HasTParam() {
-			t.SetHasTParam(true)
-		}
-		if terms[i].HasShape() {
-			t.SetHasShape(true)
-		}
-	}
-	return t
-}
-
-// NumTerms returns the number of terms in a union type.
-func (t *Type) NumTerms() int {
-	t.wantEtype(TUNION)
-	return len(t.extra.(*Union).terms)
-}
-
-// Term returns ith term of a union type as (term, tilde). If tilde is true, term
-// represents ~T, rather than just T.
-func (t *Type) Term(i int) (*Type, bool) {
-	t.wantEtype(TUNION)
-	u := t.extra.(*Union)
-	return u.terms[i], u.tildes[i]
-}
-
 const BOGUS_FUNARG_OFFSET = -1000000000
 
 func unzeroFieldOffsets(f []*Field) {
@@ -1940,10 +1796,6 @@
 	ft.Params = funargs(params, FunargParams)
 	ft.Results = funargs(results, FunargResults)
 	ft.pkg = pkg
-	if len(tparams) > 0 || fieldsHasTParam(recvs) || fieldsHasTParam(params) ||
-		fieldsHasTParam(results) {
-		t.SetHasTParam(true)
-	}
 	if fieldsHasShape(recvs) || fieldsHasShape(params) || fieldsHasShape(results) {
 		t.SetHasShape(true)
 	}
@@ -1956,9 +1808,6 @@
 	t := newType(TSTRUCT)
 	t.SetFields(fields)
 	t.extra.(*Struct).pkg = pkg
-	if fieldsHasTParam(fields) {
-		t.SetHasTParam(true)
-	}
 	if fieldsHasShape(fields) {
 		t.SetHasShape(true)
 	}