File tree 2 files changed +56
-1
lines changed
2 files changed +56
-1
lines changed Original file line number Diff line number Diff line change @@ -16,3 +16,32 @@ fn a_closure() -> u32 {
16
16
} ;
17
17
a_closure ( )
18
18
}
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
+ }
Original file line number Diff line number Diff line change @@ -27,6 +27,32 @@ LL | | };
27
27
= help: the trait `std::ops::Try` is not implemented for `{integer}`
28
28
= note: required by `std::ops::Try::from_error`
29
29
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
31
57
32
58
For more information about this error, try `rustc --explain E0277`.
You can’t perform that action at this time.
0 commit comments