Skip to content

Commit 9d7342e

Browse files
committed
Auto merge of rust-lang#135726 - jdonszelmann:attr-parsing, r=<try>
New attribute parsing infrastructure Another step in the plan outlined in rust-lang#131229 introduces infrastructure for structured parsers for attributes, as well as converting a couple of complex attributes to have such structured parsers. This PR may prove too large to review. I left some of my own comments to guide it a little. Some general notes: - The first commit is basically standalone. It just preps some mostly unrelated sources for the rest of the PR to work. It might not have enormous merit on its own, but not negative merit either. Could be merged alone, but also doesn't make the review a whole lot easier. (but it's only +274 -209) - The second commit is the one that introduces new infrastructure. It's the important one to review. - The 3rd commit uses the new infrastructure showing how some of the more complex attributes can be parsed using it. Theoretically can be split up, though the parsers in this commit are the ones that really test the new infrastructure and show that it all works. - The 4th commit fixes up rustdoc and clippy. In the previous 2 they didn't compile yet while the compiler does. Separated them out to separate concerns and make the rest more palatable. - The 5th commit blesses some test outputs. Sometimes that's just because a diagnostic happens slightly earlier than before, which I'd say is acceptable. Sometimes a diagnostic is now only emitted once where it would've been twice before (yay! fixed some bugs). One test I actually moved from crashes to fixed, because it simply doesn't crash anymore. That's why this PR Closes rust-lang#132391. I think most choices I made here are generally reasonable, but let me know if you disagree anywhere. - The 6th commit adds a derive to pretty print attributes - The 7th removes smir apis for attributes, for the time being. The api will at some point be replaced by one based on `rustc_ast_data_structures::AttributeKind` In general, a lot of the additions here are comments. I've found it very important to document new things in the 2nd commit well so other people can start using it. Closes rust-lang#132391 Closes rust-lang#136717
2 parents bb029a1 + 4daa35c commit 9d7342e

File tree

167 files changed

+3931
-2260
lines changed

Some content is hidden

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

167 files changed

+3931
-2260
lines changed

Cargo.lock

