Skip to main content

hir_ty/
next_solver.rs

1//! Things relevant to the next trait solver.
2
3// Note: in interned types defined in this module, we generally treat the lifetime as advisory
4// and transmute it as needed. This is because no real memory unsafety can be caused from an
5// incorrect lifetime here.
6
7pub mod abi;
8mod allocation;
9mod binder;
10mod consts;
11mod def_id;
12pub mod fold;
13pub mod format_proof_tree;
14pub mod fulfill;
15mod generic_arg;
16pub mod generics;
17pub mod infer;
18pub(crate) mod inspect;
19pub mod interner;
20mod ir_print;
21pub mod normalize;
22pub mod obligation_ctxt;
23mod opaques;
24pub mod predicate;
25mod region;
26mod solver;
27mod structural_normalize;
28mod ty;
29pub mod util;
30
31use std::{mem::ManuallyDrop, sync::OnceLock};
32
33pub use allocation::*;
34pub use binder::*;
35pub use consts::*;
36pub use def_id::*;
37pub use generic_arg::*;
38pub use interner::*;
39pub use opaques::*;
40pub use predicate::*;
41pub use region::*;
42use rustc_type_ir::MayBeErased;
43pub use solver::*;
44pub use ty::*;
45
46pub use crate::lower::ImplTraitIdx;
47pub use rustc_ast_ir::Mutability;
48
49pub type Binder<'db, T> = rustc_type_ir::Binder<DbInterner<'db>, T>;
50pub type EarlyBinder<'db, T> = rustc_type_ir::EarlyBinder<DbInterner<'db>, T>;
51pub type Unnormalized<'db, T> = rustc_type_ir::Unnormalized<DbInterner<'db>, T>;
52pub type Canonical<'db, T> = rustc_type_ir::Canonical<DbInterner<'db>, T>;
53pub type CanonicalVarValues<'db> = rustc_type_ir::CanonicalVarValues<DbInterner<'db>>;
54pub type CanonicalVarKind<'db> = rustc_type_ir::CanonicalVarKind<DbInterner<'db>>;
55pub type CanonicalQueryInput<'db, V> = rustc_type_ir::CanonicalQueryInput<DbInterner<'db>, V>;
56pub type AliasTy<'db> = rustc_type_ir::AliasTy<DbInterner<'db>>;
57pub type FnSig<'db> = rustc_type_ir::FnSig<DbInterner<'db>>;
58pub type PolyFnSig<'db> = Binder<'db, rustc_type_ir::FnSig<DbInterner<'db>>>;
59pub type TypingMode<'db, S = MayBeErased> = rustc_type_ir::TypingMode<DbInterner<'db>, S>;
60pub type TypeError<'db> = rustc_type_ir::error::TypeError<DbInterner<'db>>;
61pub type QueryResult<'db> = rustc_type_ir::solve::QueryResult<DbInterner<'db>>;
62pub type FxIndexMap<K, V> = rustc_type_ir::data_structures::IndexMap<K, V>;
63
64pub struct DefaultTypes<'db> {
65    pub usize: Ty<'db>,
66    pub u8: Ty<'db>,
67    pub u16: Ty<'db>,
68    pub u32: Ty<'db>,
69    pub u64: Ty<'db>,
70    pub u128: Ty<'db>,
71    pub isize: Ty<'db>,
72    pub i8: Ty<'db>,
73    pub i16: Ty<'db>,
74    pub i32: Ty<'db>,
75    pub i64: Ty<'db>,
76    pub i128: Ty<'db>,
77    pub f16: Ty<'db>,
78    pub f32: Ty<'db>,
79    pub f64: Ty<'db>,
80    pub f128: Ty<'db>,
81    pub unit: Ty<'db>,
82    pub bool: Ty<'db>,
83    pub char: Ty<'db>,
84    pub str: Ty<'db>,
85    pub never: Ty<'db>,
86    pub error: Ty<'db>,
87    /// `&'static str`
88    pub static_str_ref: Ty<'db>,
89    /// `[u8]`
90    pub u8_slice: Ty<'db>,
91    /// `&'static [u8]`
92    pub static_u8_slice: Ty<'db>,
93    /// `*mut ()`
94    pub mut_unit_ptr: Ty<'db>,
95    pub dyn_trait_dummy_self: Ty<'db>,
96}
97
98pub struct DefaultConsts<'db> {
99    pub error: Const<'db>,
100    pub u8_values: [Const<'db>; 256],
101}
102
103pub struct DefaultRegions<'db> {
104    pub error: Region<'db>,
105    pub statik: Region<'db>,
106    pub erased: Region<'db>,
107}
108
109pub struct DefaultEmpty<'db> {
110    pub tys: Tys<'db>,
111    pub generic_args: GenericArgs<'db>,
112    pub bound_var_kinds: BoundVarKinds<'db>,
113    pub canonical_vars: CanonicalVarKinds<'db>,
114    pub variances: VariancesOf<'db>,
115    pub pat_list: PatList<'db>,
116    pub predefined_opaques: PredefinedOpaques<'db>,
117    pub def_ids: SolverDefIds<'db>,
118    pub bound_existential_predicates: BoundExistentialPredicates<'db>,
119    pub clauses: Clauses<'db>,
120    pub region_assumptions: RegionAssumptions<'db>,
121    pub consts: Consts<'db>,
122    pub projection: crate::mir::Projection<'db>,
123}
124
125pub struct DefaultAny<'db> {
126    pub types: DefaultTypes<'db>,
127    pub consts: DefaultConsts<'db>,
128    pub regions: DefaultRegions<'db>,
129    pub empty: DefaultEmpty<'db>,
130    /// `[Invariant]`
131    pub one_invariant: VariancesOf<'db>,
132    /// `[Covariant]`
133    pub one_covariant: VariancesOf<'db>,
134    /// `for<'env>`
135    pub coroutine_captures_by_ref_bound_var_kinds: BoundVarKinds<'db>,
136}
137
138impl std::fmt::Debug for DefaultAny<'_> {
139    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
140        f.debug_struct("DefaultAny").finish_non_exhaustive()
141    }
142}
143
144#[inline]
145pub fn default_types<'db>() -> &'db DefaultAny<'db> {
146    static TYPES: OnceLock<DefaultAny<'static>> = OnceLock::new();
147
148    TYPES.get_or_init(|| {
149        let create_ty = |kind| {
150            let ty = Ty::new_without_interner(kind);
151            // We need to increase the refcount (forever), so that the types won't be freed.
152            let ty = ManuallyDrop::new(ty.store());
153            ty.as_ref()
154        };
155        let create_const = |kind| {
156            let ty = Const::new_without_interner(kind);
157            // We need to increase the refcount (forever), so that the types won't be freed.
158            let ty = ManuallyDrop::new(ty.store());
159            ty.as_ref()
160        };
161        let create_region = |kind| {
162            let ty = Region::new_without_interner(kind);
163            // We need to increase the refcount (forever), so that the types won't be freed.
164            let ty = ManuallyDrop::new(ty.store());
165            ty.as_ref()
166        };
167        let create_generic_args = |slice| {
168            let ty = GenericArgs::new_from_slice(slice);
169            // We need to increase the refcount (forever), so that the types won't be freed.
170            let ty = ManuallyDrop::new(ty.store());
171            ty.as_ref()
172        };
173        let create_bound_var_kinds = |slice| {
174            let ty = BoundVarKinds::new_from_slice(slice);
175            // We need to increase the refcount (forever), so that the types won't be freed.
176            let ty = ManuallyDrop::new(ty.store());
177            ty.as_ref()
178        };
179        let create_canonical_vars = |slice| {
180            let ty = CanonicalVarKinds::new_from_slice(slice);
181            // We need to increase the refcount (forever), so that the types won't be freed.
182            let ty = ManuallyDrop::new(ty.store());
183            ty.as_ref()
184        };
185        let create_variances_of = |slice| {
186            let ty = VariancesOf::new_from_slice(slice);
187            // We need to increase the refcount (forever), so that the types won't be freed.
188            let ty = ManuallyDrop::new(ty.store());
189            ty.as_ref()
190        };
191        let create_pat_list = |slice| {
192            let ty = PatList::new_from_slice(slice);
193            // We need to increase the refcount (forever), so that the types won't be freed.
194            let ty = ManuallyDrop::new(ty.store());
195            ty.as_ref()
196        };
197        let create_predefined_opaques = |slice| {
198            let ty = PredefinedOpaques::new_from_slice(slice);
199            // We need to increase the refcount (forever), so that the types won't be freed.
200            let ty = ManuallyDrop::new(ty.store());
201            ty.as_ref()
202        };
203        let create_solver_def_ids = |slice| {
204            let ty = SolverDefIds::new_from_slice(slice);
205            // We need to increase the refcount (forever), so that the types won't be freed.
206            let ty = ManuallyDrop::new(ty.store());
207            ty.as_ref()
208        };
209        let create_bound_existential_predicates = |slice| {
210            let ty = BoundExistentialPredicates::new_from_slice(slice);
211            // We need to increase the refcount (forever), so that the types won't be freed.
212            let ty = ManuallyDrop::new(ty.store());
213            ty.as_ref()
214        };
215        let create_clauses = |slice| {
216            let ty = Clauses::new_from_slice(slice);
217            // We need to increase the refcount (forever), so that the types won't be freed.
218            let ty = ManuallyDrop::new(ty.store());
219            ty.as_ref()
220        };
221        let create_region_assumptions = |slice| {
222            let ty = RegionAssumptions::new_from_slice(slice);
223            // We need to increase the refcount (forever), so that the types won't be freed.
224            let ty = ManuallyDrop::new(ty.store());
225            ty.as_ref()
226        };
227        let create_tys = |slice| {
228            let ty = Tys::new_from_slice(slice);
229            // We need to increase the refcount (forever), so that the types won't be freed.
230            let ty = ManuallyDrop::new(ty.store());
231            ty.as_ref()
232        };
233        let create_consts = |slice| {
234            let ty = Consts::new_from_slice(slice);
235            // We need to increase the refcount (forever), so that the types won't be freed.
236            let ty = ManuallyDrop::new(ty.store());
237            ty.as_ref()
238        };
239        let create_projection = |slice| {
240            let it = crate::mir::Projection::new_from_slice(slice);
241            // We need to increase the refcount (forever), so that the types won't be freed.
242            let it = ManuallyDrop::new(it.store());
243            it.as_ref()
244        };
245
246        let str = create_ty(TyKind::Str);
247        let statik = create_region(RegionKind::ReStatic);
248        let empty_tys = create_tys(&[]);
249        let unit = create_ty(TyKind::Tuple(empty_tys));
250        let u8 = create_ty(TyKind::Uint(rustc_ast_ir::UintTy::U8));
251        let u8_slice = create_ty(TyKind::Slice(u8));
252        let static_u8_slice = create_ty(TyKind::Ref(statik, u8_slice, Mutability::Not));
253        DefaultAny {
254            types: DefaultTypes {
255                usize: create_ty(TyKind::Uint(rustc_ast_ir::UintTy::Usize)),
256                u8,
257                u16: create_ty(TyKind::Uint(rustc_ast_ir::UintTy::U16)),
258                u32: create_ty(TyKind::Uint(rustc_ast_ir::UintTy::U32)),
259                u64: create_ty(TyKind::Uint(rustc_ast_ir::UintTy::U64)),
260                u128: create_ty(TyKind::Uint(rustc_ast_ir::UintTy::U128)),
261                isize: create_ty(TyKind::Int(rustc_ast_ir::IntTy::Isize)),
262                i8: create_ty(TyKind::Int(rustc_ast_ir::IntTy::I8)),
263                i16: create_ty(TyKind::Int(rustc_ast_ir::IntTy::I16)),
264                i32: create_ty(TyKind::Int(rustc_ast_ir::IntTy::I32)),
265                i64: create_ty(TyKind::Int(rustc_ast_ir::IntTy::I64)),
266                i128: create_ty(TyKind::Int(rustc_ast_ir::IntTy::I128)),
267                f16: create_ty(TyKind::Float(rustc_ast_ir::FloatTy::F16)),
268                f32: create_ty(TyKind::Float(rustc_ast_ir::FloatTy::F32)),
269                f64: create_ty(TyKind::Float(rustc_ast_ir::FloatTy::F64)),
270                f128: create_ty(TyKind::Float(rustc_ast_ir::FloatTy::F128)),
271                unit,
272                bool: create_ty(TyKind::Bool),
273                char: create_ty(TyKind::Char),
274                str,
275                never: create_ty(TyKind::Never),
276                error: create_ty(TyKind::Error(ErrorGuaranteed)),
277                static_str_ref: create_ty(TyKind::Ref(statik, str, rustc_ast_ir::Mutability::Not)),
278                u8_slice,
279                static_u8_slice,
280                mut_unit_ptr: create_ty(TyKind::RawPtr(unit, rustc_ast_ir::Mutability::Mut)),
281                // This type must not appear anywhere except here.
282                dyn_trait_dummy_self: create_ty(TyKind::Infer(rustc_type_ir::InferTy::FreshTy(0))),
283            },
284            consts: DefaultConsts {
285                error: create_const(ConstKind::Error(ErrorGuaranteed)),
286                u8_values: std::array::from_fn(|u8_value| {
287                    create_const(ConstKind::Value(ValueConst {
288                        ty: u8,
289                        value: ValTree::new(ValTreeKind::Leaf(ScalarInt::from(u8_value as u8))),
290                    }))
291                }),
292            },
293            regions: DefaultRegions {
294                error: create_region(RegionKind::ReError(ErrorGuaranteed)),
295                statik,
296                erased: create_region(RegionKind::ReErased),
297            },
298            empty: DefaultEmpty {
299                tys: empty_tys,
300                generic_args: create_generic_args(&[]),
301                bound_var_kinds: create_bound_var_kinds(&[]),
302                canonical_vars: create_canonical_vars(&[]),
303                variances: create_variances_of(&[]),
304                pat_list: create_pat_list(&[]),
305                predefined_opaques: create_predefined_opaques(&[]),
306                def_ids: create_solver_def_ids(&[]),
307                bound_existential_predicates: create_bound_existential_predicates(&[]),
308                clauses: create_clauses(&[]),
309                region_assumptions: create_region_assumptions(&[]),
310                consts: create_consts(&[]),
311                projection: create_projection(&[]),
312            },
313            one_invariant: create_variances_of(&[rustc_type_ir::Variance::Invariant]),
314            one_covariant: create_variances_of(&[rustc_type_ir::Variance::Covariant]),
315            coroutine_captures_by_ref_bound_var_kinds: create_bound_var_kinds(&[
316                BoundVariableKind::Region(BoundRegionKind::ClosureEnv),
317            ]),
318        }
319    })
320}