Skip to content

Commit b9bf099

Browse files
committed
Suggest adding Result return type for associated method in E0277.
1 parent bfa098e commit b9bf099

File tree

4 files changed

+102
-7
lines changed

4 files changed

+102
-7
lines changed

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

+27-5
Original file line numberDiff line numberDiff line change
@@ -4598,19 +4598,41 @@ impl<'tcx> TypeErrCtxt<'_, 'tcx> {
45984598
return;
45994599
}
46004600

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

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)