Skip to content
Merged
Show file tree
Hide file tree
Changes from all 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
4 changes: 2 additions & 2 deletions compiler/rustc_ast_lowering/src/expr.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1018,7 +1018,7 @@ impl<'hir> LoweringContext<'_, 'hir> {
expr.span,
hir::ExprKind::Err(self.dcx().emit_err(AwaitOnlyInAsyncFnAndBlocks {
await_kw_span,
item_span: self.current_item,
item_span: self.current_item_span,
})),
);
return hir::ExprKind::Block(
Expand Down Expand Up @@ -1712,7 +1712,7 @@ impl<'hir> LoweringContext<'_, 'hir> {
}
Some(hir::CoroutineKind::Coroutine(_)) => false,
None => {
let suggestion = self.current_item.map(|s| s.shrink_to_lo());
let suggestion = self.current_item_span.map(|s| s.shrink_to_lo());
self.dcx().emit_err(YieldInClosure { span, suggestion });
self.coroutine_kind = Some(hir::CoroutineKind::Coroutine(Movability::Movable));

Expand Down
68 changes: 9 additions & 59 deletions compiler/rustc_ast_lowering/src/item.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,8 @@ use rustc_errors::{E0570, ErrorGuaranteed, struct_span_code_err};
use rustc_hir::attrs::{AttributeKind, EiiImplResolution};
use rustc_hir::def::{DefKind, PerNS, Res};
use rustc_hir::{
self as hir, CRATE_OWNER_ID, HirId, ImplItemImplKind, LifetimeSource, PredicateOrigin, Target,
find_attr,
self as hir, HirId, ImplItemImplKind, LifetimeSource, PredicateOrigin, Target, find_attr,
};
use rustc_middle::middle::resolve::ResolverAstLowering;
use rustc_middle::ty::TyCtxt;
use rustc_middle::ty::data_structures::IndexMap;
use rustc_span::def_id::{DefId, LocalDefId};
use rustc_span::edit_distance::find_best_match_for_name;
Expand All @@ -28,14 +25,9 @@ use super::{
};
use crate::diagnostics::{ConstComptimeFn, ResolvingRestrictionKind, RestrictionAncestorOnly};

pub(super) struct ItemLowerer<'a, 'hir> {
pub(super) tcx: TyCtxt<'hir>,
pub(super) resolver: &'a ResolverAstLowering<'hir>,
}

/// When we have a ty alias we *may* have two where clauses. To give the best diagnostics, we set the span
/// to the where clause that is preferred, if it exists. Otherwise, it sets the span to the other where
/// clause if it exists.
/// When we have a ty alias we *may* have two where clauses. To give the best diagnostics, we set
/// the span to the where clause that is preferred, if it exists. Otherwise, it sets the span to
/// the other where clause if it exists.
fn add_ty_alias_where_clause(
generics: &mut ast::Generics,
after_where_clause: &ast::WhereClause,
Expand All @@ -52,48 +44,6 @@ fn add_ty_alias_where_clause(
if before.0 || !after.0 { before } else { after };
}

impl<'hir> ItemLowerer<'_, 'hir> {
fn with_lctx(
&mut self,
owner: NodeId,
f: impl FnOnce(&mut LoweringContext<'_, 'hir>) -> hir::OwnerNode<'hir>,
) -> hir::MaybeOwner<'hir> {
let mut lctx = LoweringContext::new(self.tcx, self.resolver, owner);

let item = f(&mut lctx);

let info = lctx.curr_owner.into_owner_info(self.tcx, item);
hir::MaybeOwner::Owner(lctx.arena.alloc(info))
}

#[instrument(level = "debug", skip(self, c))]
pub(super) fn lower_crate(&mut self, c: &Crate) -> hir::MaybeOwner<'hir> {
self.with_lctx(CRATE_NODE_ID, |lctx| {
debug_assert_eq!(lctx.curr_owner.owner_id(), CRATE_OWNER_ID);
let module = lctx.lower_mod(&c.items, &c.spans);
lctx.lower_attrs(hir::CRATE_HIR_ID, &c.attrs, c.spans.inner_span, Target::Crate);
hir::OwnerNode::Crate(module)
})
}

