-
Notifications
You must be signed in to change notification settings - Fork 12.8k
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
Add ok-wrapping to catch blocks, per RFC #49371
Conversation
src/librustc/hir/lowering.rs
Outdated
id: block.id, // FIXME! | ||
span: block.span, // FIXME? | ||
node: hir::ExprTup(hir_vec![]), | ||
attrs: ThinVec::new(), // FIXME? |
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 seems ok; this has to do with #[foo]
attributes
src/librustc/hir/lowering.rs
Outdated
let mut block = this.lower_block(body, true).into_inner(); | ||
let tail = block.expr.take().map_or_else( | ||
|| hir::Expr { | ||
id: block.id, // FIXME! |
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 you want to call this.next_id()
here
src/librustc/hir/lowering.rs
Outdated
let tail = block.expr.take().map_or_else( | ||
|| hir::Expr { | ||
id: block.id, // FIXME! | ||
span: block.span, // FIXME? |
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 would be where we report errors regarding this ()
expression; the block seems ok, but we might want to .. just use the closing brace }
?
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.
we probably also want to give it the "desugaring" span
let _: io::Result<()> = do catch { | ||
// `do catch` changed to IIFE for bootstrapping; consider changing back | ||
// when we get a new stage0 compiler | ||
let _: io::Result<()> = (||{ |
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.
How about we make a macro for this instead?
#[cfg(stage0)]
macro_rules do_catch {
($t:expr) = { (|| $t)() }
}
#[cfg(not(stage0))]
macro_rules do_catch {
($t:expr) = { do catch { $t } }
}
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.
And this way it can have ok-wrapping! 👍
src/librustc_mir/util/pretty.rs
Outdated
let _: io::Result<()> = do catch { | ||
// `do catch` changed to IIFE for bootstrapping; consider changing back | ||
// when we get a new stage0 compiler | ||
let _: io::Result<()> = (||{ |
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.
macro
src/librustc_mir/util/pretty.rs
Outdated
let _: io::Result<()> = do catch { | ||
// `do catch` changed to IIFE for bootstrapping; consider changing | ||
// back when we get a new stage0 compiler | ||
let _: io::Result<()> = (||{ |
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.
macro!
first round of comments =) sorry for the delay. Cursed All Hands! |
Ok, it looks like this is working now. Thanks for the pointers! I added a UI test so you can see how the spans come out in type errors; if you would like them to be different please let me know how to do that -- this is my first time in this end of the compiler 🙂 |
Your PR failed on Travis. Through arcane magic we have determined that the following fragments from the build log may contain information about the problem. Click to expand the log.
I'm a bot! I can only do what humans tell me to, so if this was not helpful or you have suggestions for improvements, please ping or otherwise contact |
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.
Left some thoughts about span
@@ -15,16 +15,16 @@ expression creates a new scope one can use the `?` operator in. | |||
use std::num::ParseIntError; | |||
|
|||
let result: Result<i32, ParseIntError> = do catch { | |||
Ok("1".parse::<i32>()? | |||
"1".parse::<i32>()? |
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.
huzzah
}; | ||
assert_eq!(cfg_init_2, 6); | ||
|
||
let my_string = "test".to_string(); | ||
let res: Result<&str, ()> = do catch { | ||
Ok(&my_string) | ||
// Unfortunately, deref doesn't fire here (#49356) | ||
&my_string[..] |
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.
interesting.
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, I filed #49356 about this. (Edit: Which I put in the comment; duh.) It feels to me like it ought to work, since the type needed here is completely determined by the context.
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 can imagine why it fails, we might be able to improve it.
LL | | //~^ ERROR type mismatch | ||
LL | | foo()?; | ||
LL | | }; | ||
| |_____^ expected i32, found () |
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.
Hmm, this is ungreat. Not sure what span would be best, maybe @estebank has thoughts. I think maybe just pointing to the end-brace would be good?:
error[E0271]: type mismatch resolving `<std::option::Option<i32> as std::ops::Try>::Ok == ()`
--> $DIR/catch-block-type-error.rs:22:35
|
LL | };
| ^ expected i32, found ()
|
= note: expected type `i32`
found type `()`
Also, the "type mismatch" line is kind of a mess, but that's separate I guess and maybe hard to fix -- we might be able to use the "extra info" from the span to help.
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.
@scottmcm If you like how this looks I can give you a tip for how to achieve it =)
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.
In general I think multi-line spans are to be avoided at basically any cost.
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.
That seems plausible, though note that the multi-line span happens today for blocks:
error[E0308]: mismatched types
--> src/main.rs:2:19
|
2 | let x: i32 = {
| ___________________^
3 | | println!("asdf");
4 | | 3;
5 | | println!("asdf");
6 | | };
| |_____^ expected i32, found ()
|
= note: expected type `i32`
found type `()`
I haven't looked into span futzing yet, so I don't know my unknowns. A few quick pointers would be great, or if you wanted to use me as a tester for a rustc book page...
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.
note that the multi-line span happens today for blocks
yes, but that doesn't make it good =)
I haven't looked into span futzing yet, so I don't know my unknowns
I believe that the end_point
function is what you want
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.
Thanks! That did the trick. And huzzah for hosted compiler docs 🎉
0757abb
to
9911de8
Compare
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.
very nice
@bors r+ |
📌 Commit 9911de8 has been approved by |
☔ The latest upstream changes (presumably #48914) made this pull request unmergeable. Please resolve the merge conflicts. |
9911de8
to
311ff5b
Compare
Rebased; please re-r+. |
@bors r+ |
📌 Commit 311ff5b has been approved by |
@bors delegate=scottmcm (For future rebases) |
✌️ @scottmcm can now approve this pull request |
Add ok-wrapping to catch blocks, per RFC Updates the `catch{}` lowering to wrap the result in `Try::from_ok`. r? @nikomatsakis Fixes #41414 Fixes #43818
☀️ Test successful - status-appveyor, status-travis |
Updates the
catch{}
lowering to wrap the result inTry::from_ok
.r? @nikomatsakis
Fixes #41414
Fixes #43818