Skip to content

Commit c202603

Browse files
committed
parser: remove Option<Vec<Attribute>> in ItemInfo.
1 parent 20ba687 commit c202603

File tree

2 files changed

+50
-59
lines changed

2 files changed

+50
-59
lines changed

src/librustc_parse/parser/item.rs

Lines changed: 49 additions & 58 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ use syntax::tokenstream::{DelimSpan, TokenStream, TokenTree};
2222
use log::debug;
2323
use std::mem;
2424

25-
pub(super) type ItemInfo = (Ident, ItemKind, Option<Vec<Attribute>>);
25+
pub(super) type ItemInfo = (Ident, ItemKind);
2626

2727
impl<'a> Parser<'a> {
2828
pub fn parse_item(&mut self) -> PResult<'a, Option<P<Item>>> {
@@ -85,8 +85,8 @@ impl<'a> Parser<'a> {
8585
let lo = self.token.span;
8686
let vis = self.parse_visibility(FollowedByType::No)?;
8787

88-
if let Some(info) = self.parse_item_kind(&mut attrs, macros_allowed, lo, &vis)? {
89-
return Ok(Some(self.mk_item_with_info(attrs, lo, vis, info)));
88+
if let Some((ident, kind)) = self.parse_item_kind(&mut attrs, macros_allowed, lo, &vis)? {
89+
return Ok(Some(self.mk_item(lo.to(self.prev_span), ident, kind, vis, attrs)));
9090
}
9191

9292
// FAILURE TO PARSE ITEM
@@ -118,18 +118,18 @@ impl<'a> Parser<'a> {
118118
// USE ITEM
119119
let tree = self.parse_use_tree()?;
120120
self.expect_semi()?;
121-
(Ident::invalid(), ItemKind::Use(P(tree)), None)
121+
(Ident::invalid(), ItemKind::Use(P(tree)))
122122
} else if self.check_fn_front_matter() {
123123
// FUNCTION ITEM
124124
let (ident, sig, generics, body) = self.parse_fn(&mut false, attrs, |_| true)?;
125-
(ident, ItemKind::Fn(sig, generics, body), None)
125+
(ident, ItemKind::Fn(sig, generics, body))
126126
} else if self.eat_keyword(kw::Extern) {
127127
if self.eat_keyword(kw::Crate) {
128128
// EXTERN CRATE
129129
self.parse_item_extern_crate()?
130130
} else {
131131
// EXTERN BLOCK
132-
self.parse_item_foreign_mod()?
132+
self.parse_item_foreign_mod(attrs)?
133133
}
134134
} else if self.is_static_global() {
135135
// STATIC ITEM
@@ -156,7 +156,7 @@ impl<'a> Parser<'a> {
156156
{
157157
// UNSAFE TRAIT ITEM
158158
let unsafety = self.parse_unsafety();
159-
self.parse_item_trait(lo, unsafety)?
159+
self.parse_item_trait(attrs, lo, unsafety)?
160160
} else if self.check_keyword(kw::Impl)
161161
|| self.check_keyword(kw::Unsafe) && self.is_keyword_ahead(1, &[kw::Impl])
162162
|| self.check_keyword(kw::Default) && self.is_keyword_ahead(1, &[kw::Impl, kw::Unsafe])
@@ -165,22 +165,22 @@ impl<'a> Parser<'a> {
165165
let defaultness = self.parse_defaultness();
166166
let unsafety = self.parse_unsafety();
167167
self.expect_keyword(kw::Impl)?;
168-
self.parse_item_impl(unsafety, defaultness)?
168+
self.parse_item_impl(attrs, unsafety, defaultness)?
169169
} else if self.eat_keyword(kw::Mod) {
170170
// MODULE ITEM
171171
self.parse_item_mod(attrs)?
172172
} else if self.eat_keyword(kw::Type) {
173173
// TYPE ITEM
174174
let (ident, ty, generics) = self.parse_type_alias()?;
175-
(ident, ItemKind::TyAlias(ty, generics), None)
175+
(ident, ItemKind::TyAlias(ty, generics))
176176
} else if self.eat_keyword(kw::Enum) {
177177
// ENUM ITEM
178178
self.parse_item_enum()?
179179
} else if self.check_keyword(kw::Trait)
180180
|| (self.check_keyword(kw::Auto) && self.is_keyword_ahead(1, &[kw::Trait]))
181181
{
182182
// TRAIT ITEM
183-
self.parse_item_trait(lo, Unsafe::No)?
183+
self.parse_item_trait(attrs, lo, Unsafe::No)?
184184
} else if self.eat_keyword(kw::Struct) {
185185
// STRUCT ITEM
186186
self.parse_item_struct()?
@@ -306,26 +306,6 @@ impl<'a> Parser<'a> {
306306
}
307307
}
308308

309-
pub(super) fn mk_item_with_info(
310-
&self,
311-
attrs: Vec<Attribute>,
312-
lo: Span,
313-
vis: Visibility,
314-
info: ItemInfo,
315-
) -> P<Item> {
316-
let (ident, item, extra_attrs) = info;
317-
let span = lo.to(self.prev_span);
318-
let attrs = Self::maybe_append(attrs, extra_attrs);
319-
self.mk_item(span, ident, item, vis, attrs)
320-
}
321-
322-
fn maybe_append<T>(mut lhs: Vec<T>, mut rhs: Option<Vec<T>>) -> Vec<T> {
323-
if let Some(ref mut rhs) = rhs {
324-
lhs.append(rhs);
325-
}
326-
lhs
327-
}
328-
329309
/// Parses an item macro, e.g., `item!();`.
330310
fn parse_item_macro(&mut self, vis: &Visibility) -> PResult<'a, ItemInfo> {
331311
self.complain_if_pub_macro(&vis.node, vis.span);
@@ -339,7 +319,7 @@ impl<'a> Parser<'a> {
339319
}
340320

341321
let mac = Mac { path, args, prior_type_ascription: self.last_type_ascription };
342-
Ok((Ident::invalid(), ItemKind::Mac(mac), None))
322+
Ok((Ident::invalid(), ItemKind::Mac(mac)))
343323
}
344324

345325
/// Emits an expected-item-after-attributes error.
@@ -428,16 +408,21 @@ impl<'a> Parser<'a> {
428408

429409
/// Parses an implementation item, `impl` keyword is already parsed.
430410
///
431-
/// impl<'a, T> TYPE { /* impl items */ }
432-
/// impl<'a, T> TRAIT for TYPE { /* impl items */ }
433-
/// impl<'a, T> !TRAIT for TYPE { /* impl items */ }
434-
/// impl<'a, T> const TRAIT for TYPE { /* impl items */ }
411+
/// ```
412+
/// impl<'a, T> TYPE { /* impl items */ }
413+
/// impl<'a, T> TRAIT for TYPE { /* impl items */ }
414+
/// impl<'a, T> !TRAIT for TYPE { /* impl items */ }
415+
/// impl<'a, T> const TRAIT for TYPE { /* impl items */ }
416+
/// ```
435417
///
436418
/// We actually parse slightly more relaxed grammar for better error reporting and recovery.
437-
/// `impl` GENERICS `const`? `!`? TYPE `for`? (TYPE | `..`) (`where` PREDICATES)? `{` BODY `}`
438-
/// `impl` GENERICS `const`? `!`? TYPE (`where` PREDICATES)? `{` BODY `}`
419+
/// ```
420+
/// "impl" GENERICS "const"? "!"? TYPE "for"? (TYPE | "..") ("where" PREDICATES)? "{" BODY "}"
421+
/// "impl" GENERICS "const"? "!"? TYPE ("where" PREDICATES)? "{" BODY "}"
422+
/// ```
439423
fn parse_item_impl(
440424
&mut self,
425+
attrs: &mut Vec<Attribute>,
441426
unsafety: Unsafe,
442427
defaultness: Defaultness,
443428
) -> PResult<'a, ItemInfo> {
@@ -492,7 +477,7 @@ impl<'a> Parser<'a> {
492477

493478
generics.where_clause = self.parse_where_clause()?;
494479

495-
let (impl_items, attrs) = self.parse_item_list(|p, at_end| p.parse_impl_item(at_end))?;
480+
let impl_items = self.parse_item_list(attrs, |p, at_end| p.parse_impl_item(at_end))?;
496481

497482
let item_kind = match ty_second {
498483
Some(ty_second) => {
@@ -545,15 +530,16 @@ impl<'a> Parser<'a> {
545530
}
546531
};
547532

548-
Ok((Ident::invalid(), item_kind, Some(attrs)))
533+
Ok((Ident::invalid(), item_kind))
549534
}
550535

551536
fn parse_item_list<T>(
552537
&mut self,
538+
attrs: &mut Vec<Attribute>,
553539
mut parse_item: impl FnMut(&mut Parser<'a>, &mut bool) -> PResult<'a, T>,
554-
) -> PResult<'a, (Vec<T>, Vec<Attribute>)> {
540+
) -> PResult<'a, Vec<T>> {
555541
self.expect(&token::OpenDelim(token::Brace))?;
556-
let attrs = self.parse_inner_attributes()?;
542+
attrs.append(&mut self.parse_inner_attributes()?);
557543

558544
let mut items = Vec::new();
559545
while !self.eat(&token::CloseDelim(token::Brace)) {
@@ -572,7 +558,7 @@ impl<'a> Parser<'a> {
572558
}
573559
}
574560
}
575-
Ok((items, attrs))
561+
Ok(items)
576562
}
577563

578564
/// Recover on a doc comment before `}`.
@@ -624,7 +610,12 @@ impl<'a> Parser<'a> {
624610
}
625611

626612
/// Parses `auto? trait Foo { ... }` or `trait Foo = Bar;`.
627-
fn parse_item_trait(&mut self, lo: Span, unsafety: Unsafe) -> PResult<'a, ItemInfo> {
613+
fn parse_item_trait(
614+
&mut self,
615+
attrs: &mut Vec<Attribute>,
616+
lo: Span,
617+
unsafety: Unsafe,
618+
) -> PResult<'a, ItemInfo> {
628619
// Parse optional `auto` prefix.
629620
let is_auto = if self.eat_keyword(kw::Auto) { IsAuto::Yes } else { IsAuto::No };
630621

@@ -662,12 +653,12 @@ impl<'a> Parser<'a> {
662653

663654
self.sess.gated_spans.gate(sym::trait_alias, whole_span);
664655

665-
Ok((ident, ItemKind::TraitAlias(tps, bounds), None))
656+
Ok((ident, ItemKind::TraitAlias(tps, bounds)))
666657
} else {
667658
// It's a normal trait.
668659
tps.where_clause = self.parse_where_clause()?;
669-
let (items, attrs) = self.parse_item_list(|p, at_end| p.parse_trait_item(at_end))?;
670-
Ok((ident, ItemKind::Trait(is_auto, unsafety, tps, bounds, items), Some(attrs)))
660+
let items = self.parse_item_list(attrs, |p, at_end| p.parse_trait_item(at_end))?;
661+
Ok((ident, ItemKind::Trait(is_auto, unsafety, tps, bounds, items)))
671662
}
672663
}
673664

@@ -854,7 +845,7 @@ impl<'a> Parser<'a> {
854845
(orig_name, None)
855846
};
856847
self.expect_semi()?;
857-
Ok((item_name, ItemKind::ExternCrate(orig_name), None))
848+
Ok((item_name, ItemKind::ExternCrate(orig_name)))
858849
}
859850

860851
fn parse_crate_name_with_dashes(&mut self) -> PResult<'a, ast::Ident> {
@@ -905,11 +896,11 @@ impl<'a> Parser<'a> {
905896
/// extern "C" {}
906897
/// extern {}
907898
/// ```
908-
fn parse_item_foreign_mod(&mut self) -> PResult<'a, ItemInfo> {
899+
fn parse_item_foreign_mod(&mut self, attrs: &mut Vec<Attribute>) -> PResult<'a, ItemInfo> {
909900
let abi = self.parse_abi(); // ABI?
910-
let (items, attrs) = self.parse_item_list(|p, at_end| p.parse_foreign_item(at_end))?;
901+
let items = self.parse_item_list(attrs, |p, at_end| p.parse_foreign_item(at_end))?;
911902
let module = ast::ForeignMod { abi, items };
912-
Ok((Ident::invalid(), ItemKind::ForeignMod(module), Some(attrs)))
903+
Ok((Ident::invalid(), ItemKind::ForeignMod(module)))
913904
}
914905

915906
/// Parses a foreign item (one in an `extern { ... }` block).
@@ -1016,7 +1007,7 @@ impl<'a> Parser<'a> {
10161007
Some(m) => ItemKind::Static(ty, m, e),
10171008
None => ItemKind::Const(ty, e),
10181009
};
1019-
Ok((id, item, None))
1010+
Ok((id, item))
10201011
}
10211012

10221013
/// We were supposed to parse `:` but instead, we're already at `=`.
@@ -1069,7 +1060,7 @@ impl<'a> Parser<'a> {
10691060

10701061
let enum_definition =
10711062
EnumDef { variants: variants.into_iter().filter_map(|v| v).collect() };
1072-
Ok((id, ItemKind::Enum(enum_definition, generics), None))
1063+
Ok((id, ItemKind::Enum(enum_definition, generics)))
10731064
}
10741065

10751066
fn parse_enum_variant(&mut self) -> PResult<'a, Option<Variant>> {
@@ -1163,7 +1154,7 @@ impl<'a> Parser<'a> {
11631154
return Err(err);
11641155
};
11651156

1166-
Ok((class_name, ItemKind::Struct(vdata, generics), None))
1157+
Ok((class_name, ItemKind::Struct(vdata, generics)))
11671158
}
11681159

