Skip to content

Commit 33d3d55

Browse files
committed
Introduce new parsing infrastructure and types for parsed attributes
1 parent 55ee36b commit 33d3d55

File tree

30 files changed

+1413
-283
lines changed

30 files changed

+1413
-283
lines changed

Cargo.lock

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3376,6 +3376,7 @@ dependencies = [
33763376
"rustc_abi",
33773377
"rustc_ast",
33783378
"rustc_ast_pretty",
3379+
"rustc_attr_parsing",
33793380
"rustc_data_structures",
33803381
"rustc_errors",
33813382
"rustc_feature",
@@ -3384,6 +3385,7 @@ dependencies = [
33843385
"rustc_index",
33853386
"rustc_macros",
33863387
"rustc_middle",
3388+
"rustc_parse",
33873389
"rustc_session",
33883390
"rustc_span",
33893391
"rustc_target",
@@ -3431,16 +3433,11 @@ version = "0.0.0"
34313433
dependencies = [
34323434
"rustc_abi",
34333435
"rustc_ast",
3434-
"rustc_ast_pretty",
34353436
"rustc_data_structures",
3436-
"rustc_errors",
3437-
"rustc_feature",
3438-
"rustc_fluent_macro",
3439-
"rustc_lexer",
34403437
"rustc_macros",
34413438
"rustc_serialize",
3442-
"rustc_session",
34433439
"rustc_span",
3440+
"thin-vec",
34443441
]
34453442

34463443
[[package]]
@@ -3455,11 +3452,13 @@ dependencies = [
34553452
"rustc_errors",
34563453
"rustc_feature",
34573454
"rustc_fluent_macro",
3455+
"rustc_hir",
34583456
"rustc_lexer",
34593457
"rustc_macros",
34603458
"rustc_serialize",
34613459
"rustc_session",
34623460
"rustc_span",
3461+
"thin-vec",
34633462
]
34643463

34653464
[[package]]
@@ -3849,6 +3848,7 @@ dependencies = [
38493848
"rustc_abi",
38503849
"rustc_arena",
38513850
"rustc_ast",
3851+
"rustc_attr_data_structures",
38523852
"rustc_data_structures",
38533853
"rustc_index",
38543854
"rustc_macros",
@@ -3895,6 +3895,7 @@ dependencies = [
38953895
"rustc_abi",
38963896
"rustc_ast",
38973897
"rustc_ast_pretty",
3898+
"rustc_attr_parsing",
38983899
"rustc_hir",
38993900
"rustc_span",
39003901
]
@@ -4404,6 +4405,7 @@ name = "rustc_query_impl"
44044405
version = "0.0.0"
44054406
dependencies = [
44064407
"measureme",
4408+
"rustc_attr_data_structures",
44074409
"rustc_data_structures",
44084410
"rustc_errors",
44094411
"rustc_hir",
@@ -4424,6 +4426,7 @@ dependencies = [
44244426
"parking_lot",
44254427
"rustc-rayon-core",
44264428
"rustc_ast",
4429+
"rustc_attr_data_structures",
44274430
"rustc_data_structures",
44284431
"rustc_errors",
44294432
"rustc_feature",

compiler/rustc_ast_lowering/Cargo.toml

Lines changed: 2 additions & 0 deletions
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

Lines changed: 1 addition & 1 deletion
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

Lines changed: 9 additions & 9 deletions
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

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,8 @@ 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};
15+
use rustc_target::spec::abi;
1516
use smallvec::{SmallVec, smallvec};
1617
use thin_vec::ThinVec;
1718
use tracing::instrument;
@@ -93,7 +94,8 @@ impl<'a, 'hir> ItemLowerer<'a, 'hir> {
9394
debug_assert_eq!(self.resolver.node_id_to_def_id[&CRATE_NODE_ID], CRATE_DEF_ID);
9495
self.with_lctx(CRATE_NODE_ID, |lctx| {
9596
let module = lctx.lower_mod(&c.items, &c.spans);
96-
lctx.lower_attrs(hir::CRATE_HIR_ID, &c.attrs);
97+
// FIXME(jdonszelman): is dummy span ever a problem here?
98+
lctx.lower_attrs(hir::CRATE_HIR_ID, &c.attrs, DUMMY_SP);
9799
hir::OwnerNode::Crate(module)
98100
})
99101
}
@@ -157,7 +159,7 @@ impl<'hir> LoweringContext<'_, 'hir> {
157159
let mut ident = i.ident;
158160
let vis_span = self.lower_span(i.vis.span);
159161
let hir_id = hir::HirId::make_owner(self.current_hir_id_owner.def_id);
160-
let attrs = self.lower_attrs(hir_id, &i.attrs);
162+
let attrs = self.lower_attrs(hir_id, &i.attrs, i.span);
161163
let kind = self.lower_item_kind(i.span, i.id, hir_id, &mut ident, attrs, vis_span, &i.kind);
162164
let item = hir::Item {
163165
owner_id: hir_id.expect_owner(),
@@ -615,7 +617,7 @@ impl<'hir> LoweringContext<'_, 'hir> {
615617
fn lower_foreign_item(&mut self, i: &ForeignItem) -> &'hir hir::ForeignItem<'hir> {
616618
let hir_id = hir::HirId::make_owner(self.current_hir_id_owner.def_id);
617619
let owner_id = hir_id.expect_owner();
618-
let attrs = self.lower_attrs(hir_id, &i.attrs);
620+
let attrs = self.lower_attrs(hir_id, &i.attrs, i.span);
619621
let item = hir::ForeignItem {
620622
owner_id,
621623
ident: self.lower_ident(i.ident),
@@ -673,7 +675,7 @@ impl<'hir> LoweringContext<'_, 'hir> {
673675

674676
fn lower_variant(&mut self, v: &Variant) -> hir::Variant<'hir> {
675677
let hir_id = self.lower_node_id(v.id);
676-
self.lower_attrs(hir_id, &v.attrs);
678+
self.lower_attrs(hir_id, &v.attrs, v.span);
677679
hir::Variant {
678680
hir_id,
679681
def_id: self.local_def_id(v.id),
@@ -735,7 +737,7 @@ impl<'hir> LoweringContext<'_, 'hir> {
735737
) -> hir::FieldDef<'hir> {
736738
let ty = self.lower_ty(&f.ty, ImplTraitContext::Disallowed(ImplTraitPosition::FieldTy));
737739
let hir_id = self.lower_node_id(f.id);
738-
self.lower_attrs(hir_id, &f.attrs);
740+
self.lower_attrs(hir_id, &f.attrs, f.span);
739741
hir::FieldDef {
740742
span: self.lower_span(f.span),
741743
hir_id,
@@ -754,7 +756,7 @@ impl<'hir> LoweringContext<'_, 'hir> {
754756

755757
fn lower_trait_item(&mut self, i: &AssocItem) -> &'hir hir::TraitItem<'hir> {
756758
let hir_id = hir::HirId::make_owner(self.current_hir_id_owner.def_id);
757-
let attrs = self.lower_attrs(hir_id, &i.attrs);
759+
let attrs = self.lower_attrs(hir_id, &i.attrs, i.span);
758760
let trait_item_def_id = hir_id.expect_owner();
759761

760762
let (generics, kind, has_default) = match &i.kind {
@@ -890,7 +892,7 @@ impl<'hir> LoweringContext<'_, 'hir> {
890892
let has_value = true;
891893
let (defaultness, _) = self.lower_defaultness(i.kind.defaultness(), has_value);
892894
let hir_id = hir::HirId::make_owner(self.current_hir_id_owner.def_id);
893-
let attrs = self.lower_attrs(hir_id, &i.attrs);
895+
let attrs = self.lower_attrs(hir_id, &i.attrs, i.span);
894896

895897
let (generics, kind) = match &i.kind {
896898
AssocItemKind::Const(box ConstItem { generics, ty, expr, .. }) => self.lower_generics(
@@ -1051,7 +1053,7 @@ impl<'hir> LoweringContext<'_, 'hir> {
10511053

10521054
fn lower_param(&mut self, param: &Param) -> hir::Param<'hir> {
10531055
let hir_id = self.lower_node_id(param.id);
1054-
self.lower_attrs(hir_id, &param.attrs);
1056+
self.lower_attrs(hir_id, &param.attrs, param.span);
10551057
hir::Param {
10561058
hir_id,
10571059
pat: self.lower_pat(&param.pat),

0 commit comments

Comments
 (0)