Skip to content
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 18 additions & 0 deletions src/librustc/traits/error_reporting/on_unimplemented.rs
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,24 @@ impl<'a, 'tcx> InferCtxt<'a, 'tcx> {
"a function"
})
})
} else if let hir::Node::TraitItem(hir::TraitItem {
kind: hir::TraitItemKind::Method(_, hir::TraitMethod::Provided(body_id)),
..
}) = &node
Comment thread
varkor marked this conversation as resolved.
Outdated
{
self.describe_generator(*body_id).or_else(|| Some("a trait method"))
} else if let hir::Node::ImplItem(hir::ImplItem {
kind: hir::ImplItemKind::Method(sig, body_id),
..
}) = &node
{
self.describe_generator(*body_id).or_else(|| {
Some(if let hir::FnHeader { asyncness: hir::IsAsync::Async, .. } = sig.header {
"an async method"
} else {
"a method"
})
})
} else if let hir::Node::Expr(hir::Expr {
kind: hir::ExprKind::Closure(_is_move, _, body_id, _, gen_movability),
..
Expand Down
29 changes: 29 additions & 0 deletions src/test/ui/try-on-option-diagnostics.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,3 +16,32 @@ fn a_closure() -> u32 {
};
a_closure()
}

fn a_method() -> u32 {
struct S;

impl S {
fn a_method() {
let x: Option<u32> = None;
x?; //~ ERROR the `?` operator
}
}

S::a_method();
22
}

fn a_trait_method() -> u32 {
struct S;
trait T {
fn a_trait_method() {
let x: Option<u32> = None;
x?; //~ ERROR the `?` operator
}
}

impl T for S { }

S::a_trait_method();
22
}
28 changes: 27 additions & 1 deletion src/test/ui/try-on-option-diagnostics.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,32 @@ LL | | };
= help: the trait `std::ops::Try` is not implemented for `{integer}`
= note: required by `std::ops::Try::from_error`

error: aborting due to 2 previous errors
error[E0277]: the `?` operator can only be used in a method that returns `Result` or `Option` (or another type that implements `std::ops::Try`)
--> $DIR/try-on-option-diagnostics.rs:26:13
|
LL | / fn a_method() {
LL | | let x: Option<u32> = None;
LL | | x?;
| | ^^ cannot use the `?` operator in a method that returns `()`
LL | | }
| |_________- this function should return `Result` or `Option` to accept `?`
|
= help: the trait `std::ops::Try` is not implemented for `()`
= note: required by `std::ops::Try::from_error`

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`)
--> $DIR/try-on-option-diagnostics.rs:39:13
|
LL | / fn a_trait_method() {
LL | | let x: Option<u32> = None;
LL | | x?;
| | ^^ cannot use the `?` operator in a trait method that returns `()`
LL | | }
| |_________- this function should return `Result` or `Option` to accept `?`
|
= help: the trait `std::ops::Try` is not implemented for `()`
= note: required by `std::ops::Try::from_error`

error: aborting due to 4 previous errors

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