Skip to content

Commit f99cd40

Browse files
authored
Rollup merge of #90538 - camelid:doc-recur-ty, r=estebank
Document how recursion is handled for `ty::Ty` Based on this forum discussion: https://internals.rust-lang.org/t/recursive-type-representation-in-rustc/15235/4 cc `@estebank`
2 parents 532d2b1 + 9931782 commit f99cd40

File tree

2 files changed

+25
-1
lines changed

2 files changed

+25
-1
lines changed

compiler/rustc_middle/src/ty/adt.rs

+24
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,30 @@ bitflags! {
6464
/// Moreover, Rust only allows recursive data types through indirection.
6565
///
6666
/// [adt]: https://en.wikipedia.org/wiki/Algebraic_data_type
67+
///
68+
/// # Recursive types
69+
///
70+
/// It may seem impossible to represent recursive types using [`Ty`],
71+
/// since [`TyKind::Adt`] includes [`AdtDef`], which includes its fields,
72+
/// creating a cycle. However, `AdtDef` does not actually include the *types*
73+
/// of its fields; it includes just their [`DefId`]s.
74+
///
75+
/// [`TyKind::Adt`]: ty::TyKind::Adt
76+
///
77+
/// For example, the following type:
78+
///
79+
/// ```
80+
/// struct S { x: Box<S> }
81+
/// ```
82+
///
83+
/// is essentially represented with [`Ty`] as the following pseudocode:
84+
///
85+
/// ```
86+
/// struct S { x }
87+
/// ```
88+
///
89+
/// where `x` here represents the `DefId` of `S.x`. Then, the `DefId`
90+
/// can be used with [`TyCtxt::type_of()`] to get the type of the field.
6791
pub struct AdtDef {
6892
/// The `DefId` of the struct, enum or union item.
6993
pub did: DefId,

compiler/rustc_middle/src/ty/mod.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -1711,7 +1711,7 @@ impl ReprOptions {
17111711

17121712
impl<'tcx> FieldDef {
17131713
/// Returns the type of this field. The resulting type is not normalized. The `subst` is
1714-
/// typically obtained via the second field of `TyKind::AdtDef`.
1714+
/// typically obtained via the second field of [`TyKind::Adt`].
17151715
pub fn ty(&self, tcx: TyCtxt<'tcx>, subst: SubstsRef<'tcx>) -> Ty<'tcx> {
17161716
tcx.type_of(self.did).subst(tcx, subst)
17171717
}

0 commit comments

Comments
 (0)