Skip to content

Commit fc816c3

Browse files
Fix await suggestion better
1 parent d914f17 commit fc816c3

File tree

3 files changed

+123
-20
lines changed

3 files changed

+123
-20
lines changed

compiler/rustc_infer/src/infer/error_reporting/mod.rs

+26-19
Original file line numberDiff line numberDiff line change
@@ -1794,31 +1794,38 @@ impl<'a, 'tcx> InferCtxt<'a, 'tcx> {
17941794
}
17951795
},
17961796
(_, Some(ty)) if ty::TyS::same_type(exp_found.expected, ty) => {
1797-
let span = match cause.code {
1798-
// scrutinee's span
1799-
ObligationCauseCode::Pattern { span: Some(span), .. } => span,
1800-
_ => exp_span,
1801-
};
1802-
diag.span_suggestion_verbose(
1803-
span.shrink_to_hi(),
1804-
"consider `await`ing on the `Future`",
1805-
".await".to_string(),
1806-
Applicability::MaybeIncorrect,
1807-
);
1808-
}
1809-
(Some(ty), _) if ty::TyS::same_type(ty, exp_found.found) => {
1810-
let span = match cause.code {
1811-
// scrutinee's span
1812-
ObligationCauseCode::Pattern { span: Some(span), .. } => span,
1813-
_ => exp_span,
1814-
};
18151797
diag.span_suggestion_verbose(
1816-
span.shrink_to_hi(),
1798+
exp_span.shrink_to_hi(),
18171799
"consider `await`ing on the `Future`",
18181800
".await".to_string(),
18191801
Applicability::MaybeIncorrect,
18201802
);
18211803
}
1804+
(Some(ty), _) if ty::TyS::same_type(ty, exp_found.found) => match cause.code {
1805+
ObligationCauseCode::Pattern { span: Some(span), .. }
1806+
| ObligationCauseCode::IfExpression(box IfExpressionCause { then: span, .. }) => {
1807+
diag.span_suggestion_verbose(
1808+
span.shrink_to_hi(),
1809+
"consider `await`ing on the `Future`",
1810+
".await".to_string(),
1811+
Applicability::MaybeIncorrect,
1812+
);
1813+
}
1814+
ObligationCauseCode::MatchExpressionArm(box MatchExpressionArmCause {
1815+
ref prior_arms,
1816+
..
1817+
}) => {
1818+
diag.multipart_suggestion_verbose(
1819+
"consider `await`ing on the `Future`",
1820+
prior_arms
1821+
.iter()
1822+
.map(|arm| (arm.shrink_to_hi(), ".await".to_string()))
1823+
.collect(),
1824+
Applicability::MaybeIncorrect,
1825+
);
1826+
}
1827+
_ => {}
1828+
},
18221829
_ => {}
18231830
}
18241831
}

src/test/ui/async-await/suggest-missing-await.rs

+28
Original file line numberDiff line numberDiff line change
@@ -26,4 +26,32 @@ async fn suggest_await_in_async_fn_return() {
2626
//~| SUGGESTION .await
2727
}
2828

29+
#[allow(unused)]
30+
async fn suggest_await_on_if() {
31+
let _x = if true {
32+
dummy()
33+
//~^ HELP consider `await`ing on the `Future`
34+
} else {
35+
dummy().await
36+
//~^ ERROR `if` and `else` have incompatible types [E0308]
37+
};
38+
}
39+
40+
#[allow(unused)]
41+
async fn suggest_await_on_previous_match_arms() {
42+
let _x = match 0usize {
43+
0 => dummy(), //~ HELP consider `await`ing on the `Future`
44+
1 => dummy(),
45+
2 => dummy().await,
46+
//~^ `match` arms have incompatible types [E0308]
47+
};
48+
}
49+
50+
#[allow(unused)]
51+
async fn suggest_await_on_match_expr() {
52+
let _x = match dummy() { //~ HELP consider `await`ing on the `Future`
53+
() => {} //~ ERROR mismatched types [E0308]
54+
};
55+
}
56+
2957
fn main() {}

src/test/ui/async-await/suggest-missing-await.stderr

+69-1
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,74 @@ help: consider using a semicolon here
3838
LL | dummy();
3939
| +
4040

41-
error: aborting due to 2 previous errors
41+
error[E0308]: `if` and `else` have incompatible types
42+
--> $DIR/suggest-missing-await.rs:35:9
43+
|
44+
LL | let _x = if true {
45+
| ______________-
46+
LL | | dummy()
47+
| | ------- expected because of this
48+
LL | |
49+
LL | | } else {
50+
LL | | dummy().await
51+
| | ^^^^^^^^^^^^^ expected opaque type, found `()`
52+
LL | |
53+
LL | | };
54+
| |_____- `if` and `else` have incompatible types
55+
|
56+
= note: expected type `impl Future`
57+
found unit type `()`
58+
help: consider `await`ing on the `Future`
59+
|
60+
LL | dummy().await
61+
| ++++++
62+
63+
error[E0308]: `match` arms have incompatible types
64+
--> $DIR/suggest-missing-await.rs:45:14
65+
|
66+
LL | let _x = match 0usize {
67+
| ______________-
68+
LL | | 0 => dummy(),
69+
| | ------- this is found to be of type `impl Future`
70+
LL | | 1 => dummy(),
71+
| | ------- this is found to be of type `impl Future`
72+
LL | | 2 => dummy().await,
73+
| | ^^^^^^^^^^^^^ expected opaque type, found `()`
74+
LL | |
75+
LL | | };
76+
| |_____- `match` arms have incompatible types
77+
|
78+
note: while checking the return type of the `async fn`
79+
--> $DIR/suggest-missing-await.rs:18:18
80+
|
81+
LL | async fn dummy() {}
82+
| ^ checked the `Output` of this `async fn`, expected opaque type
83+
= note: expected opaque type `impl Future`
84+
found unit type `()`
85+
help: consider `await`ing on the `Future`
86+
|
87+
LL ~ 0 => dummy().await,
88+
LL ~ 1 => dummy().await,
89+
|
90+
91+
error[E0308]: mismatched types
92+
--> $DIR/suggest-missing-await.rs:53:9
93+
|
94+
LL | () => {}
95+
| ^^ expected opaque type, found `()`
96+
|
97+
note: while checking the return type of the `async fn`
98+
--> $DIR/suggest-missing-await.rs:18:18
99+
|
100+
LL | async fn dummy() {}
101+
| ^ checked the `Output` of this `async fn`, expected opaque type
102+
= note: expected opaque type `impl Future`
103+
found unit type `()`
104+
help: consider `await`ing on the `Future`
105+
|
106+
LL | let _x = match dummy().await {
107+
| ++++++
108+
109+
error: aborting due to 5 previous errors
42110

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

0 commit comments

Comments
 (0)