Skip to content

Commit 02e66ba

Browse files
committed
Test try diagnostics for impl and trait methods
Signed-off-by: Philipp Gesang <[email protected]>
1 parent 5dee7dd commit 02e66ba

File tree

2 files changed

+56
-1
lines changed

2 files changed

+56
-1
lines changed

src/test/ui/try-on-option-diagnostics.rs

+29
Original file line numberDiff line numberDiff line change
@@ -16,3 +16,32 @@ fn a_closure() -> u32 {
1616
};
1717
a_closure()
1818
}
19+
20+
fn a_method() -> u32 {
21+
struct S;
22+
23+
impl S {
24+
fn a_method() {
25+
let x: Option<u32> = None;
26+
x?; //~ ERROR the `?` operator
27+
}
28+
}
29+
30+
S::a_method();
31+
22
32+
}
33+
34+
fn a_trait_method() -> u32 {
35+
struct S;
36+
trait T {
37+
fn a_trait_method() {
38+
let x: Option<u32> = None;
39+
x?; //~ ERROR the `?` operator
40+
}
41+
}
42+
43+
impl T for S { }
44+
45+
S::a_trait_method();
46+
22
47+
}

src/test/ui/try-on-option-diagnostics.stderr

+27-1
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,32 @@ LL | | };
2727
= help: the trait `std::ops::Try` is not implemented for `{integer}`
2828
= note: required by `std::ops::Try::from_error`
2929

30-
error: aborting due to 2 previous errors
30+
error[E0277]: the `?` operator can only be used in a method that returns `Result` or `Option` (or another type that implements `std::ops::Try`)
31+
--> $DIR/try-on-option-diagnostics.rs:26:13
32+
|
33+
LL | / fn a_method() {
34+
LL | | let x: Option<u32> = None;
35+
LL | | x?;
36+
| | ^^ cannot use the `?` operator in a method that returns `()`
37+
LL | | }
38+
| |_________- this function should return `Result` or `Option` to accept `?`
39+
|
40+
= help: the trait `std::ops::Try` is not implemented for `()`
41+
= note: required by `std::ops::Try::from_error`
42+
43+
error[E0277]: the `?` operator can only be used in a trait method that returns `Result` or `Option` (or another type that implements `std::ops::Try`)
44+
--> $DIR/try-on-option-diagnostics.rs:39:13
45+
|
46+
LL | / fn a_trait_method() {
47+
LL | | let x: Option<u32> = None;
48+
LL | | x?;
49+
| | ^^ cannot use the `?` operator in a trait method that returns `()`
50+
LL | | }
51+
| |_________- this function should return `Result` or `Option` to accept `?`
52+
|
53+
= help: the trait `std::ops::Try` is not implemented for `()`
54+
= note: required by `std::ops::Try::from_error`
55+
56+
error: aborting due to 4 previous errors
3157

3258
For more information about this error, try `rustc --explain E0277`.

0 commit comments

Comments
 (0)