Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

syntax: Remove Nt(Impl,Trait,Foreign)Item #69423

Merged
merged 1 commit into from
Feb 26, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 0 additions & 11 deletions src/librustc_ast_pretty/pprust.rs
Original file line number Diff line number Diff line change
Expand Up @@ -319,10 +319,7 @@ pub fn nonterminal_to_string(nt: &Nonterminal) -> String {
token::NtLifetime(e) => e.to_string(),
token::NtLiteral(ref e) => expr_to_string(e),
token::NtTT(ref tree) => tt_to_string(tree.clone()),
// FIXME(Centril): merge these variants.
token::NtImplItem(ref e) | token::NtTraitItem(ref e) => assoc_item_to_string(e),
token::NtVis(ref e) => vis_to_string(e),
token::NtForeignItem(ref e) => foreign_item_to_string(e),
}
}

Expand Down Expand Up @@ -358,10 +355,6 @@ pub fn item_to_string(i: &ast::Item) -> String {
to_string(|s| s.print_item(i))
}

fn assoc_item_to_string(i: &ast::AssocItem) -> String {
to_string(|s| s.print_assoc_item(i))
}

pub fn generic_params_to_string(generic_params: &[ast::GenericParam]) -> String {
to_string(|s| s.print_generic_params(generic_params))
}
Expand Down Expand Up @@ -404,10 +397,6 @@ pub fn param_to_string(arg: &ast::Param) -> String {
to_string(|s| s.print_param(arg, false))
}

fn foreign_item_to_string(arg: &ast::ForeignItem) -> String {
to_string(|s| s.print_foreign_item(arg))
}

