Skip to content

Commit b2c699f

Browse files
committed
Enhance docs
1 parent fc1b85c commit b2c699f

2 files changed

Lines changed: 39 additions & 13 deletions

File tree

src/refine/template.rs

Lines changed: 25 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ where
2323
}
2424
}
2525

26+
/// [`TemplateScope`] with no variables in scope.
2627
#[derive(Clone, Default)]
2728
pub struct EmptyTemplateScope;
2829

@@ -58,43 +59,45 @@ where
5859
}
5960
}
6061

62+
/// Translates [`mir_ty::Ty`] to [`rty::Type`].
63+
///
64+
/// This struct implements a translation from Rust MIR types to Thrust types.
65+
/// Thrust types may contain refinement predicates which do not exist in MIR types,
66+
/// and [`TypeBuilder`] solely builds types with null refinement (true) in
67+
/// [`TypeBuilder::build`]. This also provides [`TypeBuilder::for_template`] to build
68+
/// refinement types by filling unknown predicates with templates with predicate variables.
6169
#[derive(Clone)]
6270
pub struct TypeBuilder<'tcx> {
6371
tcx: mir_ty::TyCtxt<'tcx>,
64-
type_param_mapping: HashMap<u32, rty::TypeParamIdx>,
72+
/// Maps index in [`mir_ty::ParamTy`] to [`rty::TypeParamIdx`].
73+
/// These indices may differ because we skip lifetime parameters.
74+
/// See [`rty::TypeParamIdx`] for more details.
75+
param_idx_mapping: HashMap<u32, rty::TypeParamIdx>,
6576
}
6677

6778
impl<'tcx> TypeBuilder<'tcx> {
6879
pub fn new(tcx: mir_ty::TyCtxt<'tcx>, def_id: DefId) -> Self {
69-
// The index of TyKind::ParamTy is based on the every generic parameters in
70-
// the definition, including lifetimes. Given the following definition:
71-
//
72-
// struct X<'a, T> { f: &'a T }
73-
//
74-
// The type of field `f` is &T1 (not T0). However, in Thrust, we ignore lifetime
75-
// parameters and the index of rty::ParamType is based on type parameters only.
76-
// We're building a mapping from the original index to the new index here.
7780
let generics = tcx.generics_of(def_id);
78-
let mut type_param_mapping: HashMap<u32, rty::TypeParamIdx> = Default::default();
81+
let mut param_idx_mapping: HashMap<u32, rty::TypeParamIdx> = Default::default();
7982
for i in 0..generics.count() {
8083
let generic_param = generics.param_at(i, tcx);
8184
match generic_param.kind {
8285
mir_ty::GenericParamDefKind::Lifetime => {}
8386
mir_ty::GenericParamDefKind::Type { .. } => {
84-
type_param_mapping.insert(i as u32, type_param_mapping.len().into());
87+
param_idx_mapping.insert(i as u32, param_idx_mapping.len().into());
8588
}
8689
mir_ty::GenericParamDefKind::Const { .. } => unimplemented!(),
8790
}
8891
}
8992
Self {
9093
tcx,
91-
type_param_mapping,
94+
param_idx_mapping,
9295
}
9396
}
9497

9598
fn translate_param_type(&self, ty: &mir_ty::ParamTy) -> rty::ParamType {
9699
let index = *self
97-
.type_param_mapping
100+
.param_idx_mapping
98101
.get(&ty.index)
99102
.expect("unknown type param idx");
100103
rty::ParamType::new(index)
@@ -198,8 +201,16 @@ impl<'tcx> TypeBuilder<'tcx> {
198201
}
199202
}
200203

204+
/// Translates [`mir_ty::Ty`] to [`rty::Type`] using templates for refinements.
205+
///
206+
/// [`rty::Template`] is a refinement type in the form of `{ T | P(x1, ..., xn) }` where `P` is a
207+
/// predicate variable. When constructing a template, we need to know which variables can affect the
208+
/// predicate of the template (dependencies, `x1, ..., xn`), and they are provided by the
209+
/// [`TemplateScope`]. No variables are in scope by default and you can provide a scope using
210+
/// [`TemplateTypeBuilder::with_scope`].
201211
pub struct TemplateTypeBuilder<'tcx, 'a, R, S> {
202212
inner: TypeBuilder<'tcx>,
213+
// XXX: this can't be simply `R` because monomorphization instantiates types recursively
203214
registry: &'a mut R,
204215
scope: S,
205216
}
@@ -310,6 +321,7 @@ where
310321
}
311322
}
312323

324+
/// A builder for function template types.
313325
pub struct FunctionTemplateTypeBuilder<'tcx, 'a, R> {
314326
inner: TypeBuilder<'tcx>,
315327
registry: &'a mut R,

src/rty/params.rs

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,20 @@ use super::{Closed, RefinedType};
1111

1212
rustc_index::newtype_index! {
1313
/// An index representing a type parameter.
14+
///
15+
/// ## Note on indexing of type parameters
16+
///
17+
/// The index of [`rustc_middle::ty::ParamTy`] is based on the every generic parameters in
18+
/// the definition, including lifetimes. Given the following definition:
19+
///
20+
/// ```rust
21+
/// struct X<'a, T> { f: &'a T }
22+
/// ```
23+
///
24+
/// The type of field `f` is `&T1` (not `&T0`) in MIR. However, in Thrust, we ignore lifetime
25+
/// parameters and the index of [`rty::ParamType`](super::ParamType) is based on type parameters only, giving `f`
26+
/// the type `&T0`. [`TypeBuilder`](crate::refine::TypeBuilder) takes care of this difference when translating MIR
27+
/// types to Thrust types.
1428
#[orderable]
1529
#[debug_format = "T{}"]
1630
pub struct TypeParamIdx { }

0 commit comments

Comments
 (0)