Skip to content

Commit dc646e9

Browse files
committed
Auto merge of #155311 - cyrgani:expand-clean, r=<try>
various small `rustc_expand` cleanups
2 parents a5c825c + 2e3036d commit dc646e9

File tree

16 files changed

+102
-120
lines changed

16 files changed

+102
-120
lines changed

‎compiler/rustc_expand/src/base.rs‎

Lines changed: 10 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -409,17 +409,10 @@ pub trait GlobDelegationExpander {
409409
fn expand(&self, ecx: &mut ExtCtxt<'_>) -> ExpandResult<Vec<(Ident, Option<Ident>)>, ()>;
410410
}
411411

412-
// Use a macro because forwarding to a simple function has type system issues
413-
macro_rules! make_stmts_default {
414-
($me:expr) => {
415-
$me.make_expr().map(|e| {
416-
smallvec![ast::Stmt {
417-
id: ast::DUMMY_NODE_ID,
418-
span: e.span,
419-
kind: ast::StmtKind::Expr(e),
420-
}]
421-
})
422-
};
412+
fn make_stmts_default(expr: Option<Box<ast::Expr>>) -> Option<SmallVec<[ast::Stmt; 1]>> {
413+
expr.map(|e| {
414+
smallvec![ast::Stmt { id: ast::DUMMY_NODE_ID, span: e.span, kind: ast::StmtKind::Expr(e) }]
415+
})
423416
}
424417

425418
/// The result of a macro expansion. The return values of the various
@@ -465,7 +458,7 @@ pub trait MacResult {
465458
/// By default this attempts to create an expression statement,
466459
/// returning None if that fails.
467460
fn make_stmts(self: Box<Self>) -> Option<SmallVec<[ast::Stmt; 1]>> {
468-
make_stmts_default!(self)
461+
make_stmts_default(self.make_expr())
469462
}
470463

471464
fn make_ty(self: Box<Self>) -> Option<Box<ast::Ty>> {
@@ -571,9 +564,10 @@ impl MacResult for MacEager {
571564
}
572565

573566
fn make_stmts(self: Box<Self>) -> Option<SmallVec<[ast::Stmt; 1]>> {
574-
match self.stmts.as_ref().map_or(0, |s| s.len()) {
575-
0 => make_stmts_default!(self),
576-
_ => self.stmts,
567+
if self.stmts.as_ref().is_none_or(|s| s.is_empty()) {
568+
make_stmts_default(self.make_expr())
569+
} else {
570+
self.stmts
577571
}
578572
}
579573

@@ -898,7 +892,7 @@ impl SyntaxExtension {
898892
fn get_collapse_debuginfo(sess: &Session, attrs: &[hir::Attribute], ext: bool) -> bool {
899893
let flag = sess.opts.cg.collapse_macro_debuginfo;
900894
let attr = if let Some(info) = find_attr!(attrs, CollapseDebugInfo(info) => info) {
901-
info.clone()
895+
*info
902896
} else if find_attr!(attrs, RustcBuiltinMacro { .. }) {
903897
CollapseMacroDebuginfo::Yes
904898
} else {

‎compiler/rustc_expand/src/config.rs‎

Lines changed: 13 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -106,33 +106,24 @@ pub fn features(sess: &Session, krate_attrs: &[Attribute], crate_name: Symbol) -
106106

107107
// If the enabled feature is unstable, record it.
108108
if UNSTABLE_LANG_FEATURES.iter().find(|f| feature_ident.name == f.name).is_some() {
109-
// When the ICE comes from a standard library crate, there's a chance that the person
110-
// hitting the ICE may be using -Zbuild-std or similar with an untested target.
111-
// The bug is probably in the standard library and not the compiler in that case,
112-
// but that doesn't really matter - we want a bug report.
113-
if features.internal(feature_ident.name)
114-
&& !STDLIB_STABLE_CRATES.contains(&crate_name)
115-
{
116-
sess.using_internal_features.store(true, std::sync::atomic::Ordering::Relaxed);
117-
}
118-
119109
features.set_enabled_lang_feature(EnabledLangFeature {
120110
gate_name: feature_ident.name,
121111
attr_sp: feature_ident.span,
122112
stable_since: None,
123113
});
124-
continue;
114+
} else {
115+
// Otherwise, the feature is unknown. Enable it as a lib feature.
116+
// It will be checked later whether the feature really exists.
117+
features.set_enabled_lib_feature(EnabledLibFeature {
118+
gate_name: feature_ident.name,
119+
attr_sp: feature_ident.span,
120+
});
125121
}
126122

127-
// Otherwise, the feature is unknown. Enable it as a lib feature.
128-
// It will be checked later whether the feature really exists.
129-
features.set_enabled_lib_feature(EnabledLibFeature {
130-
gate_name: feature_ident.name,
131-
attr_sp: feature_ident.span,
132-
});
133-
134-
// Similar to above, detect internal lib features to suppress
135-
// the ICE message that asks for a report.
123+
// When the ICE comes from a standard library crate, there's a chance that the person
124+
// hitting the ICE may be using -Zbuild-std or similar with an untested target.
125+
// The bug is probably in the standard library and not the compiler in that case,
126+
// but that doesn't really matter - we want a bug report.
136127
if features.internal(feature_ident.name) && !STDLIB_STABLE_CRATES.contains(&crate_name)
137128
{
138129
sess.using_internal_features.store(true, std::sync::atomic::Ordering::Relaxed);
@@ -285,7 +276,7 @@ impl<'a> StripUnconfigured<'a> {
285276

286277
let Some((cfg_predicate, expanded_attrs)) = rustc_attr_parsing::parse_cfg_attr(
287278
cfg_attr,
288-
&self.sess,
279+
self.sess,
289280
self.features,
290281
self.lint_node_id,
291282
) else {
@@ -422,7 +413,7 @@ impl<'a> StripUnconfigured<'a> {
422413
&& !attr.span.allows_unstable(sym::stmt_expr_attributes)
423414
{
424415
let mut err = feature_err(
425-
&self.sess,
416+
self.sess,
426417
sym::stmt_expr_attributes,
427418
attr.span,
428419
msg!("attributes on expressions are experimental"),

‎compiler/rustc_expand/src/expand.rs‎

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1050,7 +1050,7 @@ impl<'a, 'b> MacroExpander<'a, 'b> {
10501050
return;
10511051
}
10521052
feature_err(
1053-
&self.cx.sess,
1053+
self.cx.sess,
10541054
sym::proc_macro_hygiene,
10551055
span,
10561056
format!("custom attributes cannot be applied to {kind}"),
@@ -1085,7 +1085,7 @@ impl<'a, 'b> MacroExpander<'a, 'b> {
10851085
}
10861086

10871087
if !self.cx.ecfg.features.proc_macro_hygiene() {
1088-
annotatable.visit_with(&mut GateProcMacroInput { sess: &self.cx.sess });
1088+
annotatable.visit_with(&mut GateProcMacroInput { sess: self.cx.sess });
10891089
}
10901090
}
10911091

@@ -1474,15 +1474,15 @@ impl InvocationCollectorNode for Box<ast::Item> {
14741474
}
14751475
}
14761476
let mut idents = Vec::new();
1477-
collect_use_tree_leaves(&ut, &mut idents);
1477+
collect_use_tree_leaves(ut, &mut idents);
14781478
idents
14791479
} else {
14801480
self.kind.ident().into_iter().collect()
14811481
}
14821482
}
14831483

14841484
fn as_target(&self) -> Target {
1485-
Target::from_ast_item(&*self)
1485+
Target::from_ast_item(self)
14861486
}
14871487
}
14881488

‎compiler/rustc_expand/src/mbe/diagnostics.rs‎

Lines changed: 22 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -288,52 +288,47 @@ pub(super) fn emit_frag_parse_err(
288288
_ => annotate_err_with_kind(&mut e, kind, site_span),
289289
};
290290

291-
let mut bindings_rules = vec![];
292-
for rule in bindings {
293-
let MacroRule::Func { lhs, .. } = rule else { continue };
294-
for param in lhs {
295-
let MatcherLoc::MetaVarDecl { bind, .. } = param else { continue };
296-
bindings_rules.push(*bind);
297-
}
298-
}
299-
300-
let mut matched_rule_bindings_rules = vec![];
301-
for param in matched_rule_bindings {
302-
let MatcherLoc::MetaVarDecl { bind, .. } = param else { continue };
303-
matched_rule_bindings_rules.push(*bind);
304-
}
305-
306-
let matched_rule_bindings_names: Vec<_> =
307-
matched_rule_bindings_rules.iter().map(|bind| bind.name).collect();
308-
let bindings_name: Vec<_> = bindings_rules.iter().map(|bind| bind.name).collect();
309291
if parser.token.kind == token::Dollar {
310292
parser.bump();
311293
if let token::Ident(name, _) = parser.token.kind {
294+
let mut bindings_names = vec![];
295+
for rule in bindings {
296+
let MacroRule::Func { lhs, .. } = rule else { continue };
297+
for param in lhs {
298+
let MatcherLoc::MetaVarDecl { bind, .. } = param else { continue };
299+
bindings_names.push(bind.name);
300+
}
301+
}
302+
303+
let mut matched_rule_bindings_names = vec![];
304+
for param in matched_rule_bindings {
305+
let MatcherLoc::MetaVarDecl { bind, .. } = param else { continue };
306+
matched_rule_bindings_names.push(bind.name);
307+
}
308+
312309
if let Some(matched_name) = rustc_span::edit_distance::find_best_match_for_name(
313310
&matched_rule_bindings_names[..],
314311
name,
315312
None,
316313
) {
317314
e.span_suggestion_verbose(
318315
parser.token.span,
319-
"there is a macro metavariable with similar name",
320-
format!("{matched_name}"),
316+
"there is a macro metavariable with a similar name",
317+
matched_name,
321318
Applicability::MaybeIncorrect,
322319
);
323-
} else if bindings_name.contains(&name) {
320+
} else if bindings_names.contains(&name) {
324321
e.span_label(
325322
parser.token.span,
326-
format!(
327-
"there is an macro metavariable with this name in another macro matcher"
328-
),
323+
"there is an macro metavariable with this name in another macro matcher",
329324
);
330325
} else if let Some(matched_name) =
331-
rustc_span::edit_distance::find_best_match_for_name(&bindings_name[..], name, None)
326+
rustc_span::edit_distance::find_best_match_for_name(&bindings_names[..], name, None)
332327
{
333328
e.span_suggestion_verbose(
334329
parser.token.span,
335330
"there is a macro metavariable with a similar name in another macro matcher",
336-
format!("{matched_name}"),
331+
matched_name,
337332
Applicability::MaybeIncorrect,
338333
);
339334
} else {
@@ -343,7 +338,7 @@ pub(super) fn emit_frag_parse_err(
343338
.collect::<Vec<_>>()
344339
.join(", ");
345340

346-
e.span_label(parser.token.span, format!("macro metavariable not found"));
341+
e.span_label(parser.token.span, "macro metavariable not found");
347342
if !matched_rule_bindings_names.is_empty() {
348343
e.note(format!("available metavariable names are: {msg}"));
349344
}

‎compiler/rustc_expand/src/mbe/macro_parser.rs‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -199,8 +199,8 @@ pub(super) fn compute_locs(matcher: &[TokenTree]) -> Vec<MatcherLoc> {
199199
let idx_seq = idx_first - 1;
200200
inner(&seq.tts, locs, next_metavar, seq_depth + 1);
201201

202-
if let Some(separator) = &seq.separator {
203-
locs.push(MatcherLoc::SequenceSep { separator: separator.clone() });
202+
if let Some(separator) = seq.separator {
203+
locs.push(MatcherLoc::SequenceSep { separator });
204204
locs.push(MatcherLoc::SequenceKleeneOpAfterSep { idx_first });
205205
} else {
206206
locs.push(MatcherLoc::SequenceKleeneOpNoSep { op, idx_first });

‎compiler/rustc_expand/src/mbe/quoted.rs‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -214,7 +214,7 @@ fn parse_tree<'a>(
214214
// during parsing.
215215
let mut next = outer_iter.next();
216216
let mut iter_storage;
217-
let mut iter: &mut TokenStreamIter<'_> = match next {
217+
let iter: &mut TokenStreamIter<'_> = match next {
218218
Some(tokenstream::TokenTree::Delimited(.., delim, tts)) if delim.skip() => {
219219
iter_storage = tts.iter();
220220
next = iter_storage.next();
@@ -284,7 +284,7 @@ fn parse_tree<'a>(
284284
let sequence = parse(tts, part, sess, node_id, features, edition);
285285
// Get the Kleene operator and optional separator
286286
let (separator, kleene) =
287-
parse_sep_and_kleene_op(&mut iter, delim_span.entire(), sess);
287+
parse_sep_and_kleene_op(iter, delim_span.entire(), sess);
288288
// Count the number of captured "names" (i.e., named metavars)
289289
let num_captures =
290290
if part.is_pattern() { count_metavar_decls(&sequence) } else { 0 };

‎compiler/rustc_expand/src/mbe/transcribe.rs‎

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -396,7 +396,7 @@ fn transcribe_sequence<'tx, 'itp>(
396396
// The first time we encounter the sequence we push it to the stack. It
397397
// then gets reused (see the beginning of the loop) until we are done
398398
// repeating.
399-
tscx.stack.push(Frame::new_sequence(seq_rep, seq.separator.clone(), seq.kleene.op));
399+
tscx.stack.push(Frame::new_sequence(seq_rep, seq.separator, seq.kleene.op));
400400
}
401401
}
402402
}
@@ -629,7 +629,7 @@ fn metavar_expr_concat<'tx>(
629629
) -> PResult<'tx, TokenTree> {
630630
let dcx = tscx.psess.dcx();
631631
let mut concatenated = String::new();
632-
for element in elements.into_iter() {
632+
for element in elements {
633633
let symbol = match element {
634634
MetaVarExprConcatElem::Ident(elem) => elem.name,
635635
MetaVarExprConcatElem::Literal(elem) => *elem,
@@ -747,7 +747,7 @@ fn maybe_use_metavar_location(
747747
TokenTree::Token(Token { kind, span }, spacing) => {
748748
let span = metavar_span.with_ctxt(span.ctxt());
749749
with_metavar_spans(|mspans| mspans.insert(span, metavar_span));
750-
TokenTree::Token(Token { kind: kind.clone(), span }, *spacing)
750+
TokenTree::Token(Token { kind: *kind, span }, *spacing)
751751
}
752752
TokenTree::Delimited(dspan, dspacing, delimiter, tts) => {
753753
let open = metavar_span.with_ctxt(dspan.open.ctxt());

‎compiler/rustc_expand/src/module.rs‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,7 @@ pub(crate) fn parse_external_mod(
7777
Some(span),
7878
));
7979
let (inner_attrs, items, inner_span) =
80-
parser.parse_mod(exp!(Eof)).map_err(|err| ModError::ParserError(err))?;
80+
parser.parse_mod(exp!(Eof)).map_err(ModError::ParserError)?;
8181
attrs.extend(inner_attrs);
8282
(items, inner_span, mp.file_path)
8383
};

0 commit comments

Comments
 (0)