Stabilize let else
by est31 · Pull Request #93628 · rust-lang/rust (original) (raw)
🎉 Stabilizes the let else
feature, added by RFC 3137. 🎉
Reference PR: rust-lang/reference#1156
closes #87335 (let else
tracking issue)
FCP: #93628 (comment)
Stabilization report
Summary
The feature allows refutable patterns in let
statements if the expression is
followed by a diverging else
:
fn get_count_item(s: &str) -> (u64, &str) { let mut it = s.split(' '); let (Some(count_str), Some(item)) = (it.next(), it.next()) else { panic!("Can't segment count item pair: '{s}'"); }; let Ok(count) = u64::from_str(count_str) else { panic!("Can't parse integer: '{count_str}'"); }; (count, item) } assert_eq!(get_count_item("3 chairs"), (3, "chairs"));
Differences from the RFC / Desugaring
Outside of desugaring I'm not aware of any differences between the implementation and the RFC. The chosen desugaring has been changed from the RFC's original. You can read a detailed discussion of the implementation history of it in @cormacrelf 's summary in this thread, as well as the followup. Since that followup, further changes have happened to the desugaring, in #98574, #99518, #99954. The later changes were mostly about the drop order: On match, temporaries drop in the same order as they would for a let
declaration. On mismatch, temporaries drop before the else
block.
Test cases
In chronological order as they were merged.
- ui/pattern/usefulness/top-level-alternation.rs to ensure the unreachable pattern lint visits patterns inside
let else
.
- ui/let-else/let-else-bool-binop-init.rs to ensure that no lazy boolean expressions (using
&&
or||
) are allowed in the expression, as the RFC mandates. - ui/let-else/let-else-brace-before-else.rs to ensure that no
}
directly preceding theelse
is allowed in the expression, as the RFC mandates. - ui/let-else/let-else-check.rs to ensure that
#[allow(...)]
attributes added to the entirelet
statement apply for theelse
block. - ui/let-else/let-else-irrefutable.rs to ensure that the
irrefutable_let_patterns
lint fires. - ui/let-else/let-else-missing-semicolon.rs to ensure the presence of semicolons at the end of the
let
statement. - ui/let-else/let-else-non-diverging.rs to ensure the
else
block diverges. - ui/let-else/let-else-run-pass.rs to ensure the feature works in some simple test case settings.
- ui/let-else/let-else-scope.rs to ensure the bindings created by the outer
let
expression are not available in theelse
block of it.
- ui/let-else/issue-89960.rs as a regression test for the ICE-on-error bug Erroneous 'ref mut' in let-else binding causes ICE #89960 . Later in 102b912 this got removed in favour of more comprehensive tests.
- ui/let-else/let-else-if.rs to test for the improved error message that points out that
let else if
is not possible.
Added by 9b45713:
- ui/let-else/let-else-allow-unused.rs as a regression test for let-else: linting for unused bindings is not handled correctly #89807, to ensure that
#[allow(...)]
attributes added to the entirelet
statement apply for bindings created by thelet else
pattern.
- ui/let-else/let-else-non-copy.rs to ensure that a copy is performed out of non-copy wrapper types. This mirrors
if let
behaviour. The test case bases on rustc internal changes originally meant for Adopt let_else across the compiler #89933 but then removed from the PR due to the error prior to the improvements of Implement let-else type annotations natively #89841. - ui/let-else/let-else-source-expr-nomove-pass.rs to ensure that while there is a move of the binding in the successful case, the
else
case can still access the non-matching value. This mirrorsif let
behaviour.
- ui/let-else/let-else-ref-bindings.rs and ui/let-else/let-else-ref-bindings-pass.rs to check
ref
andref mut
keywords in the pattern work correctly and error when needed.
- Match ergonomic tests adapted from the
rfc2005
test suite.
- ui/let-else/let-else-deref-coercion-annotated.rs and ui/let-else/let-else-deref-coercion.rs to check deref coercions.
Added since this stabilization report was originally written (2022-02-09)
- ui/let-else/let-else-destructuring.rs to give a nice error message if an user tries to do an assignment with a (possibly refutable) pattern and an
else
block, like asked for in destructuring assignment and let else #93995.
- ui/let-else/let-else-allow-in-expr.rs to test whether
#[allow(unused_variables)]
works in the expr, as well as its non presence, as well as putting it on the entirelet else
affects the expr, too. This was adding a missing test as pointed out by the stabilization report. - Expansion of
ui/let-else/let-else-allow-unused.rs
andui/let-else/let-else-check.rs
to ensure that non-presence of#[allow(unused)]
does issue the unused lint. This was adding a missing test case as pointed out by the stabilization report.
- ui/let-else/let-else-slicing-error.rs, a regression test for Wrong suggestion for slice patterns when using let-else statement #92069, which got fixed without addition of a regression test. This resolves a missing test as pointed out by the stabilization report.
- src/test/ui/async-await/async-await-let-else.rs to test the interaction of async/await with
let else
- src/test/ui/let-else/let-else-temporary-lifetime.rs as a (partial) regression test for Let-else does not drop temporaries at the end of the statement #98672
- src/test/ui/let-else/let-else-temp-borrowck.rs as a regression test for let_else returns in surprising borrowck errors with impl trait #93951
- Extension of
src/test/ui/let-else/let-else-temporary-lifetime.rs
to include a partial regression test for Let-else does not drop temporaries at the end of the statement #98672 (especially regardingelse
drop order)
- Extension of
src/test/ui/let-else/let-else-temporary-lifetime.rs
to include a partial regression test for let_else returns in surprising borrowck errors with impl trait #93951, similar tolet-else-temp-borrowck.rs
- Extension of
src/test/ui/let-else/let-else-temporary-lifetime.rs
to include a program that can now be compiled thanks to borrow checker implications of Let-else: break out scopes when a let-else pattern fails to match #99518
- src/test/ui/let-else/issue-100103.rs, as a regression test for ICE trying to use Err(1)? with let_else in a try block #100103, to ensure that there is no ICE when doing
Err(...)?
inside else blocks.
- src/test/ui/let-else/let-else-then-diverge.rs, to verify that there is no unreachable code error with the current desugaring.
- src/test/ui/let-else/issue-94176.rs, to make sure that a correct span is emitted for a missing trailing expression error. Regression test for Missing trailing expression in block with let else yields incorrectly spanned error message #94176.
- src/test/ui/unpretty/pretty-let-else.rs, as a regression test to ensure pretty printing works for
let else
(this bug surfaced in many different ways)
- src/test/ui/let-else/let-else-temporary-lifetime.rs extended to contain & borrows as well, as this was identified as an earlier issue with the desugaring: Let-else does not drop temporaries at the end of the statement #98672 (comment)
- src/test/ui/let-else/let-else-drop-order.rs a matrix based test for various drop order behaviour of
let else
. Especially, it verifies equality oflet
andlet else
drop orders, resolving a stabilization blocker.
- Edit to
src/test/ui/let-else/let-else-temporary-lifetime.rs
to add the-Zvalidate-mir
flag, as a regression test for ICE: broken mir with let-else, -Zvalidate-mir #99228
- src/test/ui/let-else/issue-99975.rs as a regression test for the ICE Internal Compiler Error: Broken MIR in Item(...) With let_else Usage #99975.
Added by this PR:
ui/let-else/let-else.rs
, a simple run-pass check, similar toui/let-else/let-else-run-pass.rs
.
Things not currently tested
The→ test added by e7730dc#[allow(...)]
tests check whether allow works, but they don't check whether the non-presence of allow causes a lint to fire.There is no→ test added by e7730dc#[allow(...)]
test for the expression, as there are tests for the pattern and the else block.→ test added by e7730dclet-else-brace-before-else.rs
forbids thelet ... = {} else {}
pattern and there is a rustfix to obtainlet ... = ({}) else {}
. I'm not sure whether the.fixed
files are checked by the tooling that they compile. But if there is no such check, it would be neat to make sure thatlet ... = ({}) else {}
compiles.Wrong suggestion for slice patterns when using let-else statement #92069 got closed as fixed, but no regression test was added. Not sure it's worth to add one.→ test added by 5bd7106consistency between→ test added by 2d8460elet else
andif let
regarding lifetimes and drop order: Stabilize let else #93628 (comment)
Edit: they are all tested now.
Possible future work / Refutable destructuring assignments
RFC 2909 specifies destructuring assignment, allowing statements like FooBar { a, b, c } = foo();
.
As it was stabilized, destructuring assignment only allows irrefutable patterns, which before the advent of let else
were the only patterns that let
supported.
So the combination of let else
and destructuring assignments gives reason to think about extensions of the destructuring assignments feature that allow refutable patterns, discussed in #93995.
A naive mapping of let else
to destructuring assignments in the form of Some(v) = foo() else { ... };
might not be the ideal way. let else
needs a diverging else
clause as it introduces new bindings, while assignments have a default behaviour to fall back to if the pattern does not match, in the form of not performing the assignment. Thus, there is no good case to require divergence, or even an else
clause at all, beyond the need for having some introducer syntax so that it is clear to readers that the assignment is not a given (enums and structs look similar). There are better candidates for introducer syntax however than an empty else {}
clause, like maybe
which could be added as a keyword on an edition boundary:
let mut v = 0; maybe Some(v) = foo(&v); maybe Some(v) = foo(&v) else { bar() };
Further design discussion is left to an RFC, or the linked issue.