// Code generated by "go test -run=Generate -write=all"; DO NOT EDIT.
// Source: ../../cmd/compile/internal/types2/under.go

// Copyright 2011 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

// under returns the true expanded underlying type.
// If it doesn't exist, the result is Typ[Invalid].
// under must only be called when a type is known
// to be fully set up.
func under( Type) Type {
	if  := asNamed();  != nil {
		return .under()
	}
	return .Underlying()
}

// If typ is a type parameter, underIs returns the result of typ.underIs(f).
// Otherwise, underIs returns the result of f(under(typ)).
func underIs( Type,  func(Type) bool) bool {
	var  bool
	typeset(, func(,  Type) bool {
		 = ()
		return 
	})
	return 
}

// typeset is an iterator over the (type/underlying type) pairs of the
// specific type terms of the type set implied by t.
// If t is a type parameter, the implied type set is the type set of t's constraint.
// In that case, if there are no specific terms, typeset calls yield with (nil, nil).
// If t is not a type parameter, the implied type set consists of just t.
// In any case, typeset is guaranteed to call yield at least once.
func typeset( Type,  func(,  Type) bool) {
	if ,  := Unalias().(*TypeParam);  != nil {
		.typeset()
		return
	}
	(, under())
}

// A typeError describes a type error.
type typeError struct {
	format_ string
	args    []any
}

var emptyTypeError typeError

func typeErrorf( string,  ...any) *typeError {
	if  == "" {
		return &emptyTypeError
	}
	return &typeError{, }
}

// format formats a type error as a string.
// check may be nil.
func ( *typeError) ( *Checker) string {
	return .sprintf(.format_, .args...)
}

// If t is a type parameter, cond is nil, and t's type set contains no channel types,
// commonUnder returns the common underlying type of all types in t's type set if
// it exists, or nil and a type error otherwise.
//
// If t is a type parameter, cond is nil, and there are channel types, t's type set
// must only contain channel types, they must all have the same element types,
// channel directions must not conflict, and commonUnder returns one of the most
// restricted channels. Otherwise, the function returns nil and a type error.
//
// If cond != nil, each pair (t, u) of type and underlying type in t's type set
// must satisfy the condition expressed by cond. If the result of cond is != nil,
// commonUnder returns nil and the type error reported by cond.
// Note that cond is called before any other conditions are checked; specifically
// cond may be called with (nil, nil) if the type set contains no specific types.
//
// If t is not a type parameter, commonUnder behaves as if t was a type parameter
// with the single type t in its type set.
func commonUnder( Type,  func(,  Type) *typeError) (Type, *typeError) {
	var ,  Type // type and respective common underlying type
	var  *typeError

	 := func( string,  ...any) bool {
		 = typeErrorf(, ...)
		return false
	}

	typeset(, func(,  Type) bool {
		if  != nil {
			if  = (, );  != nil {
				return false
			}
		}

		if  == nil {
			return ("no specific type")
		}

		// If this is the first type we're seeing, we're done.
		if  == nil {
			,  = , 
			return true
		}

		// If we've seen a channel before, and we have a channel now, they must be compatible.
		if ,  := .(*Chan);  != nil {
			if ,  := .(*Chan);  != nil {
				if !Identical(.elem, .elem) {
					return ("channels %s and %s have different element types", , )
				}
				// If we have different channel directions, keep the restricted one
				// and complain if they conflict.
				switch {
				case .dir == .dir:
					// nothing to do
				case .dir == SendRecv:
					,  = ,  // switch to restricted channel
				case .dir != SendRecv:
					return ("channels %s and %s have conflicting directions", , )
				}
				return true
			}
		}

		// Otherwise, the current type must have the same underlying type as all previous types.
		if !Identical(, ) {
			return ("%s and %s have different underlying types", , )
		}

		return true
	})

	if  != nil {
		return nil, 
	}
	return , nil
}