diff options
| author | Matthias Krüger <matthias.krueger@famsik.de> | 2021-12-04 02:26:20 +0100 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2021-12-04 02:26:20 +0100 |
| commit | f99cd4022a0f0974f183f8459c9dda86568a90a8 (patch) | |
| tree | 35b3f6982099b77ed6e1af39ec8bacc5372f0c5a | |
| parent | 532d2b14c05f9bc20b2d27cbb5f4550d28343a36 (diff) | |
| parent | 9931782a38ad717961c9dd9d92934b3504526d84 (diff) | |
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`
| -rw-r--r-- | compiler/rustc_middle/src/ty/adt.rs | 24 | ||||
| -rw-r--r-- | compiler/rustc_middle/src/ty/mod.rs | 2 |
2 files changed, 25 insertions, 1 deletions
diff --git a/compiler/rustc_middle/src/ty/adt.rs b/compiler/rustc_middle/src/ty/adt.rs index 69eb73b4255..771ce2eb884 100644 --- a/compiler/rustc_middle/src/ty/adt.rs +++ b/compiler/rustc_middle/src/ty/adt.rs @@ -64,6 +64,30 @@ bitflags! { /// Moreover, Rust only allows recursive data types through indirection. /// /// [adt]: https://en.wikipedia.org/wiki/Algebraic_data_type +/// +/// # Recursive types +/// +/// It may seem impossible to represent recursive types using [`Ty`], +/// since [`TyKind::Adt`] includes [`AdtDef`], which includes its fields, +/// creating a cycle. However, `AdtDef` does not actually include the *types* +/// of its fields; it includes just their [`DefId`]s. +/// +/// [`TyKind::Adt`]: ty::TyKind::Adt +/// +/// For example, the following type: +/// +/// ``` +/// struct S { x: Box<S> } +/// ``` +/// +/// is essentially represented with [`Ty`] as the following pseudocode: +/// +/// ``` +/// struct S { x } +/// ``` +/// +/// where `x` here represents the `DefId` of `S.x`. Then, the `DefId` +/// can be used with [`TyCtxt::type_of()`] to get the type of the field. pub struct AdtDef { /// The `DefId` of the struct, enum or union item. pub did: DefId, diff --git a/compiler/rustc_middle/src/ty/mod.rs b/compiler/rustc_middle/src/ty/mod.rs index 5070e1565b3..7e1804673df 100644 --- a/compiler/rustc_middle/src/ty/mod.rs +++ b/compiler/rustc_middle/src/ty/mod.rs @@ -1711,7 +1711,7 @@ impl ReprOptions { impl<'tcx> FieldDef { /// Returns the type of this field. The resulting type is not normalized. The `subst` is - /// typically obtained via the second field of `TyKind::AdtDef`. + /// typically obtained via the second field of [`TyKind::Adt`]. pub fn ty(&self, tcx: TyCtxt<'tcx>, subst: SubstsRef<'tcx>) -> Ty<'tcx> { tcx.type_of(self.did).subst(tcx, subst) } |
