Struct rustc_middle::ty::Ty

source ·
pub struct Ty<'tcx>(Interned<'tcx, WithCachedTypeInfo<TyKind<'tcx>>>);
Expand description

Use this rather than TyKind, whenever possible.

Tuple Fields§

§0: Interned<'tcx, WithCachedTypeInfo<TyKind<'tcx>>>

Implementations§

Checks whether a type is visibly uninhabited from a particular module.

Example
#![feature(never_type)]
enum Void {}
mod a {
    pub mod b {
        pub struct SecretlyUninhabited {
            _priv: !,
        }
    }
}

mod c {
    use super::Void;
    pub struct AlsoSecretlyUninhabited {
        _priv: Void,
    }
    mod d {
    }
}

struct Foo {
    x: a::b::SecretlyUninhabited,
    y: c::AlsoSecretlyUninhabited,
}

In this code, the type Foo will only be visibly uninhabited inside the modules b, c and d. This effects pattern-matching on Foo or types that contain Foo.

Example
let foo_result: Result<T, Foo> = ... ;
let Ok(t) = foo_result;

This code should only compile in modules where the uninhabitedness of Foo is visible.

Returns true if the type is uninhabited without regard to visibility

Returns the maximum value for the given numeric type (including chars) or returns None if the type is not numeric.

Returns the minimum value for the given numeric type (including chars) or returns None if the type is not numeric.

