-
Notifications
You must be signed in to change notification settings - Fork 13.1k
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
Changes from all commits
c4b6521
aeb2353
c88efe4
311ff5b
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -13,11 +13,11 @@ | |
struct catch {} | ||
|
||
pub fn main() { | ||
let catch_result = do catch { | ||
let catch_result: Option<_> = do catch { | ||
let x = 5; | ||
x | ||
}; | ||
assert_eq!(catch_result, 5); | ||
assert_eq!(catch_result, Some(5)); | ||
|
||
let mut catch = true; | ||
while catch { catch = false; } | ||
|
@@ -30,51 +30,50 @@ pub fn main() { | |
_ => {} | ||
}; | ||
|
||
let catch_err = do catch { | ||
let catch_err: Result<_, i32> = do catch { | ||
Err(22)?; | ||
Ok(1) | ||
1 | ||
}; | ||
assert_eq!(catch_err, Err(22)); | ||
|
||
let catch_okay: Result<i32, i32> = do catch { | ||
if false { Err(25)?; } | ||
Ok::<(), i32>(())?; | ||
Ok(28) | ||
28 | ||
}; | ||
assert_eq!(catch_okay, Ok(28)); | ||
|
||
let catch_from_loop: Result<i32, i32> = do catch { | ||
for i in 0..10 { | ||
if i < 5 { Ok::<i32, i32>(i)?; } else { Err(i)?; } | ||
} | ||
Ok(22) | ||
22 | ||
}; | ||
assert_eq!(catch_from_loop, Err(5)); | ||
|
||
let cfg_init; | ||
let _res: Result<(), ()> = do catch { | ||
cfg_init = 5; | ||
Ok(()) | ||
}; | ||
assert_eq!(cfg_init, 5); | ||
|
||
let cfg_init_2; | ||
let _res: Result<(), ()> = do catch { | ||
cfg_init_2 = 6; | ||
Err(())?; | ||
Ok(()) | ||
}; | ||
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 commentThe 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 commentThe 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 commentThe 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. |
||
}; | ||
assert_eq!(res, Ok("test")); | ||
|
||
do catch { | ||
() | ||
} | ||
let my_opt: Option<_> = do catch { () }; | ||
assert_eq!(my_opt, Some(())); | ||
|
||
(); | ||
let my_opt: Option<_> = do catch { }; | ||
assert_eq!(my_opt, Some(())); | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
// Copyright 2018 The Rust Project Developers. See the COPYRIGHT | ||
// file at the top-level directory of this distribution and at | ||
// http://rust-lang.org/COPYRIGHT. | ||
// | ||
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or | ||
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license | ||
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your | ||
// option. This file may not be copied, modified, or distributed | ||
// except according to those terms. | ||
|
||
#![feature(catch_expr)] | ||
|
||
fn foo() -> Option<()> { Some(()) } | ||
|
||
fn main() { | ||
let _: Option<f32> = do catch { | ||
foo()?; | ||
42 | ||
//~^ ERROR type mismatch | ||
}; | ||
|
||
let _: Option<i32> = do catch { | ||
foo()?; | ||
}; | ||
//~^ ERROR type mismatch | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
error[E0271]: type mismatch resolving `<std::option::Option<f32> as std::ops::Try>::Ok == {integer}` | ||
--> $DIR/catch-block-type-error.rs:18:9 | ||
| | ||
LL | 42 | ||
| ^^ expected f32, found integral variable | ||
| | ||
= note: expected type `f32` | ||
found type `{integer}` | ||
|
||
error[E0271]: type mismatch resolving `<std::option::Option<i32> as std::ops::Try>::Ok == ()` | ||
--> $DIR/catch-block-type-error.rs:24:5 | ||
| | ||
LL | }; | ||
| ^ expected i32, found () | ||
| | ||
= note: expected type `i32` | ||
found type `()` | ||
|
||
error: aborting due to 2 previous errors | ||
|
||
For more information about this error, try `rustc --explain E0271`. |
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