fn visibility_qualified(vis: &ast::Visibility, s: &str) -> String {
format!("{}{}", to_string(|s| s.print_visibility(vis)), s)
}
Expand Down
11 changes: 8 additions & 3 deletions src/librustc_expand/expand.rs
Original file line number Diff line number Diff line change
Expand Up @@ -669,12 +669,17 @@ impl<'a, 'b> MacroExpander<'a, 'b> {
SyntaxExtensionKind::Attr(expander) => {
self.gate_proc_macro_input(&item);
self.gate_proc_macro_attr_item(span, &item);
// `Annotatable` can be converted into tokens directly, but we are packing it
// into a nonterminal as a piece of AST to make the produced token stream
// look nicer in pretty-printed form. This may be no longer necessary.
let item_tok = TokenTree::token(
token::Interpolated(Lrc::new(match item {
Annotatable::Item(item) => token::NtItem(item),
Annotatable::TraitItem(item) => token::NtTraitItem(item),
Annotatable::ImplItem(item) => token::NtImplItem(item),
Annotatable::ForeignItem(item) => token::NtForeignItem(item),
Annotatable::TraitItem(item)
| Annotatable::ImplItem(item)
| Annotatable::ForeignItem(item) => {
token::NtItem(P(item.and_then(ast::AssocItem::into_item)))
}
Annotatable::Stmt(stmt) => token::NtStmt(stmt.into_inner()),
Annotatable::Expr(expr) => token::NtExpr(expr),
Annotatable::Arm(..)
Expand Down
3 changes: 0 additions & 3 deletions src/librustc_parse/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -314,9 +314,6 @@ pub fn nt_to_tokenstream(nt: &Nonterminal, sess: &ParseSess, span: Span) -> Toke
Nonterminal::NtItem(ref item) => {
prepend_attrs(sess, &item.attrs, item.tokens.as_ref(), span)
}
Nonterminal::NtTraitItem(ref item) | Nonterminal::NtImplItem(ref item) => {
prepend_attrs(sess, &item.attrs, item.tokens.as_ref(), span)
}
Nonterminal::NtIdent(ident, is_raw) => {
Some(tokenstream::TokenTree::token(token::Ident(ident.name, is_raw), ident.span).into())
}
Expand Down
8 changes: 0 additions & 8 deletions src/librustc_parse/parser/item.rs
Original file line number Diff line number Diff line change
Expand Up @@ -632,16 +632,10 @@ impl<'a> Parser<'a> {
}

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

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

Expand Down Expand Up @@ -834,8 +828,6 @@ impl<'a> Parser<'a> {

/// Parses a foreign item (one in an `extern { ... }` block).
pub fn parse_foreign_item(&mut self) -> PResult<'a, Option<Option<P<ForeignItem>>>> {
maybe_whole!(self, NtForeignItem, |item| Some(Some(item)));

Ok(self.parse_item_(|_| true)?.map(|Item { attrs, id, span, vis, ident, kind, tokens }| {
let kind = match kind {
ItemKind::Mac(a) => ForeignItemKind::Macro(a),
Expand Down
23 changes: 23 additions & 0 deletions src/libsyntax/ast.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2441,6 +2441,13 @@ impl Item {
}
}

impl<K: IntoItemKind> Item<K> {
pub fn into_item(self) -> Item {
let Item { attrs, id, span, vis, ident, kind, tokens } = self;
Item { attrs, id, span, vis, ident, kind: kind.into_item_kind(), tokens }
}
}

/// `extern` qualifier on a function item or function type.
#[derive(Clone, Copy, RustcEncodable, RustcDecodable, Debug)]
pub enum Extern {
Expand Down Expand Up @@ -2617,6 +2624,10 @@ impl ItemKind {
}
}

pub trait IntoItemKind {
fn into_item_kind(self) -> ItemKind;
}

// FIXME(Centril): These definitions should be unmerged;
// see https://github.com/rust-lang/rust/pull/69194#discussion_r379899975
pub type ForeignItem = Item<AssocItemKind>;
Expand Down Expand Up @@ -2656,3 +2667,15 @@ impl AssocItemKind {
}
}
}

impl IntoItemKind for AssocItemKind {
fn into_item_kind(self) -> ItemKind {
match self {
AssocItemKind::Const(a, b, c) => ItemKind::Const(a, b, c),
AssocItemKind::Static(a, b, c) => ItemKind::Static(a, b, c),
AssocItemKind::Fn(a, b, c, d) => ItemKind::Fn(a, b, c, d),
AssocItemKind::TyAlias(a, b, c, d) => ItemKind::TyAlias(a, b, c, d),
AssocItemKind::Macro(a) => ItemKind::Mac(a),
}
}
}
13 changes: 0 additions & 13 deletions src/libsyntax/mut_visit.rs
Original file line number Diff line number Diff line change
Expand Up @@ -711,20 +711,7 @@ pub fn noop_visit_interpolated<T: MutVisitor>(nt: &mut token::Nonterminal, vis:
}
token::NtPath(path) => vis.visit_path(path),
token::NtTT(tt) => vis.visit_tt(tt),
token::NtImplItem(item) => visit_clobber(item, |item| {
// See reasoning above.
vis.flat_map_impl_item(item).expect_one("expected visitor to produce exactly one item")
}),
token::NtTraitItem(item) => visit_clobber(item, |item| {
// See reasoning above.
vis.flat_map_trait_item(item).expect_one("expected visitor to produce exactly one item")
}),
token::NtVis(visib) => vis.visit_vis(visib),
token::NtForeignItem(item) => visit_clobber(item, |item| {
// See reasoning above.
vis.flat_map_foreign_item(item)
.expect_one("expected visitor to produce exactly one item")
}),
}
}

Expand Down
9 changes: 0 additions & 9 deletions src/libsyntax/token.rs
Original file line number Diff line number Diff line change
Expand Up @@ -712,12 +712,6 @@ pub enum Nonterminal {
NtPath(ast::Path),
NtVis(ast::Visibility),
NtTT(TokenTree),
// Used only for passing items to proc macro attributes (they are not
// strictly necessary for that, `Annotatable` can be converted into
// tokens directly, but doing that naively regresses pretty-printing).
petrochenkov marked this conversation as resolved.
Show resolved Hide resolved
NtTraitItem(P<ast::AssocItem>),
NtImplItem(P<ast::AssocItem>),
NtForeignItem(P<ast::ForeignItem>),
}

// `Nonterminal` is used a lot. Make sure it doesn't unintentionally get bigger.
Expand Down Expand Up @@ -755,9 +749,6 @@ impl fmt::Debug for Nonterminal {
NtMeta(..) => f.pad("NtMeta(..)"),
NtPath(..) => f.pad("NtPath(..)"),
NtTT(..) => f.pad("NtTT(..)"),
NtImplItem(..) => f.pad("NtImplItem(..)"),
NtTraitItem(..) => f.pad("NtTraitItem(..)"),
NtForeignItem(..) => f.pad("NtForeignItem(..)"),
NtVis(..) => f.pad("NtVis(..)"),
NtLifetime(..) => f.pad("NtLifetime(..)"),
}
Expand Down
14 changes: 14 additions & 0 deletions src/test/ui/proc-macro/trait-fn-args-2015.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
// Unnamed arguments in trait functions can be passed through proc macros on 2015 edition.

// check-pass
// aux-build:test-macros.rs

#[macro_use]
extern crate test_macros;

trait Tr {
#[identity_attr]
fn method(u8);
}

fn main() {}