Skip to content

Commit 0d95309

Browse files
committed
New attribute parsing
1 parent 654805a commit 0d95309

File tree

104 files changed

+3898
-1056
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

104 files changed

+3898
-1056
lines changed

Cargo.lock

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3273,13 +3273,15 @@ version = "0.0.0"
32733273
dependencies = [
32743274
"rustc_ast",
32753275
"rustc_ast_pretty",
3276+
"rustc_attr",
32763277
"rustc_data_structures",
32773278
"rustc_errors",
32783279
"rustc_fluent_macro",
32793280
"rustc_hir",
32803281
"rustc_index",
32813282
"rustc_macros",
32823283
"rustc_middle",
3284+
"rustc_parse",
32833285
"rustc_session",
32843286
"rustc_span",
32853287
"rustc_target",
@@ -3337,6 +3339,7 @@ dependencies = [
33373339
"rustc_serialize",
33383340
"rustc_session",
33393341
"rustc_span",
3342+
"thin-vec",
33403343
]
33413344

33423345
[[package]]
@@ -4346,6 +4349,7 @@ version = "0.0.0"
43464349
dependencies = [
43474350
"bitflags 2.6.0",
43484351
"rustc_abi",
4352+
"rustc_ast",
43494353
"rustc_data_structures",
43504354
"rustc_hir",
43514355
"rustc_middle",
@@ -4439,6 +4443,7 @@ dependencies = [
44394443
"punycode",
44404444
"rustc-demangle",
44414445
"rustc_abi",
4446+
"rustc_ast",
44424447
"rustc_data_structures",
44434448
"rustc_errors",
44444449
"rustc_hir",

compiler/rustc_ast_lowering/Cargo.toml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,13 +10,15 @@ doctest = false
1010
# tidy-alphabetical-start
1111
rustc_ast = { path = "../rustc_ast" }
1212
rustc_ast_pretty = { path = "../rustc_ast_pretty" }
13+
rustc_attr = { path = "../rustc_attr" }
1314
rustc_data_structures = { path = "../rustc_data_structures" }
1415
rustc_errors = { path = "../rustc_errors" }
1516
rustc_fluent_macro = { path = "../rustc_fluent_macro" }
1617
rustc_hir = { path = "../rustc_hir" }
1718
rustc_index = { path = "../rustc_index" }
1819
rustc_macros = { path = "../rustc_macros" }
1920
rustc_middle = { path = "../rustc_middle" }
21+
rustc_parse = { path = "../rustc_parse" }
2022
rustc_session = { path = "../rustc_session" }
2123
rustc_span = { path = "../rustc_span" }
2224
rustc_target = { path = "../rustc_target" }

compiler/rustc_ast_lowering/src/block.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -97,7 +97,7 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
9797
};
9898
let span = self.lower_span(l.span);
9999
let source = hir::LocalSource::Normal;
100-
self.lower_attrs(hir_id, &l.attrs);
100+
self.lower_attrs(hir_id, &l.attrs, l.span);
101101
self.arena.alloc(hir::LetStmt { hir_id, ty, pat, init, els, span, source })
102102
}
103103

compiler/rustc_ast_lowering/src/expr.rs

Lines changed: 21 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -78,9 +78,8 @@ impl<'hir> LoweringContext<'_, 'hir> {
7878
self.attrs.insert(
7979
ex.hir_id.local_id,
8080
&*self.arena.alloc_from_iter(
81-
e.attrs
82-
.iter()
83-
.map(|a| self.lower_attr(a))
81+
self.lower_attrs_vec(&e.attrs, e.span)
82+
.into_iter()
8483
.chain(old_attrs.iter().cloned()),
8584
),
8685
);
@@ -99,7 +98,7 @@ impl<'hir> LoweringContext<'_, 'hir> {
9998
}
10099

101100
let expr_hir_id = self.lower_node_id(e.id);
102-
self.lower_attrs(expr_hir_id, &e.attrs);
101+
self.lower_attrs(expr_hir_id, &e.attrs, e.span);
103102