#[instrument(level = "debug", skip(self))]
pub(super) fn lower_item(&mut self, item: &Item) -> hir::MaybeOwner<'hir> {
self.with_lctx(item.id, |lctx| hir::OwnerNode::Item(lctx.lower_item(item)))
}

pub(super) fn lower_trait_item(&mut self, item: &AssocItem) -> hir::MaybeOwner<'hir> {
self.with_lctx(item.id, |lctx| hir::OwnerNode::TraitItem(lctx.lower_trait_item(item)))
}

pub(super) fn lower_impl_item(&mut self, item: &AssocItem) -> hir::MaybeOwner<'hir> {
self.with_lctx(item.id, |lctx| hir::OwnerNode::ImplItem(lctx.lower_impl_item(item)))
}

pub(super) fn lower_foreign_item(&mut self, item: &ForeignItem) -> hir::MaybeOwner<'hir> {
self.with_lctx(item.id, |lctx| hir::OwnerNode::ForeignItem(lctx.lower_foreign_item(item)))
}
}

impl<'hir> LoweringContext<'_, 'hir> {
pub(super) fn lower_mod(
&mut self,
Expand Down Expand Up @@ -203,7 +153,7 @@ impl<'hir> LoweringContext<'_, 'hir> {
}
}

fn lower_item(&mut self, i: &Item) -> &'hir hir::Item<'hir> {
pub(super) fn lower_item(&mut self, i: &Item) -> &'hir hir::Item<'hir> {
let owner_id = self.curr_owner.owner_id();
let hir_id: HirId = owner_id.into();
let vis_span = self.lower_span(i.vis.span);
Expand Down Expand Up @@ -544,7 +494,7 @@ impl<'hir> LoweringContext<'_, 'hir> {
}
ItemKind::MacroDef(ident, MacroDef { body, macro_rules, eii_declaration: _ }) => {
let ident = self.lower_ident(*ident);
let body = Box::new(self.lower_delim_args(body));
let body = body.clone();
let def_id = self.curr_owner.owner.def_id;
let def_kind = self.tcx.def_kind(def_id);
let DefKind::Macro(macro_kinds) = def_kind else {
Expand Down Expand Up @@ -730,7 +680,7 @@ impl<'hir> LoweringContext<'_, 'hir> {
}
}

fn lower_foreign_item(&mut self, i: &ForeignItem) -> &'hir hir::ForeignItem<'hir> {
pub(super) fn lower_foreign_item(&mut self, i: &ForeignItem) -> &'hir hir::ForeignItem<'hir> {
let owner_id = self.curr_owner.owner_id();
let hir_id: HirId = owner_id.into();
let attrs =
Expand Down Expand Up @@ -911,7 +861,7 @@ impl<'hir> LoweringContext<'_, 'hir> {
}
}

fn lower_trait_item(&mut self, i: &AssocItem) -> &'hir hir::TraitItem<'hir> {
pub(super) fn lower_trait_item(&mut self, i: &AssocItem) -> &'hir hir::TraitItem<'hir> {
let trait_item_def_id = self.curr_owner.owner_id();
let hir_id: HirId = trait_item_def_id.into();
let attrs = self.lower_attrs(
Expand Down Expand Up @@ -1160,7 +1110,7 @@ impl<'hir> LoweringContext<'_, 'hir> {
ident
}

fn lower_impl_item(&mut self, i: &AssocItem) -> &'hir hir::ImplItem<'hir> {
pub(super) fn lower_impl_item(&mut self, i: &AssocItem) -> &'hir hir::ImplItem<'hir> {
let owner_id = self.curr_owner.owner_id();
let hir_id: HirId = owner_id.into();
let parent_id = self.tcx.local_parent(owner_id.def_id);
Expand Down
71 changes: 44 additions & 27 deletions compiler/rustc_ast_lowering/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -60,8 +60,9 @@ use rustc_hir::def_id::{DefId, LOCAL_CRATE, LocalDefId, LocalDefIdMap};
use rustc_hir::definitions::PerParentDisambiguatorState;
use rustc_hir::lints::DelayedLint;
use rustc_hir::{
self as hir, AngleBrackets, ConstArg, GenericArg, HirId, ItemLocalMap, LifetimeSource,
LifetimeSyntax, MissingLifetimeKind, ParamName, Target, TraitCandidate, find_attr,
self as hir, AngleBrackets, CRATE_OWNER_ID, ConstArg, GenericArg, HirId, ItemLocalMap,
LifetimeSource, LifetimeSyntax, MissingLifetimeKind, ParamName, Target, TraitCandidate,
find_attr,
};
use rustc_index::{Idx, IndexSlice, IndexVec};
use rustc_macros::extension;
Expand Down Expand Up @@ -296,7 +297,7 @@ struct LoweringContext<'a, 'hir> {

/// Used to get the current `fn`'s def span to point to when using `await`
/// outside of an `async fn`.
current_item: Option<Span>,
current_item_span: Option<Span>,

try_block_scope: TryBlockScope,
loop_scope: Option<HirId>,
Expand Down Expand Up @@ -366,7 +367,7 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
is_in_dyn_type: false,
coroutine_kind: None,
task_context: None,
current_item: None,
current_item_span: None,

move_expr_bindings: Vec::new(),
lowering_move_expr_initializer: false,
Expand Down Expand Up @@ -783,15 +784,37 @@ fn lower_to_hir(tcx: TyCtxt<'_>, def_id: LocalDefId) -> hir::MaybeOwner<'_> {
return fallback_to_ancestor(tcx.local_parent(def_id));
};

let mut item_lowerer = item::ItemLowerer { tcx, resolver: &*resolver };
fn with_lctx<'hir>(
tcx: TyCtxt<'hir>,
resolver: &ResolverAstLowering<'hir>,
owner: NodeId,
f: impl FnOnce(&mut LoweringContext<'_, 'hir>) -> hir::OwnerNode<'hir>,
) -> hir::MaybeOwner<'hir> {
let mut lctx = LoweringContext::new(tcx, resolver, owner);
let item = f(&mut lctx);
hir::MaybeOwner::Owner(lctx.curr_owner.into_owner_info(tcx, item))
}

let item = match &node {
// The item existed in the AST.
AstOwner::Crate(c) => item_lowerer.lower_crate(&c),
AstOwner::Item(item) => item_lowerer.lower_item(&item),
AstOwner::TraitItem(item) => item_lowerer.lower_trait_item(&item),
AstOwner::ImplItem(item) => item_lowerer.lower_impl_item(&item),
AstOwner::ForeignItem(item) => item_lowerer.lower_foreign_item(&item),
AstOwner::Crate(c) => with_lctx(tcx, &*resolver, CRATE_NODE_ID, |lctx| {
debug_assert_eq!(lctx.curr_owner.owner_id(), CRATE_OWNER_ID);
let module = lctx.lower_mod(&c.items, &c.spans);
lctx.lower_attrs(hir::CRATE_HIR_ID, &c.attrs, c.spans.inner_span, Target::Crate);
hir::OwnerNode::Crate(module)
}),
AstOwner::Item(item) => {
with_lctx(tcx, &*resolver, item.id, |lctx| hir::OwnerNode::Item(lctx.lower_item(item)))
}
AstOwner::TraitItem(item) => with_lctx(tcx, &*resolver, item.id, |lctx| {
hir::OwnerNode::TraitItem(lctx.lower_trait_item(item))
}),
AstOwner::ImplItem(item) => with_lctx(tcx, &*resolver, item.id, |lctx| {
hir::OwnerNode::ImplItem(lctx.lower_impl_item(item))
}),
AstOwner::ForeignItem(item) => with_lctx(tcx, &*resolver, item.id, |lctx| {
hir::OwnerNode::ForeignItem(lctx.lower_foreign_item(item))
}),
AstOwner::NestedUseTree(owner_id) => fallback_to_ancestor(*owner_id),
// The item existed in the AST, but is not a HIR owner.
// Fetch the correct information from its parent.
Expand Down Expand Up @@ -824,7 +847,7 @@ enum GenericArgsMode {
ParenSugar,
/// Allow RTN, don't allow paren sugar.
ReturnTypeNotation,
// Error if parenthesized generics or RTN are encountered.
/// Error if parenthesized generics or RTN are encountered.
Err,
/// Silence errors when lowering generics. Only used with `Res::Err`.
Silence,
Expand Down Expand Up @@ -982,7 +1005,7 @@ impl<'hir> LoweringContext<'_, 'hir> {
}

#[instrument(level = "trace", skip(self))]
fn lower_res(&mut self, res: Res<NodeId>) -> Res {
fn lower_res(&self, res: Res<NodeId>) -> Res {
let res: Result<Res, ()> = res.apply_id(|id| {
let owner = self.curr_owner.owner_id();
let local_id =
Expand All @@ -999,11 +1022,11 @@ impl<'hir> LoweringContext<'_, 'hir> {
res.unwrap_or(Res::Err)
}

fn expect_full_res(&mut self, id: NodeId) -> Res<NodeId> {
fn expect_full_res(&self, id: NodeId) -> Res<NodeId> {
self.get_partial_res(id).map_or(Res::Err, |pr| pr.expect_full_res())
}

fn lower_import_res(&mut self, id: NodeId, span: Span) -> PerNS<Option<Res>> {
fn lower_import_res(&self, id: NodeId, span: Span) -> PerNS<Option<Res>> {
debug_assert_eq!(id, self.curr_owner.owner.id);
let per_ns = self.curr_owner.owner.import_res.map(|res| res.map(|res| self.lower_res(res)));
if per_ns.is_empty() {
Expand Down Expand Up @@ -1154,8 +1177,8 @@ impl<'hir> LoweringContext<'_, 'hir> {
}

fn with_new_scopes<T>(&mut self, scope_span: Span, f: impl FnOnce(&mut Self) -> T) -> T {
let current_item = self.current_item;
self.current_item = Some(scope_span);
let current_item_span = self.current_item_span;
self.current_item_span = Some(scope_span);

let was_in_loop_condition = self.is_in_loop_condition;
self.is_in_loop_condition = false;
Expand All @@ -1172,7 +1195,7 @@ impl<'hir> LoweringContext<'_, 'hir> {

self.is_in_loop_condition = was_in_loop_condition;

self.current_item = current_item;
self.current_item_span = current_item_span;

ret
}
Expand Down Expand Up @@ -1261,10 +1284,6 @@ impl<'hir> LoweringContext<'_, 'hir> {
}
}

fn lower_delim_args(&self, args: &DelimArgs) -> DelimArgs {
args.clone()
}

/// Lower an associated item constraint.
#[instrument(level = "debug", skip_all)]
fn lower_assoc_item_constraint(
Expand Down Expand Up @@ -1647,8 +1666,8 @@ impl<'hir> LoweringContext<'_, 'hir> {
self.lower_array_length_to_const_arg(length),
),
TyKind::TraitObject(bounds, kind) => {
let mut lifetime_bound = None;
let (bounds, lifetime_bound) = self.with_dyn_type_scope(true, |this| {
let mut lifetime_bound = None;
let bounds =
this.arena.alloc_from_iter(bounds.iter().filter_map(|bound| match bound {
// We can safely ignore constness here since AST validation
Expand Down Expand Up @@ -1681,9 +1700,7 @@ impl<'hir> LoweringContext<'_, 'hir> {
None
}
}));
let lifetime_bound =
lifetime_bound.unwrap_or_else(|| this.elided_dyn_bound(t.span));
(bounds, lifetime_bound)
(bounds, lifetime_bound.unwrap_or_else(|| this.elided_dyn_bound(t.span)))
});
hir::TyKind::TraitObject(bounds, TaggedRef::new(lifetime_bound, *kind))
}
Expand Down Expand Up @@ -3058,15 +3075,15 @@ impl<'hir> LoweringContext<'_, 'hir> {
}))
}

fn lower_unsafe_source(&mut self, u: UnsafeSource) -> hir::UnsafeSource {
fn lower_unsafe_source(&self, u: UnsafeSource) -> hir::UnsafeSource {
match u {
CompilerGenerated => hir::UnsafeSource::CompilerGenerated,
UserProvided => hir::UnsafeSource::UserProvided,
}
}

fn lower_trait_bound_modifiers(
&mut self,
&self,
modifiers: TraitBoundModifiers,
) -> hir::TraitBoundModifiers {
let constness = match modifiers.constness {
Expand Down
Loading