Source file src/go/types/api_predicates.go

     1  // Code generated by "go test -run=Generate -write=all"; DO NOT EDIT.
     2  
     3  // Copyright 2023 The Go Authors. All rights reserved.
     4  // Use of this source code is governed by a BSD-style
     5  // license that can be found in the LICENSE file.
     6  
     7  // This file implements exported type predicates.
     8  
     9  package types
    10  
    11  // AssertableTo reports whether a value of type V can be asserted to have type T.
    12  //
    13  // The behavior of AssertableTo is unspecified in three cases:
    14  //   - if T is Typ[Invalid]
    15  //   - if V is a generalized interface; i.e., an interface that may only be used
    16  //     as a type constraint in Go code
    17  //   - if T is an uninstantiated generic type
    18  func AssertableTo(V *Interface, T Type) bool {
    19  	// Checker.newAssertableTo suppresses errors for invalid types, so we need special
    20  	// handling here.
    21  	if !isValid(T.Underlying()) {
    22  		return false
    23  	}
    24  	return (*Checker)(nil).newAssertableTo(nopos, V, T, nil)
    25  }
    26  
    27  // AssignableTo reports whether a value of type V is assignable to a variable
    28  // of type T.
    29  //
    30  // The behavior of AssignableTo is unspecified if V or T is Typ[Invalid] or an
    31  // uninstantiated generic type.
    32  func AssignableTo(V, T Type) bool {
    33  	x := operand{mode: value, typ: V}
    34  	ok, _ := x.assignableTo(nil, T, nil) // check not needed for non-constant x
    35  	return ok
    36  }
    37  
    38  // ConvertibleTo reports whether a value of type V is convertible to a value of
    39  // type T.
    40  //
    41  // The behavior of ConvertibleTo is unspecified if V or T is Typ[Invalid] or an
    42  // uninstantiated generic type.
    43  func ConvertibleTo(V, T Type) bool {
    44  	x := operand{mode: value, typ: V}
    45  	return x.convertibleTo(nil, T, nil) // check not needed for non-constant x
    46  }
    47  
    48  // Implements reports whether type V implements interface T.
    49  //
    50  // The behavior of Implements is unspecified if V is Typ[Invalid] or an uninstantiated
    51  // generic type.
    52  func Implements(V Type, T *Interface) bool {
    53  	if T.Empty() {
    54  		// All types (even Typ[Invalid]) implement the empty interface.
    55  		return true
    56  	}
    57  	// Checker.implements suppresses errors for invalid types, so we need special
    58  	// handling here.
    59  	if !isValid(V.Underlying()) {
    60  		return false
    61  	}
    62  	return (*Checker)(nil).implements(nopos, V, T, false, nil)
    63  }
    64  
    65  // Satisfies reports whether type V satisfies the constraint T.
    66  //
    67  // The behavior of Satisfies is unspecified if V is Typ[Invalid] or an uninstantiated
    68  // generic type.
    69  func Satisfies(V Type, T *Interface) bool {
    70  	return (*Checker)(nil).implements(nopos, V, T, true, nil)
    71  }
    72  
    73  // Identical reports whether x and y are identical types.
    74  // Receivers of [Signature] types are ignored.
    75  func Identical(x, y Type) bool {
    76  	var c comparer
    77  	return c.identical(x, y, nil)
    78  }
    79  
    80  // IdenticalIgnoreTags reports whether x and y are identical types if tags are ignored.
    81  // Receivers of [Signature] types are ignored.
    82  func IdenticalIgnoreTags(x, y Type) bool {
    83  	var c comparer
    84  	c.ignoreTags = true
    85  	return c.identical(x, y, nil)
    86  }
    87  

View as plain text