Skip to content

Commit 3117784

Browse files
authored
Rollup merge of rust-lang#57535 - varkor:stabilise-if-while-let-patterns, r=Centril
Stabilise irrefutable if-let and while-let patterns This stabilises RFC 2086 (rust-lang#44495). This replaces rust-lang#55639, as we want to stabilise this in time for the beta cut-off. Closes rust-lang#55639. r? @Centril
2 parents 0a8b5b4 + 0e1402d commit 3117784

30 files changed

+176
-239
lines changed

src/doc/unstable-book/src/language-features/irrefutable-let-patterns.md

-28
This file was deleted.

src/librustc/lint/builtin.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -286,7 +286,7 @@ declare_lint! {
286286

287287
declare_lint! {
288288
pub IRREFUTABLE_LET_PATTERNS,
289-
Deny,
289+
Warn,
290290
"detects irrefutable patterns in if-let and while-let statements"
291291
}
292292

src/librustc_mir/diagnostics.rs

+6-2
Original file line numberDiff line numberDiff line change
@@ -325,11 +325,13 @@ match Some(42) {
325325
"##,
326326

327327
E0162: r##"
328+
#### Note: this error code is no longer emitted by the compiler.
329+
328330
An if-let pattern attempts to match the pattern, and enters the body if the
329331
match was successful. If the match is irrefutable (when it cannot fail to
330332
match), use a regular `let`-binding instead. For instance:
331333
332-
```compile_fail,E0162
334+
```compile_pass
333335
struct Irrefutable(i32);
334336
let irr = Irrefutable(0);
335337
@@ -352,11 +354,13 @@ println!("{}", x);
352354
"##,
353355

354356
E0165: r##"
357+
#### Note: this error code is no longer emitted by the compiler.
358+
355359
A while-let pattern attempts to match the pattern, and enters the body if the
356360
match was successful. If the match is irrefutable (when it cannot fail to
357361
match), use a regular `let`-binding inside a `loop` instead. For instance:
358362
359-
```compile_fail,E0165
363+
```compile_pass,no_run
360364
struct Irrefutable(i32);
361365
let irr = Irrefutable(0);
362366

src/librustc_mir/hair/pattern/check_match.rs

+12-37
Original file line numberDiff line numberDiff line change
@@ -350,7 +350,6 @@ fn check_arms<'a, 'tcx>(cx: &mut MatchCheckCtxt<'a, 'tcx>,
350350
{
351351
let mut seen = Matrix::empty();
352352
let mut catchall = None;
353-
let mut printed_if_let_err = false;
354353
for (arm_index, &(ref pats, guard)) in arms.iter().enumerate() {
355354
for &(pat, hir_pat) in pats {
356355
let v = smallvec![pat];
@@ -359,27 +358,12 @@ fn check_arms<'a, 'tcx>(cx: &mut MatchCheckCtxt<'a, 'tcx>,
359358
NotUseful => {
360359
match source {
361360
hir::MatchSource::IfLetDesugar { .. } => {
362-
if cx.tcx.features().irrefutable_let_patterns {
363-
cx.tcx.lint_node(
364-
lint::builtin::IRREFUTABLE_LET_PATTERNS,
365-
hir_pat.id, pat.span,
366-
"irrefutable if-let pattern");
367-
} else {
368-
if printed_if_let_err {
369-
// we already printed an irrefutable if-let pattern error.
370-
// We don't want two, that's just confusing.
371-
} else {
372-
// find the first arm pattern so we can use its span
373-
let &(ref first_arm_pats, _) = &arms[0];
374-
let first_pat = &first_arm_pats[0];
375-
let span = first_pat.0.span;
376-
struct_span_err!(cx.tcx.sess, span, E0162,
377-
"irrefutable if-let pattern")
378-
.span_label(span, "irrefutable pattern")
379-
.emit();
380-
printed_if_let_err = true;
381-
}
382-
}
361+
cx.tcx.lint_node(
362+
lint::builtin::IRREFUTABLE_LET_PATTERNS,
363+
hir_pat.id,
364+
pat.span,
365+
"irrefutable if-let pattern",
366+
);
383367
}
384368

385369
hir::MatchSource::WhileLetDesugar => {
@@ -394,21 +378,12 @@ fn check_arms<'a, 'tcx>(cx: &mut MatchCheckCtxt<'a, 'tcx>,
394378
},
395379
// The arm with the wildcard pattern.
396380
1 => {
397-
if cx.tcx.features().irrefutable_let_patterns {
398-
cx.tcx.lint_node(
399-
lint::builtin::IRREFUTABLE_LET_PATTERNS,
400-
hir_pat.id, pat.span,
401-
"irrefutable while-let pattern");
402-
} else {
403-
// find the first arm pattern so we can use its span
404-
let &(ref first_arm_pats, _) = &arms[0];
405-
let first_pat = &first_arm_pats[0];
406-
let span = first_pat.0.span;
407-
struct_span_err!(cx.tcx.sess, span, E0165,
408-
"irrefutable while-let pattern")
409-
.span_label(span, "irrefutable pattern")
410-
.emit();
411-
}
381+
cx.tcx.lint_node(
382+
lint::builtin::IRREFUTABLE_LET_PATTERNS,
383+
hir_pat.id,
384+
pat.span,
385+
"irrefutable while-let pattern",
386+
);
412387
},
413388
_ => bug!(),
414389
}

src/libsyntax/feature_gate.rs

+2-3
Original file line numberDiff line numberDiff line change
@@ -411,9 +411,6 @@ declare_features! (
411411
// `#[doc(alias = "...")]`
412412
(active, doc_alias, "1.27.0", Some(50146), None),
413413

414-
// Allows irrefutable patterns in `if let` and `while let` statements (RFC 2086).
415-
(active, irrefutable_let_patterns, "1.27.0", Some(44495), None),
416-
417414
// inconsistent bounds in where clauses
418415
(active, trivial_bounds, "1.28.0", Some(48214), None),
419416

@@ -681,6 +678,8 @@ declare_features! (
681678
(accepted, underscore_imports, "1.33.0", Some(48216), None),
682679
// Allows `#[repr(packed(N))]` attribute on structs.
683680
(accepted, repr_packed, "1.33.0", Some(33158), None),
681+
// Allows irrefutable patterns in `if let` and `while let` statements (RFC 2086).
682+
(accepted, irrefutable_let_patterns, "1.33.0", Some(44495), None),
684683
// Allows calling `const unsafe fn` inside `unsafe` blocks in `const fn` functions.
685684
(accepted, min_const_unsafe_fn, "1.33.0", Some(55607), None),
686685
// Allows let bindings, assignments and destructuring in `const` functions and constants.

src/test/run-pass/binding/allow_irrefutable_let_patterns.rs

-12
This file was deleted.

src/test/ui/error-codes/E0162.rs

-8
This file was deleted.

src/test/ui/error-codes/E0162.stderr

-9
This file was deleted.

src/test/ui/error-codes/E0165.rs

-9
This file was deleted.

src/test/ui/error-codes/E0165.stderr

-9
This file was deleted.

src/test/ui/feature-gates/feature-gate-without_gate_irrefutable_pattern.rs

-8
This file was deleted.

src/test/ui/feature-gates/feature-gate-without_gate_irrefutable_pattern.stderr

-9
This file was deleted.

src/test/ui/if/if-let.rs

+8-6
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
// compile-pass
2+
13
fn macros() {
24
macro_rules! foo{
35
($p:pat, $e:expr, $b:block) => {{
@@ -10,20 +12,20 @@ fn macros() {
1012
}}
1113
}
1214

13-
foo!(a, 1, { //~ ERROR irrefutable if-let
15+
foo!(a, 1, { //~ WARN irrefutable if-let
1416
println!("irrefutable pattern");
1517
});
16-
bar!(a, 1, { //~ ERROR irrefutable if-let
18+
bar!(a, 1, { //~ WARN irrefutable if-let
1719
println!("irrefutable pattern");
1820
});
1921
}
2022

2123
pub fn main() {
22-
if let a = 1 { //~ ERROR irrefutable if-let
24+
if let a = 1 { //~ WARN irrefutable if-let
2325
println!("irrefutable pattern");
2426
}
2527

26-
if let a = 1 { //~ ERROR irrefutable if-let
28+
if let a = 1 { //~ WARN irrefutable if-let
2729
println!("irrefutable pattern");
2830
} else if true {
2931
println!("else-if in irrefutable if-let");
@@ -33,13 +35,13 @@ pub fn main() {
3335

3436
if let 1 = 2 {
3537
println!("refutable pattern");
36-
} else if let a = 1 { //~ ERROR irrefutable if-let
38+
} else if let a = 1 { //~ WARN irrefutable if-let
3739
println!("irrefutable pattern");
3840
}
3941

4042
if true {
4143
println!("if");
42-
} else if let a = 1 { //~ ERROR irrefutable if-let
44+
} else if let a = 1 { //~ WARN irrefutable if-let
4345
println!("irrefutable pattern");
4446
}
4547
}

src/test/ui/if/if-let.stderr

+50-27
Original file line numberDiff line numberDiff line change
@@ -1,39 +1,62 @@
1-
error[E0162]: irrefutable if-let pattern
2-
--> $DIR/if-let.rs:13:10
1+
warning: irrefutable if-let pattern
2+
--> $DIR/if-let.rs:6:13
33
|
4-
LL | foo!(a, 1, { //~ ERROR irrefutable if-let
5-
| ^ irrefutable pattern
6-
7-
error[E0162]: irrefutable if-let pattern
8-
--> $DIR/if-let.rs:16:10
4+
LL | if let $p = $e $b
5+
| ^^
6+
...
7+
LL | / foo!(a, 1, { //~ WARN irrefutable if-let
8+
LL | | println!("irrefutable pattern");
9+
LL | | });
10+
| |_______- in this macro invocation
911
|
10-
LL | bar!(a, 1, { //~ ERROR irrefutable if-let
11-
| ^ irrefutable pattern
12+
= note: #[warn(irrefutable_let_patterns)] on by default
1213

13-
error[E0162]: irrefutable if-let pattern
14-
--> $DIR/if-let.rs:22:12
14+
warning: irrefutable if-let pattern
15+
--> $DIR/if-let.rs:6:13
1516
|
16-
LL | if let a = 1 { //~ ERROR irrefutable if-let
17-
| ^ irrefutable pattern
17+
LL | if let $p = $e $b
18+
| ^^
19+
...
20+
LL | / bar!(a, 1, { //~ WARN irrefutable if-let
21+
LL | | println!("irrefutable pattern");
22+
LL | | });
23+
| |_______- in this macro invocation
1824

19-
error[E0162]: irrefutable if-let pattern
20-
--> $DIR/if-let.rs:26:12
25+
warning: irrefutable if-let pattern
26+
--> $DIR/if-let.rs:24:5
2127
|
22-
LL | if let a = 1 { //~ ERROR irrefutable if-let
23-
| ^ irrefutable pattern
28+
LL | / if let a = 1 { //~ WARN irrefutable if-let
29+
LL | | println!("irrefutable pattern");
30+
LL | | }
31+
| |_____^
2432

25-
error[E0162]: irrefutable if-let pattern
26-
--> $DIR/if-let.rs:36:19
33+
warning: irrefutable if-let pattern
34+
--> $DIR/if-let.rs:28:5
2735
|
28-
LL | } else if let a = 1 { //~ ERROR irrefutable if-let
29-
| ^ irrefutable pattern
36+
LL | / if let a = 1 { //~ WARN irrefutable if-let
37+
LL | | println!("irrefutable pattern");
38+
LL | | } else if true {
39+
LL | | println!("else-if in irrefutable if-let");
40+
LL | | } else {
41+
LL | | println!("else in irrefutable if-let");
42+
LL | | }
43+
| |_____^
3044

31-
error[E0162]: irrefutable if-let pattern
32-
--> $DIR/if-let.rs:42:19
45+
warning: irrefutable if-let pattern
46+
--> $DIR/if-let.rs:38:12
3347
|
34-
LL | } else if let a = 1 { //~ ERROR irrefutable if-let
35-
| ^ irrefutable pattern
48+
LL | } else if let a = 1 { //~ WARN irrefutable if-let
49+
| ____________^
50+
LL | | println!("irrefutable pattern");
51+
LL | | }
52+
| |_____^
3653

37-
error: aborting due to 6 previous errors
54+
warning: irrefutable if-let pattern
55+
--> $DIR/if-let.rs:44:12
56+
|
57+
LL | } else if let a = 1 { //~ WARN irrefutable if-let
58+
| ____________^
59+
LL | | println!("irrefutable pattern");
60+
LL | | }
61+
| |_____^
3862

39-
For more information about this error, try `rustc --explain E0162`.

src/test/ui/issues/issue-51714.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -10,5 +10,5 @@ fn main() {
1010

1111
[(); return while let Some(n) = Some(0) {}];
1212
//~^ ERROR return statement outside of function body
13-
//~^^ ERROR irrefutable while-let pattern
13+
//~^^ WARN irrefutable while-let pattern
1414
}

src/test/ui/issues/issue-51714.stderr

+7-6
Original file line numberDiff line numberDiff line change
@@ -22,13 +22,14 @@ error[E0572]: return statement outside of function body
2222
LL | [(); return while let Some(n) = Some(0) {}];
2323
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
2424

25-
error[E0165]: irrefutable while-let pattern
26-
--> $DIR/issue-51714.rs:11:27
25+
warning: irrefutable while-let pattern
26+
--> $DIR/issue-51714.rs:11:17
2727
|
2828
LL | [(); return while let Some(n) = Some(0) {}];
29-
| ^^^^^^^ irrefutable pattern
29+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
30+
|
31+
= note: #[warn(irrefutable_let_patterns)] on by default
3032

31-
error: aborting due to 5 previous errors
33+
error: aborting due to 4 previous errors
3234

33-
Some errors occurred: E0165, E0572.
34-
For more information about an error, try `rustc --explain E0165`.
35+
For more information about this error, try `rustc --explain E0572`.

0 commit comments

Comments
 (0)