Skip to content

Commit 2d0c5b4

Browse files
committed
rustc_expand: Factor out Annotatable::into_tokens to a separate method
1 parent 865b44a commit 2d0c5b4

File tree

2 files changed

+32
-32
lines changed

2 files changed

+32
-32
lines changed

src/librustc_expand/base.rs

+26-1
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ use rustc_ast::ast::{self, Attribute, Name, NodeId, PatKind};
44
use rustc_ast::mut_visit::{self, MutVisitor};
55
use rustc_ast::ptr::P;
66
use rustc_ast::token;
7-
use rustc_ast::tokenstream::{self, TokenStream};
7+
use rustc_ast::tokenstream::{self, TokenStream, TokenTree};
88
use rustc_ast::visit::{AssocCtxt, Visitor};
99
use rustc_attr::{self as attr, Deprecation, HasAttrs, Stability};
1010
use rustc_data_structures::fx::FxHashMap;
@@ -118,6 +118,31 @@ impl Annotatable {
118118
}
119119
}
120120

121+
crate fn into_tokens(self) -> TokenStream {
122+
// `Annotatable` can be converted into tokens directly, but we
123+
// are packing it into a nonterminal as a piece of AST to make
124+
// the produced token stream look nicer in pretty-printed form.
125+
let nt = match self {
126+
Annotatable::Item(item) => token::NtItem(item),
127+
Annotatable::TraitItem(item) | Annotatable::ImplItem(item) => {
128+
token::NtItem(P(item.and_then(ast::AssocItem::into_item)))
129+
}
130+
Annotatable::ForeignItem(item) => {
131+
token::NtItem(P(item.and_then(ast::ForeignItem::into_item)))
132+
}
133+
Annotatable::Stmt(stmt) => token::NtStmt(stmt.into_inner()),
134+
Annotatable::Expr(expr) => token::NtExpr(expr),
135+
Annotatable::Arm(..)
136+
| Annotatable::Field(..)
137+
| Annotatable::FieldPat(..)
138+
| Annotatable::GenericParam(..)
139+
| Annotatable::Param(..)
140+
| Annotatable::StructField(..)
141+
| Annotatable::Variant(..) => panic!("unexpected annotatable"),
142+
};
143+
TokenTree::token(token::Interpolated(Lrc::new(nt)), DUMMY_SP).into()
144+
}
145+
121146
pub fn expect_item(self) -> P<ast::Item> {
122147
match self {
123148
Annotatable::Item(i) => i,

src/librustc_expand/expand.rs

+6-31
Original file line numberDiff line numberDiff line change
@@ -10,12 +10,11 @@ use rustc_ast::ast::{ItemKind, MacArgs, MacStmtStyle, StmtKind};
1010
use rustc_ast::mut_visit::*;
1111
use rustc_ast::ptr::P;
1212
use rustc_ast::token;
13-
use rustc_ast::tokenstream::{TokenStream, TokenTree};
13+
use rustc_ast::tokenstream::TokenStream;
1414
use rustc_ast::util::map_in_place::MapInPlace;
1515
use rustc_ast::visit::{self, AssocCtxt, Visitor};
1616
use rustc_ast_pretty::pprust;
1717
use rustc_attr::{self as attr, is_builtin_attr, HasAttrs};
18-
use rustc_data_structures::sync::Lrc;
1918
use rustc_errors::{Applicability, FatalError, PResult};
2019
use rustc_feature::Features;
2120
use rustc_parse::configure;
@@ -668,38 +667,14 @@ impl<'a, 'b> MacroExpander<'a, 'b> {
668667
SyntaxExtensionKind::Attr(expander) => {
669668
self.gate_proc_macro_input(&item);
670669
self.gate_proc_macro_attr_item(span, &item);
671-
// `Annotatable` can be converted into tokens directly, but we are packing it
672-
// into a nonterminal as a piece of AST to make the produced token stream
673-
// look nicer in pretty-printed form. This may be no longer necessary.
674-
let item_tok = TokenTree::token(
675-
token::Interpolated(Lrc::new(match item {
676-
Annotatable::Item(item) => token::NtItem(item),
677-
Annotatable::TraitItem(item) | Annotatable::ImplItem(item) => {
678-
token::NtItem(P(item.and_then(ast::AssocItem::into_item)))
679-
}
680-
Annotatable::ForeignItem(item) => {
681-
token::NtItem(P(item.and_then(ast::ForeignItem::into_item)))
682-
}
683-
Annotatable::Stmt(stmt) => token::NtStmt(stmt.into_inner()),
684-
Annotatable::Expr(expr) => token::NtExpr(expr),
685-
Annotatable::Arm(..)
686-
| Annotatable::Field(..)
687-
| Annotatable::FieldPat(..)
688-
| Annotatable::GenericParam(..)
689-
| Annotatable::Param(..)
690-
| Annotatable::StructField(..)
691-
| Annotatable::Variant(..) => panic!("unexpected annotatable"),
692-
})),
693-
DUMMY_SP,
694-
)
695-
.into();
696-
let item = attr.unwrap_normal_item();
697-
if let MacArgs::Eq(..) = item.args {
670+
let tokens = item.into_tokens();
671+
let attr_item = attr.unwrap_normal_item();
672+
if let MacArgs::Eq(..) = attr_item.args {
698673
self.cx.span_err(span, "key-value macro attributes are not supported");
699674
}
700675
let tok_result =
701-
expander.expand(self.cx, span, item.args.inner_tokens(), item_tok);
702-
self.parse_ast_fragment(tok_result, fragment_kind, &item.path, span)
676+
expander.expand(self.cx, span, attr_item.args.inner_tokens(), tokens);
677+
self.parse_ast_fragment(tok_result, fragment_kind, &attr_item.path, span)
703678
}
704679
SyntaxExtensionKind::LegacyAttr(expander) => {
705680
match validate_attr::parse_meta(self.cx.parse_sess, &attr) {

0 commit comments

Comments
 (0)