Skip to content

Commit c8a4d97

Browse files
Pass the target type down to parse_attribute_list
1 parent c1f74b2 commit c8a4d97

File tree

7 files changed

+48
-25
lines changed

7 files changed

+48
-25
lines changed

compiler/rustc_ast_lowering/src/block.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
use rustc_ast::{Block, BlockCheckMode, Local, LocalKind, Stmt, StmtKind};
22
use rustc_hir as hir;
3+
use rustc_hir::Target;
34
use rustc_span::sym;
45
use smallvec::SmallVec;
56

@@ -109,7 +110,7 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
109110
};
110111
let span = self.lower_span(l.span);
111112
let source = hir::LocalSource::Normal;
112-
self.lower_attrs(hir_id, &l.attrs, l.span);
113+
self.lower_attrs(hir_id, &l.attrs, l.span, Target::Statement);
113114
self.arena.alloc(hir::LetStmt { hir_id, super_, ty, pat, init, els, span, source })
114115
}
115116

compiler/rustc_ast_lowering/src/expr.rs

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ use rustc_data_structures::stack::ensure_sufficient_stack;
77
use rustc_hir as hir;
88
use rustc_hir::attrs::AttributeKind;
99
use rustc_hir::def::{DefKind, Res};
10-
use rustc_hir::{HirId, find_attr};
10+
use rustc_hir::{HirId, Target, find_attr};
1111
use rustc_middle::span_bug;
1212
use rustc_middle::ty::TyCtxt;
1313
use rustc_session::errors::report_lit_error;
@@ -74,7 +74,7 @@ impl<'hir> LoweringContext<'_, 'hir> {
7474
if !e.attrs.is_empty() {
7575
let old_attrs = self.attrs.get(&ex.hir_id.local_id).copied().unwrap_or(&[]);
7676
let new_attrs = self
77-
.lower_attrs_vec(&e.attrs, e.span, ex.hir_id)
77+
.lower_attrs_vec(&e.attrs, e.span, ex.hir_id, Target::from_expr(e))
7878
.into_iter()
7979
.chain(old_attrs.iter().cloned());
8080
let new_attrs = &*self.arena.alloc_from_iter(new_attrs);
@@ -97,7 +97,7 @@ impl<'hir> LoweringContext<'_, 'hir> {
9797
}
9898

9999
let expr_hir_id = self.lower_node_id(e.id);
100-
let attrs = self.lower_attrs(expr_hir_id, &e.attrs, e.span);
100+
let attrs = self.lower_attrs(expr_hir_id, &e.attrs, e.span, Target::from_expr(e));
101101

102102
let kind = match &e.kind {
103103
ExprKind::Array(exprs) => hir::ExprKind::Array(self.lower_exprs(exprs)),
@@ -639,7 +639,7 @@ impl<'hir> LoweringContext<'_, 'hir> {
639639
let guard = arm.guard.as_ref().map(|cond| self.lower_expr(cond));
640640
let hir_id = self.next_id();
641641
let span = self.lower_span(arm.span);
642-
self.lower_attrs(hir_id, &arm.attrs, arm.span);
642+
self.lower_attrs(hir_id, &arm.attrs, arm.span, Target::Arm);
643643
let is_never_pattern = pat.is_never_pattern();
644644
// We need to lower the body even if it's unneeded for never pattern in match,
645645
// ensure that we can get HirId for DefId if need (issue #137708).
@@ -820,6 +820,7 @@ impl<'hir> LoweringContext<'_, 'hir> {
820820
span: unstable_span,
821821
}],
822822
span,
823+
Target::Fn,
823824
);
824825
}
825826
}
@@ -1654,7 +1655,7 @@ impl<'hir> LoweringContext<'_, 'hir> {
16541655

16551656
fn lower_expr_field(&mut self, f: &ExprField) -> hir::ExprField<'hir> {
16561657
let hir_id = self.lower_node_id(f.id);
1657-
self.lower_attrs(hir_id, &f.attrs, f.span);
1658+
self.lower_attrs(hir_id, &f.attrs, f.span, Target::ExprField);
16581659
hir::ExprField {
16591660
hir_id,
16601661
ident: self.lower_ident(f.ident),
@@ -1910,7 +1911,7 @@ impl<'hir> LoweringContext<'_, 'hir> {
19101911
//
19111912
// Also, add the attributes to the outer returned expr node.
19121913
let expr = self.expr_drop_temps_mut(for_span, match_expr);
1913-
self.lower_attrs(expr.hir_id, &e.attrs, e.span);
1914+
self.lower_attrs(expr.hir_id, &e.attrs, e.span, Target::from_expr(e));
19141915
expr
19151916
}
19161917

