Skip to content
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

Merged
merged 4 commits into from
Apr 12, 2018
Merged

Conversation

scottmcm
Copy link
Member

@scottmcm scottmcm commented Mar 26, 2018

Updates the catch{} lowering to wrap the result in Try::from_ok.

r? @nikomatsakis

Fixes #41414
Fixes #43818

@pietroalbini pietroalbini added the S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. label Mar 26, 2018
id: block.id, // FIXME!
span: block.span, // FIXME?
node: hir::ExprTup(hir_vec![]),
attrs: ThinVec::new(), // FIXME?
Copy link
Contributor

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

let mut block = this.lower_block(body, true).into_inner();
let tail = block.expr.take().map_or_else(
|| hir::Expr {
id: block.id, // FIXME!
Copy link
Contributor

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

let tail = block.expr.take().map_or_else(
|| hir::Expr {
id: block.id, // FIXME!
span: block.span, // FIXME?
Copy link
Contributor

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 }?

Copy link
Contributor

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<()> = (||{
Copy link
Contributor

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 } }
}

Copy link
Member Author

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! 👍

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<()> = (||{
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

macro

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<()> = (||{
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

macro!

@nikomatsakis
Copy link
Contributor

first round of comments =) sorry for the delay. Cursed All Hands!

@scottmcm 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
Copy link
Member Author

scottmcm commented Apr 3, 2018

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 🙂

@TimNN
Copy link
Contributor

TimNN commented Apr 3, 2018

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.
[00:00:50] configure: rust.quiet-tests     := True
---
[00:40:47] .........................................................................i..........................
[00:40:53] ................i...................................................................................
---
[00:41:28] ............................................................................................i.......
[00:41:35] ................................................................i...................................
---
[00:42:30] .............................................i......................................................
---
[00:46:24] .............................i......................................................................
[00:46:39] ..............................................................i.....................................
[00:46:54] ...............................................i....................................................
[00:47:15] ....................................................................................................
[00:47:37] ....................................................................................................
[00:47:58] ....................................................................................................
[00:48:24] .i................................................................................................i.
[00:48:50] .................................................................................test [run-pass] run-pass/mir_heavy_promoted.rs has been running for over 60 seconds
[00:49:00] ...................
[00:49:31] ....................................................................................................
[00:50:07] ...............................................................ii...................................
[00:50:53] ..........................i....................................................i.ii....test [run-pass] run-pass/saturating-float-casts.rs has been running for over 60 seconds
[00:50:57] .............
[00:51:38] .......................................................................................iiiiiii......
---
[00:53:35] ....................................i...............................................................
[00:53:43] ....................................................................................................
[00:53:51] ..................i............................................................ii.iii...............
[00:53:58] ....................................................................................................
[00:54:06] ........i..............................i............................................................
[00:54:13] ....................................................................................................
[00:54:20] .....................i..............................................................................
[00:54:29] ....................................................................................................
[00:54:39] ....................................................................................................
[00:54:49] ....................................................................................................
[00:55:00] ....................................................................................................
[00:55:14] ....................................................................................................
[00:55:23] ..............i.....................................................................................
[00:55:33] .................i..ii..............................................................................
[00:55:42] ....................................................................................................
[00:55:53] ....................................................................................................
[00:56:03] ....................................................................................i...............
[00:56:13] ..............................i.....................................................................
---
[00:56:51] ............................i.......................................................................
[00:56:52] ....................................................................i...............................
[00:56:53] .................i......................................................
---
[00:57:08] ...........i........................
---
[00:57:38] i...i..ii....i.............ii........iii......i..i...i...ii..i..i..ii.....
---
[00:57:41] i.......i......................i......
---
[00:58:19] iiii.......i..i........i..i.i.............i..........iiii...........i...i..........ii.i.i.......ii..
[00:58:20] ....ii...
---
[01:07:39] ...i................................................................................................
---
[01:09:29] ......................................i.............................................................
[01:09:48] ....................................................................................................
[01:10:08] .............................................i......................................................
---
[01:11:40] ........................................................ii..........................................
---
[01:12:45] .............................................................i......................................
---
[01:17:34] ii..................................................................................................
[01:17:53] ....................................................................................................
[01:18:09] ...................iii......i......i...i......i.....................................................
[01:18:19] ....................................................................................................
[01:18:34] ........................................iiii........ii..............................................
[01:18:45] ....................................................................................................
[01:19:02] .......................................................................................i............
---
[01:28:26] command did not execute successfully: "/checkout/obj/build/bootstrap/debug/rustdoc" "--test" "/checkout/src/doc/unstable-book/src/language-features/catch-expr.md" "--test-args" ""
---
[01:28:26] ---- /checkout/src/doc/unstable-book/src/language-features/catch-expr.md - catch_expr (line 12) stdout ----
[01:28:26]  error[E0271]: type mismatch resolving `<std::result::Result<i32, std::num::ParseIntError> as std::ops::Try>::Ok == std::result::Result<i32, _>`
[01:28:26]   --> /checkout/src/doc/unstable-book/src/language-features/catch-expr.md:18:5
[01:28:26]    |
[01:28:26] 8  | /     Ok("1".parse::<i32>()?
[01:28:26] 9  | |         + "2".parse::<i32>()?
[01:28:26] 10 | |         + "3".parse::<i32>()?)
[01:28:26]    | |______________________________^ expected i32, found enum `std::result::Result`
[01:28:26]    |
[01:28:26]    = note: expected type `i32`
[01:28:26]               found type `std::result::Result<i32, _>`
[01:28:26]
[01:28:26] error[E0271]: type mismatch resolving `<std::result::Result<i32, std::num::ParseIntError> as std::ops::Try>::Ok == std::result::Result<i32, _>`
[01:28:26]   --> /checkout/src/doc/unstable-book/src/language-features/catch-expr.md:25:5
[01:28:26]    |
[01:28:26] 15 | /     Ok("1".parse::<i32>()?
[01:28:26] 16 | |         + "foo".parse::<i32>()?
[01:28:26] 17 | |         + "3".parse::<i32>()?)
[01:28:26]    | |______________________________^ expected i32, found enum `std::result::Result`
[01:28:26]    |
[01:28:26]    = note: expected type `i32`
[01:28:26]               found type `std::result::Result<i32, _>`
[01:28:26]
[01:28:26] thread 'rustc' panicked at 'couldn't compile the test', librustdoc/test.rs:306:13
---
[01:28:26] make: *** [check] Error 1
[01:28:26] Makefile:58: recipe for target 'check' failed

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 @TimNN.

@rust-lang rust-lang deleted a comment from TimNN Apr 3, 2018
Copy link
Contributor

@nikomatsakis nikomatsakis left a 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>()?
Copy link
Contributor

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[..]
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

interesting.

Copy link
Member Author

@scottmcm scottmcm Apr 4, 2018

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.

Copy link
Contributor

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 ()
Copy link
Contributor

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.

Copy link
Contributor

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 =)

Copy link
Contributor

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.

Copy link
Member Author

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...

Copy link
Contributor

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

Copy link
Member Author

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 🎉

Copy link
Contributor

@nikomatsakis nikomatsakis left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

very nice

@nikomatsakis
Copy link
Contributor

@bors r+

@bors
Copy link
Contributor

bors commented Apr 10, 2018

📌 Commit 9911de8 has been approved by nikomatsakis

@bors bors added S-waiting-on-bors Status: Waiting on bors to run and complete tests. Bors will change the label on completion. and removed S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. labels Apr 10, 2018
@bors
Copy link
Contributor

bors commented Apr 10, 2018

☔ The latest upstream changes (presumably #48914) made this pull request unmergeable. Please resolve the merge conflicts.

@bors bors added S-waiting-on-author Status: This is awaiting some action (such as code changes or more information) from the author. and removed S-waiting-on-bors Status: Waiting on bors to run and complete tests. Bors will change the label on completion. labels Apr 10, 2018
@scottmcm
Copy link
Member Author

Rebased; please re-r+.

@scottmcm scottmcm added S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. and removed S-waiting-on-author Status: This is awaiting some action (such as code changes or more information) from the author. labels Apr 11, 2018
@nikomatsakis
Copy link
Contributor

@bors r+

@bors
Copy link
Contributor

bors commented Apr 11, 2018

📌 Commit 311ff5b has been approved by nikomatsakis

@bors bors added S-waiting-on-bors Status: Waiting on bors to run and complete tests. Bors will change the label on completion. and removed S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. labels Apr 11, 2018
@nikomatsakis
Copy link
Contributor

@bors delegate=scottmcm

(For future rebases)

@bors
Copy link
Contributor

bors commented Apr 11, 2018

✌️ @scottmcm can now approve this pull request

@bors
Copy link
Contributor

bors commented Apr 12, 2018

⌛ Testing commit 311ff5b with merge 252a459...

bors added a commit that referenced this pull request Apr 12, 2018
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
@bors
Copy link
Contributor

bors commented Apr 12, 2018

☀️ Test successful - status-appveyor, status-travis
Approved by: nikomatsakis
Pushing 252a459 to master...

@bors bors merged commit 311ff5b into rust-lang:master Apr 12, 2018
@scottmcm scottmcm deleted the catch-wrapping branch April 12, 2018 08:29
jonhoo added a commit to mit-pdos/noria that referenced this pull request Apr 19, 2018
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
S-waiting-on-bors Status: Waiting on bors to run and complete tests. Bors will change the label on completion.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

6 participants