Skip to content

Commit 76e755c

Browse files
committed
Auto merge of #88066 - LeSeulArtichaut:patterns-cleanups, r=nagisa
Use if-let guards in the codebase and various other pattern cleanups Dogfooding if-let guards as experimentation for the feature. Tracking issue #51114. Conflicts with #87937.
2 parents c4be230 + 2b0c8ff commit 76e755c

File tree

35 files changed

+288
-321
lines changed

35 files changed

+288
-321
lines changed

compiler/rustc_ast/src/attr/mod.rs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -564,11 +564,11 @@ impl NestedMetaItem {
564564
I: Iterator<Item = TokenTree>,
565565
{
566566
match tokens.peek() {
567-
Some(TokenTree::Token(token)) => {
568-
if let Ok(lit) = Lit::from_token(token) {
569-
tokens.next();
570-
return Some(NestedMetaItem::Literal(lit));
571-
}
567+
Some(TokenTree::Token(token))
568+
if let Ok(lit) = Lit::from_token(token) =>
569+
{
570+
tokens.next();
571+
return Some(NestedMetaItem::Literal(lit));
572572
}
573573
Some(TokenTree::Delimited(_, token::NoDelim, inner_tokens)) => {
574574
let inner_tokens = inner_tokens.clone();

compiler/rustc_ast/src/lib.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,10 +11,12 @@
1111
#![feature(box_patterns)]
1212
#![cfg_attr(bootstrap, feature(const_fn_transmute))]
1313
#![feature(crate_visibility_modifier)]
14+
#![feature(if_let_guard)]
1415
#![feature(iter_zip)]
1516
#![feature(label_break_value)]
1617
#![feature(nll)]
1718
#![feature(min_specialization)]
19+
#![cfg_attr(bootstrap, allow(incomplete_features))] // if_let_guard
1820
#![recursion_limit = "256"]
1921

2022
#[macro_use]

compiler/rustc_ast/src/mut_visit.rs

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1179,13 +1179,10 @@ fn noop_visit_inline_asm<T: MutVisitor>(asm: &mut InlineAsm, vis: &mut T) {
11791179
for (op, _) in &mut asm.operands {
11801180
match op {
11811181
InlineAsmOperand::In { expr, .. }
1182+
| InlineAsmOperand::Out { expr: Some(expr), .. }
11821183
| InlineAsmOperand::InOut { expr, .. }
11831184
| InlineAsmOperand::Sym { expr, .. } => vis.visit_expr(expr),
1184-
InlineAsmOperand::Out { expr, .. } => {
1185-
if let Some(expr) = expr {
1186-
vis.visit_expr(expr);
1187-
}
1188-
}
1185+
InlineAsmOperand::Out { expr: None, .. } => {}
11891186
InlineAsmOperand::SplitInOut { in_expr, out_expr, .. } => {
11901187
vis.visit_expr(in_expr);
11911188
if let Some(out_expr) = out_expr {

compiler/rustc_ast/src/visit.rs

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -714,13 +714,10 @@ fn walk_inline_asm<'a, V: Visitor<'a>>(visitor: &mut V, asm: &'a InlineAsm) {
714714
for (op, _) in &asm.operands {
715715
match op {
716716
InlineAsmOperand::In { expr, .. }
717+
| InlineAsmOperand::Out { expr: Some(expr), .. }
717718
| InlineAsmOperand::InOut { expr, .. }
718719
| InlineAsmOperand::Sym { expr, .. } => visitor.visit_expr(expr),
719-
InlineAsmOperand::Out { expr, .. } => {
720-
if let Some(expr) = expr {
721-
visitor.visit_expr(expr);
722-
}
723-
}
720+
InlineAsmOperand::Out { expr: None, .. } => {}
724721
InlineAsmOperand::SplitInOut { in_expr, out_expr, .. } => {
725722
visitor.visit_expr(in_expr);
726723
if let Some(out_expr) = out_expr {

compiler/rustc_errors/src/lib.rs

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,11 @@
55
#![doc(html_root_url = "https://doc.rust-lang.org/nightly/nightly-rustc/")]
66
#![feature(crate_visibility_modifier)]
77
#![feature(backtrace)]
8+
#![feature(if_let_guard)]
89
#![feature(format_args_capture)]
910
#![feature(iter_zip)]
1011
#![feature(nll)]
12+
#![cfg_attr(bootstrap, allow(incomplete_features))] // if_let_guard
1113

1214
#[macro_use]
1315
extern crate rustc_macros;
@@ -1027,15 +1029,15 @@ impl HandlerInner {
10271029
let mut error_codes = self
10281030
.emitted_diagnostic_codes
10291031
.iter()
1030-
.filter_map(|x| match &x {
1031-
DiagnosticId::Error(s) => {
1032-
if let Ok(Some(_explanation)) = registry.try_find_description(s) {
1033-
Some(s.clone())
1034-
} else {
1035-
None
1036-
}
1032+
.filter_map(|x| {
1033+
match &x {
1034+
DiagnosticId::Error(s)
1035+
if let Ok(Some(_explanation)) = registry.try_find_description(s) =>
1036+
{
1037+
Some(s.clone())
10371038
}
10381039
_ => None,
1040+
}
10391041
})
10401042
.collect::<Vec<_>>();
10411043
if !error_codes.is_empty() {

compiler/rustc_expand/src/config.rs

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -305,15 +305,14 @@ impl<'a> StripUnconfigured<'a> {
305305
Some((AttrAnnotatedTokenTree::Delimited(sp, delim, inner), *spacing))
306306
.into_iter()
307307
}
308+
AttrAnnotatedTokenTree::Token(ref token) if let TokenKind::Interpolated(ref nt) = token.kind => {
309+
panic!(
310+
"Nonterminal should have been flattened at {:?}: {:?}",
311+
token.span, nt
312+
);
313+
}
308314
AttrAnnotatedTokenTree::Token(token) => {
309-
if let TokenKind::Interpolated(nt) = token.kind {
310-
panic!(
311-
"Nonterminal should have been flattened at {:?}: {:?}",
312-
token.span, nt
313-
);
314-
} else {
315-
Some((AttrAnnotatedTokenTree::Token(token), *spacing)).into_iter()
316-
}
315+
Some((AttrAnnotatedTokenTree::Token(token), *spacing)).into_iter()
317316
}
318317
})
319318
.collect();

compiler/rustc_expand/src/lib.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,15 @@
1+
#![cfg_attr(bootstrap, feature(bindings_after_at))]
12
#![feature(crate_visibility_modifier)]
23
#![feature(decl_macro)]
34
#![feature(destructuring_assignment)]
45
#![feature(format_args_capture)]
6+
#![feature(if_let_guard)]
57
#![feature(iter_zip)]
68
#![feature(proc_macro_diagnostic)]
79
#![feature(proc_macro_internals)]
810
#![feature(proc_macro_span)]
911
#![feature(try_blocks)]
12+
#![cfg_attr(bootstrap, allow(incomplete_features))] // if_let_guard
1013

1114
#[macro_use]
1215
extern crate rustc_macros;

compiler/rustc_expand/src/module.rs

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -86,13 +86,12 @@ crate fn mod_dir_path(
8686
inline: Inline,
8787
) -> (PathBuf, DirOwnership) {
8888
match inline {
89+
Inline::Yes if let Some(file_path) = mod_file_path_from_attr(sess, attrs, &module.dir_path) => {
90+
// For inline modules file path from `#[path]` is actually the directory path
91+
// for historical reasons, so we don't pop the last segment here.
92+
(file_path, DirOwnership::Owned { relative: None })
93+
}
8994
Inline::Yes => {
90-
if let Some(file_path) = mod_file_path_from_attr(sess, attrs, &module.dir_path) {
91-
// For inline modules file path from `#[path]` is actually the directory path
92-
// for historical reasons, so we don't pop the last segment here.
93-
return (file_path, DirOwnership::Owned { relative: None });
94-
}
95-
9695
// We have to push on the current module name in the case of relative
9796
// paths in order to ensure that any additional module paths from inline
9897
// `mod x { ... }` come after the relative extension.

compiler/rustc_expand/src/proc_macro_server.rs

Lines changed: 12 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -178,18 +178,19 @@ impl FromInternal<(TreeAndSpacing, &'_ mut Vec<Self>, &mut Rustc<'_>)>
178178
tt!(Punct::new('#', false))
179179
}
180180

181+
Interpolated(nt)
182+
if let Some((name, is_raw)) = ident_name_compatibility_hack(&nt, span, rustc) =>
183+
{
184+
TokenTree::Ident(Ident::new(rustc.sess, name.name, is_raw, name.span))
185+
}
181186
Interpolated(nt) => {
182-
if let Some((name, is_raw)) = ident_name_compatibility_hack(&nt, span, rustc) {
183-
TokenTree::Ident(Ident::new(rustc.sess, name.name, is_raw, name.span))
184-
} else {
185-
let stream = nt_to_tokenstream(&nt, rustc.sess, CanSynthesizeMissingTokens::No);
186-
TokenTree::Group(Group {
187-
delimiter: Delimiter::None,
188-
stream,
189-
span: DelimSpan::from_single(span),
190-
flatten: crate::base::pretty_printing_compatibility_hack(&nt, rustc.sess),
191-
})
192-
}
187+
let stream = nt_to_tokenstream(&nt, rustc.sess, CanSynthesizeMissingTokens::No);
188+
TokenTree::Group(Group {
189+
delimiter: Delimiter::None,
190+
stream,
191+
span: DelimSpan::from_single(span),
192+
flatten: crate::base::pretty_printing_compatibility_hack(&nt, rustc.sess),
193+
})
193194
}
194195

195196
OpenDelim(..) | CloseDelim(..) => unreachable!(),

compiler/rustc_middle/src/lib.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@
3131
#![feature(box_patterns)]
3232
#![feature(core_intrinsics)]
3333
#![feature(discriminant_kind)]
34+
#![feature(if_let_guard)]
3435
#![feature(never_type)]
3536
#![feature(extern_types)]
3637
#![feature(new_uninit)]
@@ -52,6 +53,7 @@
5253
#![feature(try_reserve)]
5354
#![feature(try_reserve_kind)]
5455
#![feature(nonzero_ops)]
56+
#![cfg_attr(bootstrap, allow(incomplete_features))] // if_let_guard
5557
#![recursion_limit = "512"]
5658

5759
#[macro_use]

0 commit comments

Comments
 (0)