Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Split up Definitions and ResolverAstLowering. #98106

Merged
merged 7 commits into from
Jun 17, 2022
Merged
Show file tree
Hide file tree
Changes from 4 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
2 changes: 1 addition & 1 deletion Cargo.lock
Original file line number Diff line number Diff line change
Expand Up @@ -3569,6 +3569,7 @@ dependencies = [
"rustc_errors",
"rustc_hir",
"rustc_index",
"rustc_middle",
"rustc_query_system",
"rustc_session",
"rustc_span",
Expand Down Expand Up @@ -4386,7 +4387,6 @@ dependencies = [
"bitflags",
"rustc_arena",
"rustc_ast",
"rustc_ast_lowering",
"rustc_ast_pretty",
"rustc_attr",
"rustc_data_structures",
Expand Down
1 change: 1 addition & 0 deletions compiler/rustc_ast_lowering/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ rustc_hir = { path = "../rustc_hir" }
rustc_target = { path = "../rustc_target" }
rustc_data_structures = { path = "../rustc_data_structures" }
rustc_index = { path = "../rustc_index" }
rustc_middle = { path = "../rustc_middle" }
rustc_query_system = { path = "../rustc_query_system" }
rustc_span = { path = "../rustc_span" }
rustc_errors = { path = "../rustc_errors" }
Expand Down
14 changes: 4 additions & 10 deletions compiler/rustc_ast_lowering/src/asm.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use crate::{ImplTraitContext, ImplTraitPosition, ParamMode};
use crate::{ImplTraitContext, ImplTraitPosition, ParamMode, ResolverAstLoweringExt};

use super::LoweringContext;

Expand All @@ -11,7 +11,7 @@ use rustc_hir as hir;
use rustc_hir::def::{DefKind, Res};
use rustc_hir::definitions::DefPathData;
use rustc_session::parse::feature_err;
use rustc_span::{sym, ExpnId, Span};
use rustc_span::{sym, Span};
use rustc_target::asm;
use std::collections::hash_map::Entry;
use std::fmt::Write;
Expand Down Expand Up @@ -242,14 +242,8 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {

// Wrap the expression in an AnonConst.
let parent_def_id = self.current_hir_id_owner;
let node_id = self.resolver.next_node_id();
self.resolver.create_def(
parent_def_id,
node_id,
DefPathData::AnonConst,
ExpnId::root(),
*op_sp,
);
let node_id = self.next_node_id();
self.create_def(parent_def_id, node_id, DefPathData::AnonConst);
let anon_const = AnonConst { id: node_id, value: P(expr) };
hir::InlineAsmOperand::SymFn {
anon_const: self.lower_anon_const(&anon_const),
Expand Down
17 changes: 5 additions & 12 deletions compiler/rustc_ast_lowering/src/expr.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use crate::{FnDeclKind, ImplTraitPosition};

use super::ResolverAstLoweringExt;
use super::{ImplTraitContext, LoweringContext, ParamMode, ParenthesizedGenericArgs};
use crate::{FnDeclKind, ImplTraitPosition};

use rustc_ast::attr;
use rustc_ast::ptr::P as AstP;
Expand All @@ -11,7 +11,6 @@ use rustc_errors::struct_span_err;
use rustc_hir as hir;
use rustc_hir::def::Res;
use rustc_hir::definitions::DefPathData;
use rustc_span::hygiene::ExpnId;
use rustc_span::source_map::{respan, DesugaringKind, Span, Spanned};
use rustc_span::symbol::{sym, Ident};
use rustc_span::DUMMY_SP;
Expand Down Expand Up @@ -355,16 +354,10 @@ impl<'hir> LoweringContext<'_, 'hir> {
for (idx, arg) in args.into_iter().enumerate() {
if legacy_args_idx.contains(&idx) {
let parent_def_id = self.current_hir_id_owner;
let node_id = self.resolver.next_node_id();
let node_id = self.next_node_id();

// Add a definition for the in-band const def.
self.resolver.create_def(
parent_def_id,
node_id,
DefPathData::AnonConst,
ExpnId::root(),
arg.span,
);
self.create_def(parent_def_id, node_id, DefPathData::AnonConst);

let anon_const = AnonConst { id: node_id, value: arg };
generic_args.push(AngleBracketedArg::Arg(GenericArg::Const(anon_const)));
Expand Down Expand Up @@ -724,7 +717,7 @@ impl<'hir> LoweringContext<'_, 'hir> {
};

// `::std::task::Poll::Ready(result) => break result`
let loop_node_id = self.resolver.next_node_id();
let loop_node_id = self.next_node_id();
let loop_hir_id = self.lower_node_id(loop_node_id);
let ready_arm = {
let x_ident = Ident::with_dummy_span(sym::result);
Expand Down
4 changes: 3 additions & 1 deletion compiler/rustc_ast_lowering/src/index.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ use rustc_hir::definitions;
use rustc_hir::intravisit::{self, Visitor};
use rustc_hir::*;
use rustc_index::vec::{Idx, IndexVec};
use rustc_middle::span_bug;
use rustc_session::Session;
use rustc_span::source_map::SourceMap;
use rustc_span::{Span, DUMMY_SP};
Expand Down Expand Up @@ -75,7 +76,8 @@ impl<'a, 'hir> NodeCollector<'a, 'hir> {
// owner of that node.
if cfg!(debug_assertions) {
if hir_id.owner != self.owner {
panic!(
span_bug!(
span,
"inconsistent DepNode at `{:?}` for `{:?}`: \
current_dep_node_owner={} ({:?}), hir_id.owner={} ({:?})",
self.source_map.span_to_diagnostic_string(span),
Expand Down
50 changes: 30 additions & 20 deletions compiler/rustc_ast_lowering/src/item.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
use super::{AstOwner, ImplTraitContext, ImplTraitPosition, ResolverAstLowering};
use super::ResolverAstLoweringExt;
use super::{AstOwner, ImplTraitContext, ImplTraitPosition};
use super::{LoweringContext, ParamMode};
use crate::{Arena, FnDeclKind};

Expand All @@ -11,8 +12,11 @@ use rustc_errors::struct_span_err;
use rustc_hir as hir;
use rustc_hir::def::{DefKind, Res};
use rustc_hir::def_id::{LocalDefId, CRATE_DEF_ID};
use rustc_hir::definitions::Definitions;
use rustc_hir::PredicateOrigin;
use rustc_index::vec::{Idx, IndexVec};
use rustc_middle::ty::ResolverOutputs;
use rustc_session::cstore::CrateStoreDyn;
use rustc_session::Session;
use rustc_span::source_map::DesugaringKind;
use rustc_span::symbol::{kw, sym, Ident};
Expand All @@ -24,7 +28,9 @@ use std::iter;

pub(super) struct ItemLowerer<'a, 'hir> {
pub(super) sess: &'a Session,
pub(super) resolver: &'a mut dyn ResolverAstLowering,
pub(super) definitions: &'a mut Definitions,
pub(super) cstore: &'a CrateStoreDyn,
pub(super) resolver: &'a mut ResolverOutputs,
pub(super) arena: &'hir Arena<'hir>,
pub(super) ast_index: &'a IndexVec<LocalDefId, AstOwner<'a>>,
pub(super) owners: &'a mut IndexVec<LocalDefId, hir::MaybeOwner<&'hir hir::OwnerInfo<'hir>>>,
Expand Down Expand Up @@ -56,9 +62,12 @@ impl<'a, 'hir> ItemLowerer<'a, 'hir> {
owner: NodeId,
f: impl FnOnce(&mut LoweringContext<'_, 'hir>) -> hir::OwnerNode<'hir>,
) {
let next_node_id = self.resolver.next_node_id;
let mut lctx = LoweringContext {
// Pseudo-globals.
sess: &self.sess,
definitions: self.definitions,
cstore: self.cstore,
resolver: self.resolver,
arena: self.arena,

Expand All @@ -71,6 +80,8 @@ impl<'a, 'hir> ItemLowerer<'a, 'hir> {
node_id_to_local_id: Default::default(),
local_id_to_def_id: SortedMap::new(),
trait_map: Default::default(),
local_node_id_to_def_id: FxHashMap::default(),
next_node_id,

// Lowering state.
catch_scope: None,
Expand Down Expand Up @@ -118,8 +129,7 @@ impl<'a, 'hir> ItemLowerer<'a, 'hir> {

#[instrument(level = "debug", skip(self, c))]
fn lower_crate(&mut self, c: &Crate) {
debug_assert_eq!(self.resolver.local_def_id(CRATE_NODE_ID), CRATE_DEF_ID);

debug_assert_eq!(self.resolver.node_id_to_def_id[&CRATE_NODE_ID], CRATE_DEF_ID);
self.with_lctx(CRATE_NODE_ID, |lctx| {
let module = lctx.lower_mod(&c.items, &c.spans);
lctx.lower_attrs(hir::CRATE_HIR_ID, &c.attrs);
Expand All @@ -133,10 +143,10 @@ impl<'a, 'hir> ItemLowerer<'a, 'hir> {
}

fn lower_assoc_item(&mut self, item: &AssocItem, ctxt: AssocCtxt) {
let def_id = self.resolver.local_def_id(item.id);
let def_id = self.resolver.node_id_to_def_id[&item.id];

let parent_id = {
let parent = self.resolver.definitions().def_key(def_id).parent;
let parent = self.definitions.def_key(def_id).parent;
let local_def_index = parent.unwrap();
LocalDefId { local_def_index }
};
Expand Down Expand Up @@ -177,7 +187,7 @@ impl<'hir> LoweringContext<'_, 'hir> {
}

pub(super) fn lower_item_ref(&mut self, i: &Item) -> SmallVec<[hir::ItemId; 1]> {
let mut node_ids = smallvec![hir::ItemId { def_id: self.resolver.local_def_id(i.id) }];
let mut node_ids = smallvec![hir::ItemId { def_id: self.local_def_id(i.id) }];
if let ItemKind::Use(ref use_tree) = &i.kind {
self.lower_item_id_use_tree(use_tree, i.id, &mut node_ids);
}
Expand All @@ -193,7 +203,7 @@ impl<'hir> LoweringContext<'_, 'hir> {
match tree.kind {
UseTreeKind::Nested(ref nested_vec) => {
for &(ref nested, id) in nested_vec {
vec.push(hir::ItemId { def_id: self.resolver.local_def_id(id) });
vec.push(hir::ItemId { def_id: self.local_def_id(id) });
self.lower_item_id_use_tree(nested, id, vec);
}
}
Expand All @@ -202,7 +212,7 @@ impl<'hir> LoweringContext<'_, 'hir> {
for (_, &id) in
iter::zip(self.expect_full_res_from_use(base_id).skip(1), &[id1, id2])
{
vec.push(hir::ItemId { def_id: self.resolver.local_def_id(id) });
vec.push(hir::ItemId { def_id: self.local_def_id(id) });
}
}
}
Expand Down Expand Up @@ -467,7 +477,7 @@ impl<'hir> LoweringContext<'_, 'hir> {
}
ItemKind::MacroDef(MacroDef { ref body, macro_rules }) => {
let body = P(self.lower_mac_args(body));
let macro_kind = self.resolver.decl_macro_kind(self.resolver.local_def_id(id));
let macro_kind = self.resolver.decl_macro_kind(self.local_def_id(id));
hir::ItemKind::Macro(ast::MacroDef { body, macro_rules }, macro_kind)
}
ItemKind::MacCall(..) => {
Expand Down Expand Up @@ -527,7 +537,7 @@ impl<'hir> LoweringContext<'_, 'hir> {
// Essentially a single `use` which imports two names is desugared into
// two imports.
for new_node_id in [id1, id2] {
let new_id = self.resolver.local_def_id(new_node_id);
let new_id = self.local_def_id(new_node_id);
let Some(res) = resolutions.next() else {
// Associate an HirId to both ids even if there is no resolution.
let _old = self.children.insert(
Expand All @@ -540,7 +550,7 @@ impl<'hir> LoweringContext<'_, 'hir> {
let ident = *ident;
let mut path = path.clone();
for seg in &mut path.segments {
seg.id = self.resolver.next_node_id();
seg.id = self.next_node_id();
}
let span = path.span;

Expand Down Expand Up @@ -603,13 +613,13 @@ impl<'hir> LoweringContext<'_, 'hir> {

// Add all the nested `PathListItem`s to the HIR.
for &(ref use_tree, id) in trees {
let new_hir_id = self.resolver.local_def_id(id);
let new_hir_id = self.local_def_id(id);

let mut prefix = prefix.clone();

// Give the segments new node-ids since they are being cloned.
for seg in &mut prefix.segments {
seg.id = self.resolver.next_node_id();
seg.id = self.next_node_id();
}

// Each `use` import is an item and thus are owners of the
Expand Down Expand Up @@ -683,7 +693,7 @@ impl<'hir> LoweringContext<'_, 'hir> {

fn lower_foreign_item_ref(&mut self, i: &ForeignItem) -> hir::ForeignItemRef {
hir::ForeignItemRef {
id: hir::ForeignItemId { def_id: self.resolver.local_def_id(i.id) },
id: hir::ForeignItemId { def_id: self.local_def_id(i.id) },
ident: self.lower_ident(i.ident),
span: self.lower_span(i.span),
}
Expand Down Expand Up @@ -839,7 +849,7 @@ impl<'hir> LoweringContext<'_, 'hir> {
}
AssocItemKind::MacCall(..) => unimplemented!(),
};
let id = hir::TraitItemId { def_id: self.resolver.local_def_id(i.id) };
let id = hir::TraitItemId { def_id: self.local_def_id(i.id) };
let defaultness = hir::Defaultness::Default { has_value: has_default };
hir::TraitItemRef {
id,
Expand Down Expand Up @@ -919,7 +929,7 @@ impl<'hir> LoweringContext<'_, 'hir> {
let has_value = true;
let (defaultness, _) = self.lower_defaultness(i.kind.defaultness(), has_value);
hir::ImplItemRef {
id: hir::ImplItemId { def_id: self.resolver.local_def_id(i.id) },
id: hir::ImplItemId { def_id: self.local_def_id(i.id) },
ident: self.lower_ident(i.ident),
span: self.lower_span(i.span),
defaultness,
Expand Down Expand Up @@ -1331,7 +1341,7 @@ impl<'hir> LoweringContext<'_, 'hir> {
generics
.params
.iter()
.any(|p| def_id == self.resolver.local_def_id(p.id).to_def_id())
.any(|p| def_id == self.local_def_id(p.id).to_def_id())
}
// Either the `bounded_ty` is not a plain type parameter, or
// it's not found in the generic type parameters list.
Expand Down Expand Up @@ -1435,7 +1445,7 @@ impl<'hir> LoweringContext<'_, 'hir> {
match kind {
GenericParamKind::Const { .. } => None,
GenericParamKind::Type { .. } => {
let def_id = self.resolver.local_def_id(id).to_def_id();
let def_id = self.local_def_id(id).to_def_id();
let ty_path = self.arena.alloc(hir::Path {
span: param_span,
res: Res::Def(DefKind::TyParam, def_id),
Expand All @@ -1458,7 +1468,7 @@ impl<'hir> LoweringContext<'_, 'hir> {
let res = self.resolver.get_lifetime_res(id).unwrap_or_else(|| {
panic!("Missing resolution for lifetime {:?} at {:?}", id, ident.span)
});
let lt_id = self.resolver.next_node_id();
let lt_id = self.next_node_id();
let lifetime = self.new_named_lifetime_with_res(lt_id, ident_span, ident, res);
Some(hir::WherePredicate::RegionPredicate(hir::WhereRegionPredicate {
lifetime,
Expand Down
Loading