Source file src/go/types/under.go

     1  // Code generated by "go test -run=Generate -write=all"; DO NOT EDIT.
     2  // Source: ../../cmd/compile/internal/types2/under.go
     3  
     4  // Copyright 2011 The Go Authors. All rights reserved.
     5  // Use of this source code is governed by a BSD-style
     6  // license that can be found in the LICENSE file.
     7  
     8  package types
     9  
    10  import "iter"
    11  
    12  // If typ is a type parameter, underIs returns the result of typ.underIs(f).
    13  // Otherwise, underIs returns the result of f(typ.Underlying()).
    14  func underIs(typ Type, f func(Type) bool) bool {
    15  	return all(typ, func(_, u Type) bool {
    16  		return f(u)
    17  	})
    18  }
    19  
    20  // all reports whether f(t, u) is true for all (type/underlying type)
    21  // pairs in the typeset of t. See [typeset] for details of sequence.
    22  func all(t Type, f func(t, u Type) bool) bool {
    23  	if p, _ := Unalias(t).(*TypeParam); p != nil {
    24  		return p.typeset(f)
    25  	}
    26  	return f(t, t.Underlying())
    27  }
    28  
    29  // typeset is an iterator over the (type/underlying type) pairs of the
    30  // specific type terms of the type set implied by t.
    31  // If t is a type parameter, the implied type set is the type set of t's constraint.
    32  // In this case, if there are no specific terms, the iterator produces (nil, nil).
    33  // If t is not a type parameter, the implied type set consists of just t.
    34  // In any case, typeset is guaranteed to produce at least one set of results.
    35  func typeset(t Type) iter.Seq2[Type, Type] {
    36  	return func(yield func(t, u Type) bool) {
    37  		all(t, yield)
    38  	}
    39  }
    40  
    41  // A typeError describes a type error.
    42  type typeError struct {
    43  	format_ string
    44  	args    []any
    45  }
    46  
    47  var emptyTypeError typeError
    48  
    49  func typeErrorf(format string, args ...any) *typeError {
    50  	if format == "" {
    51  		return &emptyTypeError
    52  	}
    53  	return &typeError{format, args}
    54  }
    55  
    56  // format formats a type error as a string.
    57  // check may be nil.
    58  func (err *typeError) format(check *Checker) string {
    59  	return check.sprintf(err.format_, err.args...)
    60  }
    61  
    62  // CommonUnderlying returns the common underlying type of the types in
    63  // the type set implied by t, or nil if there is none.
    64  //
    65  // If t is not a type parameter, the result is t.Underlying().
    66  // (An operand of interface type has the single type t, not the type
    67  // set of the interface.)
    68  //
    69  // If t is a type parameter, the result is the single underlying type U
    70  // shared by every specific type in its constraint's type set. It is nil
    71  // if there are no specific types (e.g. any, comparable) or they do not
    72  // share an underlying type. As a special case, if all are channel types
    73  // with identical element types and directions that do not conflict, the
    74  // result is the most restrictive channel type.
    75  func CommonUnderlying(t Type) Type {
    76  	u, _ := commonUnder(t, nil)
    77  	return u
    78  }
    79  
    80  // If t is a type parameter, cond is nil, and t's type set contains no channel types,
    81  // commonUnder returns the common underlying type of all types in t's type set if
    82  // it exists, or nil and a type error otherwise.
    83  //
    84  // If t is a type parameter, cond is nil, and there are channel types, t's type set
    85  // must only contain channel types, they must all have the same element types,
    86  // channel directions must not conflict, and commonUnder returns one of the most
    87  // restricted channels. Otherwise, the function returns nil and a type error.
    88  //
    89  // If cond != nil, each pair (t, u) of type and underlying type in t's type set
    90  // must satisfy the condition expressed by cond. If the result of cond is != nil,
    91  // commonUnder returns nil and the type error reported by cond.
    92  // Note that cond is called before any other conditions are checked; specifically
    93  // cond may be called with (nil, nil) if the type set contains no specific types.
    94  //
    95  // If t is not a type parameter, commonUnder behaves as if t was a type parameter
    96  // with the single type t in its type set.
    97  func commonUnder(t Type, cond func(t, u Type) *typeError) (Type, *typeError) {
    98  	var ct, cu Type // type and respective common underlying type
    99  	for t, u := range typeset(t) {
   100  		if cond != nil {
   101  			if err := cond(t, u); err != nil {
   102  				return nil, err
   103  			}
   104  		}
   105  
   106  		if u == nil {
   107  			return nil, typeErrorf("no specific type")
   108  		}
   109  
   110  		// If this is the first type we're seeing, we're done.
   111  		if cu == nil {
   112  			ct, cu = t, u
   113  			continue
   114  		}
   115  
   116  		// If we've seen a channel before, and we have a channel now, they must be compatible.
   117  		if chu, _ := cu.(*Chan); chu != nil {
   118  			if ch, _ := u.(*Chan); ch != nil {
   119  				if !Identical(chu.elem, ch.elem) {
   120  					return nil, typeErrorf("channels %s and %s have different element types", ct, t)
   121  				}
   122  				// If we have different channel directions, keep the restricted one
   123  				// and complain if they conflict.
   124  				switch {
   125  				case chu.dir == ch.dir:
   126  					// nothing to do
   127  				case chu.dir == SendRecv:
   128  					ct, cu = t, u // switch to restricted channel
   129  				case ch.dir != SendRecv:
   130  					return nil, typeErrorf("channels %s and %s have conflicting directions", ct, t)
   131  				}
   132  				continue
   133  			}
   134  		}
   135  
   136  		// Otherwise, the current type must have the same underlying type as all previous types.
   137  		if !Identical(cu, u) {
   138  			return nil, typeErrorf("%s and %s have different underlying types", ct, t)
   139  		}
   140  	}
   141  	return cu, nil
   142  }
   143  

View as plain text