Join GitHub today
GitHub is home to over 28 million developers working together to host and review code, manage projects, and build software together.
Sign upTracking issue for `?` operator and `try` blocks (RFC 243, `question_mark` & `try_blocks` features) #31436
Comments
nikomatsakis
added
B-RFC-approved
T-lang
A-const-fn
labels
Feb 5, 2016
nikomatsakis
referenced this issue
Feb 5, 2016
Merged
First-class error handling with `?` and `catch` #243
This comment has been minimized.
This comment has been minimized.
|
The accompanying RFC discusses a desugaring based on labeled return/break, are we getting that too or will there just be special treatment for EDIT: I think labeled return/break is an excellent idea separate from |
This comment has been minimized.
This comment has been minimized.
|
Labeled return/break is purely for explanatory purposes. On Fri, Feb 5, 2016 at 3:56 PM, Jonathan Reem notifications@github.com
|
This comment has been minimized.
This comment has been minimized.
|
Another unresolved question we have to resolve before stabilizing is what the contract which |
This comment has been minimized.
This comment has been minimized.
Please do! |
This comment has been minimized.
This comment has been minimized.
|
On the subject of the |
This comment has been minimized.
This comment has been minimized.
|
How this is treated during parsing?
|
This comment has been minimized.
This comment has been minimized.
|
@petrochenkov Well, the definition couldn't affect parsing, but I think we still have a lookahead rule, based on the second token after |
This comment has been minimized.
This comment has been minimized.
|
Also
+ rust-lang/rfcs#306 if (when!) implemented. Given the examples above I'm for the simplest solution (as usual) - always treat |
nikomatsakis
removed
the
A-const-fn
label
Feb 6, 2016
This comment has been minimized.
This comment has been minimized.
|
It would be easier if a keyword was used instead of |
This comment has been minimized.
This comment has been minimized.
|
This is the keywords list: http://doc.rust-lang.org/nightly/grammar.html#keywords |
This comment has been minimized.
This comment has been minimized.
|
@bluss yeah, I admit none of them are great... |
This comment has been minimized.
This comment has been minimized.
|
|
This comment has been minimized.
This comment has been minimized.
Can't
You will get a panic stack trace starting from the first occurrence of But when debugging foreign libraries written by people who haven't implemented that trick, one relies on It would be cool if overriding Later on when the macro system gets more features you can even panic! only for specific types. If this proposal requires an RFC please let me know. |
This comment has been minimized.
This comment has been minimized.
|
Ideally |
This comment has been minimized.
This comment has been minimized.
|
Stack traces are just one example (though a very useful one, seems to me). On Sun, Feb 7, 2016 at 4:14 PM, Russell Johnston notifications@github.com
|
This comment has been minimized.
This comment has been minimized.
|
Without wanting to speculate, I think that it could work, albeit with some issues. Consider the usual case where one has code returning some This would require everybody, even those who don't want to debug, to import their wished trait implementation when using Possibly E0117 can be an issue too if wanting to do custom Having the possibility to override via a macro would provide a greater flexibility without the additional burden on the original implementor (they don't have to import their wished implementation). But I also see that rust never had a macro based operator before, and that implementing I am okay with any setup which enables one to get |
This comment has been minimized.
This comment has been minimized.
|
Just to chime in on bikeshedding: |
This comment has been minimized.
This comment has been minimized.
|
|
This comment has been minimized.
This comment has been minimized.
|
Also, not that I expect this to change anything, but a note that this is going to break multi-arm macros that were accepting |
This comment has been minimized.
This comment has been minimized.
|
Don't overdo the backwards compatibility, just treat I can also imagine some possible problems that don't involve struct literals (e.g. Didn't we have a for adding new keywords, anyways? We could offer some kind of |
This comment has been minimized.
This comment has been minimized.
|
I agree. This isn't even the first RFC where this has come up (rust-lang/rfcs#1444 is another example). I expect it won't be the last. (Also |
This comment has been minimized.
This comment has been minimized.
|
Wasn't the whole argument for not reserving several keywords prior to 1.0 that we'd definitely be introducing a way to add new keywords to the language backward compatibly (possibly by explicitly opting in), so there was no point? Seems like now would be a good time. |
This comment has been minimized.
This comment has been minimized.
|
@japaric Are you interested in reviving your old PR and taking this on? |
This comment has been minimized.
This comment has been minimized.
|
@aturon My implementation simply desugared |
This comment has been minimized.
This comment has been minimized.
|
@japaric What was the reason for restricting it to methods and function calls? Wouldn't parsing it be easier as a general postfix operator? |
added a commit
that referenced
this issue
Jul 28, 2018
added a commit
to Mark-Simulacrum/rust
that referenced
this issue
Aug 22, 2018
added a commit
that referenced
this issue
Aug 23, 2018
added a commit
that referenced
this issue
Aug 23, 2018
added a commit
that referenced
this issue
Aug 23, 2018
Centril
referenced this issue
Sep 23, 2018
Open
Strange behavior decl_macro + try_blocks + type_ascription #54455
scottmcm
changed the title
Tracking issue for `?` operator and `catch` expressions (RFC 243, `question_mark` feature)
Tracking issue for `?` operator and `try` blocks (RFC 243, `question_mark` & `try_blocks` features)
Sep 30, 2018
This comment has been minimized.
This comment has been minimized.
earthengine
commented
Oct 3, 2018
|
As all check boxes are ticked in the top post, when would us move forward? If there are still unresolved issues we need to have new check box(es) added. |
This comment has been minimized.
This comment has been minimized.
|
There are probably plenty of remaining unresolved questions not recorded. |
This comment has been minimized.
This comment has been minimized.
|
Hmm... it bothers me that these questions haven't been recorded. |
This comment has been minimized.
This comment has been minimized.
|
@mark-i-m So to clarify, I think they have been somewhere; but not in one location; it's a bit scattered atm in various RFCs and issues and such, so what we need to do is record them in the proper locations. |
This comment has been minimized.
This comment has been minimized.
|
The design of the backing trait is tracked in #42327; there's extensive discussion there about weaknesses in the current one and a possible new direction. (I'm planning of making a pre-RFC for a change there once 2018 settles a bit.) So I think only I'll add a checkbox for the one pending implementation task I know still needs to be done... |
This comment has been minimized.
This comment has been minimized.
|
@scottmcm I know @joshtriplett had concerns about OK-wrapping (noted in the |
This comment has been minimized.
This comment has been minimized.
earthengine
commented
Oct 3, 2018
•
Right now, you cannot use So my proposal is the code you shown should be allowed, and it should break the The immediate benefit, is when you see |
This comment has been minimized.
This comment has been minimized.
|
@Centril Thank you for raising those. Regarding As for |
This comment has been minimized.
This comment has been minimized.
|
@Centril Yes, I'm aware. But it's important to remember that that's re- re- raising the issue. The RFC decided to have it, it was implemented without it, but then the original intent was taken again, and the implementation changed to follow the RFC. So my big question is whether any material facts have changed, especially given that this is one of the noisiest topics I've ever seen discussed on RFCs+IRLO. |
added a commit
to bmisiak/capnproto-rust
that referenced
this issue
Oct 6, 2018
bmisiak
referenced this issue
Oct 6, 2018
Merged
Switch from try!() to ? in generated code to make it work in Rust 2018 #109
added a commit
to rust-lang/book
that referenced
this issue
Oct 23, 2018
This comment has been minimized.
This comment has been minimized.
|
@scottmcm Of course, as you know, I agree with retaining |
This comment has been minimized.
This comment has been minimized.
|
I just wanted to comment on this, not sure if this is the right thing: Essentially, a situation I have is callbacks in a GUI framework - instead of returning an let thing = match fs::read(path) {
Ok(o) => o,
Err(_) => return UpdateScreen::DontRedraw,
};Since I can't convert from a fn callback(data: &mut State) -> UpdateScreen {
fn callback_inner(data: &mut State) -> Option<()> {
let file_contents = fs::read_to_string(data.path).ok()?;
data.loaded_file = Some(file_contents);
Some(())
}
callback_inner(data).into()
}Since the callback is used as a function pointer, I can't use an impl<T> Try<Result<T>> for UpdateScreen {
fn try(original: Result<T>) -> Try<T, UpdateScreen> {
match original {
Ok(o) => Try::DontReturn(o),
Err(_) => Try::Return(UpdateScreen::DontRedraw),
}
}
}
fn callback(data: &mut State) -> UpdateScreen {
// On any Result::Err, convert to an UpdateScreeen::DontRedraw and return
let file_contents = fs::read_to_string(data.path)?;
data.loaded_file = Some(file_contents);
UpdateScreen::Redraw
}I am not sure if this would be possible with the current proposal and just wanted to add my use-case for consideration. It would be great if a custom Try operator could support something like this. |
This comment was marked as resolved.
This comment was marked as resolved.
|
EDIT: Ignore this post Could this play better with type inference, it fails even in simple cases. fn test_try(a: u32, b: u32) {
let div = if b != 0 {
Some(a / b)
} else {
None
};
let x // : Option<_> // why is this type annotation necessary
= try { div? + 1 };
println!("{:?}", x);
}If this is re-written to use a closure instead of the try block (and in the process loose auto wrapping), then we get fn test_closure(a: u32, b: u32) {
let div = if b != 0 {
Some(a / b)
} else {
None
};
let x = (|| (div? + 1).into())();
println!("{:?}", x);
}Which doesn't require a type annotation, but it does require that we wrap the result. |
This comment was marked as resolved.
This comment was marked as resolved.
|
@KrishnaSannasi your closure based example has a type inference failure as well (playground) because This seems to mostly be an issue with the |
This comment was marked as resolved.
This comment was marked as resolved.
|
Yes, I did a last minute change to my code, to make it use into, and didn't test that. My bad. |
nikomatsakis commentedFeb 5, 2016
•
edited by scottmcm
Tracking issue for rust-lang/rfcs#243 and rust-lang/rfcs#1859.
Implementation concerns:
?operator that is roughly equivalent totry!- #31954catch { ... }expression - #39849do catch { ... }syntax questiontry { .. }, - #50412Trytrait (rust-lang/rfcs#1859)Trytrait (in place ofCarrier) and convert?to use it (#42275)Optionand so forth, and a suitable family of tests (#42526)tryin new editiontry{}catch(or other following idents) to leave design space open for the future, and point people to how to do what they want withmatchinstead