Skip to content

Commit 7603c2c

Browse files
authored
Rollup merge of #69423 - petrochenkov:nont, r=Centril
syntax: Remove `Nt(Impl,Trait,Foreign)Item` Follow-up to #69366. r? @Centril
2 parents d050b00 + d134385 commit 7603c2c

File tree

8 files changed

+45
-47
lines changed

8 files changed

+45
-47
lines changed

src/librustc_ast_pretty/pprust.rs

-11
Original file line numberDiff line numberDiff line change
@@ -285,10 +285,7 @@ pub fn nonterminal_to_string(nt: &Nonterminal) -> String {
285285
token::NtLifetime(e) => e.to_string(),
286286
token::NtLiteral(ref e) => expr_to_string(e),
287287
token::NtTT(ref tree) => tt_to_string(tree.clone()),
288-
// FIXME(Centril): merge these variants.
289-
token::NtImplItem(ref e) | token::NtTraitItem(ref e) => assoc_item_to_string(e),
290288
token::NtVis(ref e) => vis_to_string(e),
291-
token::NtForeignItem(ref e) => foreign_item_to_string(e),
292289
}
293290
}
294291

@@ -324,10 +321,6 @@ pub fn item_to_string(i: &ast::Item) -> String {
324321
to_string(|s| s.print_item(i))
325322
}
326323

327-
fn assoc_item_to_string(i: &ast::AssocItem) -> String {
328-
to_string(|s| s.print_assoc_item(i))
329-
}
330-
331324
pub fn generic_params_to_string(generic_params: &[ast::GenericParam]) -> String {
332325
to_string(|s| s.print_generic_params(generic_params))
333326
}
@@ -370,10 +363,6 @@ pub fn param_to_string(arg: &ast::Param) -> String {
370363
to_string(|s| s.print_param(arg, false))
371364
}
372365

373-
fn foreign_item_to_string(arg: &ast::ForeignItem) -> String {
374-
to_string(|s| s.print_foreign_item(arg))
375-
}
376-
377366
fn visibility_qualified(vis: &ast::Visibility, s: &str) -> String {
378367
format!("{}{}", to_string(|s| s.print_visibility(vis)), s)
379368
}

src/librustc_expand/expand.rs