104103
let kind = match &e.kind {
105104
ExprKind::Array(exprs) => hir::ExprKind::Array(self.lower_exprs(exprs)),
@@ -638,7 +637,7 @@ impl<'hir> LoweringContext<'_, 'hir> {
638637
let guard = arm.guard.as_ref().map(|cond| self.lower_expr(cond));
639638
let hir_id = self.next_id();
640639
let span = self.lower_span(arm.span);
641-
self.lower_attrs(hir_id, &arm.attrs);
640+
self.lower_attrs(hir_id, &arm.attrs, arm.span);
642641
let is_never_pattern = pat.is_never_pattern();
643642
let body = if let Some(body) = &arm.body
644643
&& !is_never_pattern
@@ -796,15 +795,19 @@ impl<'hir> LoweringContext<'_, 'hir> {
796795
span,
797796
Some(Lrc::clone(&self.allow_gen_future)),
798797
);
799-
self.lower_attrs(inner_hir_id, &[Attribute {
800-
kind: AttrKind::Normal(ptr::P(NormalAttr::from_ident(Ident::new(
801-
sym::track_caller,
802-
span,
803-
)))),
804-
id: self.tcx.sess.psess.attr_id_generator.mk_attr_id(),
805-
style: AttrStyle::Outer,
806-
span: unstable_span,
807-
}]);
798+
self.lower_attrs(
799+
inner_hir_id,
800+
&[Attribute {
801+
kind: AttrKind::Normal(ptr::P(NormalAttr::from_ident(Ident::new(
802+
sym::track_caller,
803+
span,
804+
)))),
805+
id: self.tcx.sess.psess.attr_id_generator.mk_attr_id(),
806+
style: AttrStyle::Outer,
807+
span: unstable_span,
808+
}],
809+
span,
810+
);
808811
}
809812
}
810813

@@ -1609,7 +1612,7 @@ impl<'hir> LoweringContext<'_, 'hir> {
16091612

16101613
fn lower_expr_field(&mut self, f: &ExprField) -> hir::ExprField<'hir> {
16111614
let hir_id = self.lower_node_id(f.id);
1612-
self.lower_attrs(hir_id, &f.attrs);
1615+
self.lower_attrs(hir_id, &f.attrs, f.span);
16131616
hir::ExprField {
16141617
hir_id,
16151618
ident: self.lower_ident(f.ident),
@@ -1872,7 +1875,7 @@ impl<'hir> LoweringContext<'_, 'hir> {
18721875
//
18731876
// Also, add the attributes to the outer returned expr node.
18741877
let expr = self.expr_drop_temps_mut(for_span, match_expr);
1875-
self.lower_attrs(expr.hir_id, &e.attrs);
1878+
self.lower_attrs(expr.hir_id, &e.attrs, e.span);
18761879
expr
18771880
}
18781881

@@ -1929,7 +1932,7 @@ impl<'hir> LoweringContext<'_, 'hir> {
19291932
let val_ident = Ident::with_dummy_span(sym::val);
19301933
let (val_pat, val_pat_nid) = self.pat_ident(span, val_ident);
19311934
let val_expr = self.expr_ident(span, val_ident, val_pat_nid);
1932-
self.lower_attrs(val_expr.hir_id, &attrs);
1935+
self.lower_attrs(val_expr.hir_id, &attrs, span);
19331936
let continue_pat = self.pat_cf_continue(unstable_span, val_pat);
19341937
self.arm(continue_pat, val_expr)
19351938
};
@@ -1959,7 +1962,7 @@ impl<'hir> LoweringContext<'_, 'hir> {
19591962
} else {
19601963
self.arena.alloc(self.expr(try_span, hir::ExprKind::Ret(Some(from_residual_expr))))
19611964
};
1962-
self.lower_attrs(ret_expr.hir_id, &attrs);
1965+
self.lower_attrs(ret_expr.hir_id, &attrs, ret_expr.span);
19631966

19641967
let break_pat = self.pat_cf_break(try_span, residual_local);
19651968
self.arm(break_pat, ret_expr)

