rustc_middle/mir/
query.rs

1//! Values computed by queries that use MIR.
2
3use std::fmt::{self, Debug};
4
5use rustc_abi::{FieldIdx, VariantIdx};
6use rustc_data_structures::fx::FxIndexMap;
7use rustc_errors::ErrorGuaranteed;
8use rustc_hir::def_id::LocalDefId;
9use rustc_index::IndexVec;
10use rustc_index::bit_set::BitMatrix;
11use rustc_macros::{HashStable, TyDecodable, TyEncodable, TypeFoldable, TypeVisitable};
12use rustc_span::{Span, Symbol};
13
14use super::{ConstValue, SourceInfo};
15use crate::ty::{self, CoroutineArgsExt, OpaqueHiddenType, Ty};
16
17rustc_index::newtype_index! {
18    #[derive(HashStable)]
19    #[encodable]
20    #[debug_format = "_{}"]
21    pub struct CoroutineSavedLocal {}
22}
23
24#[derive(Clone, Debug, PartialEq, Eq)]
25#[derive(TyEncodable, TyDecodable, HashStable, TypeFoldable, TypeVisitable)]
26pub struct CoroutineSavedTy<'tcx> {
27    pub ty: Ty<'tcx>,
28    /// Source info corresponding to the local in the original MIR body.
29    pub source_info: SourceInfo,
30    /// Whether the local should be ignored for trait bound computations.
31    pub ignore_for_traits: bool,
32}
33
34/// The layout of coroutine state.
35#[derive(Clone, PartialEq, Eq)]
36#[derive(TyEncodable, TyDecodable, HashStable, TypeFoldable, TypeVisitable)]
37pub struct CoroutineLayout<'tcx> {
38    /// The type of every local stored inside the coroutine.
39    pub field_tys: IndexVec<CoroutineSavedLocal, CoroutineSavedTy<'tcx>>,
40
41    /// The name for debuginfo.
42    pub field_names: IndexVec<CoroutineSavedLocal, Option<Symbol>>,
43
44    /// Which of the above fields are in each variant. Note that one field may
45    /// be stored in multiple variants.
46    pub variant_fields: IndexVec<VariantIdx, IndexVec<FieldIdx, CoroutineSavedLocal>>,
47
48    /// The source that led to each variant being created (usually, a yield or
49    /// await).
50    pub variant_source_info: IndexVec<VariantIdx, SourceInfo>,
51
52    /// Which saved locals are storage-live at the same time. Locals that do not
53    /// have conflicts with each other are allowed to overlap in the computed
54    /// layout.
55    #[type_foldable(identity)]
56    #[type_visitable(ignore)]
57    pub storage_conflicts: BitMatrix<CoroutineSavedLocal, CoroutineSavedLocal>,
58}
59
60impl Debug for CoroutineLayout<'_> {
61    fn fmt(&self, fmt: &mut fmt::Formatter<'_>) -> fmt::Result {
62        fmt.debug_struct("CoroutineLayout")
63            .field_with("field_tys", |fmt| {
64                fmt.debug_map().entries(self.field_tys.iter_enumerated()).finish()
65            })
66            .field_with("variant_fields", |fmt| {
67                let mut map = fmt.debug_map();
68                for (idx, fields) in self.variant_fields.iter_enumerated() {
69                    map.key_with(|fmt| {
70                        let variant_name = ty::CoroutineArgs::variant_name(idx);
71                        if fmt.alternate() {
72                            write!(fmt, "{variant_name:9}({idx:?})")
73                        } else {
74                            write!(fmt, "{variant_name}")
75                        }
76                    });
77                    // Force variant fields to print in regular mode instead of alternate mode.
78                    map.value_with(|fmt| write!(fmt, "{fields:?}"));
79                }
80                map.finish()
81            })
82            .field("storage_conflicts", &self.storage_conflicts)
83            .finish()
84    }
85}
86
87/// All the opaque types that are restricted to concrete types
88/// by this function. Unlike the value in `TypeckResults`, this has
89/// unerased regions.
90#[derive(Default, Debug, TyEncodable, TyDecodable, HashStable)]
91pub struct ConcreteOpaqueTypes<'tcx>(pub FxIndexMap<LocalDefId, OpaqueHiddenType<'tcx>>);
92
93/// The result of the `mir_const_qualif` query.
94///
95/// Each field (except `tainted_by_errors`) corresponds to an implementer of the `Qualif` trait in
96/// `rustc_const_eval/src/transform/check_consts/qualifs.rs`. See that file for more information on each
97/// `Qualif`.
98#[derive(Clone, Copy, Debug, Default, TyEncodable, TyDecodable, HashStable)]
99pub struct ConstQualifs {
100    pub has_mut_interior: bool,
101    pub needs_drop: bool,
102    pub needs_non_const_drop: bool,
103    pub tainted_by_errors: Option<ErrorGuaranteed>,
104}
105/// Outlives-constraints can be categorized to determine whether and why they
106/// are interesting (for error reporting). Order of variants indicates sort
107/// order of the category, thereby influencing diagnostic output.
108///
109/// See also `rustc_const_eval::borrow_check::constraints`.
110#[derive(Copy, Clone, Debug, Eq, PartialEq, Hash)]
111#[derive(TyEncodable, TyDecodable, HashStable, TypeVisitable, TypeFoldable)]
112pub enum ConstraintCategory<'tcx> {
113    Return(ReturnConstraint),
114    Yield,
115    UseAsConst,
116    UseAsStatic,
117    TypeAnnotation(AnnotationSource),
118    Cast {
119        /// Whether this cast is a coercion that was automatically inserted by the compiler.
120        is_implicit_coercion: bool,
121        /// Whether this is an unsizing coercion and if yes, this contains the target type.
122        /// Region variables are erased to ReErased.
123        unsize_to: Option<Ty<'tcx>>,
124    },
125
126    /// Contains the function type if available.
127    CallArgument(Option<Ty<'tcx>>),
128    CopyBound,
129    SizedBound,
130    Assignment,
131    /// A constraint that came from a usage of a variable (e.g. in an ADT expression
132    /// like `Foo { field: my_val }`)
133    Usage,
134    OpaqueType,
135    ClosureUpvar(FieldIdx),
136
137    /// A constraint from a user-written predicate
138    /// with the provided span, written on the item
139    /// with the given `DefId`
140    Predicate(Span),
141
142    /// A "boring" constraint (caused by the given location) is one that
143    /// the user probably doesn't want to see described in diagnostics,
144    /// because it is kind of an artifact of the type system setup.
145    Boring,
146    // Boring and applicable everywhere.
147    BoringNoLocation,
148
149    /// A constraint that doesn't correspond to anything the user sees.
150    Internal,
151
152    /// An internal constraint derived from an illegal universe relation.
153    IllegalUniverse,
154}
155
156#[derive(Copy, Clone, Debug, Eq, PartialEq, Hash)]
157#[derive(TyEncodable, TyDecodable, HashStable, TypeVisitable, TypeFoldable)]
158pub enum ReturnConstraint {
159    Normal,
160    ClosureUpvar(FieldIdx),
161}
162
163#[derive(Copy, Clone, Debug, Eq, PartialEq, Hash)]
164#[derive(TyEncodable, TyDecodable, HashStable, TypeVisitable, TypeFoldable)]
165pub enum AnnotationSource {
166    Ascription,
167    Declaration,
168    OpaqueCast,
169    GenericArg,
170}
171
172/// The constituent parts of a mir constant of kind ADT or array.
173#[derive(Copy, Clone, Debug, HashStable)]
174pub struct DestructuredConstant<'tcx> {
175    pub variant: Option<VariantIdx>,
176    pub fields: &'tcx [(ConstValue<'tcx>, Ty<'tcx>)],
177}
OSZAR »