Skip to content

Commit 4add514

Browse files
authored
Rollup merge of #100256 - camelid:typeck-ctxt-doc, r=compiler-errors
Add some high-level docs to `FnCtxt` and `ItemCtxt` I haven't understood the difference between these before, but ``@compiler-errors`` helped me clear it up. Hopefully this will help other people who've been confused! r? `@compiler-errors`
2 parents b11b8d6 + 31a0518 commit 4add514

File tree

2 files changed

+32
-1
lines changed

2 files changed

+32
-1
lines changed

compiler/rustc_typeck/src/check/fn_ctxt/mod.rs

+11
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,17 @@ use rustc_trait_selection::traits::{ObligationCause, ObligationCauseCode};
2626
use std::cell::{Cell, RefCell};
2727
use std::ops::Deref;
2828

29+
/// The `FnCtxt` stores type-checking context needed to type-check bodies of
30+
/// functions, closures, and `const`s, including performing type inference
31+
/// with [`InferCtxt`].
32+
///
33+
/// This is in contrast to [`ItemCtxt`], which is used to type-check item *signatures*
34+
/// and thus does not perform type inference.
35+
///
36+
/// See [`ItemCtxt`]'s docs for more.
37+
///
38+
/// [`ItemCtxt`]: crate::collect::ItemCtxt
39+
/// [`InferCtxt`]: infer::InferCtxt
2940
pub struct FnCtxt<'a, 'tcx> {
3041
pub(super) body_id: hir::HirId,
3142

compiler/rustc_typeck/src/collect.rs

+21-1
Original file line numberDiff line numberDiff line change
@@ -94,7 +94,27 @@ pub fn provide(providers: &mut Providers) {
9494
///////////////////////////////////////////////////////////////////////////
9595

9696
/// Context specific to some particular item. This is what implements
97-
/// `AstConv`. It has information about the predicates that are defined
97+
/// [`AstConv`].
98+
///
99+
/// # `ItemCtxt` vs `FnCtxt`
100+
///
101+
/// `ItemCtxt` is primarily used to type-check item signatures and lower them
102+
/// from HIR to their [`ty::Ty`] representation, which is exposed using [`AstConv`].
103+
/// It's also used for the bodies of items like structs where the body (the fields)
104+
/// are just signatures.
105+
///
106+
/// This is in contrast to [`FnCtxt`], which is used to type-check bodies of
107+
/// functions, closures, and `const`s -- anywhere that expressions and statements show up.
108+
///
109+
/// An important thing to note is that `ItemCtxt` does no inference -- it has no [`InferCtxt`] --
110+
/// while `FnCtxt` does do inference.
111+
///
112+
/// [`FnCtxt`]: crate::check::FnCtxt
113+
/// [`InferCtxt`]: rustc_infer::infer::InferCtxt
114+
///
115+
/// # Trait predicates
116+
///
117+
/// `ItemCtxt` has information about the predicates that are defined
98118
/// on the trait. Unfortunately, this predicate information is
99119
/// available in various different forms at various points in the
100120
/// process. So we can't just store a pointer to e.g., the AST or the

0 commit comments

Comments
 (0)