Source file src/go/types/api_predicates.go

     1  // Code generated by "go test -run=Generate -write=all"; DO NOT EDIT.
     2  // Source: ../../cmd/compile/internal/types2/api_predicates.go
     3  
     4  // Copyright 2023 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  // This file implements exported type predicates.
     9  
    10  package types
    11  
    12  // AssertableTo reports whether a value of type V can be asserted to have type T.
    13  //
    14  // The behavior of AssertableTo is unspecified in three cases:
    15  //   - if T is Typ[Invalid]
    16  //   - if V is a generalized interface; i.e., an interface that may only be used
    17  //     as a type constraint in Go code
    18  //   - if T is an uninstantiated generic type
    19  func AssertableTo(V *Interface, T Type) bool {
    20  	// Checker.newAssertableTo suppresses errors for invalid types, so we need special
    21  	// handling here.
    22  	if !isValid(T.Underlying()) {
    23  		return false
    24  	}
    25  	return (*Checker)(nil).newAssertableTo(nopos, V, T, nil)
    26  }
    27  
    28  // AssignableTo reports whether a value of type V is assignable to a variable
    29  // of type T.
    30  //
    31  // The behavior of AssignableTo is unspecified if V or T is Typ[Invalid] or an
    32  // uninstantiated generic type.
    33  func AssignableTo(V, T Type) bool {
    34  	x := operand{mode: value, typ: V}
    35  	ok, _ := x.assignableTo(nil, T, nil) // check not needed for non-constant x
    36  	return ok
    37  }
    38  
    39  // ConvertibleTo reports whether a value of type V is convertible to a value of
    40  // type T.
    41  //
    42  // The behavior of ConvertibleTo is unspecified if V or T is Typ[Invalid] or an
    43  // uninstantiated generic type.
    44  func ConvertibleTo(V, T Type) bool {
    45  	x := operand{mode: value, typ: V}
    46  	return x.convertibleTo(nil, T, nil) // check not needed for non-constant x
    47  }
    48  
    49  // Implements reports whether type V implements interface T.
    50  //
    51  // The behavior of Implements is unspecified if V is Typ[Invalid] or an uninstantiated
    52  // generic type.
    53  func Implements(V Type, T *Interface) bool {
    54  	if T.Empty() {
    55  		// All types (even Typ[Invalid]) implement the empty interface.
    56  		return true
    57  	}
    58  	// Checker.implements suppresses errors for invalid types, so we need special
    59  	// handling here.
    60  	if !isValid(V.Underlying()) {
    61  		return false
    62  	}
    63  	return (*Checker)(nil).implements(nopos, V, T, false, nil)
    64  }
    65  
    66  // Satisfies reports whether type V satisfies the constraint T.
    67  //
    68  // The behavior of Satisfies is unspecified if V is Typ[Invalid] or an uninstantiated
    69  // generic type.
    70  func Satisfies(V Type, T *Interface) bool {
    71  	return (*Checker)(nil).implements(nopos, V, T, true, nil)
    72  }
    73  
    74  // Identical reports whether x and y are identical types.
    75  // Receivers of [Signature] types are ignored.
    76  //
    77  // Predicates such as [Identical], [Implements], and
    78  // [Satisfies] assume that both operands belong to a
    79  // consistent collection of symbols ([Object] values).
    80  // For example, two [Named] types can be identical only if their
    81  // [Named.Obj] methods return the same [TypeName] symbol.
    82  // A collection of symbols is consistent if, for each logical
    83  // package whose path is P, the creation of those symbols
    84  // involved at most one call to [NewPackage](P, ...).
    85  // To ensure consistency, use a single [Importer] for
    86  // all loaded packages and their dependencies.
    87  // For more information, see https://github.com/golang/go/issues/57497.
    88  func Identical(x, y Type) bool {
    89  	var c comparer
    90  	return c.identical(x, y, nil)
    91  }
    92  
    93  // IdenticalIgnoreTags reports whether x and y are identical types if tags are ignored.
    94  // Receivers of [Signature] types are ignored.
    95  func IdenticalIgnoreTags(x, y Type) bool {
    96  	var c comparer
    97  	c.ignoreTags = true
    98  	return c.identical(x, y, nil)
    99  }
   100  

View as plain text