Skip to content

Commit 8b89a12

Browse files
committed
Fix incorrect syntax suggestion with pub async fn
1 parent bf61143 commit 8b89a12

File tree

3 files changed

+104
-9
lines changed

3 files changed

+104
-9
lines changed

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

+19-9
Original file line numberDiff line numberDiff line change
@@ -1079,18 +1079,28 @@ impl<'a, 'tcx> InferCtxtExt<'tcx> for InferCtxt<'a, 'tcx> {
10791079
self.in_progress_typeck_results.map(|t| t.borrow())
10801080
&& let ty = typeck_results.expr_ty_adjusted(base)
10811081
&& let ty::FnDef(def_id, _substs) = ty.kind()
1082-
&& let Some(hir::Node::Item(hir::Item { span, ident, .. })) =
1082+
&& let Some(hir::Node::Item(hir::Item { ident, span, vis_span, .. })) =
10831083
hir.get_if_local(*def_id)
10841084
{
1085-
err.span_suggestion_verbose(
1086-
span.shrink_to_lo(),
1087-
&format!(
1088-
"alternatively, consider making `fn {}` asynchronous",
1089-
ident
1090-
),
1091-
"async ".to_string(),
1092-
Applicability::MaybeIncorrect,
1085+
let msg = format!(
1086+
"alternatively, consider making `fn {}` asynchronous",
1087+
ident
10931088
);
1089+
if vis_span.is_empty() {
1090+
err.span_suggestion_verbose(
1091+
span.shrink_to_lo(),
1092+
&msg,
1093+
"async ".to_string(),
1094+
Applicability::MaybeIncorrect,
1095+
);
1096+
} else {
1097+
err.span_suggestion_verbose(
1098+
vis_span.shrink_to_hi(),
1099+
&msg,
1100+
" async".to_string(),
1101+
Applicability::MaybeIncorrect,
1102+
);
1103+
}
10941104
}
10951105
}
10961106
}
+19
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
// edition:2018
2+
3+
async fn f() {
4+
m::f1().await; //~ ERROR `()` is not a future
5+
m::f2().await; //~ ERROR `()` is not a future
6+
m::f3().await; //~ ERROR `()` is not a future
7+
}
8+
9+
mod m {
10+
pub fn f1() {}
11+
12+
pub(crate) fn f2() {}
13+
14+
pub
15+
fn
16+
f3() {}
17+
}
18+
19+
fn main() {}
+66
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
error[E0277]: `()` is not a future
2+
--> $DIR/issue-96555.rs:4:12
3+
|
4+
LL | m::f1().await;
5+
| -------^^^^^^ `()` is not a future
6+
| |
7+
| this call returns `()`
8+
|
9+
= help: the trait `Future` is not implemented for `()`
10+
= note: () must be a future or must implement `IntoFuture` to be awaited
11+
= note: required because of the requirements on the impl of `IntoFuture` for `()`
12+
help: remove the `.await`
13+
|
14+
LL - m::f1().await;
15+
LL + m::f1();
16+
|
17+
help: alternatively, consider making `fn f1` asynchronous
18+
|
19+
LL | pub async fn f1() {}
20+
| +++++
21+
22+
error[E0277]: `()` is not a future
23+
--> $DIR/issue-96555.rs:5:12
24+
|
25+
LL | m::f2().await;
26+
| -------^^^^^^ `()` is not a future
27+
| |
28+
| this call returns `()`
29+
|
30+
= help: the trait `Future` is not implemented for `()`
31+
= note: () must be a future or must implement `IntoFuture` to be awaited
32+
= note: required because of the requirements on the impl of `IntoFuture` for `()`
33+
help: remove the `.await`
34+
|
35+
LL - m::f2().await;
36+
LL + m::f2();
37+
|
38+
help: alternatively, consider making `fn f2` asynchronous
39+
|
40+
LL | pub(crate) async fn f2() {}
41+
| +++++
42+
43+
error[E0277]: `()` is not a future
44+
--> $DIR/issue-96555.rs:6:12
45+
|
46+
LL | m::f3().await;
47+
| -------^^^^^^ `()` is not a future
48+
| |
49+
| this call returns `()`
50+
|
51+
= help: the trait `Future` is not implemented for `()`
52+
= note: () must be a future or must implement `IntoFuture` to be awaited
53+
= note: required because of the requirements on the impl of `IntoFuture` for `()`
54+
help: remove the `.await`
55+
|
56+
LL - m::f3().await;
57+
LL + m::f3();
58+
|
59+
help: alternatively, consider making `fn f3` asynchronous
60+
|
61+
LL | pub async
62+
| +++++
63+
64+
error: aborting due to 3 previous errors
65+
66+
For more information about this error, try `rustc --explain E0277`.

0 commit comments

Comments
 (0)