From 1f75142c8c3383684ac7a02222f08db7a3b30ccf Mon Sep 17 00:00:00 2001 From: Noah Lev Date: Sun, 7 Aug 2022 19:11:47 -0700 Subject: [PATCH 1/2] 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! --- compiler/rustc_typeck/src/check/fn_ctxt/mod.rs | 2 ++ compiler/rustc_typeck/src/collect.rs | 7 ++++++- 2 files changed, 8 insertions(+), 1 deletion(-) diff --git a/compiler/rustc_typeck/src/check/fn_ctxt/mod.rs b/compiler/rustc_typeck/src/check/fn_ctxt/mod.rs index 05bcc710e1625..bfc7c4ac2889a 100644 --- a/compiler/rustc_typeck/src/check/fn_ctxt/mod.rs +++ b/compiler/rustc_typeck/src/check/fn_ctxt/mod.rs @@ -26,6 +26,8 @@ use rustc_trait_selection::traits::{ObligationCause, ObligationCauseCode}; use std::cell::{Cell, RefCell}; use std::ops::Deref; +/// The `FnCtxt` stores type-checking context needed to type-check function bodies, +/// in contrast to [`ItemCtxt`], which is used to type-check item *signatures*. pub struct FnCtxt<'a, 'tcx> { pub(super) body_id: hir::HirId, diff --git a/compiler/rustc_typeck/src/collect.rs b/compiler/rustc_typeck/src/collect.rs index 99996e80c9ce9..e7bdb4a9b20c2 100644 --- a/compiler/rustc_typeck/src/collect.rs +++ b/compiler/rustc_typeck/src/collect.rs @@ -94,7 +94,12 @@ pub fn provide(providers: &mut Providers) { /////////////////////////////////////////////////////////////////////////// /// Context specific to some particular item. This is what implements -/// `AstConv`. It has information about the predicates that are defined +/// `AstConv`. +/// +/// `ItemCtxt` is primarily used to type-check item signatures, in contrast to [`FnCtxt`], +/// which is used to type-check function bodies. +/// +/// It has information about the predicates that are defined /// on the trait. Unfortunately, this predicate information is /// available in various different forms at various points in the /// process. So we can't just store a pointer to e.g., the AST or the From 31a051870b8ab18c798cacded7eb8e0d087a8224 Mon Sep 17 00:00:00 2001 From: Noah Lev Date: Sun, 7 Aug 2022 21:16:47 -0700 Subject: [PATCH 2/2] Address review comments --- .../rustc_typeck/src/check/fn_ctxt/mod.rs | 13 +++++++++-- compiler/rustc_typeck/src/collect.rs | 23 +++++++++++++++---- 2 files changed, 30 insertions(+), 6 deletions(-) diff --git a/compiler/rustc_typeck/src/check/fn_ctxt/mod.rs b/compiler/rustc_typeck/src/check/fn_ctxt/mod.rs index bfc7c4ac2889a..e008d50aa514a 100644 --- a/compiler/rustc_typeck/src/check/fn_ctxt/mod.rs +++ b/compiler/rustc_typeck/src/check/fn_ctxt/mod.rs @@ -26,8 +26,17 @@ use rustc_trait_selection::traits::{ObligationCause, ObligationCauseCode}; use std::cell::{Cell, RefCell}; use std::ops::Deref; -/// The `FnCtxt` stores type-checking context needed to type-check function bodies, -/// in contrast to [`ItemCtxt`], which is used to type-check item *signatures*. +/// The `FnCtxt` stores type-checking context needed to type-check bodies of +/// functions, closures, and `const`s, including performing type inference +/// with [`InferCtxt`]. +/// +/// This is in contrast to [`ItemCtxt`], which is used to type-check item *signatures* +/// and thus does not perform type inference. +/// +/// See [`ItemCtxt`]'s docs for more. +/// +/// [`ItemCtxt`]: crate::collect::ItemCtxt +/// [`InferCtxt`]: infer::InferCtxt pub struct FnCtxt<'a, 'tcx> { pub(super) body_id: hir::HirId, diff --git a/compiler/rustc_typeck/src/collect.rs b/compiler/rustc_typeck/src/collect.rs index e7bdb4a9b20c2..e7c5ecc60ec78 100644 --- a/compiler/rustc_typeck/src/collect.rs +++ b/compiler/rustc_typeck/src/collect.rs @@ -94,12 +94,27 @@ pub fn provide(providers: &mut Providers) { /////////////////////////////////////////////////////////////////////////// /// Context specific to some particular item. This is what implements -/// `AstConv`. +/// [`AstConv`]. /// -/// `ItemCtxt` is primarily used to type-check item signatures, in contrast to [`FnCtxt`], -/// which is used to type-check function bodies. +/// # `ItemCtxt` vs `FnCtxt` /// -/// It has information about the predicates that are defined +/// `ItemCtxt` is primarily used to type-check item signatures and lower them +/// from HIR to their [`ty::Ty`] representation, which is exposed using [`AstConv`]. +/// It's also used for the bodies of items like structs where the body (the fields) +/// are just signatures. +/// +/// This is in contrast to [`FnCtxt`], which is used to type-check bodies of +/// functions, closures, and `const`s -- anywhere that expressions and statements show up. +/// +/// An important thing to note is that `ItemCtxt` does no inference -- it has no [`InferCtxt`] -- +/// while `FnCtxt` does do inference. +/// +/// [`FnCtxt`]: crate::check::FnCtxt +/// [`InferCtxt`]: rustc_infer::infer::InferCtxt +/// +/// # Trait predicates +/// +/// `ItemCtxt` has information about the predicates that are defined /// on the trait. Unfortunately, this predicate information is /// available in various different forms at various points in the /// process. So we can't just store a pointer to e.g., the AST or the