From f5f79c47f900300e8ac962e73ae7c2c706489d67 Mon Sep 17 00:00:00 2001 From: Dan Scales Date: Sat, 7 Aug 2021 22:26:46 -0700 Subject: [dev.typeparams] cmd/compile: use types2.Constraint() rather than types2.Bound() types2.Constraint() returns the top-level constraint type, including any unions or other interface elements. Because of that, we needed to add/fix some code in the type substituter and generic type instantiater in the importer to deal with unions and non-method members of an interface. Also, NewUnion was not correctly setting the HasTParam flag. I also added a better error message when a symbol is not found in (*deadcodePass).decodeIfaceMethod(). Change-Id: Id3668dc596dce63690fa05a9e5e42295b5e2bbb5 Reviewed-on: https://go-review.googlesource.com/c/go/+/340670 Trust: Dan Scales Run-TryBot: Dan Scales Reviewed-by: Keith Randall --- src/cmd/compile/internal/typecheck/iimport.go | 12 ++++++++++-- 1 file changed, 10 insertions(+), 2 deletions(-) (limited to 'src/cmd/compile/internal/typecheck/iimport.go') diff --git a/src/cmd/compile/internal/typecheck/iimport.go b/src/cmd/compile/internal/typecheck/iimport.go index 2e8b18c0b7..d5f4bba98b 100644 --- a/src/cmd/compile/internal/typecheck/iimport.go +++ b/src/cmd/compile/internal/typecheck/iimport.go @@ -1858,18 +1858,26 @@ func substInstType(t *types.Type, baseType *types.Type, targs []*types.Type) { newfields := make([]*types.Field, baseType.Methods().Len()) for i, f := range baseType.Methods().Slice() { + if !f.IsMethod() || types.IsInterfaceMethod(f.Type) { + // Do a normal substitution if this is a non-method (which + // means this must be an interface used as a constraint) or + // an interface method. + t2 := subst.Typ(f.Type) + newfields[i] = types.NewField(f.Pos, f.Sym, t2) + continue + } recvType := f.Type.Recv().Type if recvType.IsPtr() { recvType = recvType.Elem() } // Substitute in the method using the type params used in the // method (not the type params in the definition of the generic type). - subst := Tsubster{ + msubst := Tsubster{ Tparams: recvType.RParams(), Targs: targs, SubstForwFunc: doInst, } - t2 := subst.Typ(f.Type) + t2 := msubst.Typ(f.Type) oldsym := f.Nname.Sym() newsym := MakeInstName(oldsym, targs, true) var nname *ir.Name -- cgit v1.2.3-54-g00ecf