Skip to content
Open
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
1 change: 1 addition & 0 deletions Cargo.lock
Original file line number Diff line number Diff line change
Expand Up @@ -4738,6 +4738,7 @@ dependencies = [
"rustc_arena",
"rustc_ast",
"rustc_ast_pretty",
"rustc_attr_ir",
"rustc_attr_parsing",
"rustc_data_structures",
"rustc_errors",
Expand Down
8 changes: 7 additions & 1 deletion compiler/rustc_ast_lowering/src/block.rs
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,13 @@ impl<'hir> LoweringContext<'_, 'hir> {
};
let span = self.lower_span(l.span);
let source = hir::LocalSource::Normal;
self.lower_attrs(hir_id, &l.attrs, l.span, Target::Statement);
self.lower_attrs(
hir_id,
&l.attrs,
l.span,
Target::Statement,
rustc_attr_ir::target::AstTarget::Local(l),
);
self.arena.alloc(hir::LetStmt { hir_id, super_, ty, pat, init, els, span, source })
}

Expand Down
8 changes: 7 additions & 1 deletion compiler/rustc_ast_lowering/src/contract.rs
Original file line number Diff line number Diff line change
Expand Up @@ -350,7 +350,13 @@ impl<'hir> LoweringContext<'_, 'hir> {
));

let attrs: rustc_ast::AttrVec = thin_vec![self.unreachable_code_attr(span)];
self.lower_attrs(contract_check.hir_id, &attrs, span, rustc_hir::Target::Expression);
self.lower_attrs(
contract_check.hir_id,
&attrs,
span,
rustc_hir::Target::Expression,
rustc_attr_ir::target::AstTarget::None,
);

let ret_block = self.block_all(span, arena_vec![self; ret_stmt], Some(contract_check));
self.arena.alloc(self.expr_block(self.arena.alloc(ret_block)))
Expand Down
34 changes: 26 additions & 8 deletions compiler/rustc_ast_lowering/src/expr.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ use rustc_ast::node_id::NodeMap;
use rustc_ast::visit::{Visitor, walk_expr};
use rustc_ast::*;
use rustc_attr_ir::lang_items::LangItem;
use rustc_attr_ir::target::Target;
use rustc_attr_ir::target::{AstTarget, Target};
use rustc_errors::msg;
use rustc_hir as hir;
use rustc_hir::HirId;
Expand Down Expand Up @@ -231,7 +231,13 @@ impl<'hir> LoweringContext<'_, 'hir> {
let old_attrs =
self.curr_owner.attrs.get(&ex.hir_id.local_id).copied().unwrap_or(&[]);
let new_attrs = self
.lower_attrs_vec(&e.attrs, e.span, ex.hir_id, Target::from_expr(e), None)
.lower_attrs_vec(
&e.attrs,
e.span,
ex.hir_id,
Target::from_expr(e),
AstTarget::None,
)
.into_iter()
.chain(old_attrs.iter().cloned());
let new_attrs = &*self.arena.alloc_from_iter(new_attrs);
Expand All @@ -255,7 +261,7 @@ impl<'hir> LoweringContext<'_, 'hir> {
}

let expr_hir_id = self.lower_node_id(e.id);
self.lower_attrs(expr_hir_id, &e.attrs, e.span, Target::from_expr(e));
self.lower_attrs(expr_hir_id, &e.attrs, e.span, Target::from_expr(e), AstTarget::Expr(e));

