Skip to content

Commit fdbec62

Browse files
committed
Port ConsiderAddingAwait
1 parent 9f06c3d commit fdbec62

File tree

3 files changed

+74
-38
lines changed

3 files changed

+74
-38
lines changed

compiler/rustc_error_messages/locales/en-US/infer.ftl

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -335,3 +335,7 @@ infer_srs_remove_and_box = consider removing this semicolon and boxing the expre
335335
infer_srs_remove = consider removing this semicolon
336336
infer_srs_add = consider returning the local binding `{$ident}`
337337
infer_srs_add_one = consider returning one of these bindings
338+
339+
infer_await_both_futures = consider `await`ing on both `Future`s
340+
infer_await_future = consider `await`ing on the `Future`
341+
infer_await_note = calling an async function returns a future

compiler/rustc_infer/src/errors/mod.rs

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1050,3 +1050,46 @@ pub enum SuggestRemoveSemiOrReturnBinding {
10501050
spans: MultiSpan,
10511051
},
10521052
}
1053+
1054+
#[derive(Subdiagnostic)]
1055+
pub enum ConsiderAddingAwait {
1056+
#[help(infer_await_both_futures)]
1057+
BothFuturesHelp,
1058+
#[multipart_suggestion(infer_await_both_futures, applicability = "maybe-incorrect")]
1059+
BothFuturesSugg {
1060+
#[suggestion_part(code = ".await")]
1061+
first: Span,
1062+
#[suggestion_part(code = ".await")]
1063+
second: Span,
1064+
},
1065+
#[suggestion(
1066+
infer_await_future,
1067+
code = ".await",
1068+
style = "verbose",
1069+
applicability = "maybe-incorrect"
1070+
)]
1071+
FutureSugg {
1072+
#[primary_span]
1073+
span: Span,
1074+
},
1075+
#[suggestion(
1076+
infer_await_future,
1077+
code = ".await",
1078+
style = "verbose",
1079+
applicability = "maybe-incorrect"
1080+
)]
1081+
#[note(infer_await_note)]
1082+
FutureSuggWithNote {
1083+
#[primary_span]
1084+
span: Span,
1085+
},
1086+
#[multipart_suggestion(
1087+
infer_await_future,
1088+
style = "verbose",
1089+
applicability = "maybe-incorrect"
1090+
)]
1091+
FutureSuggMultiple {
1092+
#[suggestion_part(code = ".await")]
1093+
spans: Vec<Span>,
1094+
},
1095+
}

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

Lines changed: 27 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,9 @@ use rustc_middle::ty::print::with_no_trimmed_paths;
1111
use rustc_middle::ty::{self as ty, IsSuggestable, Ty, TypeVisitable};
1212
use rustc_span::{sym, BytePos, Span};
1313

14-
use crate::errors::{SuggAddLetForLetChains, SuggestRemoveSemiOrReturnBinding};
14+
use crate::errors::{
15+
ConsiderAddingAwait, SuggAddLetForLetChains, SuggestRemoveSemiOrReturnBinding,
16+
};
1517

1618
use super::TypeErrCtxt;
1719

@@ -191,7 +193,7 @@ impl<'tcx> TypeErrCtxt<'_, 'tcx> {
191193
return;
192194
}
193195

194-
match (
196+
let subdiag = match (
195197
self.get_impl_future_output_ty(exp_found.expected),
196198
self.get_impl_future_output_ty(exp_found.found),
197199
) {
@@ -200,65 +202,52 @@ impl<'tcx> TypeErrCtxt<'_, 'tcx> {
200202
{
201203
ObligationCauseCode::IfExpression(box IfExpressionCause { then_id, .. }) => {
202204
let then_span = self.find_block_span_from_hir_id(*then_id);
203-
diag.multipart_suggestion(
204-
"consider `await`ing on both `Future`s",
205-
vec![
206-
(then_span.shrink_to_hi(), ".await".to_string()),
207-
(exp_span.shrink_to_hi(), ".await".to_string()),
208-
],
209-
Applicability::MaybeIncorrect,
210-
);
205+
Some(ConsiderAddingAwait::BothFuturesSugg {
206+
first: then_span.shrink_to_hi(),
207+
second: exp_span.shrink_to_hi(),
208+
})
211209
}
212210
ObligationCauseCode::MatchExpressionArm(box MatchExpressionArmCause {
213211
prior_arms,
214212
..
215213
}) => {
216214
if let [.., arm_span] = &prior_arms[..] {
217-
diag.multipart_suggestion(
218-
"consider `await`ing on both `Future`s",
219-
vec![
220-
(arm_span.shrink_to_hi(), ".await".to_string()),
221-
(exp_span.shrink_to_hi(), ".await".to_string()),
222-
],
223-
Applicability::MaybeIncorrect,
224-
);
215+
Some(ConsiderAddingAwait::BothFuturesSugg {
216+
first: arm_span.shrink_to_hi(),
217+
second: exp_span.shrink_to_hi(),
218+
})
225219
} else {
226-
diag.help("consider `await`ing on both `Future`s");
220+
Some(ConsiderAddingAwait::BothFuturesHelp)
227221
}
228222
}
229-
_ => {
230-
diag.help("consider `await`ing on both `Future`s");
231-
}
223+
_ => Some(ConsiderAddingAwait::BothFuturesHelp),
232224
},
233225
(_, Some(ty)) if self.same_type_modulo_infer(exp_found.expected, ty) => {
234-
self.suggest_await_on_future(diag, exp_span);
235-
diag.span_note(exp_span, "calling an async function returns a future");
226+
Some(ConsiderAddingAwait::FutureSuggWithNote { span: exp_span.shrink_to_hi() })
236227
}
237228
(Some(ty), _) if self.same_type_modulo_infer(ty, exp_found.found) => match cause.code()
238229
{
239230
ObligationCauseCode::Pattern { span: Some(then_span), .. } => {
240-
self.suggest_await_on_future(diag, then_span.shrink_to_hi());
231+
Some(ConsiderAddingAwait::FutureSugg { span: then_span.shrink_to_hi() })
241232
}
242233
ObligationCauseCode::IfExpression(box IfExpressionCause { then_id, .. }) => {
243234
let then_span = self.find_block_span_from_hir_id(*then_id);
244-
self.suggest_await_on_future(diag, then_span.shrink_to_hi());
235+
Some(ConsiderAddingAwait::FutureSugg { span: then_span.shrink_to_hi() })
245236
}
246237
ObligationCauseCode::MatchExpressionArm(box MatchExpressionArmCause {
247238
ref prior_arms,
248239
..
249-
}) => {
250-
diag.multipart_suggestion_verbose(
251-
"consider `await`ing on the `Future`",
252-
prior_arms
253-
.iter()
254-
.map(|arm| (arm.shrink_to_hi(), ".await".to_string()))
255-
.collect(),
256-
Applicability::MaybeIncorrect,
257-
);
258-
}
259-
_ => {}
240+
}) => Some({
241+
ConsiderAddingAwait::FutureSuggMultiple {
242+
spans: prior_arms.iter().map(|arm| arm.shrink_to_hi()).collect(),
243+
}
244+
}),
245+
_ => None,
260246
},
261-
_ => {}
247+
_ => None,
248+
};
249+
if let Some(subdiag) = subdiag {
250+
diag.subdiagnostic(subdiag);
262251
}
263252
}
264253

0 commit comments

Comments
 (0)