Skip to content

Commit 6c9249f

Browse files
Don't call await a method
1 parent e6077fc commit 6c9249f

File tree

8 files changed

+43
-7
lines changed

8 files changed

+43
-7
lines changed

compiler/rustc_borrowck/messages.ftl

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -203,6 +203,15 @@ borrowck_moved_due_to_method_call =
203203
*[false] call
204204
}
205205
206+
borrowck_moved_due_to_await =
207+
{$place_name} {$is_partial ->
208+
[true] partially moved
209+
*[false] moved
210+
} due to this {$is_loop_message ->
211+
[true] await, in previous iteration of loop
212+
*[false] await
213+
}
214+
206215
borrowck_value_moved_here =
207216
value {$is_partial ->
208217
[true] partially moved

compiler/rustc_borrowck/src/diagnostics/mod.rs

Lines changed: 15 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1085,12 +1085,21 @@ impl<'cx, 'tcx> MirBorrowckCtxt<'cx, 'tcx> {
10851085
}
10861086
}
10871087
} else {
1088-
err.subdiagnostic(CaptureReasonLabel::MethodCall {
1089-
fn_call_span,
1090-
place_name: &place_name,
1091-
is_partial,
1092-
is_loop_message,
1093-
});
1088+
if let Some((CallDesugaringKind::Await, _)) = desugaring {
1089+
err.subdiagnostic(CaptureReasonLabel::Await {
1090+
fn_call_span,
1091+
place_name: &place_name,
1092+
is_partial,
1093+
is_loop_message,
1094+
});
1095+
} else {
1096+
err.subdiagnostic(CaptureReasonLabel::MethodCall {
1097+
fn_call_span,
1098+
place_name: &place_name,
1099+
is_partial,
1100+
is_loop_message,
1101+
});
1102+
}
10941103
// Erase and shadow everything that could be passed to the new infcx.
10951104
let ty = moved_place.ty(self.body, tcx).ty;
10961105

compiler/rustc_borrowck/src/session_diagnostics.rs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -338,6 +338,14 @@ pub(crate) enum CaptureReasonLabel<'a> {
338338
is_partial: bool,
339339
is_loop_message: bool,
340340
},
341+
#[label(borrowck_moved_due_to_await)]
342+
Await {
343+
#[primary_span]
344+
fn_call_span: Span,
345+
place_name: &'a str,
346+
is_partial: bool,
347+
is_loop_message: bool,
348+
},
341349
#[label(borrowck_value_moved_here)]
342350
MovedHere {
343351
#[primary_span]

compiler/rustc_const_eval/src/transform/check_consts/ops.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -184,6 +184,9 @@ impl<'tcx> NonConstOp<'tcx> for FnCallNonConst<'tcx> {
184184
CallDesugaringKind::TryBlockFromOutput => {
185185
error!("`try` block cannot convert `{}` to the result in {}s")
186186
}
187+
CallDesugaringKind::Await => {
188+
error!("cannot convert `{}` into a future in {}s")
189+
}
187190
};
188191

189192
diag_trait(&mut err, self_ty, kind.trait_def_id(tcx));

compiler/rustc_middle/src/util/call_kind.rs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,8 @@ pub enum CallDesugaringKind {
1919
QuestionFromResidual,
2020
/// try { ..; x } calls type_of(x)::from_output(x)
2121
TryBlockFromOutput,
22+
/// `.await` calls `IntoFuture::into_future`
23+
Await,
2224
}
2325

2426
impl CallDesugaringKind {
@@ -29,6 +31,7 @@ impl CallDesugaringKind {
2931
tcx.require_lang_item(LangItem::Try, None)
3032
}
3133
Self::QuestionFromResidual => tcx.get_diagnostic_item(sym::FromResidual).unwrap(),
34+
Self::Await => tcx.get_diagnostic_item(sym::IntoFuture).unwrap(),
3235
}
3336
}
3437
}
@@ -129,6 +132,8 @@ pub fn call_kind<'tcx>(
129132
&& fn_call_span.desugaring_kind() == Some(DesugaringKind::TryBlock)
130133
{
131134
Some((CallDesugaringKind::TryBlockFromOutput, method_substs.type_at(0)))
135+
} else if fn_call_span.is_desugaring(DesugaringKind::Await) {
136+
Some((CallDesugaringKind::Await, method_substs.type_at(0)))
132137
} else {
133138
None
134139
};

compiler/rustc_span/src/symbol.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -208,6 +208,7 @@ symbols! {
208208
Input,
209209
Into,
210210
IntoDiagnostic,
211+
IntoFuture,
211212
IntoIterator,
212213
IoRead,
213214
IoWrite,

library/core/src/future/into_future.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -99,6 +99,7 @@ use crate::future::Future;
9999
/// }
100100
/// ```
101101
#[stable(feature = "into_future", since = "1.64.0")]
102+
#[rustc_diagnostic_item = "IntoFuture"]
102103
pub trait IntoFuture {
103104
/// The output that the future will produce on completion.
104105
#[stable(feature = "into_future", since = "1.64.0")]

tests/ui/async-await/clone-suggestion.stderr

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ error[E0382]: use of moved value: `f`
44
LL | let f = SharedFuture;
55
| - move occurs because `f` has type `SharedFuture`, which does not implement the `Copy` trait
66
LL | f.await;
7-
| ----- `f` moved due to this method call
7+
| ----- `f` moved due to this await
88
LL | f.await;
99
| ^ value used here after move
1010
|

0 commit comments

Comments
 (0)