11691160
/// Parses `union Foo { ... }`.
@@ -1187,7 +1178,7 @@ impl<'a> Parser<'a> {
11871178
return Err(err);
11881179
};
11891180

1190-
Ok((class_name, ItemKind::Union(vdata, generics), None))
1181+
Ok((class_name, ItemKind::Union(vdata, generics)))
11911182
}
11921183

11931184
pub(super) fn is_union_item(&self) -> bool {
@@ -1369,7 +1360,7 @@ impl<'a> Parser<'a> {
13691360
};
13701361

13711362
self.sess.gated_spans.gate(sym::decl_macro, lo.to(self.prev_span));
1372-
Ok((ident, ItemKind::MacroDef(ast::MacroDef { body, legacy: false }), None))
1363+
Ok((ident, ItemKind::MacroDef(ast::MacroDef { body, legacy: false })))
13731364
}
13741365

13751366
/// Is this unambiguously the start of a `macro_rules! foo` item defnition?
@@ -1391,7 +1382,7 @@ impl<'a> Parser<'a> {
13911382
self.report_invalid_macro_expansion_item();
13921383
}
13931384

1394-
Ok((ident, ItemKind::MacroDef(ast::MacroDef { body, legacy: true }), None))
1385+
Ok((ident, ItemKind::MacroDef(ast::MacroDef { body, legacy: true })))
13951386
}
13961387