Checks whether values of this type T are moved or copied when referenced – this amounts to a check for whether T: Copy, but note that we don’t consider lifetimes when doing this check. This means that we may generate MIR which does copies even when the type actually doesn’t satisfy the full requirements for the Copy trait (cc #29149) – this winds up being reported as an error during NLL borrow check.

Checks whether values of this type T have a size known at compile time (i.e., whether T: Sized). Lifetimes are ignored for the purposes of this check, so it can be an over-approximation in generic contexts, where one can have strange rules like <T as Foo<'static>>::Bar: Sized that actually carry lifetime requirements.

Checks whether values of this type T implement the Freeze trait – frozen types are those that do not contain an UnsafeCell anywhere. This is a language concept used to distinguish “true immutability”, which is relevant to optimization as well as the rules around static values. Note that the Freeze trait is not exposed to end users and is effectively an implementation detail.

Fast path helper for testing if a type is Freeze.

Returning true means the type is known to be Freeze. Returning false means nothing – could be Freeze, might not be.

Checks whether values of this type T implement the Unpin trait.

Fast path helper for testing if a type is Unpin.

Returning true means the type is known to be Unpin. Returning false means nothing – could be Unpin, might not be.

If ty.needs_drop(...) returns true, then ty is definitely non-copy and might have a destructor attached; if it returns false, then ty definitely has no destructor (i.e., no drop glue).

(Note that this implies that if ty has a destructor attached, then needs_drop will definitely return true for ty.)

Note that this method is used to check eligible types in unions.

Checks if ty has a significant drop.

Note that this method can return false even if ty has a destructor attached; even if that is the case then the adt has been marked with the attribute rustc_insignificant_dtor.

Note that this method is used to check for change in drop order for 2229 drop reorder migration analysis.

Returns true if equality for this type is both reflexive and structural.

Reflexive equality for a type is indicated by an Eq impl for that type.

Primitive types (u32, str) have structural equality by definition. For composite data types, equality for the type as a whole is structural when it is the same as equality between all components (fields, array elements, etc.) of that type. For ADTs, structural equality is indicated by an implementation of PartialStructuralEq and StructuralEq for that type.

This function is “shallow” because it may return true for a composite type whose fields are not StructuralEq. For example, [T; 4] has structural equality regardless of T because equality for arrays is determined by the equality of each array element. If you want to know whether a given call to PartialEq::eq will proceed structurally all the way down, you will need to use a type visitor.

Peel off all reference types in this type until there are none left.

This method is idempotent, i.e. ty.peel_refs().peel_refs() == ty.peel_refs().

Examples
  • u8 -> u8
  • &'a mut u8 -> u8
  • &'a &'b u8 -> u8
  • &'a *const &'b u8 -> *const &'b u8

Iterator that walks self and any types reachable from self, in depth-first order. Note that just walks the types that appear in self, it does not descend into the fields of structs or variants. For example:

isize => { isize }
Foo<Bar<isize>> => { Foo<Bar<isize>>, Bar<isize>, isize }
[isize] => { [isize], isize }

Similar to Ty::is_primitive, but also considers inferred numeric values to be primitive.

Whether the type is succinctly representable as a type instead of just referred to with a description in error messages. This is used in the main error message.

Whether the type is succinctly representable as a type instead of just referred to with a description in error messages. This is used in the primary span label. Beyond what is_simple_ty includes, it also accepts ADTs with no type arguments and references to ADTs with no type arguments.

Type utilities

Returns true if this type is a str.

Get the mutability of the reference or None when not a reference

Tests if this is any kind of primitive pointer type (reference, raw pointer, fn pointer).

Panics if called on any type other than Box<T>.

A scalar type is one that denotes an atomic datum, with no sub-components. (A RawPtr is scalar because it represents a non-managed pointer, so its contents are abstract to rustc.)

Returns true if this type is a floating point type.

Checks whether a type recursively contains another type

Example: Option<()> contains ()

Returns the type and mutability of *ty.

The parameter explicit indicates if this is an explicit dereference. Some types – notably unsafe ptrs – can only be dereferenced explicitly.

Returns the type of ty[i].

Iterates over tuple fields. Panics when called on anything but a tuple.

If the type contains variants, returns the valid range of variant indices.

If the type contains variants, returns the variant for variant_index. Panics if variant_index is out of range.

Returns the type of the discriminant of this type.

Returns the type of metadata for (potentially fat) pointers to this type, and a boolean signifying if this is conditional on this type being Sized.

When we create a closure, we record its kind (i.e., what trait it implements) into its ClosureSubsts using a type parameter. This is kind of a phantom type, except that the most convenient thing for us to are the integral types. This function converts such a special type into the closure kind. To go the other way, use closure_kind.to_ty(tcx).

Note that during type checking, we use an inference variable to represent the closure kind, because it has not yet been inferred. Once upvar inference (in rustc_hir_analysis/src/check/upvar.rs) is complete, that type variable will be unified.

Fast path helper for testing if a type is Sized.

Returning true means the type is known to be sized. Returning false means nothing – could be sized, might not be.

Note that we could never rely on the fact that a type such as [_] is trivially !Sized because we could be in a type environment with a bound such as [_]: Copy. A function with such a bound obviously never can be called, but that doesn’t mean it shouldn’t typecheck. This is why this method doesn’t return Option<bool>.

Fast path helper for primitives which are always Copy and which have a side-effect-free Clone impl.

Returning true means the type is known to be pure and Copy+Clone. Returning false means nothing – could be Copy, might not be.

This is mostly useful for optimizations, as there are the types on which we can replace cloning with dereferencing.

If self is a primitive, return its Symbol.

Trait Implementations§

Returns a copy of the value. Read more
Performs copy-assignment from source. Read more
Formats the value using the given formatter. Read more
Formats the value using the given formatter. Read more
Converts to this type from the input type.
Converts to this type from the input type.
Feeds this value into the given Hasher. Read more
Feeds a slice of this type into the given Hasher. Read more
Given an instance of this key, what crate is it referring to? This is used to find the provider. Read more
In the event that a cycle occurs, if no explicit span has been given for a query with key self, what span should we use? Read more
If the key is a DefId or DefId–equivalent, return that DefId. Otherwise, return None. Read more
This method returns an Ordering between self and other. Read more
Compares and returns the maximum of two values. Read more
Compares and returns the minimum of two values. Read more
Restrict a value to a certain interval. Read more
This method tests for self and other values to be equal, and is used by ==. Read more
This method tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason. Read more
This method returns an ordering between self and other values if one exists. Read more
This method tests less than (for self and other) and is used by the < operator. Read more
This method tests less than or equal to (for self and other) and is used by the <= operator. Read more
This method tests greater than (for self and other) and is used by the > operator. Read more
This method tests greater than or equal to (for self and other) and is used by the >= operator. Read more
The entry point for folding. To fold a value t with a folder f call: t.try_fold_with(f). Read more
A convenient alternative to try_fold_with for use with infallible folders. Do not override this method, to ensure coherence with try_fold_with. Read more
Provides a default fold for a type of interest. This should only be called within TypeFolder methods, when a non-custom traversal is desired for the value of the type of interest passed to that method. For example, in MyFolder::try_fold_ty(ty), it is valid to call ty.try_super_fold_with(self), but any other folding should be done with xyz.try_fold_with(self). Read more
A convenient alternative to try_super_fold_with for use with infallible folders. Do not override this method, to ensure coherence with try_super_fold_with. Read more
Provides a default visit for a type of interest. This should only be called within TypeVisitor methods, when a non-custom traversal is desired for the value of the type of interest passed to that method. For example, in MyVisitor::visit_ty(ty), it is valid to call ty.super_visit_with(self), but any other visiting should be done with xyz.visit_with(self). Read more
The entry point for visiting. To visit a value t with a visitor v call: t.visit_with(v). Read more
Returns true if self has any late-bound regions that are either bound by binder or bound by some binder outside of binder. If binder is ty::INNERMOST, this indicates whether there are any late-bound regions that appear free. Read more
Returns true if this type has any regions that escape binder (and hence are not bound by it). Read more
Return true if this type has regions that are not a part of the type. For example, for<'a> fn(&'a i32) return false, while fn(&'a i32) would return true. The latter can occur when traversing through the former. Read more
“Free” regions in this context means that it has any region that is not (a) erased or (b) late-bound. Read more
True if there are any un-erased free regions.
Indicates whether this value references only ‘global’ generic parameters that are the same regardless of what fn we are in. This is used for caching. Read more
True if there are any late-bound regions
Indicates whether this value still has parameters/placeholders/inference variables which could be replaced later, in a way that would change the results of impl specialization. Read more

Auto Trait Implementations§

Blanket Implementations§

Gets the TypeId of self. Read more
Immutably borrows from an owned value. Read more
Mutably borrows from an owned value. Read more
This method turns the parameters of a DepNodeConstructor into an opaque Fingerprint to be used in DepNode. Not all DepNodeParams support being turned into a Fingerprint (they don’t need to if the corresponding DepNode is anonymous). Read more
This method tries to recover the query key from the given DepNode, something which is needed when forcing DepNodes during red-green evaluation. The query system will only call this method if fingerprint_style() is not FingerprintStyle::Opaque. It is always valid to return None here, in which case incremental compilation will treat the query as having changed instead of forcing it. Read more

Returns the argument unchanged.

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

The resulting type after obtaining ownership.
Creates owned data from borrowed data, usually by cloning. Read more
Uses borrowed data to replace owned data, usually by cloning. Read more
Converts the given value to a String. Read more
The type returned in the event of a conversion error.
Performs the conversion.
The type returned in the event of a conversion error.
Performs the conversion.

Layout§

Note: Most layout information is completely unstable and may even differ between compilations. The only exception is types with certain repr(...) attributes. Please see the Rust Reference’s “Type Layout” chapter for details on type layout guarantees.

Size: 8 bytes