Skip to content

Stabilize feature result_flattening #141072

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 0 additions & 1 deletion compiler/rustc_driver_impl/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@
#![feature(decl_macro)]
#![feature(panic_backtrace_config)]
#![feature(panic_update_hook)]
#![feature(result_flattening)]
#![feature(rustdoc_internals)]
#![feature(try_blocks)]
// tidy-alphabetical-end
Expand Down
7 changes: 3 additions & 4 deletions library/core/src/result.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1692,7 +1692,6 @@ impl<T, E> Result<Result<T, E>, E> {
/// # Examples
///
/// ```
/// #![feature(result_flattening)]
/// let x: Result<Result<&'static str, u32>, u32> = Ok(Ok("hello"));
/// assert_eq!(Ok("hello"), x.flatten());
///
Expand All @@ -1706,14 +1705,14 @@ impl<T, E> Result<Result<T, E>, E> {
/// Flattening only removes one level of nesting at a time:
///
/// ```
/// #![feature(result_flattening)]
/// let x: Result<Result<Result<&'static str, u32>, u32>, u32> = Ok(Ok(Ok("hello")));
/// assert_eq!(Ok(Ok("hello")), x.flatten());
/// assert_eq!(Ok("hello"), x.flatten().flatten());
/// ```
#[inline]
#[unstable(feature = "result_flattening", issue = "70142")]
#[rustc_const_unstable(feature = "result_flattening", issue = "70142")]
#[stable(feature = "result_flattening", since = "CURRENT_RUSTC_VERSION")]
#[rustc_allow_const_fn_unstable(const_precise_live_drops)]
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Uses of this attribute should always be called out in the PR as they require special attention. However, this one should be fine, for the same reason as all the other rustc_allow_const_fn_unstable(const_precise_live_drops): if we change how const_precise_live_drops works, we'll ensure it keeps accepting these cases.

Cc @rust-lang/wg-const-eval

#[rustc_const_stable(feature = "result_flattening", since = "CURRENT_RUSTC_VERSION")]
pub const fn flatten(self) -> Result<T, E> {
// FIXME(const-hack): could be written with `and_then`
match self {
Expand Down
2 changes: 1 addition & 1 deletion src/tools/clippy/tests/ui/map_flatten.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
#![warn(clippy::map_flatten)]
#![feature(result_flattening)]

//@no-rustfix
// issue #8506, multi-line
#[rustfmt::skip]
Expand Down
1 change: 0 additions & 1 deletion src/tools/clippy/tests/ui/map_flatten_fixable.fixed
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
#![feature(result_flattening)]
#![allow(
clippy::let_underscore_untyped,
clippy::missing_docs_in_private_items,
Expand Down
1 change: 0 additions & 1 deletion src/tools/clippy/tests/ui/map_flatten_fixable.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
#![feature(result_flattening)]
#![allow(
clippy::let_underscore_untyped,
clippy::missing_docs_in_private_items,
Expand Down
18 changes: 9 additions & 9 deletions src/tools/clippy/tests/ui/map_flatten_fixable.stderr
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
error: called `map(..).flatten()` on `Iterator`
--> tests/ui/map_flatten_fixable.rs:17:47
--> tests/ui/map_flatten_fixable.rs:16:47
|
LL | let _: Vec<_> = vec![5_i8; 6].into_iter().map(option_id).flatten().collect();
| ^^^^^^^^^^^^^^^^^^^^^^^^ help: try replacing `map` with `filter_map` and remove the `.flatten()`: `filter_map(option_id)`
Expand All @@ -8,43 +8,43 @@ LL | let _: Vec<_> = vec![5_i8; 6].into_iter().map(option_id).flatten().coll
= help: to override `-D warnings` add `#[allow(clippy::map_flatten)]`

error: called `map(..).flatten()` on `Iterator`
--> tests/ui/map_flatten_fixable.rs:19:47
--> tests/ui/map_flatten_fixable.rs:18:47
|
LL | let _: Vec<_> = vec![5_i8; 6].into_iter().map(option_id_ref).flatten().collect();
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: try replacing `map` with `filter_map` and remove the `.flatten()`: `filter_map(option_id_ref)`

error: called `map(..).flatten()` on `Iterator`
--> tests/ui/map_flatten_fixable.rs:21:47
--> tests/ui/map_flatten_fixable.rs:20:47
|
LL | let _: Vec<_> = vec![5_i8; 6].into_iter().map(option_id_closure).flatten().collect();
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: try replacing `map` with `filter_map` and remove the `.flatten()`: `filter_map(option_id_closure)`

error: called `map(..).flatten()` on `Iterator`
--> tests/ui/map_flatten_fixable.rs:23:47
--> tests/ui/map_flatten_fixable.rs:22:47
|
LL | let _: Vec<_> = vec![5_i8; 6].into_iter().map(|x| x.checked_add(1)).flatten().collect();
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: try replacing `map` with `filter_map` and remove the `.flatten()`: `filter_map(|x| x.checked_add(1))`

error: called `map(..).flatten()` on `Iterator`
--> tests/ui/map_flatten_fixable.rs:27:47
--> tests/ui/map_flatten_fixable.rs:26:47
|
LL | let _: Vec<_> = vec![5_i8; 6].into_iter().map(|x| 0..x).flatten().collect();
| ^^^^^^^^^^^^^^^^^^^^^^^ help: try replacing `map` with `flat_map` and remove the `.flatten()`: `flat_map(|x| 0..x)`

error: called `map(..).flatten()` on `Option`
--> tests/ui/map_flatten_fixable.rs:31:40
--> tests/ui/map_flatten_fixable.rs:30:40
|
LL | let _: Option<_> = (Some(Some(1))).map(|x| x).flatten();
| ^^^^^^^^^^^^^^^^^^^^ help: try replacing `map` with `and_then` and remove the `.flatten()`: `and_then(|x| x)`

error: called `map(..).flatten()` on `Result`
--> tests/ui/map_flatten_fixable.rs:35:42
--> tests/ui/map_flatten_fixable.rs:34:42
|
LL | let _: Result<_, &str> = (Ok(Ok(1))).map(|x| x).flatten();
| ^^^^^^^^^^^^^^^^^^^^ help: try replacing `map` with `and_then` and remove the `.flatten()`: `and_then(|x| x)`

error: called `map(..).flatten()` on `Iterator`
--> tests/ui/map_flatten_fixable.rs:45:10
--> tests/ui/map_flatten_fixable.rs:44:10
|
LL | .map(|n| match n {
| __________^
Expand Down Expand Up @@ -74,7 +74,7 @@ LL ~ });
|

error: called `map(..).flatten()` on `Option`
--> tests/ui/map_flatten_fixable.rs:66:10
--> tests/ui/map_flatten_fixable.rs:65:10
|
LL | .map(|_| {
| __________^
Expand Down
Loading