+8-3
Original file line numberDiff line numberDiff line change
@@ -669,12 +669,17 @@ impl<'a, 'b> MacroExpander<'a, 'b> {
669669
SyntaxExtensionKind::Attr(expander) => {
670670
self.gate_proc_macro_input(&item);
671671
self.gate_proc_macro_attr_item(span, &item);
672+
// `Annotatable` can be converted into tokens directly, but we are packing it
673+
// into a nonterminal as a piece of AST to make the produced token stream
674+
// look nicer in pretty-printed form. This may be no longer necessary.
672675
let item_tok = TokenTree::token(
673676
token::Interpolated(Lrc::new(match item {
674677
Annotatable::Item(item) => token::NtItem(item),
675-
Annotatable::TraitItem(item) => token::NtTraitItem(item),
676-
Annotatable::ImplItem(item) => token::NtImplItem(item),
677-
Annotatable::ForeignItem(item) => token::NtForeignItem(item),
678+
Annotatable::TraitItem(item)
679+
| Annotatable::ImplItem(item)
680+
| Annotatable::ForeignItem(item) => {
681+
token::NtItem(P(item.and_then(ast::AssocItem::into_item)))
682+
}
678683
Annotatable::Stmt(stmt) => token::NtStmt(stmt.into_inner()),
679684
Annotatable::Expr(expr) => token::NtExpr(expr),
680685
Annotatable::Arm(..)

src/librustc_parse/lib.rs

-3
Original file line numberDiff line numberDiff line change
@@ -314,9 +314,6 @@ pub fn nt_to_tokenstream(nt: &Nonterminal, sess: &ParseSess, span: Span) -> Toke
314314
Nonterminal::NtItem(ref item) => {
315315
prepend_attrs(sess, &item.attrs, item.tokens.as_ref(), span)
316316
}
317-
Nonterminal::NtTraitItem(ref item) | Nonterminal::NtImplItem(ref item) => {
318-
prepend_attrs(sess, &item.attrs, item.tokens.as_ref(), span)
319-
}
320317
Nonterminal::NtIdent(ident, is_raw) => {
321318
Some(tokenstream::TokenTree::token(token::Ident(ident.name, is_raw), ident.span).into())
322319
}

src/librustc_parse/parser/item.rs

-8
Original file line numberDiff line numberDiff line change
@@ -632,16 +632,10 @@ impl<'a> Parser<'a> {
632632
}
633633

634634
pub fn parse_impl_item(&mut self) -> PResult<'a, Option<Option<P<AssocItem>>>> {
635-
maybe_whole!(self, NtImplItem, |x| Some(Some(x)));
636635
self.parse_assoc_item(|_| true)
637636
}
638637

639638
pub fn parse_trait_item(&mut self) -> PResult<'a, Option<Option<P<AssocItem>>>> {
640-
maybe_whole!(self, NtTraitItem, |x| Some(Some(x)));
641-
// This is somewhat dubious; We don't want to allow
642-
// param names to be left off if there is a definition...
643-
//
644-
// We don't allow param names to be left off in edition 2018.
645639
self.parse_assoc_item(|t| t.span.rust_2018())
646640
}
647641

@@ -834,8 +828,6 @@ impl<'a> Parser<'a> {
834828

835829
/// Parses a foreign item (one in an `extern { ... }` block).
836830
pub fn parse_foreign_item(&mut self) -> PResult<'a, Option<Option<P<ForeignItem>>>> {
837-
maybe_whole!(self, NtForeignItem, |item| Some(Some(item)));
838-
839831
Ok(self.parse_item_(|_| true)?.map(|Item { attrs, id, span, vis, ident, kind, tokens }| {
840832
let kind = match kind {
841833
ItemKind::Mac(a) => ForeignItemKind::Macro(a),

src/libsyntax/ast.rs

+23
Original file line numberDiff line numberDiff line change
@@ -2441,6 +2441,13 @@ impl Item {
24412441
}
24422442
}
24432443

2444+
impl<K: IntoItemKind> Item<K> {
2445+
pub fn into_item(self) -> Item {
2446+
let Item { attrs, id, span, vis, ident, kind, tokens } = self;
2447+
Item { attrs, id, span, vis, ident, kind: kind.into_item_kind(), tokens }
2448+
}
2449+
}
2450+
24442451
/// `extern` qualifier on a function item or function type.
24452452
#[derive(Clone, Copy, RustcEncodable, RustcDecodable, Debug)]
24462453
pub enum Extern {
@@ -2617,6 +2624,10 @@ impl ItemKind {
26172624
}
26182625
}
26192626

2627+
pub trait IntoItemKind {
2628+
fn into_item_kind(self) -> ItemKind;
2629+
}
2630+
26202631
// FIXME(Centril): These definitions should be unmerged;
26212632
// see https://github.com/rust-lang/rust/pull/69194#discussion_r379899975
26222633
pub type ForeignItem = Item<AssocItemKind>;
@@ -2656,3 +2667,15 @@ impl AssocItemKind {
26562667
}
26572668
}
26582669
}
2670+
2671+
impl IntoItemKind for AssocItemKind {
2672+
fn into_item_kind(self) -> ItemKind {
2673+
match self {
2674+
AssocItemKind::Const(a, b, c) => ItemKind::Const(a, b, c),
2675+
AssocItemKind::Static(a, b, c) => ItemKind::Static(a, b, c),
2676+
AssocItemKind::Fn(a, b, c, d) => ItemKind::Fn(a, b, c, d),
2677+
AssocItemKind::TyAlias(a, b, c, d) => ItemKind::TyAlias(a, b, c, d),
2678+
AssocItemKind::Macro(a) => ItemKind::Mac(a),
2679+
}
2680+
}
2681+
}

src/libsyntax/mut_visit.rs

-13
Original file line numberDiff line numberDiff line change
@@ -711,20 +711,7 @@ pub fn noop_visit_interpolated<T: MutVisitor>(nt: &mut token::Nonterminal, vis:
711711
}
712712
token::NtPath(path) => vis.visit_path(path),
713713
token::NtTT(tt) => vis.visit_tt(tt),
714-
token::NtImplItem(item) => visit_clobber(item, |item| {
715-
// See reasoning above.
716-
vis.flat_map_impl_item(item).expect_one("expected visitor to produce exactly one item")
717-
}),
718-
token::NtTraitItem(item) => visit_clobber(item, |item| {
719-
// See reasoning above.
720-
vis.flat_map_trait_item(item).expect_one("expected visitor to produce exactly one item")
721-
}),
722714
token::NtVis(visib) => vis.visit_vis(visib),
723-
token::NtForeignItem(item) => visit_clobber(item, |item| {
724-
// See reasoning above.
725-
vis.flat_map_foreign_item(item)
726-
.expect_one("expected visitor to produce exactly one item")
727-
}),
728715
}
729716
}
730717

src/libsyntax/token.rs

-9
Original file line numberDiff line numberDiff line change
@@ -712,12 +712,6 @@ pub enum Nonterminal {
712712
NtPath(ast::Path),
713713
NtVis(ast::Visibility),
714714
NtTT(TokenTree),
715-
// Used only for passing items to proc macro attributes (they are not
716-
// strictly necessary for that, `Annotatable` can be converted into
717-
// tokens directly, but doing that naively regresses pretty-printing).
718-
NtTraitItem(P<ast::AssocItem>),
719-
NtImplItem(P<ast::AssocItem>),
720-
NtForeignItem(P<ast::ForeignItem>),
721715
}
722716

723717
// `Nonterminal` is used a lot. Make sure it doesn't unintentionally get bigger.
@@ -755,9 +749,6 @@ impl fmt::Debug for Nonterminal {
755749
NtMeta(..) => f.pad("NtMeta(..)"),
756750
NtPath(..) => f.pad("NtPath(..)"),
757751
NtTT(..) => f.pad("NtTT(..)"),
758-
NtImplItem(..) => f.pad("NtImplItem(..)"),
759-
NtTraitItem(..) => f.pad("NtTraitItem(..)"),
760-
NtForeignItem(..) => f.pad("NtForeignItem(..)"),
761752
NtVis(..) => f.pad("NtVis(..)"),
762753
NtLifetime(..) => f.pad("NtLifetime(..)"),
763754
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
// Unnamed arguments in trait functions can be passed through proc macros on 2015 edition.
2+
3+
// check-pass
4+
// aux-build:test-macros.rs
5+
6+
#[macro_use]
7+
extern crate test_macros;
8+
9+
trait Tr {
10+
#[identity_attr]
11+
fn method(u8);
12+
}
13+
14+
fn main() {}

0 commit comments

Comments
 (0)