1
Fork 0

Stuff a TyCtxt into the Resolver

This commit is contained in:
Oli Scherer 2022-12-09 22:53:31 +00:00
parent 9fb91b8742
commit 4953d70e2f
5 changed files with 32 additions and 41 deletions

View file

@ -221,12 +221,11 @@ impl<'tcx> Queries<'tcx> {
let arenas = Resolver::arenas(); let arenas = Resolver::arenas();
let mut resolver = Resolver::new( let mut resolver = Resolver::new(
sess, tcx,
&krate, &krate,
crate_name, crate_name,
self.codegen_backend().metadata_loader(), self.codegen_backend().metadata_loader(),
&arenas, &arenas,
tcx.untracked(),
); );
let krate = passes::configure_and_expand( let krate = passes::configure_and_expand(
sess, sess,

View file

@ -155,7 +155,7 @@ impl<'a, 'tcx> Resolver<'a, 'tcx> {
if !candidates.is_empty() { if !candidates.is_empty() {
show_candidates( show_candidates(
&self.tcx.sess, &self.tcx.sess,
&self.untracked.source_span.read(), &self.tcx.untracked().source_span.read(),
&mut err, &mut err,
span, span,
&candidates, &candidates,
@ -688,7 +688,7 @@ impl<'a, 'tcx> Resolver<'a, 'tcx> {
} }
show_candidates( show_candidates(
&self.tcx.sess, &self.tcx.sess,
&self.untracked.source_span.read(), &self.tcx.untracked().source_span.read(),
&mut err, &mut err,
Some(span), Some(span),
&import_suggestions, &import_suggestions,
@ -1353,7 +1353,7 @@ impl<'a, 'tcx> Resolver<'a, 'tcx> {
self.lookup_import_candidates(ident, Namespace::MacroNS, parent_scope, is_expected); self.lookup_import_candidates(ident, Namespace::MacroNS, parent_scope, is_expected);
show_candidates( show_candidates(
&self.tcx.sess, &self.tcx.sess,
&self.untracked.source_span.read(), &self.tcx.untracked().source_span.read(),
err, err,
None, None,
&import_suggestions, &import_suggestions,

View file

@ -110,7 +110,7 @@ impl<'r, 'a, 'tcx> EffectiveVisibilitiesVisitor<'r, 'a, 'tcx> {
r.effective_visibilities.update_eff_vis( r.effective_visibilities.update_eff_vis(
r.local_def_id(node_id), r.local_def_id(node_id),
eff_vis, eff_vis,
ResolverTree(&r.untracked), ResolverTree(&r.tcx.untracked()),
) )
} }
} }

View file

@ -549,7 +549,7 @@ impl<'a, 'b, 'tcx> ImportResolver<'a, 'b, 'tcx> {
match &import.kind { match &import.kind {
ImportKind::Single { nested: false, source, target, .. } => import_candidates( ImportKind::Single { nested: false, source, target, .. } => import_candidates(
self.r.tcx.sess, self.r.tcx.sess,
&self.r.untracked.source_span.read(), &self.r.tcx.untracked().source_span.read(),
&mut diag, &mut diag,
Some(err.span), Some(err.span),
&candidates, &candidates,
@ -562,7 +562,7 @@ impl<'a, 'b, 'tcx> ImportResolver<'a, 'b, 'tcx> {
ImportKind::Single { nested: true, source, target, .. } => { ImportKind::Single { nested: true, source, target, .. } => {
import_candidates( import_candidates(
self.r.tcx.sess, self.r.tcx.sess,
&self.r.untracked.source_span.read(), &self.r.tcx.untracked().source_span.read(),
&mut diag, &mut diag,
None, None,
&candidates, &candidates,

View file

@ -27,7 +27,7 @@ use rustc_ast::{self as ast, NodeId, CRATE_NODE_ID};
use rustc_ast::{AngleBracketedArg, Crate, Expr, ExprKind, GenericArg, GenericArgs, LitKind, Path}; use rustc_ast::{AngleBracketedArg, Crate, Expr, ExprKind, GenericArg, GenericArgs, LitKind, Path};
use rustc_data_structures::fx::{FxHashMap, FxHashSet, FxIndexMap, FxIndexSet}; use rustc_data_structures::fx::{FxHashMap, FxHashSet, FxIndexMap, FxIndexSet};
use rustc_data_structures::intern::Interned; use rustc_data_structures::intern::Interned;
use rustc_data_structures::sync::{Lrc, MappedReadGuard, ReadGuard}; use rustc_data_structures::sync::{Lrc, MappedReadGuard};
use rustc_errors::{Applicability, DiagnosticBuilder, ErrorGuaranteed}; use rustc_errors::{Applicability, DiagnosticBuilder, ErrorGuaranteed};
use rustc_expand::base::{DeriveResolutions, SyntaxExtension, SyntaxExtensionKind}; use rustc_expand::base::{DeriveResolutions, SyntaxExtension, SyntaxExtensionKind};
use rustc_hir::def::Namespace::{self, *}; use rustc_hir::def::Namespace::{self, *};
@ -41,12 +41,11 @@ use rustc_metadata::creader::{CStore, CrateLoader};
use rustc_middle::metadata::ModChild; use rustc_middle::metadata::ModChild;
use rustc_middle::middle::privacy::EffectiveVisibilities; use rustc_middle::middle::privacy::EffectiveVisibilities;
use rustc_middle::span_bug; use rustc_middle::span_bug;
use rustc_middle::ty::{self, DefIdTree, MainDefinition, RegisteredTools}; use rustc_middle::ty::{self, DefIdTree, MainDefinition, RegisteredTools, TyCtxt};
use rustc_middle::ty::{ResolverGlobalCtxt, ResolverOutputs}; use rustc_middle::ty::{ResolverGlobalCtxt, ResolverOutputs};
use rustc_query_system::ich::StableHashingContext; use rustc_query_system::ich::StableHashingContext;
use rustc_session::cstore::{CrateStore, MetadataLoaderDyn, Untracked}; use rustc_session::cstore::{CrateStore, MetadataLoaderDyn, Untracked};
use rustc_session::lint::LintBuffer; use rustc_session::lint::LintBuffer;
use rustc_session::Session;
use rustc_span::hygiene::{ExpnId, LocalExpnId, MacroKind, SyntaxContext, Transparency}; use rustc_span::hygiene::{ExpnId, LocalExpnId, MacroKind, SyntaxContext, Transparency};
use rustc_span::source_map::Spanned; use rustc_span::source_map::Spanned;
use rustc_span::symbol::{kw, sym, Ident, Symbol}; use rustc_span::symbol::{kw, sym, Ident, Symbol};
@ -861,10 +860,6 @@ struct MacroData {
macro_rules: bool, macro_rules: bool,
} }
struct TyCtxt<'tcx> {
sess: &'tcx Session,
}
/// The main resolver class. /// The main resolver class.
/// ///
/// This is the visitor that walks the whole crate. /// This is the visitor that walks the whole crate.
@ -962,7 +957,6 @@ pub struct Resolver<'a, 'tcx> {
local_crate_name: Symbol, local_crate_name: Symbol,
metadata_loader: Box<MetadataLoaderDyn>, metadata_loader: Box<MetadataLoaderDyn>,
untracked: &'tcx Untracked,
used_extern_options: FxHashSet<Symbol>, used_extern_options: FxHashSet<Symbol>,
macro_names: FxHashSet<Ident>, macro_names: FxHashSet<Ident>,
builtin_macros: FxHashMap<Symbol, BuiltinMacroState>, builtin_macros: FxHashMap<Symbol, BuiltinMacroState>,
@ -1141,7 +1135,7 @@ impl DefIdTree for ResolverTree<'_> {
impl<'a, 'b, 'tcx> DefIdTree for &'a Resolver<'b, 'tcx> { impl<'a, 'b, 'tcx> DefIdTree for &'a Resolver<'b, 'tcx> {
#[inline] #[inline]
fn opt_parent(self, id: DefId) -> Option<DefId> { fn opt_parent(self, id: DefId) -> Option<DefId> {
ResolverTree(&self.untracked).opt_parent(id) ResolverTree(&self.tcx.untracked()).opt_parent(id)
} }
} }
@ -1168,10 +1162,11 @@ impl<'tcx> Resolver<'_, 'tcx> {
"adding a def'n for node-id {:?} and data {:?} but a previous def'n exists: {:?}", "adding a def'n for node-id {:?} and data {:?} but a previous def'n exists: {:?}",
node_id, node_id,
data, data,
self.untracked.definitions.read().def_key(self.node_id_to_def_id[&node_id]), self.tcx.definitions_untracked().def_key(self.node_id_to_def_id[&node_id]),
); );
let def_id = self.untracked.definitions.write().create_def(parent, data); // FIXME: remove `def_span` body, pass in the right spans here and call `tcx.at().create_def()`
let def_id = self.tcx.untracked().definitions.write().create_def(parent, data);
// Create the definition. // Create the definition.
if expn_id != ExpnId::root() { if expn_id != ExpnId::root() {
@ -1180,7 +1175,7 @@ impl<'tcx> Resolver<'_, 'tcx> {
// A relative span's parent must be an absolute span. // A relative span's parent must be an absolute span.
debug_assert_eq!(span.data_untracked().parent, None); debug_assert_eq!(span.data_untracked().parent, None);
let _id = self.untracked.source_span.write().push(span); let _id = self.tcx.untracked().source_span.write().push(span);
debug_assert_eq!(_id, def_id); debug_assert_eq!(_id, def_id);
// Some things for which we allocate `LocalDefId`s don't correspond to // Some things for which we allocate `LocalDefId`s don't correspond to
@ -1206,15 +1201,12 @@ impl<'tcx> Resolver<'_, 'tcx> {
impl<'a, 'tcx> Resolver<'a, 'tcx> { impl<'a, 'tcx> Resolver<'a, 'tcx> {
pub fn new( pub fn new(
session: &'tcx Session, tcx: TyCtxt<'tcx>,
krate: &Crate, krate: &Crate,
crate_name: Symbol, crate_name: Symbol,
metadata_loader: Box<MetadataLoaderDyn>, metadata_loader: Box<MetadataLoaderDyn>,
arenas: &'a ResolverArenas<'a>, arenas: &'a ResolverArenas<'a>,
untracked: &'tcx Untracked,
) -> Resolver<'a, 'tcx> { ) -> Resolver<'a, 'tcx> {
let tcx = TyCtxt { sess: session };
let root_def_id = CRATE_DEF_ID.to_def_id(); let root_def_id = CRATE_DEF_ID.to_def_id();
let mut module_map = FxHashMap::default(); let mut module_map = FxHashMap::default();
let graph_root = arenas.new_module( let graph_root = arenas.new_module(
@ -1222,7 +1214,7 @@ impl<'a, 'tcx> Resolver<'a, 'tcx> {
ModuleKind::Def(DefKind::Mod, root_def_id, kw::Empty), ModuleKind::Def(DefKind::Mod, root_def_id, kw::Empty),
ExpnId::root(), ExpnId::root(),
krate.spans.inner_span, krate.spans.inner_span,
session.contains_name(&krate.attrs, sym::no_implicit_prelude), tcx.sess.contains_name(&krate.attrs, sym::no_implicit_prelude),
&mut module_map, &mut module_map,
); );
let empty_module = arenas.new_module( let empty_module = arenas.new_module(
@ -1245,7 +1237,8 @@ impl<'a, 'tcx> Resolver<'a, 'tcx> {
let mut invocation_parents = FxHashMap::default(); let mut invocation_parents = FxHashMap::default();
invocation_parents.insert(LocalExpnId::ROOT, (CRATE_DEF_ID, ImplTraitContext::Existential)); invocation_parents.insert(LocalExpnId::ROOT, (CRATE_DEF_ID, ImplTraitContext::Existential));
let mut extern_prelude: FxHashMap<Ident, ExternPreludeEntry<'_>> = session let mut extern_prelude: FxHashMap<Ident, ExternPreludeEntry<'_>> = tcx
.sess
.opts .opts
.externs .externs
.iter() .iter()
@ -1253,16 +1246,16 @@ impl<'a, 'tcx> Resolver<'a, 'tcx> {
.map(|(name, _)| (Ident::from_str(name), Default::default())) .map(|(name, _)| (Ident::from_str(name), Default::default()))
.collect(); .collect();
if !session.contains_name(&krate.attrs, sym::no_core) { if !tcx.sess.contains_name(&krate.attrs, sym::no_core) {
extern_prelude.insert(Ident::with_dummy_span(sym::core), Default::default()); extern_prelude.insert(Ident::with_dummy_span(sym::core), Default::default());
if !session.contains_name(&krate.attrs, sym::no_std) { if !tcx.sess.contains_name(&krate.attrs, sym::no_std) {
extern_prelude.insert(Ident::with_dummy_span(sym::std), Default::default()); extern_prelude.insert(Ident::with_dummy_span(sym::std), Default::default());
} }
} }
let registered_tools = macros::registered_tools(session, &krate.attrs); let registered_tools = macros::registered_tools(tcx.sess, &krate.attrs);
let features = session.features_untracked(); let features = tcx.sess.features_untracked();
let mut resolver = Resolver { let mut resolver = Resolver {
tcx, tcx,
@ -1322,16 +1315,15 @@ impl<'a, 'tcx> Resolver<'a, 'tcx> {
metadata_loader, metadata_loader,
local_crate_name: crate_name, local_crate_name: crate_name,
used_extern_options: Default::default(), used_extern_options: Default::default(),
untracked,
macro_names: FxHashSet::default(), macro_names: FxHashSet::default(),
builtin_macros: Default::default(), builtin_macros: Default::default(),
builtin_macro_kinds: Default::default(), builtin_macro_kinds: Default::default(),
registered_tools, registered_tools,
macro_use_prelude: FxHashMap::default(), macro_use_prelude: FxHashMap::default(),
macro_map: FxHashMap::default(), macro_map: FxHashMap::default(),
dummy_ext_bang: Lrc::new(SyntaxExtension::dummy_bang(session.edition())), dummy_ext_bang: Lrc::new(SyntaxExtension::dummy_bang(tcx.sess.edition())),
dummy_ext_derive: Lrc::new(SyntaxExtension::dummy_derive(session.edition())), dummy_ext_derive: Lrc::new(SyntaxExtension::dummy_derive(tcx.sess.edition())),
non_macro_attr: Lrc::new(SyntaxExtension::non_macro_attr(session.edition())), non_macro_attr: Lrc::new(SyntaxExtension::non_macro_attr(tcx.sess.edition())),
invocation_parent_scopes: Default::default(), invocation_parent_scopes: Default::default(),
output_macro_rules_scopes: Default::default(), output_macro_rules_scopes: Default::default(),
macro_rules_scopes: Default::default(), macro_rules_scopes: Default::default(),
@ -1469,7 +1461,7 @@ impl<'a, 'tcx> Resolver<'a, 'tcx> {
} }
fn create_stable_hashing_context(&self) -> StableHashingContext<'_> { fn create_stable_hashing_context(&self) -> StableHashingContext<'_> {
StableHashingContext::new(self.tcx.sess, self.untracked) StableHashingContext::new(self.tcx.sess, self.tcx.untracked())
} }
fn crate_loader<T>(&mut self, f: impl FnOnce(&mut CrateLoader<'_>) -> T) -> T { fn crate_loader<T>(&mut self, f: impl FnOnce(&mut CrateLoader<'_>) -> T) -> T {
@ -1477,14 +1469,14 @@ impl<'a, 'tcx> Resolver<'a, 'tcx> {
&self.tcx.sess, &self.tcx.sess,
&*self.metadata_loader, &*self.metadata_loader,
self.local_crate_name, self.local_crate_name,
&mut *self.untracked.cstore.write().untracked_as_any().downcast_mut().unwrap(), &mut *self.tcx.untracked().cstore.write().untracked_as_any().downcast_mut().unwrap(),
self.untracked.definitions.read(), self.tcx.definitions_untracked(),
&mut self.used_extern_options, &mut self.used_extern_options,
)) ))
} }
fn cstore(&self) -> MappedReadGuard<'_, CStore> { fn cstore(&self) -> MappedReadGuard<'_, CStore> {
ReadGuard::map(self.untracked.cstore.read(), |r| r.as_any().downcast_ref().unwrap()) CStore::from_tcx(self.tcx)
} }
fn dummy_ext(&self, macro_kind: MacroKind) -> Lrc<SyntaxExtension> { fn dummy_ext(&self, macro_kind: MacroKind) -> Lrc<SyntaxExtension> {
@ -1535,7 +1527,7 @@ impl<'a, 'tcx> Resolver<'a, 'tcx> {
}); });
// Make sure we don't mutate the cstore from here on. // Make sure we don't mutate the cstore from here on.
self.untracked.cstore.leak(); self.tcx.untracked().cstore.leak();
} }
fn traits_in_scope( fn traits_in_scope(
@ -1925,14 +1917,14 @@ impl<'a, 'tcx> Resolver<'a, 'tcx> {
/// Retrieves the span of the given `DefId` if `DefId` is in the local crate. /// Retrieves the span of the given `DefId` if `DefId` is in the local crate.
#[inline] #[inline]
fn opt_span(&self, def_id: DefId) -> Option<Span> { fn opt_span(&self, def_id: DefId) -> Option<Span> {
def_id.as_local().map(|def_id| self.untracked.source_span.read()[def_id]) def_id.as_local().map(|def_id| self.tcx.source_span(def_id))
} }
/// Retrieves the name of the given `DefId`. /// Retrieves the name of the given `DefId`.
#[inline] #[inline]
fn opt_name(&self, def_id: DefId) -> Option<Symbol> { fn opt_name(&self, def_id: DefId) -> Option<Symbol> {
let def_key = match def_id.as_local() { let def_key = match def_id.as_local() {
Some(def_id) => self.untracked.definitions.read().def_key(def_id), Some(def_id) => self.tcx.definitions_untracked().def_key(def_id),
None => self.cstore().def_key(def_id), None => self.cstore().def_key(def_id),
}; };
def_key.get_opt_name() def_key.get_opt_name()