+14-5
Original file line numberDiff line numberDiff line change
@@ -3207,6 +3207,7 @@ dependencies = [
32073207
"rustc_abi",
32083208
"rustc_ast",
32093209
"rustc_ast_pretty",
3210+
"rustc_attr_parsing",
32103211
"rustc_data_structures",
32113212
"rustc_errors",
32123213
"rustc_feature",
@@ -3215,6 +3216,7 @@ dependencies = [
32153216
"rustc_index",
32163217
"rustc_macros",
32173218
"rustc_middle",
3219+
"rustc_parse",
32183220
"rustc_session",
32193221
"rustc_span",
32203222
"rustc_target",
@@ -3263,14 +3265,10 @@ dependencies = [
32633265
"rustc_ast",
32643266
"rustc_ast_pretty",
32653267
"rustc_data_structures",
3266-
"rustc_errors",
3267-
"rustc_feature",
3268-
"rustc_fluent_macro",
3269-
"rustc_lexer",
32703268
"rustc_macros",
32713269
"rustc_serialize",
3272-
"rustc_session",
32733270
"rustc_span",
3271+
"thin-vec",
32743272
]
32753273

32763274
[[package]]
@@ -3285,11 +3283,13 @@ dependencies = [
32853283
"rustc_errors",
32863284
"rustc_feature",
32873285
"rustc_fluent_macro",
3286+
"rustc_hir",
32883287
"rustc_lexer",
32893288
"rustc_macros",
32903289
"rustc_serialize",
32913290
"rustc_session",
32923291
"rustc_span",
3292+
"thin-vec",
32933293
]
32943294

32953295
[[package]]
@@ -3342,6 +3342,7 @@ dependencies = [
33423342
"rustc_expand",
33433343
"rustc_feature",
33443344
"rustc_fluent_macro",
3345+
"rustc_hir",
33453346
"rustc_index",
33463347
"rustc_lexer",
33473348
"rustc_lint_defs",
@@ -3598,6 +3599,7 @@ dependencies = [
35983599
"rustc_abi",
35993600
"rustc_ast",
36003601
"rustc_ast_pretty",
3602+
"rustc_attr_data_structures",
36013603
"rustc_data_structures",
36023604
"rustc_error_codes",
36033605
"rustc_error_messages",
@@ -3632,6 +3634,7 @@ dependencies = [
36323634
"rustc_errors",
36333635
"rustc_feature",
36343636
"rustc_fluent_macro",
3637+
"rustc_hir",
36353638
"rustc_lexer",
36363639
"rustc_lint_defs",
36373640
"rustc_macros",
@@ -3690,6 +3693,7 @@ dependencies = [
36903693
"rustc_abi",
36913694
"rustc_arena",
36923695
"rustc_ast",
3696+
"rustc_attr_data_structures",
36933697
"rustc_data_structures",
36943698
"rustc_hashes",
36953699
"rustc_index",
@@ -3737,6 +3741,7 @@ dependencies = [
37373741
"rustc_abi",
37383742
"rustc_ast",
37393743
"rustc_ast_pretty",
3744+
"rustc_attr_parsing",
37403745
"rustc_hir",
37413746
"rustc_span",
37423747
]
@@ -4244,6 +4249,7 @@ name = "rustc_query_impl"
42444249
version = "0.0.0"
42454250
dependencies = [
42464251
"measureme",
4252+
"rustc_attr_data_structures",
42474253
"rustc_data_structures",
42484254
"rustc_errors",
42494255
"rustc_hashes",
@@ -4266,6 +4272,7 @@ dependencies = [
42664272
"rustc-rayon-core",
42674273
"rustc_abi",
42684274
"rustc_ast",
4275+
"rustc_attr_data_structures",
42694276
"rustc_data_structures",
42704277
"rustc_errors",
42714278
"rustc_feature",
@@ -4316,6 +4323,7 @@ version = "0.0.0"
43164323
dependencies = [
43174324
"bitflags",
43184325
"rustc_abi",
4326+
"rustc_ast",
43194327
"rustc_data_structures",
43204328
"rustc_hir",
43214329
"rustc_middle",
@@ -4412,6 +4420,7 @@ dependencies = [
44124420
"punycode",
44134421
"rustc-demangle",
44144422
"rustc_abi",
4423+
"rustc_ast",
44154424
"rustc_data_structures",
44164425
"rustc_errors",
44174426
"rustc_hashes",

compiler/rustc_ast_lowering/Cargo.toml

+2
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ doctest = false
1111
rustc_abi = { path = "../rustc_abi" }
1212
rustc_ast = { path = "../rustc_ast" }
1313
rustc_ast_pretty = { path = "../rustc_ast_pretty" }
14+
rustc_attr_parsing = { path = "../rustc_attr_parsing" }
1415
rustc_data_structures = { path = "../rustc_data_structures" }
1516
rustc_errors = { path = "../rustc_errors" }
1617
rustc_feature = { path = "../rustc_feature" }
@@ -19,6 +20,7 @@ rustc_hir = { path = "../rustc_hir" }
1920
rustc_index = { path = "../rustc_index" }
2021
rustc_macros = { path = "../rustc_macros" }
2122
rustc_middle = { path = "../rustc_middle" }
23+
rustc_parse = { path = "../rustc_parse" }
2224
rustc_session = { path = "../rustc_session" }
2325
rustc_span = { path = "../rustc_span" }
2426
rustc_target = { path = "../rustc_target" }

compiler/rustc_ast_lowering/src/block.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -108,7 +108,7 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
108108
};
109109
let span = self.lower_span(l.span);
110110
let source = hir::LocalSource::Normal;
111-
self.lower_attrs(hir_id, &l.attrs);
111+
self.lower_attrs(hir_id, &l.attrs, l.span);
112112
self.arena.alloc(hir::LetStmt { hir_id, ty, pat, init, els, span, source })
113113
}
114114

compiler/rustc_ast_lowering/src/expr.rs

+9-9
Original file line numberDiff line numberDiff line change
@@ -77,9 +77,8 @@ impl<'hir> LoweringContext<'_, 'hir> {
7777
self.attrs.insert(
7878
ex.hir_id.local_id,
7979
&*self.arena.alloc_from_iter(
80-
e.attrs
81-
.iter()
82-
.map(|a| self.lower_attr(a))
80+
self.lower_attrs_vec(&e.attrs, e.span)
81+
.into_iter()
8382
.chain(old_attrs.iter().cloned()),
8483
),
8584
);
@@ -98,7 +97,7 @@ impl<'hir> LoweringContext<'_, 'hir> {
9897
}
9998

10099
let expr_hir_id = self.lower_node_id(e.id);
101-
self.lower_attrs(expr_hir_id, &e.attrs);
100+
self.lower_attrs(expr_hir_id, &e.attrs, e.span);
102101

103102
let kind = match &e.kind {
104103
ExprKind::Array(exprs) => hir::ExprKind::Array(self.lower_exprs(exprs)),
@@ -670,7 +669,7 @@ impl<'hir> LoweringContext<'_, 'hir> {
670669
let guard = arm.guard.as_ref().map(|cond| self.lower_expr(cond));
671670
let hir_id = self.next_id();
672671
let span = self.lower_span(arm.span);
673-
self.lower_attrs(hir_id, &arm.attrs);
672+
self.lower_attrs(hir_id, &arm.attrs, arm.span);
674673
let is_never_pattern = pat.is_never_pattern();
675674
let body = if let Some(body) = &arm.body
676675
&& !is_never_pattern
@@ -839,6 +838,7 @@ impl<'hir> LoweringContext<'_, 'hir> {
839838
style: AttrStyle::Outer,
840839
span: unstable_span,
841840
}],
841+
span,
842842
);
843843
}
844844
}
@@ -1673,7 +1673,7 @@ impl<'hir> LoweringContext<'_, 'hir> {
16731673

16741674
fn lower_expr_field(&mut self, f: &ExprField) -> hir::ExprField<'hir> {
16751675
let hir_id = self.lower_node_id(f.id);
1676-
self.lower_attrs(hir_id, &f.attrs);
1676+
self.lower_attrs(hir_id, &f.attrs, f.span);
16771677
hir::ExprField {
16781678
hir_id,
16791679
ident: self.lower_ident(f.ident),
@@ -1936,7 +1936,7 @@ impl<'hir> LoweringContext<'_, 'hir> {
19361936
//
19371937
// Also, add the attributes to the outer returned expr node.
19381938
let expr = self.expr_drop_temps_mut(for_span, match_expr);
1939-
self.lower_attrs(expr.hir_id, &e.attrs);
1939+
self.lower_attrs(expr.hir_id, &e.attrs, e.span);
19401940
expr
19411941
}
19421942

@@ -1993,7 +1993,7 @@ impl<'hir> LoweringContext<'_, 'hir> {
19931993
let val_ident = Ident::with_dummy_span(sym::val);
19941994
let (val_pat, val_pat_nid) = self.pat_ident(span, val_ident);
19951995
let val_expr = self.expr_ident(span, val_ident, val_pat_nid);
1996-
self.lower_attrs(val_expr.hir_id, &attrs);
1996+
self.lower_attrs(val_expr.hir_id, &attrs, span);
19971997
let continue_pat = self.pat_cf_continue(unstable_span, val_pat);
19981998
self.arm(continue_pat, val_expr)
19991999
};
@@ -2024,7 +2024,7 @@ impl<'hir> LoweringContext<'_, 'hir> {
20242024
let ret_expr = self.checked_return(Some(from_residual_expr));
20252025
self.arena.alloc(self.expr(try_span, ret_expr))
20262026
};
2027-
self.lower_attrs(ret_expr.hir_id, &attrs);
2027+
self.lower_attrs(ret_expr.hir_id, &attrs, ret_expr.span);
20282028

20292029
let break_pat = self.pat_cf_break(try_span, residual_local);
20302030
self.arm(break_pat, ret_expr)

compiler/rustc_ast_lowering/src/item.rs

+10-9
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ use rustc_index::{IndexSlice, IndexVec};
1111
use rustc_middle::span_bug;
1212
use rustc_middle::ty::{ResolverAstLowering, TyCtxt};
1313
use rustc_span::edit_distance::find_best_match_for_name;
14-
use rustc_span::{DesugaringKind, Ident, Span, Symbol, kw, sym};
14+
use rustc_span::{DUMMY_SP, DesugaringKind, Ident, Span, Symbol, kw, sym};
1515
use smallvec::{SmallVec, smallvec};
1616
use thin_vec::ThinVec;
1717
use tracing::instrument;
@@ -93,7 +93,8 @@ impl<'a, 'hir> ItemLowerer<'a, 'hir> {
9393
debug_assert_eq!(self.resolver.node_id_to_def_id[&CRATE_NODE_ID], CRATE_DEF_ID);
9494
self.with_lctx(CRATE_NODE_ID, |lctx| {
9595
let module = lctx.lower_mod(&c.items, &c.spans);
96-
lctx.lower_attrs(hir::CRATE_HIR_ID, &c.attrs);
96+
// FIXME(jdonszelman): is dummy span ever a problem here?
97+
lctx.lower_attrs(hir::CRATE_HIR_ID, &c.attrs, DUMMY_SP);
9798
hir::OwnerNode::Crate(module)
9899
})
99100
}
@@ -157,7 +158,7 @@ impl<'hir> LoweringContext<'_, 'hir> {
157158
let mut ident = i.ident;
158159
let vis_span = self.lower_span(i.vis.span);
159160
let hir_id = hir::HirId::make_owner(self.current_hir_id_owner.def_id);
160-
let attrs = self.lower_attrs(hir_id, &i.attrs);
161+
let attrs = self.lower_attrs(hir_id, &i.attrs, i.span);
161162
let kind = self.lower_item_kind(i.span, i.id, hir_id, &mut ident, attrs, vis_span, &i.kind);
162163
let item = hir::Item {
163164
owner_id: hir_id.expect_owner(),
@@ -620,7 +621,7 @@ impl<'hir> LoweringContext<'_, 'hir> {
620621
fn lower_foreign_item(&mut self, i: &ForeignItem) -> &'hir hir::ForeignItem<'hir> {
621622
let hir_id = hir::HirId::make_owner(self.current_hir_id_owner.def_id);
622623
let owner_id = hir_id.expect_owner();
623-
let attrs = self.lower_attrs(hir_id, &i.attrs);
624+
let attrs = self.lower_attrs(hir_id, &i.attrs, i.span);
624625
let item = hir::ForeignItem {
625626
owner_id,
626627
ident: self.lower_ident(i.ident),
@@ -678,7 +679,7 @@ impl<'hir> LoweringContext<'_, 'hir> {
678679

679680
fn lower_variant(&mut self, v: &Variant) -> hir::Variant<'hir> {
680681
let hir_id = self.lower_node_id(v.id);
681-
self.lower_attrs(hir_id, &v.attrs);
682+
self.lower_attrs(hir_id, &v.attrs, v.span);
682683
hir::Variant {
683684
hir_id,
684685
def_id: self.local_def_id(v.id),
@@ -740,7 +741,7 @@ impl<'hir> LoweringContext<'_, 'hir> {
740741
) -> hir::FieldDef<'hir> {
741742
let ty = self.lower_ty(&f.ty, ImplTraitContext::Disallowed(ImplTraitPosition::FieldTy));
742743
let hir_id = self.lower_node_id(f.id);
743-
self.lower_attrs(hir_id, &f.attrs);
744+
self.lower_attrs(hir_id, &f.attrs, f.span);
744745
hir::FieldDef {
745746
span: self.lower_span(f.span),
746747
hir_id,
@@ -759,7 +760,7 @@ impl<'hir> LoweringContext<'_, 'hir> {
759760

760761
fn lower_trait_item(&mut self, i: &AssocItem) -> &'hir hir::TraitItem<'hir> {
761762
let hir_id = hir::HirId::make_owner(self.current_hir_id_owner.def_id);
762-
let attrs = self.lower_attrs(hir_id, &i.attrs);
763+
let attrs = self.lower_attrs(hir_id, &i.attrs, i.span);
763764
let trait_item_def_id = hir_id.expect_owner();
764765

765766
let (generics, kind, has_default) = match &i.kind {
@@ -895,7 +896,7 @@ impl<'hir> LoweringContext<'_, 'hir> {
895896
let has_value = true;
896897
let (defaultness, _) = self.lower_defaultness(i.kind.defaultness(), has_value);
897898
let hir_id = hir::HirId::make_owner(self.current_hir_id_owner.def_id);
898-
let attrs = self.lower_attrs(hir_id, &i.attrs);
899+
let attrs = self.lower_attrs(hir_id, &i.attrs, i.span);
899900

900901
let (generics, kind) = match &i.kind {
901902
AssocItemKind::Const(box ConstItem { generics, ty, expr, .. }) => self.lower_generics(
@@ -1056,7 +1057,7 @@ impl<'hir> LoweringContext<'_, 'hir> {
10561057

10571058
fn lower_param(&mut self, param: &Param) -> hir::Param<'hir> {
10581059
let hir_id = self.lower_node_id(param.id);
1059-
self.lower_attrs(hir_id, &param.attrs);
1060+
self.lower_attrs(hir_id, &param.attrs, param.span);
10601061
hir::Param {
10611062
hir_id,
10621063
pat: self.lower_pat(&param.pat),

0 commit comments

Comments
 (0)