-
Notifications
You must be signed in to change notification settings - Fork 12.7k
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
Use a Carrier trait with the ?
operator
#33389
Conversation
(rust_highfive has picked a reviewer for you, use r? to override) |
The big thing for me isn't Result <-> Option -- I'd like to experiment with some kind of DebugResult that builds up a backtrace as it passes through question marks. |
@@ -2155,3 +2157,126 @@ pub trait BoxPlace<Data: ?Sized> : Place<Data> { | |||
/// Creates a globally fresh place. | |||
fn make_place() -> Self; | |||
} | |||
|
|||
/// A trait for types which have success and error states and are meant to work | |||
/// with the quiestion mark operator. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
quiestion --> question
} | ||
|
||
fn g(x: i32) -> MyResult<i32, String> { | ||
let _y = f(x)?; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
this was supposed to be x-1
(or on line 46)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
it terminates with the given input, I don't care too much about it being a sensible program
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yeah, it terminates with a stack overflow...
On May 4, 2016 4:49 PM, "Nick Cameron" notifications@github.com wrote:
In src/test/run-pass/try-operator-custom.rs
#33389 (comment):
MyResult::Terrible(e) => T::from_error(e.into()),
}
- }
+}
+fn f(x: i32) -> Result<i32, String> {
- if x == 0 {
Ok(42)
- } else {
let y = g(x)?;
Ok(y)
- }
+}
+fn g(x: i32) -> MyResult<i32, String> {
- let _y = f(x)?;
it terminates with the given input, I don't care too much about it being a
sensible program—
You are receiving this because you commented.
Reply to this email directly or view it on GitHub
https://github.com/rust-lang/rust/pull/33389/files/0de7b23ac739307c71249116d106c5d074f30b23#r62111982
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The best kind of termination!
Thanks, this is fixed now.
e89b9e4
to
79a1f5c
Compare
where T: Carrier<Success=U, Error=V> | ||
{ | ||
match self { | ||
Ok(u) => T::from_success(u.into()), |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It seems to me that these into
s are useless: they're always "converting" from U
to U
, and V
to V
. Maybe the signature of translate
wants to be something like:
fn translate<T>(self)
where T: Carrier<Success = Self::Success>,
Self::Error: Into<T::Error>,
(The thinking being converting the main value implicitly is not nearly as desirable as converting the error one.)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think the signature is OK, but the into
s shouldn't be there. I started with a more flexible approach, but decided it wasn't necessary. I think the into
s are left over from that. Not really sure why it even compiles.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Makes sense.
Not really sure why it even compiles.
T: Into<T>
for all (sized) T
:
Lines 231 to 243 in 0e7cb8b
// From implies Into | |
#[stable(feature = "rust1", since = "1.0.0")] | |
impl<T, U> Into<U> for T where U: From<T> { | |
fn into(self) -> U { | |
U::from(self) | |
} | |
} | |
// From (and thus Into) is reflexive | |
#[stable(feature = "rust1", since = "1.0.0")] | |
impl<T> From<T> for T { | |
fn from(t: T) -> T { t } | |
} |
☔ The latest upstream changes (presumably #33443) made this pull request unmergeable. Please resolve the merge conflicts. |
FWIW newer versions of the RFC called this |
Crater run results: https://gist.github.com/nikomatsakis/eff9c3cb0d79e95b073283369ab37b01
|
The 1 regression appears to be a timeout (false positive). |
The language team decided that we should accept this PR, on the understanding that it is for experimentation and will have a period of discussion and examination before FCP. This discuss thread has more details. |
Allows use with `Option` and custom `Result`-like types.
ping @alexcrichton see comment above from the lang team, also rebased. |
This does not seem to change Otherwise, +1 :-) I hope this goes in. |
/// The type of the value when computation succeeds. | ||
type Success; | ||
/// The type of the value when computation errors out. | ||
type Error; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Perhaps EarlyReturn
could be a better name than Error
, as it more closely resembles what actually happens? Returning early does not have to be an error.
☔ The latest upstream changes (presumably #33654) made this pull request unmergeable. Please resolve the merge conflicts. |
Sorry for the delayed writeup. The libs team also talked about this PR, and a number of team members felt uncomfortable merging based on the accepted RFC, strongly preferring to amend the RFC with an explicit route for experimentation, especially given the contentious nature of the original RFC. Personally, I can see arguments on both sides, and I think the high-order bit here is that the broader community needs some clear opportunity to weigh in -- whether via an RFC amendment or the new discuss threads. Given that the two teams reached different conclusions, I propose escalating this decision to the core team, and am tagging accordingly. We'll be meeting today. |
We discussed the situation in the core team meeting, and the TL;DR is that we believe an amendment RFC needs to happen before this PR is landed. While not everyone agreed that it was strictly necessary, it is the conservative choice, which seems prudent given how contentious the original RFC was. (Sorry @nrc!) The core issue is that the original RFC did not lay out specific plans for expansion here, and while the lang team did say that we would "revisit" the question prior to stabilization, there were no details about that process. Those opposed to this generalization did not push hard on that point in the original RFC because it was taken off the table. And at least some people don't consider this generalization to be something that needs experimental evidence to decide, but rather is a question of mental model for the feature. In the future, any "planned expansions" should be clearly laid out in the RFC and made part of the tracking issue. It's unclear whether the amendment RFC should await some consensus on the discuss thread, or should be started now to drive traffic in that direction. I'll cross-post there for the time being. |
@nrc ping (just cleaning out some old PRs) Should we close this for now? |
Yeah, I guess so, we're not going to merge it without an RFC, so may as well close in the meantime |
Allows use with
Option
and customResult
-like types.Note that this part of RFC 243 did not get approved, so this PR is just for experimentation purposes.
This supports conversion between result types when they have the same number of type args, but not otherwise (e.g,
Result
toOption
), that requires #33108Part of #31436