Skip to content

Commit 0dcf324

Browse files
committed
Require that a <_ as Try>::Residual implement Residual
The `Residual` trait was even more experimental than `Try`, but now that RFC3721 is merged, I think it would make sense to require this.
1 parent 23903d0 commit 0dcf324

5 files changed

Lines changed: 41 additions & 11 deletions

File tree

library/core/src/ops/try_trait.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -157,7 +157,7 @@ pub const trait Try: [const] FromResidual {
157157
/// type: that type will have a "hole" in the correct place, and will maintain the
158158
/// "foo-ness" of the residual so other types need to opt-in to interconversion.
159159
#[unstable(feature = "try_trait_v2", issue = "84277", old_name = "try_trait")]
160-
type Residual;
160+
type Residual: Residual<Self::Output>;
161161

162162
/// Constructs the type from its `Output` type.
163163
///

tests/ui/consts/const-try.rs

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,10 +6,11 @@
66

77
#![crate_type = "lib"]
88
#![feature(try_trait_v2)]
9+
#![feature(try_trait_v2_residual)]
910
#![feature(const_trait_impl)]
1011
#![feature(const_try)]
1112

12-
use std::ops::{ControlFlow, FromResidual, Try};
13+
use std::ops::{ControlFlow, FromResidual, Residual, Try};
1314

1415
struct TryMe;
1516
struct Error;
@@ -31,6 +32,10 @@ impl const Try for TryMe {
3132
}
3233
}
3334

35+
impl Residual<()> for Error {
36+
type TryType = TryMe;
37+
}
38+
3439
const fn t() -> TryMe {
3540
TryMe?;
3641
TryMe

tests/ui/traits/const-traits/ice-126148-failed-to-normalize.rs

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
#![allow(incomplete_features)]
2-
#![feature(const_trait_impl, try_trait_v2, const_try)]
3-
use std::ops::{FromResidual, Try};
2+
#![feature(const_trait_impl, try_trait_v2, try_trait_v2_residual, const_try, const_try_residual)]
3+
use std::ops::{FromResidual, Residual, Try};
44

55
struct TryMe;
66
struct Error;
@@ -14,6 +14,10 @@ impl const Try for TryMe {
1414
type Residual = Error;
1515
}
1616

17+
impl const Residual<()> for Error {
18+
type TryType = TryMe;
19+
}
20+
1721
const fn t() -> TryMe {
1822
TryMe?;
1923
TryMe

tests/ui/traits/const-traits/trait-default-body-stability.rs

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,11 +5,12 @@
55
#![feature(const_trait_impl)]
66
#![feature(const_t_try)]
77
#![feature(const_try)]
8+
#![feature(const_try_residual)]
89
#![feature(try_trait_v2)]
9-
10+
#![feature(try_trait_v2_residual)]
1011
#![stable(feature = "foo", since = "1.0")]
1112

12-
use std::ops::{ControlFlow, FromResidual, Try};
13+
use std::ops::{ControlFlow, FromResidual, Residual, Try};
1314

1415
#[stable(feature = "foo", since = "1.0")]
1516
pub struct T;
@@ -29,6 +30,12 @@ impl const Try for T {
2930
}
3031
}
3132

33+
#[stable(feature = "foo", since = "1.0")]
34+
#[rustc_const_unstable(feature = "const_t_try", issue = "none")]
35+
impl const Residual<T> for T {
36+
type TryType = T;
37+
}
38+
3239
#[stable(feature = "foo", since = "1.0")]
3340
#[rustc_const_unstable(feature = "const_t_try", issue = "none")]
3441
impl const FromResidual for T {

tests/ui/try-trait/try-operator-custom.rs

Lines changed: 19 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,13 @@
11
//@ run-pass
22

33
#![feature(try_trait_v2)]
4+
#![feature(try_trait_v2_residual)]
45

5-
use std::ops::{ControlFlow, FromResidual, Try};
6+
use std::ops::{ControlFlow, FromResidual, Residual, Try};
67

78
enum MyResult<T, U> {
89
Awesome(T),
9-
Terrible(U)
10+
Terrible(U),
1011
}
1112

1213
enum Never {}
@@ -27,25 +28,38 @@ impl<U, V> Try for MyResult<U, V> {
2728
}
2829
}
2930

30-
impl<U, V, W> FromResidual<MyResult<Never, V>> for MyResult<U, W> where V: Into<W> {
31+
impl<U, V, W> FromResidual<MyResult<Never, V>> for MyResult<U, W>
32+
where
33+
V: Into<W>,
34+
{
3135
fn from_residual(x: MyResult<Never, V>) -> Self {
3236
match x {
3337
MyResult::Terrible(e) => MyResult::Terrible(e.into()),
3438
}
3539
}
3640
}
3741

42+
impl<U, V> Residual<U> for MyResult<Never, V> {
43+
type TryType = MyResult<U, V>;
44+
}
45+
3846
type ResultResidual<E> = Result<std::convert::Infallible, E>;
3947

40-
impl<U, V, W> FromResidual<ResultResidual<V>> for MyResult<U, W> where V: Into<W> {
48+
impl<U, V, W> FromResidual<ResultResidual<V>> for MyResult<U, W>
49+
where
50+
V: Into<W>,
51+
{
4152
fn from_residual(x: ResultResidual<V>) -> Self {
4253
match x {
4354
Err(e) => MyResult::Terrible(e.into()),
4455
}
4556
}
4657
}
4758

48-
impl<U, V, W> FromResidual<MyResult<Never, V>> for Result<U, W> where V: Into<W> {
59+
impl<U, V, W> FromResidual<MyResult<Never, V>> for Result<U, W>
60+
where
61+
V: Into<W>,
62+
{
4963
fn from_residual(x: MyResult<Never, V>) -> Self {
5064
match x {
5165
MyResult::Terrible(e) => Err(e.into()),

0 commit comments

Comments
 (0)