Skip to content

Commit 22b2d56

Browse files
committed
Suggest adding Result return type for associated method in E0277.
1 parent 9b00430 commit 22b2d56

File tree

4 files changed

+102
-7
lines changed

4 files changed

+102
-7
lines changed

compiler/rustc_trait_selection/src/error_reporting/traits/suggestions.rs

+27-5
Original file line numberDiff line numberDiff line change
@@ -4594,19 +4594,41 @@ impl<'a, 'tcx> TypeErrCtxt<'a, 'tcx> {
45944594
return;
45954595
}
45964596

4597+
fn choose_suggest_items<'tcx, 'hir>(
4598+
tcx: TyCtxt<'tcx>,
4599+
node: hir::Node<'hir>,
4600+
) -> Option<(&'hir hir::FnDecl<'hir>, hir::BodyId)> {
4601+
match node {
4602+
hir::Node::Item(item) if let hir::ItemKind::Fn(sig, _, body_id) = item.kind => {
4603+
Some((sig.decl, body_id))
4604+
}
4605+
hir::Node::ImplItem(item)
4606+
if let hir::ImplItemKind::Fn(sig, body_id) = item.kind =>
4607+
{
4608+
let parent = tcx.parent_hir_node(item.hir_id());
4609+
if let hir::Node::Item(item) = parent
4610+
&& let hir::ItemKind::Impl(imp) = item.kind
4611+
&& imp.of_trait.is_none()
4612+
{
4613+
return Some((sig.decl, body_id));
4614+
}
4615+
None
4616+
}
4617+
_ => None,
4618+
}
4619+
}
4620+
45974621
let node = self.tcx.hir_node_by_def_id(obligation.cause.body_id);
4598-
if let hir::Node::Item(item) = node
4599-
&& let hir::ItemKind::Fn(sig, _, body_id) = item.kind
4600-
&& let hir::FnRetTy::DefaultReturn(ret_span) = sig.decl.output
4622+
if let Some((fn_decl, body_id)) = choose_suggest_items(self.tcx, node)
4623+
&& let hir::FnRetTy::DefaultReturn(ret_span) = fn_decl.output
46014624
&& self.tcx.is_diagnostic_item(sym::FromResidual, trait_pred.def_id())
46024625
&& trait_pred.skip_binder().trait_ref.args.type_at(0).is_unit()
46034626
&& let ty::Adt(def, _) = trait_pred.skip_binder().trait_ref.args.type_at(1).kind()
46044627
&& self.tcx.is_diagnostic_item(sym::Result, def.did())
46054628
{
4606-
let body = self.tcx.hir().body(body_id);
46074629
let mut sugg_spans =
46084630
vec![(ret_span, " -> Result<(), Box<dyn std::error::Error>>".to_string())];
4609-
4631+
let body = self.tcx.hir().body(body_id);
46104632
if let hir::ExprKind::Block(b, _) = body.value.kind
46114633
&& b.expr.is_none()
46124634
{

tests/ui/return/return-from-residual-sugg-issue-125997.fixed

+19
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,25 @@ macro_rules! mac {
3333
};
3434
}
3535

36+
struct A;
37+
38+
impl A {
39+
fn test4(&self) -> Result<(), Box<dyn std::error::Error>> {
40+
let mut _file = File::create("foo.txt")?;
41+
//~^ ERROR the `?` operator can only be used in a method
42+
43+
Ok(())
44+
}
45+
46+
fn test5(&self) -> Result<(), Box<dyn std::error::Error>> {
47+
let mut _file = File::create("foo.txt")?;
48+
//~^ ERROR the `?` operator can only be used in a method
49+
println!();
50+
51+
Ok(())
52+
}
53+
}
54+
3655
fn main() -> Result<(), Box<dyn std::error::Error>> {
3756
let mut _file = File::create("foo.txt")?;
3857
//~^ ERROR the `?` operator can only be used in a function

tests/ui/return/return-from-residual-sugg-issue-125997.rs

+15
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,21 @@ macro_rules! mac {
2727
};
2828
}
2929

30+
struct A;
31+
32+
impl A {
33+
fn test4(&self) {
34+
let mut _file = File::create("foo.txt")?;
35+
//~^ ERROR the `?` operator can only be used in a method
36+
}
37+
38+
fn test5(&self) {
39+
let mut _file = File::create("foo.txt")?;
40+
//~^ ERROR the `?` operator can only be used in a method
41+
println!();
42+
}
43+
}
44+
3045
fn main() {
3146
let mut _file = File::create("foo.txt")?;
3247
//~^ ERROR the `?` operator can only be used in a function

tests/ui/return/return-from-residual-sugg-issue-125997.stderr

+41-2
Original file line numberDiff line numberDiff line change
@@ -37,8 +37,47 @@ LL + Ok(())
3737
LL + }
3838
|
3939

40+
error[E0277]: the `?` operator can only be used in a method that returns `Result` or `Option` (or another type that implements `FromResidual`)
41+
--> $DIR/return-from-residual-sugg-issue-125997.rs:34:48
42+
|
43+
LL | fn test4(&self) {
44+
| --------------- this function should return `Result` or `Option` to accept `?`
45+
LL | let mut _file = File::create("foo.txt")?;
46+
| ^ cannot use the `?` operator in a method that returns `()`
47+
|
48+
= help: the trait `FromResidual<Result<Infallible, std::io::Error>>` is not implemented for `()`
49+
help: consider adding return type
50+
|
51+
LL ~ fn test4(&self) -> Result<(), Box<dyn std::error::Error>> {
52+
LL | let mut _file = File::create("foo.txt")?;
53+
LL |
54+
LL ~
55+
LL + Ok(())
56+
LL + }
57+
|
58+
59+
error[E0277]: the `?` operator can only be used in a method that returns `Result` or `Option` (or another type that implements `FromResidual`)
60+
--> $DIR/return-from-residual-sugg-issue-125997.rs:39:48
61+
|
62+
LL | fn test5(&self) {
63+
| --------------- this function should return `Result` or `Option` to accept `?`
64+
LL | let mut _file = File::create("foo.txt")?;
65+
| ^ cannot use the `?` operator in a method that returns `()`
66+
|
67+
= help: the trait `FromResidual<Result<Infallible, std::io::Error>>` is not implemented for `()`
68+
help: consider adding return type
69+
|
70+
LL ~ fn test5(&self) -> Result<(), Box<dyn std::error::Error>> {
71+
LL | let mut _file = File::create("foo.txt")?;
72+
LL |
73+
LL | println!();
74+
LL ~
75+
LL + Ok(())
76+
LL + }
77+
|
78+
4079
error[E0277]: the `?` operator can only be used in a function that returns `Result` or `Option` (or another type that implements `FromResidual`)
41-
--> $DIR/return-from-residual-sugg-issue-125997.rs:31:44
80+
--> $DIR/return-from-residual-sugg-issue-125997.rs:46:44
4281
|
4382
LL | fn main() {
4483
| --------- this function should return `Result` or `Option` to accept `?`
@@ -81,6 +120,6 @@ LL + Ok(())
81120
LL + }
82121
|
83122

84-
error: aborting due to 4 previous errors
123+
error: aborting due to 6 previous errors
85124

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

0 commit comments

Comments
 (0)