Join GitHub today
GitHub is home to over 31 million developers working together to host and review code, manage projects, and build software together.
Sign upAdd ok-wrapping to catch blocks, per RFC #49371
Conversation
scottmcm
assigned
nikomatsakis
Mar 26, 2018
pietroalbini
added
the
S-waiting-on-review
label
Mar 26, 2018
kennytm
added
the
E-needs-mentor
label
Mar 29, 2018
nikomatsakis
reviewed
Apr 2, 2018
| id: block.id, // FIXME! | ||
| span: block.span, // FIXME? | ||
| node: hir::ExprTup(hir_vec![]), | ||
| attrs: ThinVec::new(), // FIXME? |
This comment has been minimized.
This comment has been minimized.
| let mut block = this.lower_block(body, true).into_inner(); | ||
| let tail = block.expr.take().map_or_else( | ||
| || hir::Expr { | ||
| id: block.id, // FIXME! |
This comment has been minimized.
This comment has been minimized.
| let tail = block.expr.take().map_or_else( | ||
| || hir::Expr { | ||
| id: block.id, // FIXME! | ||
| span: block.span, // FIXME? |
This comment has been minimized.
This comment has been minimized.
nikomatsakis
Apr 2, 2018
Contributor
this would be where we report errors regarding this () expression; the block seems ok, but we might want to .. just use the closing brace }?
This comment has been minimized.
This comment has been minimized.
| 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<()> = (||{ |
This comment has been minimized.
This comment has been minimized.
nikomatsakis
Apr 2, 2018
Contributor
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 } }
}
This comment has been minimized.
This comment has been minimized.
| 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<()> = (||{ |
This comment has been minimized.
This comment has been minimized.
| 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<()> = (||{ |
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
|
first round of comments =) sorry for the delay. Cursed All Hands! |
scottmcm
force-pushed the
scottmcm:catch-wrapping
branch
from
b9ea258
to
a957c61
Apr 3, 2018
scottmcm
changed the title
[Assistance Requested] Add ok-wrapping to catch blocks, per RFC
Add ok-wrapping to catch blocks, per RFC
Apr 3, 2018
scottmcm
force-pushed the
scottmcm:catch-wrapping
branch
from
7923aba
to
316a16f
Apr 3, 2018
This comment has been minimized.
This comment has been minimized.
|
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 |
This comment has been minimized.
This comment has been minimized.
|
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 |
rust-lang
deleted a comment from
TimNN
Apr 3, 2018
nikomatsakis
requested changes
Apr 4, 2018
|
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>()? | |||
This comment has been minimized.
This comment has been minimized.
| }; | ||
| 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[..] |
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
scottmcm
Apr 4, 2018
•
Author
Member
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.
This comment has been minimized.
This comment has been minimized.
| LL | | //~^ ERROR type mismatch | ||
| LL | | foo()?; | ||
| LL | | }; | ||
| | |_____^ expected i32, found () |
This comment has been minimized.
This comment has been minimized.
nikomatsakis
Apr 4, 2018
Contributor
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.
This comment has been minimized.
This comment has been minimized.
nikomatsakis
Apr 6, 2018
Contributor
@scottmcm If you like how this looks I can give you a tip for how to achieve it =)
This comment has been minimized.
This comment has been minimized.
nikomatsakis
Apr 6, 2018
Contributor
In general I think multi-line spans are to be avoided at basically any cost.
This comment has been minimized.
This comment has been minimized.
scottmcm
Apr 6, 2018
Author
Member
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...
This comment has been minimized.
This comment has been minimized.
nikomatsakis
Apr 9, 2018
Contributor
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
This comment has been minimized.
This comment has been minimized.
scottmcm
Apr 10, 2018
Author
Member
Thanks! That did the trick. And huzzah for hosted compiler docs
scottmcm
force-pushed the
scottmcm:catch-wrapping
branch
from
0757abb
to
9911de8
Apr 10, 2018
nikomatsakis
approved these changes
Apr 10, 2018
|
very nice |
This comment has been minimized.
This comment has been minimized.
|
@bors r+ |
This comment has been minimized.
This comment has been minimized.
|
|
bors
added
S-waiting-on-bors
and removed
S-waiting-on-review
labels
Apr 10, 2018
This comment has been minimized.
This comment has been minimized.
|
|
bors
added
S-waiting-on-author
and removed
S-waiting-on-bors
labels
Apr 10, 2018
scottmcm
added some commits
Mar 25, 2018
scottmcm
force-pushed the
scottmcm:catch-wrapping
branch
from
9911de8
to
311ff5b
Apr 11, 2018
This comment has been minimized.
This comment has been minimized.
|
Rebased; please re-r+. |
scottmcm
added
S-waiting-on-review
and removed
S-waiting-on-author
labels
Apr 11, 2018
This comment has been minimized.
This comment has been minimized.
|
@bors r+ |
This comment has been minimized.
This comment has been minimized.
|
|
bors
added
S-waiting-on-bors
and removed
S-waiting-on-review
labels
Apr 11, 2018
This comment has been minimized.
This comment has been minimized.
|
@bors delegate=scottmcm (For future rebases) |
This comment has been minimized.
This comment has been minimized.
|
|
This comment has been minimized.
This comment has been minimized.
bors
added a commit
that referenced
this pull request
Apr 12, 2018
This comment has been minimized.
This comment has been minimized.
|
|
scottmcm commentedMar 26, 2018
•
edited
Updates the
catch{}lowering to wrap the result inTry::from_ok.r? @nikomatsakis
Fixes #41414
Fixes #43818