@@ -1967,7 +1968,7 @@ impl<'hir> LoweringContext<'_, 'hir> {
19671968
let val_ident = Ident::with_dummy_span(sym::val);
19681969
let (val_pat, val_pat_nid) = self.pat_ident(span, val_ident);
19691970
let val_expr = self.expr_ident(span, val_ident, val_pat_nid);
1970-
self.lower_attrs(val_expr.hir_id, &attrs, span);
1971+
self.lower_attrs(val_expr.hir_id, &attrs, span, Target::Expression);
19711972
let continue_pat = self.pat_cf_continue(unstable_span, val_pat);
19721973
self.arm(continue_pat, val_expr)
19731974
};
@@ -1998,7 +1999,7 @@ impl<'hir> LoweringContext<'_, 'hir> {
19981999
let ret_expr = self.checked_return(Some(from_residual_expr));
19992000
self.arena.alloc(self.expr(try_span, ret_expr))
20002001
};
2001-
self.lower_attrs(ret_expr.hir_id, &attrs, span);
2002+
self.lower_attrs(ret_expr.hir_id, &attrs, span, Target::Expression);
20022003

20032004
let break_pat = self.pat_cf_break(try_span, residual_local);
20042005
self.arm(break_pat, ret_expr)

compiler/rustc_ast_lowering/src/item.rs

