Skip to content

Commit 2446a21

Browse files
committed
Auto merge of rust-lang#91324 - eggyal:avoid-uneccesary-clone-of-annotatable, r=Aaron1011
Avoid uneccessary clone of Annotatable Addresses FIXME comment created in rust-lang#82608 r? `@Aaron1011`
2 parents d384ff7 + d05e4d2 commit 2446a21

File tree

1 file changed

+30
-22
lines changed

1 file changed

+30
-22
lines changed

compiler/rustc_builtin_macros/src/cfg_eval.rs

+30-22
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ use rustc_expand::base::{Annotatable, ExtCtxt};
1111
use rustc_expand::config::StripUnconfigured;
1212
use rustc_expand::configure;
1313
use rustc_feature::Features;
14-
use rustc_parse::parser::ForceCollect;
14+
use rustc_parse::parser::{ForceCollect, Parser};
1515
use rustc_session::utils::FlattenNonterminals;
1616
use rustc_session::Session;
1717
use rustc_span::symbol::sym;
@@ -138,8 +138,34 @@ impl CfgEval<'_, '_> {
138138
// the location of `#[cfg]` and `#[cfg_attr]` in the token stream. The tokenization
139139
// process is lossless, so this process is invisible to proc-macros.
140140

141-
// FIXME - get rid of this clone
142-
let nt = annotatable.clone().into_nonterminal();
141+
let parse_annotatable_with: fn(&mut Parser<'_>) -> _ = match annotatable {
142+
Annotatable::Item(_) => {
143+
|parser| Annotatable::Item(parser.parse_item(ForceCollect::Yes).unwrap().unwrap())
144+
}
145+
Annotatable::TraitItem(_) => |parser| {
146+
Annotatable::TraitItem(
147+
parser.parse_trait_item(ForceCollect::Yes).unwrap().unwrap().unwrap(),
148+
)
149+
},
150+
Annotatable::ImplItem(_) => |parser| {
151+
Annotatable::ImplItem(
152+
parser.parse_impl_item(ForceCollect::Yes).unwrap().unwrap().unwrap(),
153+
)
154+
},
155+
Annotatable::ForeignItem(_) => |parser| {
156+
Annotatable::ForeignItem(
157+
parser.parse_foreign_item(ForceCollect::Yes).unwrap().unwrap().unwrap(),
158+
)
159+
},
160+
Annotatable::Stmt(_) => |parser| {
161+
Annotatable::Stmt(P(parser.parse_stmt(ForceCollect::Yes).unwrap().unwrap()))
162+
},
163+
Annotatable::Expr(_) => {
164+
|parser| Annotatable::Expr(parser.parse_expr_force_collect().unwrap())
165+
}
166+
_ => unreachable!(),
167+
};
168+
let nt = annotatable.into_nonterminal();
143169

144170
let mut orig_tokens = rustc_parse::nt_to_tokenstream(
145171
&nt,
@@ -173,25 +199,7 @@ impl CfgEval<'_, '_> {
173199
let mut parser =
174200
rustc_parse::stream_to_parser(&self.cfg.sess.parse_sess, orig_tokens, None);
175201
parser.capture_cfg = true;
176-
annotatable = match annotatable {
177-
Annotatable::Item(_) => {
178-
Annotatable::Item(parser.parse_item(ForceCollect::Yes).unwrap().unwrap())
179-
}
180-
Annotatable::TraitItem(_) => Annotatable::TraitItem(
181-
parser.parse_trait_item(ForceCollect::Yes).unwrap().unwrap().unwrap(),
182-
),
183-
Annotatable::ImplItem(_) => Annotatable::ImplItem(
184-
parser.parse_impl_item(ForceCollect::Yes).unwrap().unwrap().unwrap(),
185-
),
186-
Annotatable::ForeignItem(_) => Annotatable::ForeignItem(
187-
parser.parse_foreign_item(ForceCollect::Yes).unwrap().unwrap().unwrap(),
188-
),
189-
Annotatable::Stmt(_) => {
190-
Annotatable::Stmt(P(parser.parse_stmt(ForceCollect::Yes).unwrap().unwrap()))
191-
}
192-
Annotatable::Expr(_) => Annotatable::Expr(parser.parse_expr_force_collect().unwrap()),
193-
_ => unreachable!(),
194-
};
202+
annotatable = parse_annotatable_with(&mut parser);
195203

196204
// Now that we have our re-parsed `AttrAnnotatedTokenStream`, recursively configuring
197205
// our attribute target will correctly the tokens as well.

0 commit comments

Comments
 (0)