rustc_middle/traits/
query.rs

1//! Experimental types for the trait query interface. The methods
2//! defined in this module are all based on **canonicalization**,
3//! which makes a canonical query by replacing unbound inference
4//! variables and regions, so that results can be reused more broadly.
5//! The providers for the queries defined here can be found in
6//! `rustc_traits`.
7
8use rustc_macros::{HashStable, TypeFoldable, TypeVisitable};
9use rustc_span::Span;
10
11use crate::error::DropCheckOverflow;
12use crate::infer::canonical::{Canonical, CanonicalQueryInput, QueryResponse};
13pub use crate::traits::solve::NoSolution;
14use crate::ty::{self, GenericArg, Ty, TyCtxt};
15
16pub mod type_op {
17    use rustc_macros::{HashStable, TypeFoldable, TypeVisitable};
18
19    use crate::ty::{Predicate, Ty, UserType};
20
21    #[derive(Copy, Clone, Debug, Hash, PartialEq, Eq, HashStable, TypeFoldable, TypeVisitable)]
22    pub struct AscribeUserType<'tcx> {
23        pub mir_ty: Ty<'tcx>,
24        pub user_ty: UserType<'tcx>,
25    }
26
27    #[derive(Copy, Clone, Debug, Hash, PartialEq, Eq, HashStable, TypeFoldable, TypeVisitable)]
28    pub struct Eq<'tcx> {
29        pub a: Ty<'tcx>,
30        pub b: Ty<'tcx>,
31    }
32
33    #[derive(Copy, Clone, Debug, Hash, PartialEq, Eq, HashStable, TypeFoldable, TypeVisitable)]
34    pub struct Subtype<'tcx> {
35        pub sub: Ty<'tcx>,
36        pub sup: Ty<'tcx>,
37    }
38
39    #[derive(Copy, Clone, Debug, Hash, PartialEq, Eq, HashStable, TypeFoldable, TypeVisitable)]
40    pub struct ProvePredicate<'tcx> {
41        pub predicate: Predicate<'tcx>,
42    }
43
44    /// Normalizes, but not in the new solver.
45    #[derive(Copy, Clone, Debug, Hash, PartialEq, Eq, HashStable, TypeFoldable, TypeVisitable)]
46    pub struct Normalize<T> {
47        pub value: T,
48    }
49
50    /// Normalizes, and deeply normalizes in the new solver.
51    #[derive(Copy, Clone, Debug, Hash, PartialEq, Eq, HashStable, TypeFoldable, TypeVisitable)]
52    pub struct DeeplyNormalize<T> {
53        pub value: T,
54    }
55
56    #[derive(Copy, Clone, Debug, Hash, PartialEq, Eq, HashStable, TypeFoldable, TypeVisitable)]
57    pub struct ImpliedOutlivesBounds<'tcx> {
58        pub ty: Ty<'tcx>,
59    }
60
61    #[derive(Copy, Clone, Debug, Hash, PartialEq, Eq, HashStable, TypeFoldable, TypeVisitable)]
62    pub struct DropckOutlives<'tcx> {
63        pub dropped_ty: Ty<'tcx>,
64    }
65}
66
67pub type CanonicalAliasGoal<'tcx> =
68    CanonicalQueryInput<'tcx, ty::ParamEnvAnd<'tcx, ty::AliasTy<'tcx>>>;
69
70pub type CanonicalTyGoal<'tcx> = CanonicalQueryInput<'tcx, ty::ParamEnvAnd<'tcx, Ty<'tcx>>>;
71
72pub type CanonicalPredicateGoal<'tcx> =
73    CanonicalQueryInput<'tcx, ty::ParamEnvAnd<'tcx, ty::Predicate<'tcx>>>;
74
75pub type CanonicalTypeOpAscribeUserTypeGoal<'tcx> =
76    CanonicalQueryInput<'tcx, ty::ParamEnvAnd<'tcx, type_op::AscribeUserType<'tcx>>>;
77
78pub type CanonicalTypeOpProvePredicateGoal<'tcx> =
79    CanonicalQueryInput<'tcx, ty::ParamEnvAnd<'tcx, type_op::ProvePredicate<'tcx>>>;
80
81pub type CanonicalTypeOpNormalizeGoal<'tcx, T> =
82    CanonicalQueryInput<'tcx, ty::ParamEnvAnd<'tcx, type_op::Normalize<T>>>;
83
84pub type CanonicalTypeOpDeeplyNormalizeGoal<'tcx, T> =
85    CanonicalQueryInput<'tcx, ty::ParamEnvAnd<'tcx, type_op::DeeplyNormalize<T>>>;
86
87pub type CanonicalImpliedOutlivesBoundsGoal<'tcx> =
88    CanonicalQueryInput<'tcx, ty::ParamEnvAnd<'tcx, type_op::ImpliedOutlivesBounds<'tcx>>>;
89
90pub type CanonicalDropckOutlivesGoal<'tcx> =
91    CanonicalQueryInput<'tcx, ty::ParamEnvAnd<'tcx, type_op::DropckOutlives<'tcx>>>;
92
93#[derive(Clone, Debug, Default, HashStable, TypeFoldable, TypeVisitable)]
94pub struct DropckOutlivesResult<'tcx> {
95    pub kinds: Vec<GenericArg<'tcx>>,
96    pub overflows: Vec<Ty<'tcx>>,
97}
98
99impl<'tcx> DropckOutlivesResult<'tcx> {
100    pub fn report_overflows(&self, tcx: TyCtxt<'tcx>, span: Span, ty: Ty<'tcx>) {
101        if let Some(overflow_ty) = self.overflows.get(0) {
102            tcx.dcx().emit_err(DropCheckOverflow { span, ty, overflow_ty: *overflow_ty });
103        }
104    }
105}
106
107/// A set of constraints that need to be satisfied in order for
108/// a type to be valid for destruction.
109#[derive(Clone, Debug, HashStable)]
110pub struct DropckConstraint<'tcx> {
111    /// Types that are required to be alive in order for this
112    /// type to be valid for destruction.
113    pub outlives: Vec<ty::GenericArg<'tcx>>,
114
115    /// Types that could not be resolved: projections and params.
116    pub dtorck_types: Vec<Ty<'tcx>>,
117
118    /// If, during the computation of the dtorck constraint, we
119    /// overflow, that gets recorded here. The caller is expected to
120    /// report an error.
121    pub overflows: Vec<Ty<'tcx>>,
122}
123
124impl<'tcx> DropckConstraint<'tcx> {
125    pub fn empty() -> DropckConstraint<'tcx> {
126        DropckConstraint { outlives: vec![], dtorck_types: vec![], overflows: vec![] }
127    }
128}
129
130impl<'tcx> FromIterator<DropckConstraint<'tcx>> for DropckConstraint<'tcx> {
131    fn from_iter<I: IntoIterator<Item = DropckConstraint<'tcx>>>(iter: I) -> Self {
132        let mut result = Self::empty();
133
134        for DropckConstraint { outlives, dtorck_types, overflows } in iter {
135            result.outlives.extend(outlives);
136            result.dtorck_types.extend(dtorck_types);
137            result.overflows.extend(overflows);
138        }
139
140        result
141    }
142}
143
144#[derive(Debug, HashStable)]
145pub struct CandidateStep<'tcx> {
146    pub self_ty: Canonical<'tcx, QueryResponse<'tcx, Ty<'tcx>>>,
147    pub autoderefs: usize,
148    /// `true` if the type results from a dereference of a raw pointer.
149    /// when assembling candidates, we include these steps, but not when
150    /// picking methods. This so that if we have `foo: *const Foo` and `Foo` has methods
151    /// `fn by_raw_ptr(self: *const Self)` and `fn by_ref(&self)`, then
152    /// `foo.by_raw_ptr()` will work and `foo.by_ref()` won't.
153    pub from_unsafe_deref: bool,
154    pub unsize: bool,
155    /// We will generate CandidateSteps which are reachable via a chain
156    /// of following `Receiver`. The first 'n' of those will be reachable
157    /// by following a chain of 'Deref' instead (since there's a blanket
158    /// implementation of Receiver for Deref).
159    /// We use the entire set of steps when identifying method candidates
160    /// (e.g. identifying relevant `impl` blocks) but only those that are
161    /// reachable via Deref when examining what the receiver type can
162    /// be converted into by autodereffing.
163    pub reachable_via_deref: bool,
164}
165
166#[derive(Copy, Clone, Debug, HashStable)]
167pub struct MethodAutoderefStepsResult<'tcx> {
168    /// The valid autoderef steps that could be found by following a chain
169    /// of `Receiver<Target=T>` or `Deref<Target=T>` trait implementations.
170    pub steps: &'tcx [CandidateStep<'tcx>],
171    /// If Some(T), a type autoderef reported an error on.
172    pub opt_bad_ty: Option<&'tcx MethodAutoderefBadTy<'tcx>>,
173    /// If `true`, `steps` has been truncated due to reaching the
174    /// recursion limit.
175    pub reached_recursion_limit: bool,
176}
177
178#[derive(Debug, HashStable)]
179pub struct MethodAutoderefBadTy<'tcx> {
180    pub reached_raw_pointer: bool,
181    pub ty: Canonical<'tcx, QueryResponse<'tcx, Ty<'tcx>>>,
182}
183
184/// Result of the `normalize_canonicalized_{{,inherent_}projection,weak}_ty` queries.
185#[derive(Clone, Debug, HashStable, TypeFoldable, TypeVisitable)]
186pub struct NormalizationResult<'tcx> {
187    /// Result of the normalization.
188    pub normalized_ty: Ty<'tcx>,
189}
190
191/// Outlives bounds are relationships between generic parameters,
192/// whether they both be regions (`'a: 'b`) or whether types are
193/// involved (`T: 'a`). These relationships can be extracted from the
194/// full set of predicates we understand or also from types (in which
195/// case they are called implied bounds). They are fed to the
196/// `OutlivesEnv` which in turn is supplied to the region checker and
197/// other parts of the inference system.
198#[derive(Copy, Clone, Debug, TypeFoldable, TypeVisitable, HashStable)]
199pub enum OutlivesBound<'tcx> {
200    RegionSubRegion(ty::Region<'tcx>, ty::Region<'tcx>),
201    RegionSubParam(ty::Region<'tcx>, ty::ParamTy),
202    RegionSubAlias(ty::Region<'tcx>, ty::AliasTy<'tcx>),
203}