Skip to content

Commit afcb938

Browse files
varkorNokel81
andcommitted
Stabilise irrefutable if-let and while-let patterns
This stabilises RFC 2086 (#44495). Co-Authored-By: Sebastian Malton <[email protected]>
1 parent b439861 commit afcb938

30 files changed

+175
-239
lines changed

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

Lines changed: 0 additions & 28 deletions
This file was deleted.

src/librustc/lint/builtin.rs

Lines changed: 1 addition & 1 deletion
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

Lines changed: 6 additions & 2 deletions
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

Lines changed: 12 additions & 37 deletions
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

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -414,9 +414,6 @@ declare_features! (
414414
// `#[doc(alias = "...")]`
415415
(active, doc_alias, "1.27.0", Some(50146), None),
416416

417-
// Allows irrefutable patterns in `if let` and `while let` statements (RFC 2086).
418-
(active, irrefutable_let_patterns, "1.27.0", Some(44495), None),
419-
420417
// inconsistent bounds in where clauses
421418
(active, trivial_bounds, "1.28.0", Some(48214), None),
422419

@@ -684,6 +681,8 @@ declare_features! (
684681
(accepted, underscore_imports, "1.33.0", Some(48216), None),
685682
// Allows `#[repr(packed(N))]` attribute on structs.
686683
(accepted, repr_packed, "1.33.0", Some(33158), None),
684+
// Allows irrefutable patterns in `if let` and `while let` statements (RFC 2086).
685+
(accepted, irrefutable_let_patterns, "1.33.0", Some(44495), None),
687686
// Allows calling `const unsafe fn` inside `unsafe` blocks in `const fn` functions.
688687
(accepted, min_const_unsafe_fn, "1.33.0", Some(55607), None),
689688
// `#[cfg_attr(predicate, multiple, attributes, here)]`

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

Lines changed: 0 additions & 12 deletions
This file was deleted.

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

Lines changed: 0 additions & 8 deletions
This file was deleted.

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

Lines changed: 0 additions & 9 deletions
This file was deleted.

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

Lines changed: 0 additions & 9 deletions
This file was deleted.

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

Lines changed: 0 additions & 9 deletions
This file was deleted.

0 commit comments

Comments
 (0)