let kind = match &e.kind {
ExprKind::Array(exprs) => hir::ExprKind::Array(self.lower_exprs(exprs)),
Expand Down Expand Up @@ -795,7 +801,7 @@ impl<'hir> LoweringContext<'_, 'hir> {
let guard = arm.guard.as_ref().map(|guard| self.lower_expr(&guard.cond));
let hir_id = self.next_id();
let span = self.lower_span(arm.span);
self.lower_attrs(hir_id, &arm.attrs, arm.span, Target::Arm);
self.lower_attrs(hir_id, &arm.attrs, arm.span, Target::Arm, AstTarget::Arm(arm));
let is_never_pattern = pat.is_never_pattern();
// We need to lower the body even if it's unneeded for never pattern in match,
// ensure that we can get HirId for DefId if need (issue #137708).
Expand Down Expand Up @@ -1659,7 +1665,7 @@ impl<'hir> LoweringContext<'_, 'hir> {

fn lower_expr_field(&mut self, f: &ExprField) -> hir::ExprField<'hir> {
let hir_id = self.lower_node_id(f.id);
self.lower_attrs(hir_id, &f.attrs, f.span, Target::ExprField);
self.lower_attrs(hir_id, &f.attrs, f.span, Target::ExprField, AstTarget::ExprField(f));
hir::ExprField {
hir_id,
ident: self.lower_ident(f.ident),
Expand Down Expand Up @@ -1926,7 +1932,7 @@ impl<'hir> LoweringContext<'_, 'hir> {
//
// Also, add the attributes to the outer returned expr node.
let expr = self.expr_drop_temps_mut(for_span, match_expr);
self.lower_attrs(expr.hir_id, &e.attrs, e.span, Target::from_expr(e));
self.lower_attrs(expr.hir_id, &e.attrs, e.span, Target::from_expr(e), AstTarget::Expr(e));
expr
}

Expand Down Expand Up @@ -1974,7 +1980,13 @@ impl<'hir> LoweringContext<'_, 'hir> {
let val_ident = Ident::with_dummy_span(sym::val);
let (val_pat, val_pat_nid) = self.pat_ident(span, val_ident);
let val_expr = self.expr_ident(span, val_ident, val_pat_nid);
self.lower_attrs(val_expr.hir_id, &attrs, span, Target::Expression);
self.lower_attrs(
val_expr.hir_id,
&attrs,
span,
Target::Expression,
AstTarget::Expr(sub_expr),
);
let continue_pat = self.pat_cf_continue(unstable_span, val_pat);
self.arm(continue_pat, val_expr, try_span)
};
Expand Down Expand Up @@ -2016,7 +2028,13 @@ impl<'hir> LoweringContext<'_, 'hir> {
let ret_expr = self.checked_return(Some(from_residual_expr));
self.arena.alloc(self.expr(try_span, ret_expr))
};
self.lower_attrs(ret_expr.hir_id, &attrs, span, Target::Expression);
self.lower_attrs(
ret_expr.hir_id,
&attrs,
span,
Target::Expression,
AstTarget::Expr(sub_expr),
);

let break_pat = self.pat_cf_break(try_span, residual_local);
self.arm(break_pat, ret_expr, try_span)
Expand Down
8 changes: 7 additions & 1 deletion compiler/rustc_ast_lowering/src/expr/closure.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,13 @@ impl<'hir> LoweringContext<'_, 'hir> {
closure: &Closure,
) -> hir::Expr<'hir> {
let expr_hir_id = self.lower_node_id(e.id);
let attrs = self.lower_attrs(expr_hir_id, &e.attrs, e.span, Target::from_expr(e));
let attrs = self.lower_attrs(
expr_hir_id,
&e.attrs,
e.span,
Target::from_expr(e),
rustc_attr_ir::target::AstTarget::Closure(closure),
);

match closure.coroutine_marker {
Some(coroutine_marker) => self.lower_expr_coroutine_closure_with_move_exprs(
Expand Down
36 changes: 28 additions & 8 deletions compiler/rustc_ast_lowering/src/item.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
use rustc_abi::ExternAbi;
use rustc_ast::visit::AssocCtxt;
use rustc_ast::*;
use rustc_attr_ir::target::AstTarget;
use rustc_errors::{E0570, ErrorGuaranteed, struct_span_code_err};
use rustc_hir::attrs::{AttributeKind, EiiImplResolution};
use rustc_hir::def::{DefKind, PerNS, Res};
Expand Down Expand Up @@ -71,7 +72,13 @@ impl<'hir> ItemLowerer<'_, '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);
lctx.lower_attrs(
hir::CRATE_HIR_ID,
&c.attrs,
c.spans.inner_span,
Target::Crate,
AstTarget::Crate(c),
);
hir::OwnerNode::Crate(module)
})
}
Expand Down Expand Up @@ -214,7 +221,7 @@ impl<'hir> LoweringContext<'_, 'hir> {
&i.attrs,
i.span,
Target::from_ast_item(i),
Some(i),
AstTarget::Item(i),
&extra_hir_attributes,
);

