Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
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
18 changes: 10 additions & 8 deletions compiler/rustc_hir_typeck/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ fn used_trait_imports(tcx: TyCtxt<'_>, def_id: LocalDefId) -> &UnordSet<LocalDef
&tcx.typeck(def_id).used_trait_imports
}

fn typeck<'tcx>(tcx: TyCtxt<'tcx>, def_id: LocalDefId) -> &'tcx ty::TypeckResults<'tcx> {
fn typeck_root<'tcx>(tcx: TyCtxt<'tcx>, def_id: LocalDefId) -> &'tcx ty::TypeckResults<'tcx> {
typeck_with_inspect(tcx, def_id, None)
}

Expand All @@ -95,6 +95,13 @@ pub fn inspect_typeck<'tcx>(
def_id: LocalDefId,
inspect: ObligationInspector<'tcx>,
) -> &'tcx ty::TypeckResults<'tcx> {
// Closures' typeck results come from their outermost function,
// as they are part of the same "inference environment".
let typeck_root_def_id = tcx.typeck_root_def_id_local(def_id);
if typeck_root_def_id != def_id {
return tcx.typeck(typeck_root_def_id);
}

typeck_with_inspect(tcx, def_id, Some(inspect))
}

Expand All @@ -104,12 +111,7 @@ fn typeck_with_inspect<'tcx>(
def_id: LocalDefId,
inspector: Option<ObligationInspector<'tcx>>,
) -> &'tcx ty::TypeckResults<'tcx> {
// Closures' typeck results come from their outermost function,
// as they are part of the same "inference environment".
let typeck_root_def_id = tcx.typeck_root_def_id_local(def_id);
if typeck_root_def_id != def_id {
return tcx.typeck(typeck_root_def_id);
}
assert!(!tcx.is_typeck_child(def_id.to_def_id()));
Copy link
Copy Markdown
Contributor Author

@zetanumbers zetanumbers Mar 25, 2026

Choose a reason for hiding this comment

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

This assert might be a bit costly due its call to def_kind query.

Copy link
Copy Markdown
Contributor Author

@zetanumbers zetanumbers Mar 25, 2026

Choose a reason for hiding this comment

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

This was debug_assert at the time rustc perf was run, so we should probably measure it again. I've changed it to non-debug assert to detect if some compiler dev would call it directly. If it increases compile time, I know how to make a TypeckRoot newtype over LocalDefId as an input to typeck_root to avoid this assertion. Or maybe I'm just too paranoid and it would be fine as debug too.


let id = tcx.local_def_id_to_hir_id(def_id);
let node = tcx.hir_node(id);
Expand Down Expand Up @@ -660,7 +662,7 @@ fn fatally_break_rust(tcx: TyCtxt<'_>, span: Span) -> ! {
pub fn provide(providers: &mut Providers) {
*providers = Providers {
method_autoderef_steps: method::probe::method_autoderef_steps,
typeck,
typeck_root,
used_trait_imports,
check_transmutes: intrinsicck::check_transmutes,
..*providers
Expand Down
2 changes: 1 addition & 1 deletion compiler/rustc_incremental/src/persist/clean.rs
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ const BASE_FN: &[&str] = &[
label_strs::type_of,
// And a big part of compilation (that we eventually want to cache) is type inference
// information:
label_strs::typeck,
label_strs::typeck_root,
];

/// DepNodes for Hir, which is pretty much everything
Expand Down
4 changes: 2 additions & 2 deletions compiler/rustc_middle/src/queries.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1222,9 +1222,9 @@ rustc_queries! {
separate_provide_extern
}

query typeck(key: LocalDefId) -> &'tcx ty::TypeckResults<'tcx> {
query typeck_root(key: LocalDefId) -> &'tcx ty::TypeckResults<'tcx> {
desc { "type-checking `{}`", tcx.def_path_str(key) }
cache_on_disk_if { !tcx.is_typeck_child(key.to_def_id()) }
cache_on_disk_if { true }
}

query used_trait_imports(key: LocalDefId) -> &'tcx UnordSet<LocalDefId> {
Expand Down
19 changes: 17 additions & 2 deletions compiler/rustc_middle/src/query/plumbing.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,15 @@ use rustc_data_structures::hash_table::HashTable;
use rustc_data_structures::sharded::Sharded;
use rustc_data_structures::sync::{AtomicU64, Lock, WorkerLocal};
use rustc_errors::Diag;
use rustc_hir::def_id::LocalDefId;
use rustc_span::Span;

use crate::dep_graph::{DepKind, DepNodeIndex, QuerySideEffect, SerializedDepNodeIndex};
use crate::ich::StableHashingContext;
use crate::queries::{ExternProviders, Providers, QueryArenas, QueryVTables, TaggedQueryKey};
use crate::query::on_disk_cache::OnDiskCache;
use crate::query::{QueryCache, QueryJob, QueryStackFrame};
use crate::ty::TyCtxt;
use crate::query::{IntoQueryKey, QueryCache, QueryJob, QueryStackFrame};
use crate::ty::{self, TyCtxt};

/// For a particular query, keeps track of "active" keys, i.e. keys whose
/// evaluation has started but has not yet finished successfully.
Expand Down Expand Up @@ -200,7 +201,21 @@ pub struct TyCtxtEnsureDone<'tcx> {
pub tcx: TyCtxt<'tcx>,
}

impl<'tcx> TyCtxtEnsureOk<'tcx> {
pub fn typeck(self, def_id: impl IntoQueryKey<LocalDefId>) {
self.typeck_root(
self.tcx.typeck_root_def_id(def_id.into_query_key().to_def_id()).expect_local(),
)
}
}

impl<'tcx> TyCtxt<'tcx> {
pub fn typeck(self, def_id: impl IntoQueryKey<LocalDefId>) -> &'tcx ty::TypeckResults<'tcx> {
self.typeck_root(
self.typeck_root_def_id(def_id.into_query_key().to_def_id()).expect_local(),
)
}

/// Returns a transparent wrapper for `TyCtxt` which uses
/// `span` as the location of queries performed through it.
#[inline(always)]
Expand Down
13 changes: 4 additions & 9 deletions src/librustdoc/core.rs
Original file line number Diff line number Diff line change
Expand Up @@ -309,19 +309,14 @@ pub(crate) fn create_config(
&EMPTY_SET
};
// In case typeck does end up being called, don't ICE in case there were name resolution errors
providers.queries.typeck = move |tcx, def_id| {
// Closures' tables come from their outermost function,
// as they are part of the same "inference environment".
// This avoids emitting errors for the parent twice (see similar code in `typeck_with_fallback`)
let typeck_root_def_id = tcx.typeck_root_def_id(def_id.to_def_id()).expect_local();
if typeck_root_def_id != def_id {
return tcx.typeck(typeck_root_def_id);
}
providers.queries.typeck_root = move |tcx, def_id| {
// Panic before code below breaks in case of someone calls typeck_root directly
assert!(!tcx.is_typeck_child(def_id.to_def_id()));

let body = tcx.hir_body_owned_by(def_id);
debug!("visiting body for {def_id:?}");
EmitIgnoredResolutionErrors::new(tcx).visit_body(body);
(rustc_interface::DEFAULT_QUERY_PROVIDERS.queries.typeck)(tcx, def_id)
(rustc_interface::DEFAULT_QUERY_PROVIDERS.queries.typeck_root)(tcx, def_id)
};
}),
extra_symbols: Vec::new(),
Expand Down
2 changes: 1 addition & 1 deletion tests/incremental/callee_caller_cross_crate/b.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@

extern crate a;

#[rustc_clean(except="typeck", cfg="rpass2")]
#[rustc_clean(except="typeck_root", cfg="rpass2")]
pub fn call_function0() {
a::function0(77);
}
Expand Down
12 changes: 6 additions & 6 deletions tests/incremental/change_add_field/struct_point.rs
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ pub mod point {
pub mod fn_with_type_in_sig {
use point::Point;

#[rustc_clean(except="typeck,fn_sig,optimized_mir", cfg="cfail2")]
#[rustc_clean(except="typeck_root,fn_sig,optimized_mir", cfg="cfail2")]
pub fn boop(p: Option<&Point>) -> f32 {
p.map(|p| p.total()).unwrap_or(0.0)
}
Expand All @@ -86,7 +86,7 @@ pub mod fn_with_type_in_sig {
pub mod call_fn_with_type_in_sig {
use fn_with_type_in_sig;

#[rustc_clean(except="typeck,optimized_mir", cfg="cfail2")]
#[rustc_clean(except="typeck_root,optimized_mir", cfg="cfail2")]
pub fn bip() -> f32 {
fn_with_type_in_sig::boop(None)
}
Expand All @@ -102,7 +102,7 @@ pub mod call_fn_with_type_in_sig {
pub mod fn_with_type_in_body {
use point::Point;

#[rustc_clean(except="typeck,optimized_mir", cfg="cfail2")]
#[rustc_clean(except="typeck_root,optimized_mir", cfg="cfail2")]
pub fn boop() -> f32 {
Point::origin().total()
}
Expand All @@ -125,7 +125,7 @@ pub mod call_fn_with_type_in_body {
pub mod fn_make_struct {
use point::Point;

#[rustc_clean(except="typeck,fn_sig,optimized_mir", cfg="cfail2")]
#[rustc_clean(except="typeck_root,fn_sig,optimized_mir", cfg="cfail2")]
pub fn make_origin(p: Point) -> Point {
Point { ..p }
}
Expand All @@ -135,7 +135,7 @@ pub mod fn_make_struct {
pub mod fn_read_field {
use point::Point;

#[rustc_clean(except="typeck,fn_sig,optimized_mir", cfg="cfail2")]
#[rustc_clean(except="typeck_root,fn_sig,optimized_mir", cfg="cfail2")]
pub fn get_x(p: Point) -> f32 {
p.x
}
Expand All @@ -145,7 +145,7 @@ pub mod fn_read_field {
pub mod fn_write_field {
use point::Point;

#[rustc_clean(except="typeck,fn_sig,optimized_mir", cfg="cfail2")]
#[rustc_clean(except="typeck_root,fn_sig,optimized_mir", cfg="cfail2")]
pub fn inc_x(p: &mut Point) {
p.x += 1.0;
}
Expand Down
2 changes: 1 addition & 1 deletion tests/incremental/change_crate_order/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ extern crate a;
use a::A;
use b::B;

//? #[rustc_clean(label="typeck", cfg="rpass2")]
//? #[rustc_clean(label="typeck_root", cfg="rpass2")]
pub fn main() {
A + B;
}
4 changes: 2 additions & 2 deletions tests/incremental/change_private_fn/struct_point.rs
Original file line number Diff line number Diff line change
Expand Up @@ -54,8 +54,8 @@ pub mod fn_calls_methods_in_same_impl {
// The cached result should actually be loaded from disk
// (not just marked green) - for example, `DeadVisitor`
// always runs during compilation as a "pass", and loads
// the typeck results for bodies.
#[rustc_clean(cfg="cfail2", loaded_from_disk="typeck")]
// the typeck_root results for bodies.
#[rustc_clean(cfg="cfail2", loaded_from_disk="typeck_root")]
pub fn check() {
let x = Point { x: 2.0, y: 2.0 };
x.distance_from_origin();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ pub mod point {
pub mod fn_calls_changed_method {
use point::Point;

#[rustc_clean(except="typeck,optimized_mir", cfg="cfail2")]
#[rustc_clean(except="typeck_root,optimized_mir", cfg="cfail2")]
pub fn check() {
let p = Point { x: 2.0, y: 2.0 };
p.distance_from_point(None);
Expand Down
6 changes: 3 additions & 3 deletions tests/incremental/clean.rs
Original file line number Diff line number Diff line change
Expand Up @@ -36,14 +36,14 @@ mod y {
//[cfail2]~| ERROR `predicates_of(y)` should be dirty but is not
//[cfail2]~| ERROR `type_of(y)` should be dirty but is not
//[cfail2]~| ERROR `fn_sig(y)` should be dirty but is not
//[cfail2]~| ERROR `typeck(y)` should be clean but is not
//[cfail2]~| ERROR `typeck_root(y)` should be clean but is not
x::x();
}
}

mod z {
#[rustc_clean(except="typeck", cfg="cfail2")]
#[rustc_clean(except="typeck_root", cfg="cfail2")]
pub fn z() {
//[cfail2]~^ ERROR `typeck(z)` should be dirty but is not
//[cfail2]~^ ERROR `typeck_root(z)` should be dirty but is not
}
}
24 changes: 12 additions & 12 deletions tests/incremental/hashes/call_expressions.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,9 +29,9 @@ pub fn change_callee_function() {
}

#[cfg(not(any(cfail1,cfail4)))]
#[rustc_clean(cfg="cfail2", except="opt_hir_owner_nodes,optimized_mir,typeck")]
#[rustc_clean(cfg="cfail2", except="opt_hir_owner_nodes,optimized_mir,typeck_root")]
#[rustc_clean(cfg="cfail3")]
#[rustc_clean(cfg="cfail5", except="opt_hir_owner_nodes,optimized_mir,typeck")]
#[rustc_clean(cfg="cfail5", except="opt_hir_owner_nodes,optimized_mir,typeck_root")]
#[rustc_clean(cfg="cfail6")]
pub fn change_callee_function() {
callee2(1, 2)
Expand Down Expand Up @@ -63,9 +63,9 @@ mod change_callee_indirectly_function {
#[cfg(not(any(cfail1,cfail4)))]
use super::callee2 as callee;

#[rustc_clean(except="opt_hir_owner_nodes,typeck", cfg="cfail2")]
#[rustc_clean(except="opt_hir_owner_nodes,typeck_root", cfg="cfail2")]
#[rustc_clean(cfg="cfail3")]
#[rustc_clean(except="opt_hir_owner_nodes,typeck", cfg="cfail5")]
#[rustc_clean(except="opt_hir_owner_nodes,typeck_root", cfg="cfail5")]
#[rustc_clean(cfg="cfail6")]
pub fn change_callee_indirectly_function() {
callee(1, 2)
Expand All @@ -87,9 +87,9 @@ pub fn change_callee_method() {
}

#[cfg(not(any(cfail1,cfail4)))]
#[rustc_clean(cfg="cfail2", except="opt_hir_owner_nodes,optimized_mir,typeck")]
#[rustc_clean(cfg="cfail2", except="opt_hir_owner_nodes,optimized_mir,typeck_root")]
#[rustc_clean(cfg="cfail3")]
#[rustc_clean(cfg="cfail5", except="opt_hir_owner_nodes,optimized_mir,typeck")]
#[rustc_clean(cfg="cfail5", except="opt_hir_owner_nodes,optimized_mir,typeck_root")]
#[rustc_clean(cfg="cfail6")]
pub fn change_callee_method() {
let s = Struct;
Expand Down Expand Up @@ -125,9 +125,9 @@ pub fn change_ufcs_callee_method() {
}

#[cfg(not(any(cfail1,cfail4)))]
#[rustc_clean(cfg="cfail2", except="opt_hir_owner_nodes,optimized_mir,typeck")]
#[rustc_clean(cfg="cfail2", except="opt_hir_owner_nodes,optimized_mir,typeck_root")]
#[rustc_clean(cfg="cfail3")]
#[rustc_clean(cfg="cfail5", except="opt_hir_owner_nodes,optimized_mir,typeck")]
#[rustc_clean(cfg="cfail5", except="opt_hir_owner_nodes,optimized_mir,typeck_root")]
#[rustc_clean(cfg="cfail6")]
pub fn change_ufcs_callee_method() {
let s = Struct;
Expand Down Expand Up @@ -163,9 +163,9 @@ pub fn change_to_ufcs() {
}

#[cfg(not(any(cfail1,cfail4)))]
#[rustc_clean(cfg="cfail2", except="opt_hir_owner_nodes,optimized_mir,typeck")]
#[rustc_clean(cfg="cfail2", except="opt_hir_owner_nodes,optimized_mir,typeck_root")]
#[rustc_clean(cfg="cfail3")]
#[rustc_clean(cfg="cfail5", except="opt_hir_owner_nodes,optimized_mir,typeck")]
#[rustc_clean(cfg="cfail5", except="opt_hir_owner_nodes,optimized_mir,typeck_root")]
#[rustc_clean(cfg="cfail6")]
// One might think this would be expanded in the opt_hir_owner_nodes/Mir, but it actually
// results in slightly different hir_owner/Mir.
Expand All @@ -187,9 +187,9 @@ pub mod change_ufcs_callee_indirectly {
#[cfg(not(any(cfail1,cfail4)))]
use super::Struct2 as Struct;

#[rustc_clean(cfg="cfail2", except="opt_hir_owner_nodes,optimized_mir,typeck")]
#[rustc_clean(cfg="cfail2", except="opt_hir_owner_nodes,optimized_mir,typeck_root")]
#[rustc_clean(cfg="cfail3")]
#[rustc_clean(cfg="cfail5", except="opt_hir_owner_nodes,optimized_mir,typeck")]
#[rustc_clean(cfg="cfail5", except="opt_hir_owner_nodes,optimized_mir,typeck_root")]
#[rustc_clean(cfg="cfail6")]
pub fn change_ufcs_callee_indirectly() {
let s = Struct;
Expand Down
16 changes: 8 additions & 8 deletions tests/incremental/hashes/closure_expressions.rs
Original file line number Diff line number Diff line change
Expand Up @@ -43,9 +43,9 @@ pub fn add_parameter() {
}

#[cfg(not(any(cfail1,cfail4)))]
#[rustc_clean(cfg="cfail2", except="opt_hir_owner_nodes, typeck")]
#[rustc_clean(cfg="cfail2", except="opt_hir_owner_nodes, typeck_root")]
#[rustc_clean(cfg="cfail3")]
#[rustc_clean(cfg="cfail5", except="opt_hir_owner_nodes, typeck")]
#[rustc_clean(cfg="cfail5", except="opt_hir_owner_nodes, typeck_root")]
#[rustc_clean(cfg="cfail6")]
pub fn add_parameter() {
let x = 0u32;
Expand All @@ -61,9 +61,9 @@ pub fn change_parameter_pattern() {
}

#[cfg(not(any(cfail1,cfail4)))]
#[rustc_clean(cfg="cfail2", except="opt_hir_owner_nodes, typeck")]
#[rustc_clean(cfg="cfail2", except="opt_hir_owner_nodes, typeck_root")]
#[rustc_clean(cfg="cfail3")]
#[rustc_clean(cfg="cfail5", except="opt_hir_owner_nodes, typeck")]
#[rustc_clean(cfg="cfail5", except="opt_hir_owner_nodes, typeck_root")]
#[rustc_clean(cfg="cfail6")]
pub fn change_parameter_pattern() {
let _ = |(x,): (u32,)| x;
Expand Down Expand Up @@ -96,9 +96,9 @@ pub fn add_type_ascription_to_parameter() {
}

#[cfg(not(any(cfail1,cfail4)))]
#[rustc_clean(cfg = "cfail2", except = "opt_hir_owner_nodes, typeck")]
#[rustc_clean(cfg = "cfail2", except = "opt_hir_owner_nodes, typeck_root")]
#[rustc_clean(cfg = "cfail3")]
#[rustc_clean(cfg = "cfail5", except = "opt_hir_owner_nodes, typeck")]
#[rustc_clean(cfg = "cfail5", except = "opt_hir_owner_nodes, typeck_root")]
#[rustc_clean(cfg = "cfail6")]
pub fn add_type_ascription_to_parameter() {
let closure = |x: u32| x + 1u32;
Expand All @@ -115,9 +115,9 @@ pub fn change_parameter_type() {
}

#[cfg(not(any(cfail1,cfail4)))]
#[rustc_clean(cfg="cfail2", except="opt_hir_owner_nodes, optimized_mir, typeck")]
#[rustc_clean(cfg="cfail2", except="opt_hir_owner_nodes, optimized_mir, typeck_root")]
#[rustc_clean(cfg="cfail3")]
#[rustc_clean(cfg="cfail5", except="opt_hir_owner_nodes, optimized_mir, typeck")]
#[rustc_clean(cfg="cfail5", except="opt_hir_owner_nodes, optimized_mir, typeck_root")]
#[rustc_clean(cfg="cfail6")]
pub fn change_parameter_type() {
let closure = |x: u16| (x as u64) + 1;
Expand Down
Loading
Loading