Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add some high-level docs to FnCtxt and ItemCtxt #100256

Merged
merged 2 commits into from
Aug 10, 2022
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions compiler/rustc_typeck/src/check/fn_ctxt/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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*.
Copy link
Member

@compiler-errors compiler-errors Aug 8, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's a bit more accurate to mention:

  1. that this is used to lower things where we don't care about the body. For example, ItemCtxt is used to check structs and stuff too.
    i. An important distinction is that FnCtxt has an InferCtxt, while ItemCtxt expects no inference to go on in there.
  2. This is also (via the AstConv trait) primarily used for lowering things from HIR -> middle representations of things. For example, we use it in the tcx.type_of query, which does little checking and mostly lowering.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks! I added the info you suggested.

The one thing is that I couldn't find an InferCtxt field -- or any mention of InferCtxt for that matter -- in FnCtxt. Is it accessed in some indirect way?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is it accessed in some indirect way

Yea, good question. It's stored in inh: &Inherited -- this is how several FnCtxts actually share the same InferCtxt. This Inherited struct is important because closures can share inference variables and other info to their containing functions, for example.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah, that makes sense! Thanks :)

pub struct FnCtxt<'a, 'tcx> {
pub(super) body_id: hir::HirId,

Expand Down
7 changes: 6 additions & 1 deletion compiler/rustc_typeck/src/collect.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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.
camelid marked this conversation as resolved.
Show resolved Hide resolved
///
/// 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
Expand Down