compiler/rustc_ast_lowering/src/item.rs

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ use rustc_middle::span_bug;
1111
use rustc_middle::ty::{ResolverAstLowering, TyCtxt};
1212
use rustc_span::edit_distance::find_best_match_for_name;
1313
use rustc_span::symbol::{Ident, kw, sym};
14-
use rustc_span::{DesugaringKind, Span, Symbol};
14+
use rustc_span::{DUMMY_SP, DesugaringKind, Span, Symbol};
1515
use rustc_target::spec::abi;
1616
use smallvec::{SmallVec, smallvec};
1717
use thin_vec::ThinVec;
@@ -91,7 +91,8 @@ impl<'a, 'hir> ItemLowerer<'a, 'hir> {
9191
debug_assert_eq!(self.resolver.node_id_to_def_id[&CRATE_NODE_ID], CRATE_DEF_ID);
9292
self.with_lctx(CRATE_NODE_ID, |lctx| {
9393
let module = lctx.lower_mod(&c.items, &c.spans);
94-
lctx.lower_attrs(hir::CRATE_HIR_ID, &c.attrs);
94+
// FIXME(jdonszelman): is dummy span ever a problem here?
95+
lctx.lower_attrs(hir::CRATE_HIR_ID, &c.attrs, DUMMY_SP);
9596
hir::OwnerNode::Crate(module)
9697
})
9798
}
@@ -155,7 +156,7 @@ impl<'hir> LoweringContext<'_, 'hir> {
155156
let mut ident = i.ident;
156157
let vis_span = self.lower_span(i.vis.span);
157158
let hir_id = hir::HirId::make_owner(self.current_hir_id_owner.def_id);
158-
let attrs = self.lower_attrs(hir_id, &i.attrs);
159+
let attrs = self.lower_attrs(hir_id, &i.attrs, i.span);
159160
let kind = self.lower_item_kind(i.span, i.id, hir_id, &mut ident, attrs, vis_span, &i.kind);
160161
let item = hir::Item {
161162
owner_id: hir_id.expect_owner(),
@@ -606,7 +607,7 @@ impl<'hir> LoweringContext<'_, 'hir> {
606607
fn lower_foreign_item(&mut self, i: &ForeignItem) -> &'hir hir::ForeignItem<'hir> {
607608
let hir_id = hir::HirId::make_owner(self.current_hir_id_owner.def_id);
608609
let owner_id = hir_id.expect_owner();
609-
self.lower_attrs(hir_id, &i.attrs);
610+
self.lower_attrs(hir_id, &i.attrs, i.span);
610611
let item = hir::ForeignItem {
611612
owner_id,
612613
ident: self.lower_ident(i.ident),
@@ -664,7 +665,7 @@ impl<'hir> LoweringContext<'_, 'hir> {
664665

665666
fn lower_variant(&mut self, v: &Variant) -> hir::Variant<'hir> {
666667
let hir_id = self.lower_node_id(v.id);
667-
self.lower_attrs(hir_id, &v.attrs);
668+
self.lower_attrs(hir_id, &v.attrs, v.span);
668669
hir::Variant {
669670
hir_id,
670671
def_id: self.local_def_id(v.id),
@@ -712,7 +713,7 @@ impl<'hir> LoweringContext<'_, 'hir> {
712713
) -> hir::FieldDef<'hir> {
713714
let ty = self.lower_ty(&f.ty, ImplTraitContext::Disallowed(ImplTraitPosition::FieldTy));
714715
let hir_id = self.lower_node_id(f.id);
715-
self.lower_attrs(hir_id, &f.attrs);
716+
self.lower_attrs(hir_id, &f.attrs, f.span);
716717
hir::FieldDef {
717718
span: self.lower_span(f.span),
718719
hir_id,
@@ -730,7 +731,7 @@ impl<'hir> LoweringContext<'_, 'hir> {
730731

731732
fn lower_trait_item(&mut self, i: &AssocItem) -> &'hir hir::TraitItem<'hir> {
732733
let hir_id = hir::HirId::make_owner(self.current_hir_id_owner.def_id);
733-
self.lower_attrs(hir_id, &i.attrs);
734+
self.lower_attrs(hir_id, &i.attrs, i.span);
734735
let trait_item_def_id = hir_id.expect_owner();
735736

736737
let (generics, kind, has_default) = match &i.kind {
@@ -860,7 +861,7 @@ impl<'hir> LoweringContext<'_, 'hir> {
860861
let has_value = true;
861862
let (defaultness, _) = self.lower_defaultness(i.kind.defaultness(), has_value);
862863
let hir_id = hir::HirId::make_owner(self.current_hir_id_owner.def_id);
863-
self.lower_attrs(hir_id, &i.attrs);
864+
self.lower_attrs(hir_id, &i.attrs, i.span);
864865

865866
let (generics, kind) = match &i.kind {
866867
AssocItemKind::Const(box ConstItem { generics, ty, expr, .. }) => self.lower_generics(
@@ -1015,7 +1016,7 @@ impl<'hir> LoweringContext<'_, 'hir> {
10151016

10161017
fn lower_param(&mut self, param: &Param) -> hir::Param<'hir> {
10171018
let hir_id = self.lower_node_id(param.id);
1018-
self.lower_attrs(hir_id, &param.attrs);
1019+
self.lower_attrs(hir_id, &param.attrs, param.span);
10191020
hir::Param {
10201021
hir_id,
10211022
pat: self.lower_pat(&param.pat),

compiler/rustc_ast_lowering/src/lib.rs

Lines changed: 22 additions & 59 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@
4242

4343
use rustc_ast::node_id::NodeMap;
4444
use rustc_ast::{self as ast, *};
45+
use rustc_attr::{AttributeParseContext, OmitDoc};
4546
use rustc_data_structures::captures::Captures;
4647
use rustc_data_structures::fingerprint::Fingerprint;
4748
use rustc_data_structures::sorted_map::SortedMap;
@@ -133,10 +134,13 @@ struct LoweringContext<'a, 'hir> {
133134
allow_async_iterator: Lrc<[Symbol]>,
134135
allow_for_await: Lrc<[Symbol]>,
135136
allow_async_fn_traits: Lrc<[Symbol]>,
137+
138+
attribute_parse_context: AttributeParseContext<'hir>,
136139
}
137140

138141
impl<'a, 'hir> LoweringContext<'a, 'hir> {
139142
fn new(tcx: TyCtxt<'hir>, resolver: &'a mut ResolverAstLowering) -> Self {
143+
let registered_tools = tcx.registered_tools(()).iter().map(|x| x.name).collect();
140144
Self {
141145
// Pseudo-globals.
142146
tcx,
@@ -176,6 +180,12 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
176180
// FIXME(gen_blocks): how does `closure_track_caller`/`async_fn_track_caller`
177181
// interact with `gen`/`async gen` blocks
178182
allow_async_iterator: [sym::gen_future, sym::async_iterator].into(),
183+
184+
attribute_parse_context: AttributeParseContext::new(
185+
tcx.sess,
186+
tcx.features(),
187+
registered_tools,
188+
),
179189
}
180190
}
181191

@@ -211,7 +221,6 @@ impl ResolverAstLowering {
211221
None
212222
}
213223

214-
/// Obtains resolution for a `NodeId` with a single resolution.
215224
fn get_partial_res(&self, id: NodeId) -> Option<PartialRes> {
216225
self.partial_res_map.get(&id).copied()
217226
}
@@ -839,45 +848,27 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
839848
ret
840849
}
841850

842-
fn lower_attrs(&mut self, id: HirId, attrs: &[Attribute]) -> &'hir [hir::Attribute] {
851+
fn lower_attrs(
852+
&mut self,
853+
id: HirId,
854+
attrs: &[Attribute],
855+
target_span: Span,
856+
) -> &'hir [hir::Attribute] {
843857
if attrs.is_empty() {
844858
&[]
845859
} else {
860+
let lowered_attrs = self.lower_attrs_vec(attrs, target_span);
861+
846862
debug_assert_eq!(id.owner, self.current_hir_id_owner);
847-
let ret = self.arena.alloc_from_iter(attrs.iter().map(|a| self.lower_attr(a)));
863+
let ret = self.arena.alloc_from_iter(lowered_attrs);
848864
debug_assert!(!ret.is_empty());
849865
self.attrs.insert(id.local_id, ret);
850866
ret
851867
}
852868
}
853869

854-
fn lower_attr(&self, attr: &Attribute) -> hir::Attribute {
855-
// Note that we explicitly do not walk the path. Since we don't really
856-
// lower attributes (we use the AST version) there is nowhere to keep
857-
// the `HirId`s. We don't actually need HIR version of attributes anyway.
858-
// Tokens are also not needed after macro expansion and parsing.
859-
let kind = match attr.kind {
860-
AttrKind::Normal(ref normal) => hir::AttrKind::Normal(Box::new(hir::AttrItem {
861-
unsafety: self.lower_safety(normal.item.unsafety, hir::Safety::Safe),
862-
path: hir::AttrPath {
863-
segments: normal
864-
.item
865-
.path
866-
.segments
867-
.iter()
868-
.map(|i| i.ident)
869-
.collect::<Vec<_>>()
870-
.into_boxed_slice(),
871-
span: normal.item.path.span,
872-
},
873-
args: self.lower_attr_args(&normal.item.args),
874-
})),
875-
AttrKind::DocComment(comment_kind, data) => {
876-
hir::AttrKind::DocComment(comment_kind, data)
877-
}
878-
};
879-
880-
hir::Attribute { kind, id: attr.id, style: attr.style, span: self.lower_span(attr.span) }
870+
fn lower_attrs_vec(&self, attrs: &[Attribute], target_span: Span) -> Vec<hir::Attribute> {
871+
self.attribute_parse_context.parse_attribute_list(attrs, target_span, OmitDoc::Lower)
881872
}
882873

883874
fn alias_attrs(&mut self, id: HirId, target_id: HirId) {
@@ -889,34 +880,6 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
889880
}
890881
}
891882

892-
fn lower_attr_args(&self, args: &AttrArgs) -> hir::AttrArgs {
893-
match args {
894-
AttrArgs::Empty => hir::AttrArgs::Empty,
895-
AttrArgs::Delimited(args) => hir::AttrArgs::Delimited(self.lower_delim_args(args)),
896-
// This is an inert key-value attribute - it will never be visible to macros
897-
// after it gets lowered to HIR. Therefore, we can extract literals to handle
898-
// nonterminals in `#[doc]` (e.g. `#[doc = $e]`).
899-
&AttrArgs::Eq { eq_span, ref expr } => {
900-
// In valid code the value always ends up as a single literal. Otherwise, a dummy
901-
// literal suffices because the error is handled elsewhere.
902-
let lit = if let ExprKind::Lit(token_lit) = expr.kind
903-
&& let Ok(lit) = MetaItemLit::from_token_lit(token_lit, expr.span)
904-
{
905-
lit
906-
} else {
907-
let guar = self.dcx().has_errors().unwrap();
908-
MetaItemLit {
909-
symbol: kw::Empty,
910-
suffix: None,
911-
kind: LitKind::Err(guar),
912-
span: DUMMY_SP,
913-
}
914-
};
915-
hir::AttrArgs::Eq { eq_span, expr: lit }
916-
}
917-
}
918-
}
919-
920883
fn lower_delim_args(&self, args: &DelimArgs) -> DelimArgs {
921884
DelimArgs { dspan: args.dspan, delim: args.delim, tokens: args.tokens.flattened() }
922885
}
@@ -1807,7 +1770,7 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
18071770
let (name, kind) = self.lower_generic_param_kind(param, source);
18081771

18091772
let hir_id = self.lower_node_id(param.id);
1810-
self.lower_attrs(hir_id, &param.attrs);
1773+
self.lower_attrs(hir_id, &param.attrs, param.span());
18111774
hir::GenericParam {
18121775
hir_id,
18131776
def_id: self.local_def_id(param.id),

compiler/rustc_ast_lowering/src/pat.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -82,7 +82,7 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
8282

8383
let fs = self.arena.alloc_from_iter(fields.iter().map(|f| {
8484
let hir_id = self.lower_node_id(f.id);
85-
self.lower_attrs(hir_id, &f.attrs);
85+
self.lower_attrs(hir_id, &f.attrs, f.span);
8686

8787
hir::PatField {
8888
hir_id,

0 commit comments

Comments
 (0)