Separate the lifetime of the session and the arena in the resolver

This commit is contained in:
Oli Scherer 2022-12-08 12:59:02 +00:00
parent e9ab7872fd
commit 43a5cc383d
12 changed files with 79 additions and 70 deletions

View file

@ -82,7 +82,7 @@ mod boxed_resolver {
struct BoxedResolverInner {
session: Lrc<Session>,
resolver_arenas: Option<ResolverArenas<'static>>,
resolver: Option<Resolver<'static>>,
resolver: Option<Resolver<'static, 'static>>,
_pin: PhantomPinned,
}
@ -98,7 +98,10 @@ mod boxed_resolver {
impl BoxedResolver {
pub(super) fn new(
session: Lrc<Session>,
make_resolver: impl for<'a> FnOnce(&'a Session, &'a ResolverArenas<'a>) -> Resolver<'a>,
make_resolver: impl for<'a, 'tcx> FnOnce(
&'tcx Session,
&'a ResolverArenas<'a>,
) -> Resolver<'a, 'tcx>,
) -> BoxedResolver {
let mut boxed_resolver = Box::new(BoxedResolverInner {
session,
@ -121,7 +124,10 @@ mod boxed_resolver {
}
}
pub fn access<F: for<'a> FnOnce(&mut Resolver<'a>) -> R, R>(&mut self, f: F) -> R {
pub fn access<F: for<'a, 'tcx> FnOnce(&mut Resolver<'a, 'tcx>) -> R, R>(
&mut self,
f: F,
) -> R {
// SAFETY: The resolver doesn't need to be pinned.
let mut resolver = unsafe {
self.0.as_mut().map_unchecked_mut(|boxed_resolver| &mut boxed_resolver.resolver)
@ -256,7 +262,7 @@ pub fn configure_and_expand(
lint_store: &LintStore,
mut krate: ast::Crate,
crate_name: Symbol,
resolver: &mut Resolver<'_>,
resolver: &mut Resolver<'_, '_>,
) -> Result<ast::Crate> {
trace!("configure_and_expand");
pre_expansion_lint(sess, lint_store, resolver.registered_tools(), &krate, crate_name);

View file

@ -65,7 +65,7 @@ impl<'a, Id: Into<DefId>> ToNameBinding<'a> for (Res, ty::Visibility<Id>, Span,
}
}
impl<'a> Resolver<'a> {
impl<'a, 'tcx> Resolver<'a, 'tcx> {
/// Defines `name` in namespace `ns` of module `parent` to be `def` if it is not yet defined;
/// otherwise, reports an error.
pub(crate) fn define<T>(&mut self, parent: Module<'a>, ident: Ident, ns: Namespace, def: T)
@ -214,18 +214,18 @@ impl<'a> Resolver<'a> {
}
}
struct BuildReducedGraphVisitor<'a, 'b> {
r: &'b mut Resolver<'a>,
struct BuildReducedGraphVisitor<'a, 'b, 'tcx> {
r: &'b mut Resolver<'a, 'tcx>,
parent_scope: ParentScope<'a>,
}
impl<'a> AsMut<Resolver<'a>> for BuildReducedGraphVisitor<'a, '_> {
fn as_mut(&mut self) -> &mut Resolver<'a> {
impl<'a, 'tcx> AsMut<Resolver<'a, 'tcx>> for BuildReducedGraphVisitor<'a, '_, 'tcx> {
fn as_mut(&mut self) -> &mut Resolver<'a, 'tcx> {
self.r
}
}
impl<'a, 'b> BuildReducedGraphVisitor<'a, 'b> {
impl<'a, 'b, 'tcx> BuildReducedGraphVisitor<'a, 'b, 'tcx> {
fn resolve_visibility(&mut self, vis: &ast::Visibility) -> ty::Visibility {
self.try_resolve_visibility(vis, true).unwrap_or_else(|err| {
self.r.report_vis_error(err);
@ -1315,7 +1315,7 @@ macro_rules! method {
};
}
impl<'a, 'b> Visitor<'b> for BuildReducedGraphVisitor<'a, 'b> {
impl<'a, 'b, 'tcx> Visitor<'b> for BuildReducedGraphVisitor<'a, 'b, 'tcx> {
method!(visit_expr: ast::Expr, ast::ExprKind::MacCall, walk_expr);
method!(visit_pat: ast::Pat, ast::PatKind::MacCall, walk_pat);
method!(visit_ty: ast::Ty, ast::TyKind::MacCall, walk_ty);

View file

@ -49,8 +49,8 @@ impl<'a> UnusedImport<'a> {
}
}
struct UnusedImportCheckVisitor<'a, 'b> {
r: &'a mut Resolver<'b>,
struct UnusedImportCheckVisitor<'a, 'b, 'tcx> {
r: &'a mut Resolver<'b, 'tcx>,
/// All the (so far) unused imports, grouped path list
unused_imports: FxIndexMap<ast::NodeId, UnusedImport<'a>>,
base_use_tree: Option<&'a ast::UseTree>,
@ -58,7 +58,7 @@ struct UnusedImportCheckVisitor<'a, 'b> {
item_span: Span,
}
impl<'a, 'b> UnusedImportCheckVisitor<'a, 'b> {
impl<'a, 'b, 'tcx> UnusedImportCheckVisitor<'a, 'b, 'tcx> {
// We have information about whether `use` (import) items are actually
// used now. If an import is not used at all, we signal a lint error.
fn check_import(&mut self, id: ast::NodeId) {
@ -94,7 +94,7 @@ impl<'a, 'b> UnusedImportCheckVisitor<'a, 'b> {
}
}
impl<'a, 'b> Visitor<'a> for UnusedImportCheckVisitor<'a, 'b> {
impl<'a, 'b, 'tcx> Visitor<'a> for UnusedImportCheckVisitor<'a, 'b, 'tcx> {
fn visit_item(&mut self, item: &'a ast::Item) {
self.item_span = item.span_with_attributes();
@ -222,7 +222,7 @@ fn calc_unused_spans(
}
}
impl Resolver<'_> {
impl Resolver<'_, '_> {
pub(crate) fn check_unused(&mut self, krate: &ast::Crate) {
for import in self.potentially_unused_imports.iter() {
match import.kind {

View file

@ -9,7 +9,7 @@ use rustc_span::symbol::sym;
use rustc_span::Span;
pub(crate) fn collect_definitions(
resolver: &mut Resolver<'_>,
resolver: &mut Resolver<'_, '_>,
fragment: &AstFragment,
expansion: LocalExpnId,
) {
@ -18,14 +18,14 @@ pub(crate) fn collect_definitions(
}
/// Creates `DefId`s for nodes in the AST.
struct DefCollector<'a, 'b> {
resolver: &'a mut Resolver<'b>,
struct DefCollector<'a, 'b, 'tcx> {
resolver: &'a mut Resolver<'b, 'tcx>,
parent_def: LocalDefId,
impl_trait_context: ImplTraitContext,
expansion: LocalExpnId,
}
impl<'a, 'b> DefCollector<'a, 'b> {
impl<'a, 'b, 'tcx> DefCollector<'a, 'b, 'tcx> {
fn create_def(&mut self, node_id: NodeId, data: DefPathData, span: Span) -> LocalDefId {
let parent_def = self.parent_def;
debug!("create_def(node_id={:?}, data={:?}, parent_def={:?})", node_id, data, parent_def);
@ -81,7 +81,7 @@ impl<'a, 'b> DefCollector<'a, 'b> {
}
}
impl<'a, 'b> visit::Visitor<'a> for DefCollector<'a, 'b> {
impl<'a, 'b, 'tcx> visit::Visitor<'a> for DefCollector<'a, 'b, 'tcx> {
fn visit_item(&mut self, i: &'a Item) {
debug!("visit_item: {:?}", i);

View file

@ -114,7 +114,7 @@ fn reduce_impl_span_to_impl_keyword(sm: &SourceMap, impl_span: Span) -> Span {
sm.span_until_whitespace(impl_span)
}
impl<'a> Resolver<'a> {
impl<'a, 'tcx> Resolver<'a, 'tcx> {
pub(crate) fn report_errors(&mut self, krate: &Crate) {
self.report_with_use_injections(krate);
@ -1883,7 +1883,7 @@ impl<'a> Resolver<'a> {
}
}
impl<'a, 'b> ImportResolver<'a, 'b> {
impl<'a, 'b, 'tcx> ImportResolver<'a, 'b, 'tcx> {
/// Adds suggestions for a path that cannot be resolved.
pub(crate) fn make_path_suggestion(
&mut self,

View file

@ -29,8 +29,8 @@ impl ParentId<'_> {
}
}
pub(crate) struct EffectiveVisibilitiesVisitor<'r, 'a> {
r: &'r mut Resolver<'a>,
pub(crate) struct EffectiveVisibilitiesVisitor<'r, 'a, 'tcx> {
r: &'r mut Resolver<'a, 'tcx>,
def_effective_visibilities: EffectiveVisibilities,
/// While walking import chains we need to track effective visibilities per-binding, and def id
/// keys in `Resolver::effective_visibilities` are not enough for that, because multiple
@ -41,7 +41,7 @@ pub(crate) struct EffectiveVisibilitiesVisitor<'r, 'a> {
changed: bool,
}
impl Resolver<'_> {
impl Resolver<'_, '_> {
fn nearest_normal_mod(&mut self, def_id: LocalDefId) -> LocalDefId {
self.get_nearest_non_block_module(def_id.to_def_id()).nearest_parent_mod().expect_local()
}
@ -67,18 +67,21 @@ impl Resolver<'_> {
}
}
impl<'a, 'b> IntoDefIdTree for &'b mut Resolver<'a> {
type Tree = &'b Resolver<'a>;
impl<'a, 'b, 'tcx> IntoDefIdTree for &'b mut Resolver<'a, 'tcx> {
type Tree = &'b Resolver<'a, 'tcx>;
fn tree(self) -> Self::Tree {
self
}
}
impl<'r, 'a> EffectiveVisibilitiesVisitor<'r, 'a> {
impl<'r, 'a, 'tcx> EffectiveVisibilitiesVisitor<'r, 'a, 'tcx> {
/// Fills the `Resolver::effective_visibilities` table with public & exported items
/// For now, this doesn't resolve macros (FIXME) and cannot resolve Impl, as we
/// need access to a TyCtxt for that.
pub(crate) fn compute_effective_visibilities<'c>(r: &'r mut Resolver<'a>, krate: &'c Crate) {
pub(crate) fn compute_effective_visibilities<'c>(
r: &'r mut Resolver<'a, 'tcx>,
krate: &'c Crate,
) {
let mut visitor = EffectiveVisibilitiesVisitor {
r,
def_effective_visibilities: Default::default(),
@ -192,7 +195,7 @@ impl<'r, 'a> EffectiveVisibilitiesVisitor<'r, 'a> {
}
}
impl<'r, 'ast> Visitor<'ast> for EffectiveVisibilitiesVisitor<'ast, 'r> {
impl<'r, 'ast, 'tcx> Visitor<'ast> for EffectiveVisibilitiesVisitor<'ast, 'r, 'tcx> {
fn visit_item(&mut self, item: &'ast ast::Item) {
let def_id = self.r.local_def_id(item.id);
// Update effective visibilities of nested items.

View file

@ -28,7 +28,7 @@ use RibKind::*;
type Visibility = ty::Visibility<LocalDefId>;
impl<'a> Resolver<'a> {
impl<'a, 'tcx> Resolver<'a, 'tcx> {
/// A generic scope visitor.
/// Visits scopes in order to resolve some identifier in them or perform other actions.
/// If the callback returns `Some` result, we stop visiting scopes and return it.

View file

@ -225,7 +225,7 @@ fn pub_use_of_private_extern_crate_hack(import: &Import<'_>, binding: &NameBindi
}
}
impl<'a> Resolver<'a> {
impl<'a, 'tcx> Resolver<'a, 'tcx> {
/// Given a binding and an import that resolves to it,
/// return the corresponding binding defined by the import.
pub(crate) fn import(
@ -333,7 +333,7 @@ impl<'a> Resolver<'a> {
// If the resolution becomes a success, define it in the module's glob importers.
fn update_resolution<T, F>(&mut self, module: Module<'a>, key: BindingKey, f: F) -> T
where
F: FnOnce(&mut Resolver<'a>, &mut NameResolution<'a>) -> T,
F: FnOnce(&mut Resolver<'a, 'tcx>, &mut NameResolution<'a>) -> T,
{
// Ensure that `resolution` isn't borrowed when defining in the module's glob importers,
// during which the resolution might end up getting re-defined via a glob cycle.
@ -405,11 +405,11 @@ struct UnresolvedImportError {
candidates: Option<Vec<ImportSuggestion>>,
}
pub(crate) struct ImportResolver<'a, 'b> {
pub r: &'a mut Resolver<'b>,
pub(crate) struct ImportResolver<'a, 'b, 'tcx> {
pub r: &'a mut Resolver<'b, 'tcx>,
}
impl<'a, 'b> ImportResolver<'a, 'b> {
impl<'a, 'b, 'tcx> ImportResolver<'a, 'b, 'tcx> {
// Import resolution
//
// This is a fixed-point algorithm. We resolve imports until our efforts

View file

@ -505,7 +505,7 @@ enum MaybeExported<'a> {
}
impl MaybeExported<'_> {
fn eval(self, r: &Resolver<'_>) -> bool {
fn eval(self, r: &Resolver<'_, '_>) -> bool {
let def_id = match self {
MaybeExported::Ok(node_id) => Some(r.local_def_id(node_id)),
MaybeExported::Impl(Some(trait_def_id)) | MaybeExported::ImplItem(Ok(trait_def_id)) => {
@ -584,8 +584,8 @@ struct DiagnosticMetadata<'ast> {
current_elision_failures: Vec<MissingLifetime>,
}
struct LateResolutionVisitor<'a, 'b, 'ast> {
r: &'b mut Resolver<'a>,
struct LateResolutionVisitor<'a, 'b, 'ast, 'tcx> {
r: &'b mut Resolver<'a, 'tcx>,
/// The module that represents the current item scope.
parent_scope: ParentScope<'a>,
@ -628,7 +628,7 @@ struct LateResolutionVisitor<'a, 'b, 'ast> {
}
/// Walks the whole crate in DFS order, visiting each item, resolving names as it goes.
impl<'a: 'ast, 'ast> Visitor<'ast> for LateResolutionVisitor<'a, '_, 'ast> {
impl<'a: 'ast, 'ast, 'tcx> Visitor<'ast> for LateResolutionVisitor<'a, '_, 'ast, 'tcx> {
fn visit_attribute(&mut self, _: &'ast Attribute) {
// We do not want to resolve expressions that appear in attributes,
// as they do not correspond to actual code.
@ -1199,8 +1199,8 @@ impl<'a: 'ast, 'ast> Visitor<'ast> for LateResolutionVisitor<'a, '_, 'ast> {
}
}
impl<'a: 'ast, 'b, 'ast> LateResolutionVisitor<'a, 'b, 'ast> {
fn new(resolver: &'b mut Resolver<'a>) -> LateResolutionVisitor<'a, 'b, 'ast> {
impl<'a: 'ast, 'b, 'ast, 'tcx> LateResolutionVisitor<'a, 'b, 'ast, 'tcx> {
fn new(resolver: &'b mut Resolver<'a, 'tcx>) -> LateResolutionVisitor<'a, 'b, 'ast, 'tcx> {
// During late resolution we only track the module component of the parent scope,
// although it may be useful to track other components as well for diagnostics.
let graph_root = resolver.graph_root;
@ -2029,13 +2029,13 @@ impl<'a: 'ast, 'b, 'ast> LateResolutionVisitor<'a, 'b, 'ast> {
/// List all the lifetimes that appear in the provided type.
fn find_lifetime_for_self(&self, ty: &'ast Ty) -> Set1<LifetimeRes> {
struct SelfVisitor<'r, 'a> {
r: &'r Resolver<'a>,
struct SelfVisitor<'r, 'a, 'tcx> {
r: &'r Resolver<'a, 'tcx>,
impl_self: Option<Res>,
lifetime: Set1<LifetimeRes>,
}
impl SelfVisitor<'_, '_> {
impl SelfVisitor<'_, '_, '_> {
// Look for `self: &'a Self` - also desugared from `&'a self`,
// and if that matches, use it for elision and return early.
fn is_self_ty(&self, ty: &Ty) -> bool {
@ -2053,7 +2053,7 @@ impl<'a: 'ast, 'b, 'ast> LateResolutionVisitor<'a, 'b, 'ast> {
}
}
impl<'a> Visitor<'a> for SelfVisitor<'_, '_> {
impl<'a> Visitor<'a> for SelfVisitor<'_, '_, '_> {
fn visit_ty(&mut self, ty: &'a Ty) {
trace!("SelfVisitor considering ty={:?}", ty);
if let TyKind::Ref(lt, ref mt) = ty.kind && self.is_self_ty(&mt.ty) {
@ -4288,13 +4288,13 @@ impl<'a: 'ast, 'b, 'ast> LateResolutionVisitor<'a, 'b, 'ast> {
}
}
struct LifetimeCountVisitor<'a, 'b> {
r: &'b mut Resolver<'a>,
struct LifetimeCountVisitor<'a, 'b, 'tcx> {
r: &'b mut Resolver<'a, 'tcx>,
}
/// Walks the whole crate in DFS order, visiting each item, counting the declared number of
/// lifetime generic parameters.
impl<'ast> Visitor<'ast> for LifetimeCountVisitor<'_, '_> {
impl<'ast> Visitor<'ast> for LifetimeCountVisitor<'_, '_, '_> {
fn visit_item(&mut self, item: &'ast Item) {
match &item.kind {
ItemKind::TyAlias(box TyAlias { ref generics, .. })
@ -4328,7 +4328,7 @@ impl<'ast> Visitor<'ast> for LifetimeCountVisitor<'_, '_> {
}
}
impl<'a> Resolver<'a> {
impl<'a, 'tcx> Resolver<'a, 'tcx> {
pub(crate) fn late_resolve_crate(&mut self, krate: &Crate) {
visit::walk_crate(&mut LifetimeCountVisitor { r: self }, krate);
let mut late_resolution_visitor = LateResolutionVisitor::new(self);

View file

@ -166,7 +166,7 @@ impl TypoCandidate {
}
}
impl<'a: 'ast, 'ast> LateResolutionVisitor<'a, '_, 'ast> {
impl<'a: 'ast, 'ast, 'tcx> LateResolutionVisitor<'a, '_, 'ast, 'tcx> {
fn def_span(&self, def_id: DefId) -> Option<Span> {
match def_id.krate {
LOCAL_CRATE => self.r.opt_span(def_id),
@ -318,7 +318,7 @@ impl<'a: 'ast, 'ast> LateResolutionVisitor<'a, '_, 'ast> {
span: Span,
source: PathSource<'_>,
res: Option<Res>,
) -> (DiagnosticBuilder<'a, ErrorGuaranteed>, Vec<ImportSuggestion>) {
) -> (DiagnosticBuilder<'tcx, ErrorGuaranteed>, Vec<ImportSuggestion>) {
debug!(?res, ?source);
let base_error = self.make_base_error(path, span, source, res);
let code = source.error_code(res.is_some());

View file

@ -147,7 +147,7 @@ struct ParentScope<'a> {
impl<'a> ParentScope<'a> {
/// Creates a parent scope with the passed argument used as the module scope component,
/// and other scope components set to default empty values.
fn module(module: Module<'a>, resolver: &Resolver<'a>) -> ParentScope<'a> {
fn module(module: Module<'a>, resolver: &Resolver<'a, '_>) -> ParentScope<'a> {
ParentScope {
module,
expansion: LocalExpnId::ROOT,
@ -528,9 +528,9 @@ impl<'a> ModuleData<'a> {
}
}
fn for_each_child<R, F>(&'a self, resolver: &mut R, mut f: F)
fn for_each_child<'tcx, R, F>(&'a self, resolver: &mut R, mut f: F)
where
R: AsMut<Resolver<'a>>,
R: AsMut<Resolver<'a, 'tcx>>,
F: FnMut(&mut R, Ident, Namespace, &'a NameBinding<'a>),
{
for (key, name_resolution) in resolver.as_mut().resolutions(self).borrow().iter() {
@ -541,9 +541,9 @@ impl<'a> ModuleData<'a> {
}
/// This modifies `self` in place. The traits will be stored in `self.traits`.
fn ensure_traits<R>(&'a self, resolver: &mut R)
fn ensure_traits<'tcx, R>(&'a self, resolver: &mut R)
where
R: AsMut<Resolver<'a>>,
R: AsMut<Resolver<'a, 'tcx>>,
{
let mut traits = self.traits.borrow_mut();
if traits.is_none() {
@ -864,8 +864,8 @@ struct MacroData {
/// The main resolver class.
///
/// This is the visitor that walks the whole crate.
pub struct Resolver<'a> {
session: &'a Session,
pub struct Resolver<'a, 'tcx> {
session: &'tcx Session,
/// Item with a given `LocalDefId` was defined during macro expansion with ID `ExpnId`.
expn_that_defined: FxHashMap<LocalDefId, ExpnId>,
@ -949,7 +949,7 @@ pub struct Resolver<'a> {
/// Ambiguity errors are delayed for deduplication.
ambiguity_errors: Vec<AmbiguityError<'a>>,
/// `use` injections are delayed for better placement and deduplication.
use_injections: Vec<UseError<'a>>,
use_injections: Vec<UseError<'tcx>>,
/// Crate-local macro expanded `macro_export` referred to by a module-relative path.
macro_expanded_macro_export_errors: BTreeSet<(Span, Span)>,
@ -1111,8 +1111,8 @@ impl<'a> ResolverArenas<'a> {
}
}
impl<'a> AsMut<Resolver<'a>> for Resolver<'a> {
fn as_mut(&mut self) -> &mut Resolver<'a> {
impl<'a, 'tcx> AsMut<Resolver<'a, 'tcx>> for Resolver<'a, 'tcx> {
fn as_mut(&mut self) -> &mut Resolver<'a, 'tcx> {
self
}
}
@ -1134,14 +1134,14 @@ impl DefIdTree for ResolverTree<'_> {
}
}
impl<'a, 'b> DefIdTree for &'a Resolver<'b> {
impl<'a, 'b, 'tcx> DefIdTree for &'a Resolver<'b, 'tcx> {
#[inline]
fn opt_parent(self, id: DefId) -> Option<DefId> {
ResolverTree(&self.untracked).opt_parent(id)
}
}
impl<'a> Resolver<'a> {
impl<'tcx> Resolver<'_, 'tcx> {
fn opt_local_def_id(&self, node: NodeId) -> Option<LocalDefId> {
self.node_id_to_def_id.get(&node).copied()
}
@ -1200,14 +1200,14 @@ impl<'a> Resolver<'a> {
}
}
impl<'a> Resolver<'a> {
impl<'a, 'tcx> Resolver<'a, 'tcx> {
pub fn new(
session: &'a Session,
session: &'tcx Session,
krate: &Crate,
crate_name: Symbol,
metadata_loader: Box<MetadataLoaderDyn>,
arenas: &'a ResolverArenas<'a>,
) -> Resolver<'a> {
) -> Resolver<'a, 'tcx> {
let root_def_id = CRATE_DEF_ID.to_def_id();
let mut module_map = FxHashMap::default();
let graph_root = arenas.new_module(

View file

@ -160,7 +160,7 @@ fn soft_custom_inner_attributes_gate(path: &ast::Path, invoc: &Invocation) -> bo
false
}
impl<'a> ResolverExpand for Resolver<'a> {
impl<'a, 'tcx> ResolverExpand for Resolver<'a, 'tcx> {
fn next_node_id(&mut self) -> NodeId {
self.next_node_id()
}
@ -467,7 +467,7 @@ impl<'a> ResolverExpand for Resolver<'a> {
}
}
impl<'a> Resolver<'a> {
impl<'a, 'tcx> Resolver<'a, 'tcx> {
/// Resolve macro path with error reporting and recovery.
/// Uses dummy syntax extensions for unresolved macros or macros with unexpected resolutions
/// for better error recovery.