Lines changed: 21 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ use rustc_errors::{E0570, ErrorGuaranteed, struct_span_code_err};
55
use rustc_hir::attrs::AttributeKind;
66
use rustc_hir::def::{DefKind, PerNS, Res};
77
use rustc_hir::def_id::{CRATE_DEF_ID, LocalDefId};
8-
use rustc_hir::{self as hir, HirId, LifetimeSource, PredicateOrigin, find_attr};
8+
use rustc_hir::{self as hir, HirId, LifetimeSource, PredicateOrigin, Target, find_attr};
99
use rustc_index::{IndexSlice, IndexVec};
1010
use rustc_middle::span_bug;
1111
use rustc_middle::ty::{ResolverAstLowering, TyCtxt};
@@ -80,7 +80,7 @@ impl<'a, 'hir> ItemLowerer<'a, 'hir> {
8080
self.with_lctx(CRATE_NODE_ID, |lctx| {
8181
let module = lctx.lower_mod(&c.items, &c.spans);
8282
// FIXME(jdonszelman): is dummy span ever a problem here?
83-
lctx.lower_attrs(hir::CRATE_HIR_ID, &c.attrs, DUMMY_SP);
83+
lctx.lower_attrs(hir::CRATE_HIR_ID, &c.attrs, DUMMY_SP, Target::Crate);
8484
hir::OwnerNode::Crate(module)
8585
})
8686
}
@@ -136,7 +136,7 @@ impl<'hir> LoweringContext<'_, 'hir> {
136136
fn lower_item(&mut self, i: &Item) -> &'hir hir::Item<'hir> {
137137
let vis_span = self.lower_span(i.vis.span);
138138
let hir_id = hir::HirId::make_owner(self.current_hir_id_owner.def_id);
139-
let attrs = self.lower_attrs(hir_id, &i.attrs, i.span);
139+
let attrs = self.lower_attrs(hir_id, &i.attrs, i.span, Target::from_ast_item(i));
140140
let kind = self.lower_item_kind(i.span, i.id, hir_id, attrs, vis_span, &i.kind);
141141
let item = hir::Item {
142142
owner_id: hir_id.expect_owner(),
@@ -649,7 +649,8 @@ impl<'hir> LoweringContext<'_, 'hir> {
649649
fn lower_foreign_item(&mut self, i: &ForeignItem) -> &'hir hir::ForeignItem<'hir> {
650650
let hir_id = hir::HirId::make_owner(self.current_hir_id_owner.def_id);
651651
let owner_id = hir_id.expect_owner();
652-
let attrs = self.lower_attrs(hir_id, &i.attrs, i.span);
652+
let attrs =
653+
self.lower_attrs(hir_id, &i.attrs, i.span, Target::from_foreign_item_kind(&i.kind));
653654
let (ident, kind) = match &i.kind {
654655
ForeignItemKind::Fn(box Fn { sig, ident, generics, define_opaque, .. }) => {
655656
let fdec = &sig.decl;
@@ -718,7 +719,7 @@ impl<'hir> LoweringContext<'_, 'hir> {
718719

719720
fn lower_variant(&mut self, item_kind: &ItemKind, v: &Variant) -> hir::Variant<'hir> {
720721
let hir_id = self.lower_node_id(v.id);
721-
self.lower_attrs(hir_id, &v.attrs, v.span);
722+
self.lower_attrs(hir_id, &v.attrs, v.span, Target::Variant);
722723
hir::Variant {
723724
hir_id,
724725
def_id: self.local_def_id(v.id),
@@ -801,7 +802,7 @@ impl<'hir> LoweringContext<'_, 'hir> {
801802
) -> hir::FieldDef<'hir> {
802803
let ty = self.lower_ty(&f.ty, ImplTraitContext::Disallowed(ImplTraitPosition::FieldTy));
803804
let hir_id = self.lower_node_id(f.id);
804-
self.lower_attrs(hir_id, &f.attrs, f.span);
805+
self.lower_attrs(hir_id, &f.attrs, f.span, Target::Field);
805806
hir::FieldDef {
806807
span: self.lower_span(f.span),
807808
hir_id,
@@ -820,7 +821,12 @@ impl<'hir> LoweringContext<'_, 'hir> {
820821

821822
fn lower_trait_item(&mut self, i: &AssocItem) -> &'hir hir::TraitItem<'hir> {
822823
let hir_id = hir::HirId::make_owner(self.current_hir_id_owner.def_id);
823-
let attrs = self.lower_attrs(hir_id, &i.attrs, i.span);
824+
let attrs = self.lower_attrs(
825+
hir_id,
826+
&i.attrs,
827+
i.span,
828+
Target::from_assoc_item_kind(&i.kind, AssocCtxt::Trait),
829+
);
824830
let trait_item_def_id = hir_id.expect_owner();
825831

826832
let (ident, generics, kind, has_default) = match &i.kind {
@@ -991,7 +997,12 @@ impl<'hir> LoweringContext<'_, 'hir> {
991997
let has_value = true;
992998
let (defaultness, _) = self.lower_defaultness(i.kind.defaultness(), has_value);
993999
let hir_id = hir::HirId::make_owner(self.current_hir_id_owner.def_id);
994-
let attrs = self.lower_attrs(hir_id, &i.attrs, i.span);
1000+
let attrs = self.lower_attrs(
1001+
hir_id,
1002+
&i.attrs,
1003+
i.span,
1004+
Target::from_assoc_item_kind(&i.kind, AssocCtxt::Impl { of_trait: is_in_trait_impl }),
1005+
);
9951006

9961007
let (ident, (generics, kind)) = match &i.kind {
9971008
AssocItemKind::Const(box ConstItem {
@@ -1161,7 +1172,7 @@ impl<'hir> LoweringContext<'_, 'hir> {
11611172

11621173
fn lower_param(&mut self, param: &Param) -> hir::Param<'hir> {
11631174
let hir_id = self.lower_node_id(param.id);
1164-
self.lower_attrs(hir_id, &param.attrs, param.span);
1175+
self.lower_attrs(hir_id, &param.attrs, param.span, Target::Param);
11651176
hir::Param {
11661177
hir_id,
11671178
pat: self.lower_pat(&param.pat),
@@ -1841,7 +1852,7 @@ impl<'hir> LoweringContext<'_, 'hir> {
18411852
) -> hir::WherePredicate<'hir> {
18421853
let hir_id = self.lower_node_id(pred.id);
18431854
let span = self.lower_span(pred.span);
1844-
self.lower_attrs(hir_id, &pred.attrs, span);
1855+
self.lower_attrs(hir_id, &pred.attrs, span, Target::WherePredicate);
18451856
let kind = self.arena.alloc(match &pred.kind {
18461857
WherePredicateKind::BoundPredicate(WhereBoundPredicate {
18471858
bound_generic_params,

compiler/rustc_ast_lowering/src/lib.rs

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ use rustc_hir::def_id::{CRATE_DEF_ID, LOCAL_CRATE, LocalDefId};
5454
use rustc_hir::lints::DelayedLint;
5555
use rustc_hir::{
5656
self as hir, AngleBrackets, ConstArg, GenericArg, HirId, ItemLocalMap, LifetimeSource,
57-
LifetimeSyntax, ParamName, TraitCandidate,
57+
LifetimeSyntax, ParamName, Target, TraitCandidate,
5858
};
5959
use rustc_index::{Idx, IndexSlice, IndexVec};
6060
use rustc_macros::extension;
@@ -942,11 +942,13 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
942942
id: HirId,
943943
attrs: &[Attribute],
944944
target_span: Span,
945+
target: Target,
945946
) -> &'hir [hir::Attribute] {
946947
if attrs.is_empty() {
947948
&[]
948949
} else {
949-
let lowered_attrs = self.lower_attrs_vec(attrs, self.lower_span(target_span), id);
950+
let lowered_attrs =
951+
self.lower_attrs_vec(attrs, self.lower_span(target_span), id, target);
950952

951953
assert_eq!(id.owner, self.current_hir_id_owner);
952954
let ret = self.arena.alloc_from_iter(lowered_attrs);
@@ -971,12 +973,14 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
971973
attrs: &[Attribute],
972974
target_span: Span,
973975
target_hir_id: HirId,
976+
target: Target,
974977
) -> Vec<hir::Attribute> {
975978
let l = self.span_lowerer();
976979
self.attribute_parser.parse_attribute_list(
977980
attrs,
978981
target_span,
979982
target_hir_id,
983+
target,
980984
OmitDoc::Lower,
981985
|s| l.lower(s),
982986
|l| {
@@ -1939,7 +1943,7 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
19391943
let (name, kind) = self.lower_generic_param_kind(param, source);
19401944

19411945
let hir_id = self.lower_node_id(param.id);
1942-
self.lower_attrs(hir_id, &param.attrs, param.span());
1946+
self.lower_attrs(hir_id, &param.attrs, param.span(), Target::Param);
19431947
hir::GenericParam {
19441948
hir_id,
19451949
def_id: self.local_def_id(param.id),

compiler/rustc_ast_lowering/src/pat.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ use std::sync::Arc;
33
use rustc_ast::*;
44
use rustc_data_structures::stack::ensure_sufficient_stack;
55
use rustc_hir::def::{DefKind, Res};
6-
use rustc_hir::{self as hir, LangItem};
6+
use rustc_hir::{self as hir, LangItem, Target};
77
use rustc_middle::span_bug;
88
use rustc_span::source_map::{Spanned, respan};
99
use rustc_span::{DesugaringKind, Ident, Span};
@@ -93,7 +93,7 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
9393

9494
let fs = self.arena.alloc_from_iter(fields.iter().map(|f| {
9595
let hir_id = self.lower_node_id(f.id);
96-
self.lower_attrs(hir_id, &f.attrs, f.span);
96+
self.lower_attrs(hir_id, &f.attrs, f.span, Target::PatField);
9797

9898
hir::PatField {
9999
hir_id,

compiler/rustc_attr_parsing/src/context.rs

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,9 @@ use rustc_errors::{DiagCtxtHandle, Diagnostic};
99
use rustc_feature::{AttributeTemplate, Features};
1010
use rustc_hir::attrs::AttributeKind;
1111
use rustc_hir::lints::{AttributeLint, AttributeLintKind};
12-
use rustc_hir::{AttrArgs, AttrItem, AttrPath, Attribute, HashIgnoredAttrId, HirId};
12+
use rustc_hir::{
13+
AttrArgs, AttrItem, AttrPath, Attribute, HashIgnoredAttrId, HirId, MethodKind, Target,
14+
};
1315
use rustc_session::Session;
1416
use rustc_span::{DUMMY_SP, ErrorGuaranteed, Span, Symbol, sym};
1517

@@ -687,6 +689,7 @@ impl<'sess> AttributeParser<'sess, Early> {
687689
attrs,
688690
target_span,
689691
target_node_id,
692+
Target::Crate, // Does not matter, we're not going to emit errors anyways
690693
OmitDoc::Skip,
691694
std::convert::identity,
692695
|_lint| {
@@ -773,6 +776,7 @@ impl<'sess, S: Stage> AttributeParser<'sess, S> {
773776
attrs: &[ast::Attribute],
774777
target_span: Span,
775778
target_id: S::Id,
779+
target: Target,
776780
omit_doc: OmitDoc,
777781

778782
lower_span: impl Copy + Fn(Span) -> Span,

compiler/rustc_resolve/src/def_collector.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ use rustc_ast::*;
55
use rustc_attr_parsing::{AttributeParser, Early, OmitDoc, ShouldEmit};
66
use rustc_expand::expand::AstFragment;
77
use rustc_hir as hir;
8+
use rustc_hir::Target;
89
use rustc_hir::def::{CtorKind, CtorOf, DefKind};
910
use rustc_hir::def_id::LocalDefId;
1011
use rustc_middle::span_bug;
@@ -138,6 +139,7 @@ impl<'a, 'ra, 'tcx> visit::Visitor<'a> for DefCollector<'a, 'ra, 'tcx> {
138139
&i.attrs,
139140
i.span,
140141
i.id,
142+
Target::MacroDef,
141143
OmitDoc::Skip,
142144
std::convert::identity,
143145
|_l| {

0 commit comments

Comments
 (0)