Expand Down Expand Up @@ -731,8 +738,13 @@ impl<'hir> LoweringContext<'_, 'hir> {
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 =
self.lower_attrs(hir_id, &i.attrs, i.span, Target::from_foreign_item_kind(&i.kind));
let attrs = self.lower_attrs(
hir_id,
&i.attrs,
i.span,
Target::from_foreign_item_kind(&i.kind),
AstTarget::ForeignItem(i),
);
let (ident, kind) = match &i.kind {
ForeignItemKind::Fn(Fn { sig, ident, generics, define_opaque, .. }) => {
let fdec = &sig.decl;
Expand Down Expand Up @@ -802,7 +814,7 @@ impl<'hir> LoweringContext<'_, 'hir> {
self.dcx().span_fatal(v.span, "unnamed enum variants are not yet implemented");
}
let hir_id = self.lower_node_id(v.id);
self.lower_attrs(hir_id, &v.attrs, v.span, Target::Variant);
self.lower_attrs(hir_id, &v.attrs, v.span, Target::Variant, AstTarget::Variant(v));
hir::Variant {
hir_id,
def_id: self.local_def_id(v.id),
Expand Down Expand Up @@ -889,7 +901,7 @@ impl<'hir> LoweringContext<'_, 'hir> {
let ty =
self.lower_ty_alloc(&f.ty, ImplTraitContext::Disallowed(ImplTraitPosition::FieldTy));
let hir_id = self.lower_node_id(f.id);
self.lower_attrs(hir_id, &f.attrs, f.span, Target::Field);
self.lower_attrs(hir_id, &f.attrs, f.span, Target::Field, AstTarget::FieldDef(f));
hir::FieldDef {
span: self.lower_span(f.span),
hir_id,
Expand Down Expand Up @@ -917,6 +929,7 @@ impl<'hir> LoweringContext<'_, 'hir> {
&i.attrs,
i.span,
Target::from_assoc_item_kind(&i.kind, AssocCtxt::Trait),
AstTarget::AssocItem(i),
);

let (ident, generics, kind, has_value) = match &i.kind {
Expand Down Expand Up @@ -1174,6 +1187,7 @@ impl<'hir> LoweringContext<'_, 'hir> {
&i.attrs,
i.span,
Target::from_assoc_item_kind(&i.kind, AssocCtxt::Impl { of_trait: is_in_trait_impl }),
AstTarget::AssocItem(i),
);

let (ident, (generics, kind)) = match &i.kind {
Expand Down Expand Up @@ -1340,7 +1354,7 @@ impl<'hir> LoweringContext<'_, 'hir> {

fn lower_param(&mut self, param: &Param) -> hir::Param<'hir> {
let hir_id = self.lower_node_id(param.id);
self.lower_attrs(hir_id, &param.attrs, param.span, Target::Param);
self.lower_attrs(hir_id, &param.attrs, param.span, Target::Param, AstTarget::Param(param));
hir::Param {
hir_id,
pat: self.lower_pat(&param.pat),
Expand Down Expand Up @@ -2036,7 +2050,13 @@ impl<'hir> LoweringContext<'_, 'hir> {
) -> hir::WherePredicate<'hir> {
let hir_id = self.lower_node_id(pred.id);
let span = self.lower_span(pred.span);
self.lower_attrs(hir_id, &pred.attrs, span, Target::WherePredicate);
self.lower_attrs(
hir_id,
&pred.attrs,
span,
Target::WherePredicate,
AstTarget::WherePredicate(pred),
);
let kind = self.arena.alloc(match &pred.kind {
WherePredicateKind::BoundPredicate(WhereBoundPredicate {
bound_generic_params,
Expand Down
32 changes: 23 additions & 9 deletions compiler/rustc_ast_lowering/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ use rustc_ast::mut_visit::{self, MutVisitor};
use rustc_ast::node_id::NodeMap;
use rustc_ast::visit::{self, Visitor};
use rustc_ast::{self as ast, *};
use rustc_attr_ir::target::AstTarget;
use rustc_attr_parsing::{AttributeParser, Recovery, ShouldEmit};
use rustc_data_structures::fx::FxIndexMap;
use rustc_data_structures::sorted_map::SortedMap;
Expand Down Expand Up @@ -1180,8 +1181,9 @@ impl<'hir> LoweringContext<'_, 'hir> {
attrs: &[Attribute],
target_span: Span,
target: Target,
ast_target: AstTarget<'_>,
) -> &'hir [hir::Attribute] {
self.lower_attrs_with_extra(id, attrs, target_span, target, None, &[])
self.lower_attrs_with_extra(id, attrs, target_span, target, ast_target, &[])
}

fn lower_attrs_with_extra(
Expand All @@ -1190,14 +1192,14 @@ impl<'hir> LoweringContext<'_, 'hir> {
attrs: &[Attribute],
target_span: Span,
target: Target,
target_item: Option<&ast::Item>,
ast_target: AstTarget<'_>,
extra_hir_attributes: &[hir::Attribute],
) -> &'hir [hir::Attribute] {
if attrs.is_empty() && extra_hir_attributes.is_empty() {
&[]
} else {
let mut lowered_attrs =
self.lower_attrs_vec(attrs, self.lower_span(target_span), id, target, target_item);
self.lower_attrs_vec(attrs, self.lower_span(target_span), id, target, ast_target);
lowered_attrs.extend(extra_hir_attributes.iter().cloned());

assert_eq!(id.owner, self.curr_owner.owner_id);
Expand All @@ -1224,14 +1226,14 @@ impl<'hir> LoweringContext<'_, 'hir> {
target_span: Span,
target_hir_id: HirId,
target: Target,
target_item: Option<&ast::Item>,
ast_target: AstTarget<'_>,
) -> Vec<hir::Attribute> {
let l = self.span_lowerer();
self.attribute_parser.parse_attribute_list(
attrs,
target_span,
target,
target_item,
ast_target,
|s| l.lower(s),
|lint_id, span, kind| {
self.curr_owner.delayed_lints.push(DelayedLint {
Expand Down Expand Up @@ -2292,7 +2294,7 @@ impl<'hir> LoweringContext<'_, 'hir> {
let hir_id = self.lower_node_id(param.id);
let param_attrs = &param.attrs;
let param_span = param.span();
let param = hir::GenericParam {
let param_hir = hir::GenericParam {
hir_id,
def_id: self.local_def_id(param.id),
name,
Expand All @@ -2302,8 +2304,14 @@ impl<'hir> LoweringContext<'_, 'hir> {
colon_span: param.colon_span.map(|s| self.lower_span(s)),
source,
};
self.lower_attrs(hir_id, param_attrs, param_span, Target::from(&param));
param
self.lower_attrs(
hir_id,
param_attrs,
param_span,
Target::from(&param_hir),
AstTarget::GenericParam(param),
);
param_hir
}

fn lower_generic_param_kind(
Expand Down Expand Up @@ -2897,7 +2905,13 @@ impl<'hir> LoweringContext<'_, 'hir> {
// FIXME(mgca): This might result in lowering attributes that
// then go unused as the `Target::ExprField` is not actually
// corresponding to `Node::ExprField`.
self.lower_attrs(hir_id, &f.attrs, f.span, Target::ExprField);
self.lower_attrs(
hir_id,
&f.attrs,
f.span,
Target::ExprField,
AstTarget::Expr(expr),
);
let expr = self.lower_expr_to_const_arg_direct(&f.expr, None);

&*self.arena.alloc(hir::ConstArgExprField {
Expand Down
8 changes: 7 additions & 1 deletion compiler/rustc_ast_lowering/src/pat.rs
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,13 @@ impl<'hir> LoweringContext<'_, 'hir> {

let fs = self.arena.alloc_from_iter(fields.iter().map(|f| {
let hir_id = self.lower_node_id(f.id);
self.lower_attrs(hir_id, &f.attrs, f.span, Target::PatField);
self.lower_attrs(
hir_id,
&f.attrs,
f.span,
Target::PatField,
rustc_attr_ir::target::AstTarget::Pat(pattern),
);

hir::PatField {
hir_id,
Expand Down
Loading
Loading