1
Fork 0

Auto merge of #108118 - oli-obk:lazy_typeck, r=cjgillot

Run various queries from other queries instead of explicitly in phases

These are just legacy leftovers from when rustc didn't have a query system. While there are more cleanups of this sort that can be done here, I want to land them in smaller steps.

This phased order of query invocations was already a lie, as any query that looks at types (e.g. the wf checks run before) can invoke e.g. const eval which invokes borrowck, which invokes typeck, ...
This commit is contained in:
bors 2023-04-23 13:34:31 +00:00
commit 3462f79e94
42 changed files with 400 additions and 423 deletions

View file

@ -364,7 +364,7 @@ impl<'tcx> DepNodeParams<TyCtxt<'tcx>> for HirId {
#[inline(always)]
fn to_debug_str(&self, tcx: TyCtxt<'tcx>) -> String {
let HirId { owner, local_id } = *self;
format!("{}.{}", tcx.def_path_str(owner.to_def_id()), local_id.as_u32())
format!("{}.{}", tcx.def_path_str(owner), local_id.as_u32())
}
#[inline(always)]

View file

@ -1217,7 +1217,7 @@ fn upstream_crates(tcx: TyCtxt<'_>) -> Vec<(StableCrateId, Svh)> {
}
fn hir_id_to_string(map: Map<'_>, id: HirId) -> String {
let path_str = |def_id: LocalDefId| map.tcx.def_path_str(def_id.to_def_id());
let path_str = |def_id: LocalDefId| map.tcx.def_path_str(def_id);
let span_str = || map.tcx.sess.source_map().span_to_snippet(map.span(id)).unwrap_or_default();
let node_str = |prefix| format!("{id} ({prefix} `{}`)", span_str());

View file

@ -83,7 +83,7 @@ rustc_queries! {
/// Avoid calling this query directly.
query hir_module_items(key: LocalDefId) -> &'tcx rustc_middle::hir::ModuleItems {
arena_cache
desc { |tcx| "getting HIR module items in `{}`", tcx.def_path_str(key.to_def_id()) }
desc { |tcx| "getting HIR module items in `{}`", tcx.def_path_str(key) }
cache_on_disk_if { true }
}
@ -92,14 +92,14 @@ rustc_queries! {
/// This can be conveniently accessed by methods on `tcx.hir()`.
/// Avoid calling this query directly.
query hir_owner(key: hir::OwnerId) -> Option<crate::hir::Owner<'tcx>> {
desc { |tcx| "getting HIR owner of `{}`", tcx.def_path_str(key.to_def_id()) }
desc { |tcx| "getting HIR owner of `{}`", tcx.def_path_str(key) }
}
/// Gives access to the HIR ID for the given `LocalDefId` owner `key` if any.
///
/// Definitions that were generated with no HIR, would be fed to return `None`.
query opt_local_def_id_to_hir_id(key: LocalDefId) -> Option<hir::HirId>{
desc { |tcx| "getting HIR ID of `{}`", tcx.def_path_str(key.to_def_id()) }
desc { |tcx| "getting HIR ID of `{}`", tcx.def_path_str(key) }
feedable
}
@ -108,7 +108,7 @@ rustc_queries! {
/// This can be conveniently accessed by methods on `tcx.hir()`.
/// Avoid calling this query directly.
query hir_owner_parent(key: hir::OwnerId) -> hir::HirId {
desc { |tcx| "getting HIR parent of `{}`", tcx.def_path_str(key.to_def_id()) }
desc { |tcx| "getting HIR parent of `{}`", tcx.def_path_str(key) }
}
/// Gives access to the HIR nodes and bodies inside the HIR owner `key`.
@ -116,7 +116,7 @@ rustc_queries! {
/// This can be conveniently accessed by methods on `tcx.hir()`.
/// Avoid calling this query directly.
query hir_owner_nodes(key: hir::OwnerId) -> hir::MaybeOwner<&'tcx hir::OwnerNodes<'tcx>> {
desc { |tcx| "getting HIR owner items in `{}`", tcx.def_path_str(key.to_def_id()) }
desc { |tcx| "getting HIR owner items in `{}`", tcx.def_path_str(key) }
}
/// Gives access to the HIR attributes inside the HIR owner `key`.
@ -124,7 +124,7 @@ rustc_queries! {
/// This can be conveniently accessed by methods on `tcx.hir()`.
/// Avoid calling this query directly.
query hir_attrs(key: hir::OwnerId) -> &'tcx hir::AttributeMap<'tcx> {
desc { |tcx| "getting HIR owner attributes in `{}`", tcx.def_path_str(key.to_def_id()) }
desc { |tcx| "getting HIR owner attributes in `{}`", tcx.def_path_str(key) }
}
/// Given the def_id of a const-generic parameter, computes the associated default const
@ -295,7 +295,7 @@ rustc_queries! {
query shallow_lint_levels_on(key: hir::OwnerId) -> &'tcx rustc_middle::lint::ShallowLintLevelMap {
eval_always // fetches `resolutions`
arena_cache
desc { |tcx| "looking up lint levels for `{}`", tcx.def_path_str(key.to_def_id()) }
desc { |tcx| "looking up lint levels for `{}`", tcx.def_path_str(key) }
}
query lint_expectations(_: ()) -> &'tcx Vec<(LintExpectationId, LintExpectation)> {
@ -305,7 +305,7 @@ rustc_queries! {
query parent_module_from_def_id(key: LocalDefId) -> LocalDefId {
eval_always
desc { |tcx| "getting the parent module of `{}`", tcx.def_path_str(key.to_def_id()) }
desc { |tcx| "getting the parent module of `{}`", tcx.def_path_str(key) }
}
query expn_that_defined(key: DefId) -> rustc_span::ExpnId {
@ -321,7 +321,7 @@ rustc_queries! {
/// Checks whether a type is representable or infinitely sized
query representability(_: LocalDefId) -> rustc_middle::ty::Representability {
desc { "checking if `{}` is representable", tcx.def_path_str(key.to_def_id()) }
desc { "checking if `{}` is representable", tcx.def_path_str(key) }
// infinitely sized types will cause a cycle
cycle_delay_bug
// we don't want recursive representability calls to be forced with
@ -349,21 +349,21 @@ rustc_queries! {
query thir_body(key: LocalDefId) -> Result<(&'tcx Steal<thir::Thir<'tcx>>, thir::ExprId), ErrorGuaranteed> {
// Perf tests revealed that hashing THIR is inefficient (see #85729).
no_hash
desc { |tcx| "building THIR for `{}`", tcx.def_path_str(key.to_def_id()) }
desc { |tcx| "building THIR for `{}`", tcx.def_path_str(key) }
}
/// Create a THIR tree for debugging.
query thir_tree(key: LocalDefId) -> &'tcx String {
no_hash
arena_cache
desc { |tcx| "constructing THIR tree for `{}`", tcx.def_path_str(key.to_def_id()) }
desc { |tcx| "constructing THIR tree for `{}`", tcx.def_path_str(key) }
}
/// Create a list-like THIR representation for debugging.
query thir_flat(key: LocalDefId) -> &'tcx String {
no_hash
arena_cache
desc { |tcx| "constructing flat THIR representation for `{}`", tcx.def_path_str(key.to_def_id()) }
desc { |tcx| "constructing flat THIR representation for `{}`", tcx.def_path_str(key) }
}
/// Set of all the `DefId`s in this crate that have MIR associated with
@ -386,7 +386,7 @@ rustc_queries! {
/// Fetch the MIR for a given `DefId` right after it's built - this includes
/// unreachable code.
query mir_built(key: LocalDefId) -> &'tcx Steal<mir::Body<'tcx>> {
desc { |tcx| "building MIR for `{}`", tcx.def_path_str(key.to_def_id()) }
desc { |tcx| "building MIR for `{}`", tcx.def_path_str(key) }
}
/// Fetch the MIR for a given `DefId` up till the point where it is
@ -394,7 +394,7 @@ rustc_queries! {
///
/// See the README for the `mir` module for details.
query mir_const(key: LocalDefId) -> &'tcx Steal<mir::Body<'tcx>> {
desc { |tcx| "preparing `{}` for borrow checking", tcx.def_path_str(key.to_def_id()) }
desc { |tcx| "preparing `{}` for borrow checking", tcx.def_path_str(key) }
no_hash
}
@ -410,7 +410,7 @@ rustc_queries! {
query mir_drops_elaborated_and_const_checked(key: LocalDefId) -> &'tcx Steal<mir::Body<'tcx>> {
no_hash
desc { |tcx| "elaborating drops for `{}`", tcx.def_path_str(key.to_def_id()) }
desc { |tcx| "elaborating drops for `{}`", tcx.def_path_str(key) }
}
query mir_for_ctfe(
@ -426,13 +426,13 @@ rustc_queries! {
&'tcx Steal<IndexVec<mir::Promoted, mir::Body<'tcx>>>
) {
no_hash
desc { |tcx| "promoting constants in MIR for `{}`", tcx.def_path_str(key.to_def_id()) }
desc { |tcx| "promoting constants in MIR for `{}`", tcx.def_path_str(key) }
}
query closure_typeinfo(key: LocalDefId) -> ty::ClosureTypeInfo<'tcx> {
desc {
|tcx| "finding symbols for captures of closure `{}`",
tcx.def_path_str(key.to_def_id())
tcx.def_path_str(key)
}
}
@ -444,7 +444,7 @@ rustc_queries! {
}
query check_generator_obligations(key: LocalDefId) {
desc { |tcx| "verify auto trait bounds for generator interior type `{}`", tcx.def_path_str(key.to_def_id()) }
desc { |tcx| "verify auto trait bounds for generator interior type `{}`", tcx.def_path_str(key) }
}
/// MIR after our optimization passes have run. This is MIR that is ready
@ -526,7 +526,7 @@ rustc_queries! {
/// `explicit_predicates_of` and `explicit_item_bounds` will then take
/// the appropriate subsets of the predicates here.
query trait_explicit_predicates_and_bounds(key: LocalDefId) -> ty::GenericPredicates<'tcx> {
desc { |tcx| "computing explicit predicates of trait `{}`", tcx.def_path_str(key.to_def_id()) }
desc { |tcx| "computing explicit predicates of trait `{}`", tcx.def_path_str(key) }
}
/// Returns the predicates written explicitly by the user.
@ -769,14 +769,14 @@ rustc_queries! {
/// The result of unsafety-checking this `LocalDefId`.
query unsafety_check_result(key: LocalDefId) -> &'tcx mir::UnsafetyCheckResult {
desc { |tcx| "unsafety-checking `{}`", tcx.def_path_str(key.to_def_id()) }
desc { |tcx| "unsafety-checking `{}`", tcx.def_path_str(key) }
cache_on_disk_if { true }
}
/// Unsafety-check this `LocalDefId` with THIR unsafeck. This should be
/// used with `-Zthir-unsafeck`.
query thir_check_unsafety(key: LocalDefId) {
desc { |tcx| "unsafety-checking `{}`", tcx.def_path_str(key.to_def_id()) }
desc { |tcx| "unsafety-checking `{}`", tcx.def_path_str(key) }
cache_on_disk_if { true }
}
@ -833,7 +833,7 @@ rustc_queries! {
desc { |tcx| "checking privacy in {}", describe_as_module(key, tcx) }
}
query check_liveness(key: DefId) {
query check_liveness(key: LocalDefId) {
desc { |tcx| "checking liveness of variables in `{}`", tcx.def_path_str(key) }
}
@ -872,21 +872,17 @@ rustc_queries! {
separate_provide_extern
}
query typeck_item_bodies(_: ()) -> () {
desc { "type-checking all item bodies" }
}
query typeck(key: LocalDefId) -> &'tcx ty::TypeckResults<'tcx> {
desc { |tcx| "type-checking `{}`", tcx.def_path_str(key.to_def_id()) }
desc { |tcx| "type-checking `{}`", tcx.def_path_str(key) }
cache_on_disk_if { true }
}
query diagnostic_only_typeck(key: LocalDefId) -> &'tcx ty::TypeckResults<'tcx> {
desc { |tcx| "type-checking `{}`", tcx.def_path_str(key.to_def_id()) }
desc { |tcx| "type-checking `{}`", tcx.def_path_str(key) }
cache_on_disk_if { true }
}
query used_trait_imports(key: LocalDefId) -> &'tcx UnordSet<LocalDefId> {
desc { |tcx| "finding used_trait_imports `{}`", tcx.def_path_str(key.to_def_id()) }
desc { |tcx| "finding used_trait_imports `{}`", tcx.def_path_str(key) }
cache_on_disk_if { true }
}
@ -901,7 +897,7 @@ rustc_queries! {
/// Borrow-checks the function body. If this is a closure, returns
/// additional requirements that the closure's creator must verify.
query mir_borrowck(key: LocalDefId) -> &'tcx mir::BorrowCheckResult<'tcx> {
desc { |tcx| "borrow-checking `{}`", tcx.def_path_str(key.to_def_id()) }
desc { |tcx| "borrow-checking `{}`", tcx.def_path_str(key) }
cache_on_disk_if(tcx) { tcx.is_typeck_child(key.to_def_id()) }
}
@ -923,7 +919,7 @@ rustc_queries! {
query orphan_check_impl(key: LocalDefId) -> Result<(), ErrorGuaranteed> {
desc { |tcx|
"checking whether impl `{}` follows the orphan rules",
tcx.def_path_str(key.to_def_id()),
tcx.def_path_str(key),
}
}
@ -935,7 +931,7 @@ rustc_queries! {
desc { |tcx|
"computing if `{}` (transitively) calls `{}`",
key.0,
tcx.def_path_str(key.1.to_def_id()),
tcx.def_path_str(key.1),
}
}
@ -1026,7 +1022,7 @@ rustc_queries! {
}
query check_match(key: LocalDefId) {
desc { |tcx| "match-checking `{}`", tcx.def_path_str(key.to_def_id()) }
desc { |tcx| "match-checking `{}`", tcx.def_path_str(key) }
cache_on_disk_if { true }
}
@ -1366,7 +1362,7 @@ rustc_queries! {
separate_provide_extern
}
query has_ffi_unwind_calls(key: LocalDefId) -> bool {
desc { |tcx| "checking if `{}` contains FFI-unwind calls", tcx.def_path_str(key.to_def_id()) }
desc { |tcx| "checking if `{}` contains FFI-unwind calls", tcx.def_path_str(key) }
cache_on_disk_if { true }
}
query required_panic_strategy(_: CrateNum) -> Option<PanicStrategy> {
@ -1412,7 +1408,7 @@ rustc_queries! {
}
query check_well_formed(key: hir::OwnerId) -> () {
desc { |tcx| "checking that `{}` is well-formed", tcx.def_path_str(key.to_def_id()) }
desc { |tcx| "checking that `{}` is well-formed", tcx.def_path_str(key) }
}
// The `DefId`s of all non-generic functions and statics in the given crate
@ -1441,7 +1437,7 @@ rustc_queries! {
query is_unreachable_local_definition(def_id: LocalDefId) -> bool {
desc { |tcx|
"checking whether `{}` is reachable from outside the crate",
tcx.def_path_str(def_id.to_def_id()),
tcx.def_path_str(def_id),
}
}
@ -1635,7 +1631,7 @@ rustc_queries! {
separate_provide_extern
}
query extern_mod_stmt_cnum(def_id: LocalDefId) -> Option<CrateNum> {
desc { |tcx| "computing crate imported by `{}`", tcx.def_path_str(def_id.to_def_id()) }
desc { |tcx| "computing crate imported by `{}`", tcx.def_path_str(def_id) }
}
query lib_features(_: ()) -> &'tcx LibFeatures {
@ -1735,7 +1731,7 @@ rustc_queries! {
desc { "fetching potentially unused trait imports" }
}
query names_imported_by_glob_use(def_id: LocalDefId) -> &'tcx UnordSet<Symbol> {
desc { |tcx| "finding names imported by glob use for `{}`", tcx.def_path_str(def_id.to_def_id()) }
desc { |tcx| "finding names imported by glob use for `{}`", tcx.def_path_str(def_id) }
}
query stability_index(_: ()) -> &'tcx stability::Index {
@ -2058,7 +2054,7 @@ rustc_queries! {
query compare_impl_const(
key: (LocalDefId, DefId)
) -> Result<(), ErrorGuaranteed> {
desc { |tcx| "checking assoc const `{}` has the same type as trait item", tcx.def_path_str(key.0.to_def_id()) }
desc { |tcx| "checking assoc const `{}` has the same type as trait item", tcx.def_path_str(key.0) }
}
query deduced_param_attrs(def_id: DefId) -> &'tcx [ty::DeducedParamAttrs] {

View file

@ -80,6 +80,8 @@ use std::iter;
use std::mem;
use std::ops::{Bound, Deref};
use super::query::IntoQueryParam;
const TINY_CONST_EVAL_LIMIT: Limit = Limit(20);
pub trait OnDiskCache<'tcx>: rustc_data_structures::sync::Sync {
@ -824,7 +826,8 @@ impl<'tcx> TyCtxt<'tcx> {
self.features_query(())
}
pub fn def_key(self, id: DefId) -> rustc_hir::definitions::DefKey {
pub fn def_key(self, id: impl IntoQueryParam<DefId>) -> rustc_hir::definitions::DefKey {
let id = id.into_query_param();
// Accessing the DefKey is ok, since it is part of DefPathHash.
if let Some(id) = id.as_local() {
self.definitions_untracked().def_key(id)

View file

@ -327,6 +327,6 @@ pub fn describe_as_module(def_id: LocalDefId, tcx: TyCtxt<'_>) -> String {
if def_id.is_top_level_module() {
"top-level module".to_string()
} else {
format!("module `{}`", tcx.def_path_str(def_id.to_def_id()))
format!("module `{}`", tcx.def_path_str(def_id))
}
}

View file

@ -1,4 +1,5 @@
use crate::mir::interpret::{AllocRange, GlobalAlloc, Pointer, Provenance, Scalar};
use crate::ty::query::IntoQueryParam;
use crate::ty::{
self, ConstInt, ParamConst, ScalarInt, Term, TermKind, Ty, TyCtxt, TypeFoldable,
TypeSuperFoldable, TypeSuperVisitable, TypeVisitable, TypeVisitableExt,
@ -1801,17 +1802,27 @@ fn guess_def_namespace(tcx: TyCtxt<'_>, def_id: DefId) -> Namespace {
impl<'t> TyCtxt<'t> {
/// Returns a string identifying this `DefId`. This string is
/// suitable for user output.
pub fn def_path_str(self, def_id: DefId) -> String {
pub fn def_path_str(self, def_id: impl IntoQueryParam<DefId>) -> String {
self.def_path_str_with_substs(def_id, &[])
}
pub fn def_path_str_with_substs(self, def_id: DefId, substs: &'t [GenericArg<'t>]) -> String {
pub fn def_path_str_with_substs(
self,
def_id: impl IntoQueryParam<DefId>,
substs: &'t [GenericArg<'t>],
) -> String {
let def_id = def_id.into_query_param();
let ns = guess_def_namespace(self, def_id);
debug!("def_path_str: def_id={:?}, ns={:?}", def_id, ns);
FmtPrinter::new(self, ns).print_def_path(def_id, substs).unwrap().into_buffer()
}
pub fn value_path_str_with_substs(self, def_id: DefId, substs: &'t [GenericArg<'t>]) -> String {
pub fn value_path_str_with_substs(
self,
def_id: impl IntoQueryParam<DefId>,
substs: &'t [GenericArg<'t>],
) -> String {
let def_id = def_id.into_query_param();
let ns = guess_def_namespace(self, def_id);
debug!("value_path_str: def_id={:?}, ns={:?}", def_id, ns);
FmtPrinter::new(self, ns).print_value_path(def_id, substs).unwrap().into_buffer()

View file

@ -587,7 +587,7 @@ mod sealed {
}
}
use sealed::IntoQueryParam;
pub use sealed::IntoQueryParam;
impl<'tcx> TyCtxt<'tcx> {
pub fn def_kind(self, def_id: impl IntoQueryParam<DefId>) -> DefKind {

View file

@ -158,8 +158,8 @@ pub fn recursive_type_error(
}
let items_list = {
let mut s = String::new();
for (i, (item_id, _)) in item_and_field_ids.iter().enumerate() {
let path = tcx.def_path_str(item_id.to_def_id());
for (i, &(item_id, _)) in item_and_field_ids.iter().enumerate() {
let path = tcx.def_path_str(item_id);
write!(&mut s, "`{path}`").unwrap();
if i == (ITEM_LIMIT - 1) && cycle_len > ITEM_LIMIT {
write!(&mut s, " and {} more", cycle_len - 5).unwrap();