Make async removal span more resilient to macro expansions
This commit is contained in:
parent
d4200276f2
commit
6d6c904431
7 changed files with 68 additions and 24 deletions
|
|
@ -108,7 +108,7 @@ pub struct BaseExpressionDoubleDot {
|
|||
pub struct AwaitOnlyInAsyncFnAndBlocks {
|
||||
#[primary_span]
|
||||
#[label]
|
||||
pub dot_await_span: Span,
|
||||
pub await_kw_span: Span,
|
||||
#[label(ast_lowering_this_not_async)]
|
||||
pub item_span: Option<Span>,
|
||||
}
|
||||
|
|
|
|||
|
|
@ -185,15 +185,7 @@ impl<'hir> LoweringContext<'_, 'hir> {
|
|||
hir::AsyncGeneratorKind::Block,
|
||||
|this| this.with_new_scopes(|this| this.lower_block_expr(block)),
|
||||
),
|
||||
ExprKind::Await(expr, await_kw_span) => {
|
||||
let await_kw_span = if expr.span.hi() < await_kw_span.hi() {
|
||||
*await_kw_span
|
||||
} else {
|
||||
// this is a recovered `await expr`
|
||||
e.span
|
||||
};
|
||||
self.lower_expr_await(await_kw_span, expr)
|
||||
}
|
||||
ExprKind::Await(expr, await_kw_span) => self.lower_expr_await(*await_kw_span, expr),
|
||||
ExprKind::Closure(box Closure {
|
||||
binder,
|
||||
capture_clause,
|
||||
|
|
@ -710,7 +702,7 @@ impl<'hir> LoweringContext<'_, 'hir> {
|
|||
Some(hir::GeneratorKind::Async(_)) => {}
|
||||
Some(hir::GeneratorKind::Gen) | None => {
|
||||
self.tcx.sess.emit_err(AwaitOnlyInAsyncFnAndBlocks {
|
||||
dot_await_span: await_kw_span,
|
||||
await_kw_span,
|
||||
item_span: self.current_item,
|
||||
});
|
||||
}
|
||||
|
|
|
|||
|
|
@ -1592,23 +1592,25 @@ impl<'tcx> TypeErrCtxtExt<'tcx> for TypeErrCtxt<'_, 'tcx> {
|
|||
// could also check if it is an fn call (very likely) and suggest changing *that*, if
|
||||
// it is from the local crate.
|
||||
|
||||
if let hir::Node::Expr(parent_expr) = hir.get_parent(*hir_id)
|
||||
// Peel off the DesugaringKind from the span
|
||||
&& let Some(desugar_parent_span) = parent_expr.span.parent_callsite()
|
||||
// use nth(1) to skip one layer of desugaring from `IntoIter::into_iter`
|
||||
if let Some((_, hir::Node::Expr(await_expr))) = hir.parent_iter(*hir_id).nth(1)
|
||||
&& let Some(expr_span) = expr.span.find_ancestor_inside(await_expr.span)
|
||||
{
|
||||
let removal_span = self.tcx
|
||||
.sess
|
||||
.source_map()
|
||||
.span_extend_while(expr.span, char::is_whitespace)
|
||||
.unwrap_or(expr.span)
|
||||
.span_extend_while(expr_span, char::is_whitespace)
|
||||
.unwrap_or(expr_span)
|
||||
.shrink_to_hi()
|
||||
.to(desugar_parent_span);
|
||||
.to(await_expr.span.shrink_to_hi());
|
||||
err.span_suggestion(
|
||||
removal_span,
|
||||
"remove the `.await`",
|
||||
"",
|
||||
Applicability::MachineApplicable,
|
||||
);
|
||||
} else {
|
||||
err.span_label(obligation.cause.span, "remove the `.await`");
|
||||
}
|
||||
// FIXME: account for associated `async fn`s.
|
||||
if let hir::Expr { span, kind: hir::ExprKind::Call(base, _), .. } = expr {
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue