Skip to content

Commit

Permalink
Auto merge of rust-lang#123099 - oli-obk:span_tcx, r=petrochenkov
Browse files Browse the repository at this point in the history
Replace some `CrateStore` trait methods with hooks.

Just like with the `CrateStore` trait, this avoids the cyclic definition issues with `CStore` being
defined after TyCtxt, but needing to be used in TyCtxt.
  • Loading branch information
bors committed Apr 9, 2024
2 parents 59c808f + 24fc6e9 commit bd12986
Show file tree
Hide file tree
Showing 5 changed files with 46 additions and 54 deletions.
38 changes: 20 additions & 18 deletions compiler/rustc_metadata/src/rmeta/decoder/cstore_impl.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ use rustc_middle::ty::{self, TyCtxt};
use rustc_middle::util::Providers;
use rustc_session::cstore::{CrateStore, ExternCrate};
use rustc_session::{Session, StableCrateId};
use rustc_span::hygiene::{ExpnHash, ExpnId};
use rustc_span::hygiene::ExpnId;
use rustc_span::symbol::{kw, Symbol};
use rustc_span::Span;

Expand Down Expand Up @@ -378,6 +378,7 @@ provide! { tcx, def_id, other, cdata,
}

pub(in crate::rmeta) fn provide(providers: &mut Providers) {
provide_cstore_hooks(providers);
// FIXME(#44234) - almost all of these queries have no sub-queries and
// therefore no actual inputs, they're just reading tables calculated in
// resolve! Does this work? Unsure! That's what the issue is about
Expand Down Expand Up @@ -649,26 +650,27 @@ impl CrateStore for CStore {
fn def_path_hash(&self, def: DefId) -> DefPathHash {
self.get_crate_data(def.krate).def_path_hash(def.index)
}
}

fn def_path_hash_to_def_id(&self, cnum: CrateNum, hash: DefPathHash) -> DefId {
let def_index = self.get_crate_data(cnum).def_path_hash_to_def_index(hash);
fn provide_cstore_hooks(providers: &mut Providers) {
providers.hooks.def_path_hash_to_def_id_extern = |tcx, hash, stable_crate_id| {
// If this is a DefPathHash from an upstream crate, let the CrateStore map
// it to a DefId.
let cstore = CStore::from_tcx(tcx.tcx);
let cnum = cstore.stable_crate_id_to_crate_num(stable_crate_id);
let def_index = cstore.get_crate_data(cnum).def_path_hash_to_def_index(hash);
DefId { krate: cnum, index: def_index }
}

fn expn_hash_to_expn_id(
&self,
sess: &Session,
cnum: CrateNum,
index_guess: u32,
hash: ExpnHash,
) -> ExpnId {
self.get_crate_data(cnum).expn_hash_to_expn_id(sess, index_guess, hash)
}
};

fn import_source_files(&self, sess: &Session, cnum: CrateNum) {
let cdata = self.get_crate_data(cnum);
providers.hooks.expn_hash_to_expn_id = |tcx, cnum, index_guess, hash| {
let cstore = CStore::from_tcx(tcx.tcx);
cstore.get_crate_data(cnum).expn_hash_to_expn_id(tcx.sess, index_guess, hash)
};
providers.hooks.import_source_files = |tcx, cnum| {
let cstore = CStore::from_tcx(tcx.tcx);
let cdata = cstore.get_crate_data(cnum);
for file_index in 0..cdata.root.source_map.size() {
cdata.imported_source_file(file_index as u32, sess);
cdata.imported_source_file(file_index as u32, tcx.sess);
}
}
};
}
27 changes: 23 additions & 4 deletions compiler/rustc_middle/src/hooks/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,10 @@
use crate::mir;
use crate::query::TyCtxtAt;
use crate::ty::{Ty, TyCtxt};
use rustc_span::def_id::LocalDefId;
use rustc_span::DUMMY_SP;
use rustc_hir::def_id::{DefId, DefPathHash};
use rustc_session::StableCrateId;
use rustc_span::def_id::{CrateNum, LocalDefId};
use rustc_span::{ExpnHash, ExpnId, DUMMY_SP};

macro_rules! declare_hooks {
($($(#[$attr:meta])*hook $name:ident($($arg:ident: $K:ty),*) -> $V:ty;)*) => {
Expand All @@ -16,7 +18,6 @@ macro_rules! declare_hooks {
$(
$(#[$attr])*
#[inline(always)]
#[must_use]
pub fn $name(self, $($arg: $K,)*) -> $V
{
self.at(DUMMY_SP).$name($($arg,)*)
Expand All @@ -28,7 +29,6 @@ macro_rules! declare_hooks {
$(
$(#[$attr])*
#[inline(always)]
#[must_use]
#[instrument(level = "debug", skip(self), ret)]
pub fn $name(self, $($arg: $K,)*) -> $V
{
Expand Down Expand Up @@ -83,4 +83,23 @@ declare_hooks! {
/// You do not want to call this yourself, instead use the cached version
/// via `mir_built`
hook build_mir(key: LocalDefId) -> mir::Body<'tcx>;


/// Imports all `SourceFile`s from the given crate into the current session.
/// This normally happens automatically when we decode a `Span` from
/// that crate's metadata - however, the incr comp cache needs
/// to trigger this manually when decoding a foreign `Span`
hook import_source_files(key: CrateNum) -> ();

hook expn_hash_to_expn_id(
cnum: CrateNum,
index_guess: u32,
hash: ExpnHash
) -> ExpnId;

/// Converts a `DefPathHash` to its corresponding `DefId` in the current compilation
/// session, if it still exists. This is used during incremental compilation to
/// turn a deserialized `DefPathHash` into its current `DefId`.
/// Will fetch a DefId from a DefPathHash for a foreign crate.
hook def_path_hash_to_def_id_extern(hash: DefPathHash, stable_crate_id: StableCrateId) -> DefId;
}
11 changes: 2 additions & 9 deletions compiler/rustc_middle/src/query/on_disk_cache.rs
Original file line number Diff line number Diff line change
Expand Up @@ -492,9 +492,7 @@ impl<'a, 'tcx> CacheDecoder<'a, 'tcx> {
// expansion, so we use `import_source_files` to ensure that the foreign
// source files are actually imported before we call `source_file_by_stable_id`.
if source_file_cnum != LOCAL_CRATE {
self.tcx
.cstore_untracked()
.import_source_files(self.tcx.sess, source_file_cnum);
self.tcx.import_source_files(source_file_cnum);
}

source_map
Expand Down Expand Up @@ -634,12 +632,7 @@ impl<'a, 'tcx> SpanDecoder for CacheDecoder<'a, 'tcx> {
expn_id
} else {
let index_guess = self.foreign_expn_data[&hash];
self.tcx.cstore_untracked().expn_hash_to_expn_id(
self.tcx.sess,
krate,
index_guess,
hash,
)
self.tcx.expn_hash_to_expn_id(krate, index_guess, hash)
};

debug_assert_eq!(expn_id.krate, krate);
Expand Down
6 changes: 1 addition & 5 deletions compiler/rustc_middle/src/ty/context.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1165,11 +1165,7 @@ impl<'tcx> TyCtxt<'tcx> {
.local_def_path_hash_to_def_id(hash, err_msg)
.to_def_id()
} else {
// If this is a DefPathHash from an upstream crate, let the CrateStore map
// it to a DefId.
let cstore = &*self.cstore_untracked();
let cnum = cstore.stable_crate_id_to_crate_num(stable_crate_id);
cstore.def_path_hash_to_def_id(cnum, hash)
self.def_path_hash_to_def_id_extern(hash, stable_crate_id)
}
}

Expand Down
18 changes: 0 additions & 18 deletions compiler/rustc_session/src/cstore.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,10 @@

use crate::search_paths::PathKind;
use crate::utils::NativeLibKind;
use crate::Session;
use rustc_ast as ast;
use rustc_data_structures::sync::{self, AppendOnlyIndexVec, FreezeLock};
use rustc_hir::def_id::{CrateNum, DefId, LocalDefId, StableCrateId, LOCAL_CRATE};
use rustc_hir::definitions::{DefKey, DefPath, DefPathHash, Definitions};
use rustc_span::hygiene::{ExpnHash, ExpnId};
use rustc_span::symbol::Symbol;
use rustc_span::Span;
use rustc_target::spec::abi::Abi;
Expand Down Expand Up @@ -220,22 +218,6 @@ pub trait CrateStore: std::fmt::Debug {
fn crate_name(&self, cnum: CrateNum) -> Symbol;
fn stable_crate_id(&self, cnum: CrateNum) -> StableCrateId;
fn stable_crate_id_to_crate_num(&self, stable_crate_id: StableCrateId) -> CrateNum;

/// Fetch a DefId from a DefPathHash for a foreign crate.
fn def_path_hash_to_def_id(&self, cnum: CrateNum, hash: DefPathHash) -> DefId;
fn expn_hash_to_expn_id(
&self,
sess: &Session,
cnum: CrateNum,
index_guess: u32,
hash: ExpnHash,
) -> ExpnId;

/// Imports all `SourceFile`s from the given crate into the current session.
/// This normally happens automatically when we decode a `Span` from
/// that crate's metadata - however, the incr comp cache needs
/// to trigger this manually when decoding a foreign `Span`
fn import_source_files(&self, sess: &Session, cnum: CrateNum);
}

pub type CrateStoreDyn = dyn CrateStore + sync::DynSync + sync::DynSend;
Expand Down

0 comments on commit bd12986

Please sign in to comment.