Skip to content

Commit ce49e32

Browse files
committed
Expand EO308 to mention try!
Fixes #28953
1 parent 03f4950 commit ce49e32

File tree

1 file changed

+24
-0
lines changed

1 file changed

+24
-0
lines changed

src/librustc/diagnostics.rs

+24
Original file line numberDiff line numberDiff line change
@@ -1822,6 +1822,30 @@ let x: i32 = "I am not a number!";
18221822
// |
18231823
// type `i32` assigned to variable `x`
18241824
```
1825+
1826+
Another situation in which this occurs is when you attempt to use the `try!`
1827+
macro inside a function that does not return a `Result<T, E>`:
1828+
1829+
```
1830+
use std::fs::File;
1831+
1832+
fn main() {
1833+
let mut f = try!(File::create("foo.txt"));
1834+
}
1835+
```
1836+
1837+
This code gives an error like this:
1838+
1839+
```text
1840+
<std macros>:5:8: 6:42 error: mismatched types:
1841+
expected `()`,
1842+
found `core::result::Result<_, _>`
1843+
(expected (),
1844+
found enum `core::result::Result`) [E0308]
1845+
```
1846+
1847+
`try!` returns a `Result<T, E>`, and so the function must. But `main()` has
1848+
`()` as its return type, hence the error.
18251849
"##,
18261850

18271851
E0309: r##"

0 commit comments

Comments
 (0)