Source file src/cmd/compile/internal/types2/typeset.go

     1  // Copyright 2021 The Go Authors. All rights reserved.
     2  // Use of this source code is governed by a BSD-style
     3  // license that can be found in the LICENSE file.
     4  
     5  package types2
     6  
     7  import (
     8  	"cmd/compile/internal/syntax"
     9  	. "internal/types/errors"
    10  	"slices"
    11  	"strings"
    12  )
    13  
    14  // ----------------------------------------------------------------------------
    15  // API
    16  
    17  // A _TypeSet represents the type set of an interface.
    18  // Because of existing language restrictions, methods can be "factored out"
    19  // from the terms. The actual type set is the intersection of the type set
    20  // implied by the methods and the type set described by the terms and the
    21  // comparable bit. To test whether a type is included in a type set
    22  // ("implements" relation), the type must implement all methods _and_ be
    23  // an element of the type set described by the terms and the comparable bit.
    24  // If the term list describes the set of all types and comparable is true,
    25  // only comparable types are meant; in all other cases comparable is false.
    26  type _TypeSet struct {
    27  	methods    []*Func  // all methods of the interface; sorted by unique ID
    28  	terms      termlist // type terms of the type set
    29  	comparable bool     // invariant: !comparable || terms.isAll()
    30  }
    31  
    32  // IsEmpty reports whether s is the empty set.
    33  func (s *_TypeSet) IsEmpty() bool { return s.terms.isEmpty() }
    34  
    35  // IsAll reports whether s is the set of all types (corresponding to the empty interface).
    36  func (s *_TypeSet) IsAll() bool { return s.IsMethodSet() && len(s.methods) == 0 }
    37  
    38  // IsMethodSet reports whether the interface t is fully described by its method set.
    39  func (s *_TypeSet) IsMethodSet() bool { return !s.comparable && s.terms.isAll() }
    40  
    41  // IsComparable reports whether each type in the set is comparable.
    42  func (s *_TypeSet) IsComparable(seen map[Type]bool) bool {
    43  	if s.terms.isAll() {
    44  		return s.comparable
    45  	}
    46  	return s.is(func(t *term) bool {
    47  		return t != nil && comparableType(t.typ, false, seen, nil)
    48  	})
    49  }
    50  
    51  // NumMethods returns the number of methods available.
    52  func (s *_TypeSet) NumMethods() int { return len(s.methods) }
    53  
    54  // Method returns the i'th method of s for 0 <= i < s.NumMethods().
    55  // The methods are ordered by their unique ID.
    56  func (s *_TypeSet) Method(i int) *Func { return s.methods[i] }
    57  
    58  // LookupMethod returns the index of and method with matching package and name, or (-1, nil).
    59  func (s *_TypeSet) LookupMethod(pkg *Package, name string, foldCase bool) (int, *Func) {
    60  	return methodIndex(s.methods, pkg, name, foldCase)
    61  }
    62  
    63  func (s *_TypeSet) String() string {
    64  	switch {
    65  	case s.IsEmpty():
    66  		return "∅"
    67  	case s.IsAll():
    68  		return "𝓤"
    69  	}
    70  
    71  	hasMethods := len(s.methods) > 0
    72  	hasTerms := s.hasTerms()
    73  
    74  	var buf strings.Builder
    75  	buf.WriteByte('{')
    76  	if s.comparable {
    77  		buf.WriteString("comparable")
    78  		if hasMethods || hasTerms {
    79  			buf.WriteString("; ")
    80  		}
    81  	}
    82  	for i, m := range s.methods {
    83  		if i > 0 {
    84  			buf.WriteString("; ")
    85  		}
    86  		buf.WriteString(m.String())
    87  	}
    88  	if hasMethods && hasTerms {
    89  		buf.WriteString("; ")
    90  	}
    91  	if hasTerms {
    92  		buf.WriteString(s.terms.String())
    93  	}
    94  	buf.WriteString("}")
    95  	return buf.String()
    96  }
    97  
    98  // ----------------------------------------------------------------------------
    99  // Implementation
   100  
   101  // hasTerms reports whether s has specific type terms.
   102  func (s *_TypeSet) hasTerms() bool { return !s.terms.isEmpty() && !s.terms.isAll() }
   103  
   104  // subsetOf reports whether s1 ⊆ s2.
   105  func (s1 *_TypeSet) subsetOf(s2 *_TypeSet) bool { return s1.terms.subsetOf(s2.terms) }
   106  
   107  // typeset is an iterator over the (type/underlying type) pairs in s.
   108  // If s has no specific terms, typeset calls yield with (nil, nil).
   109  // In any case, typeset is guaranteed to call yield at least once.
   110  func (s *_TypeSet) typeset(yield func(t, u Type) bool) {
   111  	if !s.hasTerms() {
   112  		yield(nil, nil)
   113  		return
   114  	}
   115  
   116  	for _, t := range s.terms {
   117  		assert(t.typ != nil)
   118  		// Unalias(x) == under(x) for ~x terms
   119  		u := Unalias(t.typ)
   120  		if !t.tilde {
   121  			u = under(u)
   122  		}
   123  		if debug {
   124  			assert(Identical(u, under(u)))
   125  		}
   126  		if !yield(t.typ, u) {
   127  			break
   128  		}
   129  	}
   130  }
   131  
   132  // is calls f with the specific type terms of s and reports whether
   133  // all calls to f returned true. If there are no specific terms, is
   134  // returns the result of f(nil).
   135  func (s *_TypeSet) is(f func(*term) bool) bool {
   136  	if !s.hasTerms() {
   137  		return f(nil)
   138  	}
   139  	for _, t := range s.terms {
   140  		assert(t.typ != nil)
   141  		if !f(t) {
   142  			return false
   143  		}
   144  	}
   145  	return true
   146  }
   147  
   148  // topTypeSet may be used as type set for the empty interface.
   149  var topTypeSet = _TypeSet{terms: allTermlist}
   150  
   151  // computeInterfaceTypeSet may be called with check == nil.
   152  func computeInterfaceTypeSet(check *Checker, pos syntax.Pos, ityp *Interface) *_TypeSet {
   153  	if ityp.tset != nil {
   154  		return ityp.tset
   155  	}
   156  
   157  	// If the interface is not fully set up yet, the type set will
   158  	// not be complete, which may lead to errors when using the
   159  	// type set (e.g. missing method). Don't compute a partial type
   160  	// set (and don't store it!), so that we still compute the full
   161  	// type set eventually. Instead, return the top type set and
   162  	// let any follow-on errors play out.
   163  	//
   164  	// TODO(gri) Consider recording when this happens and reporting
   165  	// it as an error (but only if there were no other errors so
   166  	// to not have unnecessary follow-on errors).
   167  	if !ityp.complete {
   168  		return &topTypeSet
   169  	}
   170  
   171  	if check != nil && check.conf.Trace {
   172  		// Types don't generally have position information.
   173  		// If we don't have a valid pos provided, try to use
   174  		// one close enough.
   175  		if !pos.IsKnown() && len(ityp.methods) > 0 {
   176  			pos = ityp.methods[0].pos
   177  		}
   178  
   179  		check.trace(pos, "-- type set for %s", ityp)
   180  		check.indent++
   181  		defer func() {
   182  			check.indent--
   183  			check.trace(pos, "=> %s ", ityp.typeSet())
   184  		}()
   185  	}
   186  
   187  	// An infinitely expanding interface (due to a cycle) is detected
   188  	// elsewhere (Checker.validType), so here we simply assume we only
   189  	// have valid interfaces. Mark the interface as complete to avoid
   190  	// infinite recursion if the validType check occurs later for some
   191  	// reason.
   192  	ityp.tset = &_TypeSet{terms: allTermlist} // TODO(gri) is this sufficient?
   193  
   194  	var unionSets map[*Union]*_TypeSet
   195  	if check != nil {
   196  		if check.unionTypeSets == nil {
   197  			check.unionTypeSets = make(map[*Union]*_TypeSet)
   198  		}
   199  		unionSets = check.unionTypeSets
   200  	} else {
   201  		unionSets = make(map[*Union]*_TypeSet)
   202  	}
   203  
   204  	// Methods of embedded interfaces are collected unchanged; i.e., the identity
   205  	// of a method I.m's Func Object of an interface I is the same as that of
   206  	// the method m in an interface that embeds interface I. On the other hand,
   207  	// if a method is embedded via multiple overlapping embedded interfaces, we
   208  	// don't provide a guarantee which "original m" got chosen for the embedding
   209  	// interface. See also go.dev/issue/34421.
   210  	//
   211  	// If we don't care to provide this identity guarantee anymore, instead of
   212  	// reusing the original method in embeddings, we can clone the method's Func
   213  	// Object and give it the position of a corresponding embedded interface. Then
   214  	// we can get rid of the mpos map below and simply use the cloned method's
   215  	// position.
   216  
   217  	var seen objset
   218  	var allMethods []*Func
   219  	mpos := make(map[*Func]syntax.Pos) // method specification or method embedding position, for good error messages
   220  	addMethod := func(pos syntax.Pos, m *Func, explicit bool) {
   221  		switch other := seen.insert(m); {
   222  		case other == nil:
   223  			allMethods = append(allMethods, m)
   224  			mpos[m] = pos
   225  		case explicit:
   226  			if check != nil {
   227  				err := check.newError(DuplicateDecl)
   228  				err.addf(atPos(pos), "duplicate method %s", m.name)
   229  				err.addf(atPos(mpos[other.(*Func)]), "other declaration of method %s", m.name)
   230  				err.report()
   231  			}
   232  		default:
   233  			// We have a duplicate method name in an embedded (not explicitly declared) method.
   234  			// Check method signatures after all types are computed (go.dev/issue/33656).
   235  			// If we're pre-go1.14 (overlapping embeddings are not permitted), report that
   236  			// error here as well (even though we could do it eagerly) because it's the same
   237  			// error message.
   238  			if check != nil {
   239  				check.later(func() {
   240  					if pos.IsKnown() && !check.allowVersion(go1_14) || !Identical(m.typ, other.Type()) {
   241  						err := check.newError(DuplicateDecl)
   242  						err.addf(atPos(pos), "duplicate method %s", m.name)
   243  						err.addf(atPos(mpos[other.(*Func)]), "other declaration of method %s", m.name)
   244  						err.report()
   245  					}
   246  				}).describef(atPos(pos), "duplicate method check for %s", m.name)
   247  			}
   248  		}
   249  	}
   250  
   251  	for _, m := range ityp.methods {
   252  		addMethod(m.pos, m, true)
   253  	}
   254  
   255  	// collect embedded elements
   256  	allTerms := allTermlist
   257  	allComparable := false
   258  	for i, typ := range ityp.embeddeds {
   259  		// The embedding position is nil for imported interfaces.
   260  		// We don't need to do version checks in those cases.
   261  		var pos syntax.Pos // embedding position
   262  		if ityp.embedPos != nil {
   263  			pos = (*ityp.embedPos)[i]
   264  		}
   265  		var comparable bool
   266  		var terms termlist
   267  		switch u := under(typ).(type) {
   268  		case *Interface:
   269  			// For now we don't permit type parameters as constraints.
   270  			assert(!isTypeParam(typ))
   271  			tset := computeInterfaceTypeSet(check, pos, u)
   272  			// If typ is local, an error was already reported where typ is specified/defined.
   273  			if pos.IsKnown() && check != nil && check.isImportedConstraint(typ) && !check.verifyVersionf(atPos(pos), go1_18, "embedding constraint interface %s", typ) {
   274  				continue
   275  			}
   276  			comparable = tset.comparable
   277  			for _, m := range tset.methods {
   278  				addMethod(pos, m, false) // use embedding position pos rather than m.pos
   279  			}
   280  			terms = tset.terms
   281  		case *Union:
   282  			if pos.IsKnown() && check != nil && !check.verifyVersionf(atPos(pos), go1_18, "embedding interface element %s", u) {
   283  				continue
   284  			}
   285  			tset := computeUnionTypeSet(check, unionSets, pos, u)
   286  			if tset == &invalidTypeSet {
   287  				continue // ignore invalid unions
   288  			}
   289  			assert(!tset.comparable)
   290  			assert(len(tset.methods) == 0)
   291  			terms = tset.terms
   292  		default:
   293  			if !isValid(u) {
   294  				continue
   295  			}
   296  			if pos.IsKnown() && check != nil && !check.verifyVersionf(atPos(pos), go1_18, "embedding non-interface type %s", typ) {
   297  				continue
   298  			}
   299  			terms = termlist{{false, typ}}
   300  		}
   301  
   302  		// The type set of an interface is the intersection of the type sets of all its elements.
   303  		// Due to language restrictions, only embedded interfaces can add methods, they are handled
   304  		// separately. Here we only need to intersect the term lists and comparable bits.
   305  		allTerms, allComparable = intersectTermLists(allTerms, allComparable, terms, comparable)
   306  	}
   307  
   308  	ityp.tset.comparable = allComparable
   309  	if len(allMethods) != 0 {
   310  		sortMethods(allMethods)
   311  		ityp.tset.methods = allMethods
   312  	}
   313  	ityp.tset.terms = allTerms
   314  
   315  	return ityp.tset
   316  }
   317  
   318  // TODO(gri) The intersectTermLists function belongs to the termlist implementation.
   319  //           The comparable type set may also be best represented as a term (using
   320  //           a special type).
   321  
   322  // intersectTermLists computes the intersection of two term lists and respective comparable bits.
   323  // xcomp, ycomp are valid only if xterms.isAll() and yterms.isAll() respectively.
   324  func intersectTermLists(xterms termlist, xcomp bool, yterms termlist, ycomp bool) (termlist, bool) {
   325  	terms := xterms.intersect(yterms)
   326  	// If one of xterms or yterms is marked as comparable,
   327  	// the result must only include comparable types.
   328  	comp := xcomp || ycomp
   329  	if comp && !terms.isAll() {
   330  		// only keep comparable terms
   331  		i := 0
   332  		for _, t := range terms {
   333  			assert(t.typ != nil)
   334  			if comparableType(t.typ, false /* strictly comparable */, nil, nil) {
   335  				terms[i] = t
   336  				i++
   337  			}
   338  		}
   339  		terms = terms[:i]
   340  		if !terms.isAll() {
   341  			comp = false
   342  		}
   343  	}
   344  	assert(!comp || terms.isAll()) // comparable invariant
   345  	return terms, comp
   346  }
   347  
   348  func compareFunc(a, b *Func) int {
   349  	return a.cmp(&b.object)
   350  }
   351  
   352  func sortMethods(list []*Func) {
   353  	slices.SortFunc(list, compareFunc)
   354  }
   355  
   356  func assertSortedMethods(list []*Func) {
   357  	if !debug {
   358  		panic("assertSortedMethods called outside debug mode")
   359  	}
   360  	if !slices.IsSortedFunc(list, compareFunc) {
   361  		panic("methods not sorted")
   362  	}
   363  }
   364  
   365  // invalidTypeSet is a singleton type set to signal an invalid type set
   366  // due to an error. It's also a valid empty type set, so consumers of
   367  // type sets may choose to ignore it.
   368  var invalidTypeSet _TypeSet
   369  
   370  // computeUnionTypeSet may be called with check == nil.
   371  // The result is &invalidTypeSet if the union overflows.
   372  func computeUnionTypeSet(check *Checker, unionSets map[*Union]*_TypeSet, pos syntax.Pos, utyp *Union) *_TypeSet {
   373  	if tset, _ := unionSets[utyp]; tset != nil {
   374  		return tset
   375  	}
   376  
   377  	// avoid infinite recursion (see also computeInterfaceTypeSet)
   378  	unionSets[utyp] = new(_TypeSet)
   379  
   380  	var allTerms termlist
   381  	for _, t := range utyp.terms {
   382  		var terms termlist
   383  		u := under(t.typ)
   384  		if ui, _ := u.(*Interface); ui != nil {
   385  			// For now we don't permit type parameters as constraints.
   386  			assert(!isTypeParam(t.typ))
   387  			terms = computeInterfaceTypeSet(check, pos, ui).terms
   388  		} else if !isValid(u) {
   389  			continue
   390  		} else {
   391  			if t.tilde && !Identical(t.typ, u) {
   392  				// There is no underlying type which is t.typ.
   393  				// The corresponding type set is empty.
   394  				t = nil // ∅ term
   395  			}
   396  			terms = termlist{(*term)(t)}
   397  		}
   398  		// The type set of a union expression is the union
   399  		// of the type sets of each term.
   400  		allTerms = allTerms.union(terms)
   401  		if len(allTerms) > maxTermCount {
   402  			if check != nil {
   403  				check.errorf(atPos(pos), InvalidUnion, "cannot handle more than %d union terms (implementation limitation)", maxTermCount)
   404  			}
   405  			unionSets[utyp] = &invalidTypeSet
   406  			return unionSets[utyp]
   407  		}
   408  	}
   409  	unionSets[utyp].terms = allTerms
   410  
   411  	return unionSets[utyp]
   412  }
   413  

View as plain text