13971388
pub(super) fn eat_macro_def(
@@ -1400,14 +1391,14 @@ impl<'a> Parser<'a> {
14001391
vis: &Visibility,
14011392
lo: Span,
14021393
) -> PResult<'a, Option<P<Item>>> {
1403-
let info = if self.eat_keyword(kw::Macro) {
1394+
let (ident, kind) = if self.eat_keyword(kw::Macro) {
14041395
self.parse_item_decl_macro(lo)?
14051396
} else if self.is_macro_rules_item() {
14061397
self.parse_item_macro_rules(vis)?
14071398
} else {
14081399
return Ok(None);
14091400
};
1410-
Ok(Some(self.mk_item_with_info(attrs.to_vec(), lo, vis.clone(), info)))
1401+
Ok(Some(self.mk_item(lo.to(self.prev_span), ident, kind, vis.clone(), attrs.to_vec())))
14111402
}
14121403

14131404
fn complain_if_pub_macro(&self, vis: &VisibilityKind, sp: Span) {

src/librustc_parse/parser/module.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,7 @@ impl<'a> Parser<'a> {
6767
(module, inner_attrs)
6868
};
6969
attrs.append(&mut inner_attrs);
70-
Ok((id, ItemKind::Mod(module), None))
70+
Ok((id, ItemKind::Mod(module)))
7171
}
7272

7373
/// Given a termination token, parses all of the items in a module.

0 commit comments

Comments
 (0)