Skip to content

Commit f0fc4f9

Browse files
Tweak await span
1 parent 12a2f24 commit f0fc4f9

File tree

86 files changed

+479
-400
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

86 files changed

+479
-400
lines changed

compiler/rustc_ast/src/ast.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1430,8 +1430,8 @@ pub enum ExprKind {
14301430
/// The async block used to have a `NodeId`, which was removed in favor of
14311431
/// using the parent `NodeId` of the parent `Expr`.
14321432
Async(CaptureBy, P<Block>),
1433-
/// An await expression (`my_future.await`).
1434-
Await(P<Expr>),
1433+
/// An await expression (`my_future.await`). Span is of await keyword.
1434+
Await(P<Expr>, Span),
14351435

14361436
/// A try block (`try { ... }`).
14371437
TryBlock(P<Block>),

compiler/rustc_ast/src/mut_visit.rs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1415,7 +1415,10 @@ pub fn noop_visit_expr<T: MutVisitor>(
14151415
ExprKind::Async(_capture_by, body) => {
14161416
vis.visit_block(body);
14171417
}
1418-
ExprKind::Await(expr) => vis.visit_expr(expr),
1418+
ExprKind::Await(expr, await_kw_span) => {
1419+
vis.visit_expr(expr);
1420+
vis.visit_span(await_kw_span);
1421+
}
14191422
ExprKind::Assign(el, er, _) => {
14201423
vis.visit_expr(el);
14211424
vis.visit_expr(er);

compiler/rustc_ast/src/util/parser.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -388,7 +388,7 @@ pub fn contains_exterior_struct_lit(value: &ast::Expr) -> bool {
388388
// X { y: 1 } + X { y: 2 }
389389
contains_exterior_struct_lit(lhs) || contains_exterior_struct_lit(rhs)
390390
}
391-
ast::ExprKind::Await(x)
391+
ast::ExprKind::Await(x, _)
392392
| ast::ExprKind::Unary(_, x)
393393
| ast::ExprKind::Cast(x, _)
394394
| ast::ExprKind::Type(x, _)

compiler/rustc_ast/src/visit.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -864,7 +864,7 @@ pub fn walk_expr<'a, V: Visitor<'a>>(visitor: &mut V, expression: &'a Expr) {
864864
ExprKind::Async(_, body) => {
865865
visitor.visit_block(body);
866866
}
867-
ExprKind::Await(expr) => visitor.visit_expr(expr),
867+
ExprKind::Await(expr, _) => visitor.visit_expr(expr),
868868
ExprKind::Assign(lhs, rhs, _) => {
869869
visitor.visit_expr(lhs);
870870
visitor.visit_expr(rhs);

compiler/rustc_ast_lowering/src/expr.rs

Lines changed: 8 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -185,20 +185,14 @@ impl<'hir> LoweringContext<'_, 'hir> {
185185
hir::AsyncGeneratorKind::Block,
186186
|this| this.with_new_scopes(|this| this.lower_block_expr(block)),
187187
),
188-
ExprKind::Await(expr) => {
189-
let dot_await_span = if expr.span.hi() < e.span.hi() {
190-
let span_with_whitespace = self
191-
.tcx
192-
.sess
193-
.source_map()
194-
.span_extend_while(expr.span, char::is_whitespace)
195-
.unwrap_or(expr.span);
196-
span_with_whitespace.shrink_to_hi().with_hi(e.span.hi())
188+
ExprKind::Await(expr, await_kw_span) => {
189+
let await_kw_span = if expr.span.hi() < await_kw_span.hi() {
190+
*await_kw_span
197191
} else {
198192
// this is a recovered `await expr`
199193
e.span
200194
};
201-
self.lower_expr_await(dot_await_span, expr)
195+
self.lower_expr_await(await_kw_span, expr)
202196
}
203197
ExprKind::Closure(box Closure {
204198
binder,
@@ -710,18 +704,18 @@ impl<'hir> LoweringContext<'_, 'hir> {
710704
/// }
711705
/// }
712706
/// ```
713-
fn lower_expr_await(&mut self, dot_await_span: Span, expr: &Expr) -> hir::ExprKind<'hir> {
714-
let full_span = expr.span.to(dot_await_span);
707+
fn lower_expr_await(&mut self, await_kw_span: Span, expr: &Expr) -> hir::ExprKind<'hir> {
708+
let full_span = expr.span.to(await_kw_span);
715709
match self.generator_kind {
716710
Some(hir::GeneratorKind::Async(_)) => {}
717711
Some(hir::GeneratorKind::Gen) | None => {
718712
self.tcx.sess.emit_err(AwaitOnlyInAsyncFnAndBlocks {
719-
dot_await_span,
713+
dot_await_span: await_kw_span,
720714
item_span: self.current_item,
721715
});
722716
}
723717
}
724-
let span = self.mark_span_with_reason(DesugaringKind::Await, dot_await_span, None);
718+
let span = self.mark_span_with_reason(DesugaringKind::Await, await_kw_span, None);
725719
let gen_future_span = self.mark_span_with_reason(
726720
DesugaringKind::Await,
727721
full_span,

compiler/rustc_ast_lowering/src/format.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -583,7 +583,7 @@ fn may_contain_yield_point(e: &ast::Expr) -> bool {
583583

584584
impl Visitor<'_> for MayContainYieldPoint {
585585
fn visit_expr(&mut self, e: &ast::Expr) {
586-
if let ast::ExprKind::Await(_) | ast::ExprKind::Yield(_) = e.kind {
586+
if let ast::ExprKind::Await(_, _) | ast::ExprKind::Yield(_) = e.kind {
587587
self.0 = true;
588588
} else {
589589
visit::walk_expr(self, e);

compiler/rustc_ast_pretty/src/pprust/state/expr.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -447,7 +447,7 @@ impl<'a> State<'a> {
447447
self.ibox(0);
448448
self.print_block_with_attrs(blk, attrs);
449449
}
450-
ast::ExprKind::Await(expr) => {
450+
ast::ExprKind::Await(expr, _) => {
451451
self.print_expr_maybe_paren(expr, parser::PREC_POSTFIX);
452452
self.word(".await");
453453
}

compiler/rustc_builtin_macros/src/assert/context.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -288,7 +288,7 @@ impl<'cx, 'a> Context<'cx, 'a> {
288288
ExprKind::Assign(_, _, _)
289289
| ExprKind::AssignOp(_, _, _)
290290
| ExprKind::Async(_, _)
291-
| ExprKind::Await(_)
291+
| ExprKind::Await(_, _)
292292
| ExprKind::Block(_, _)
293293
| ExprKind::Break(_, _)
294294
| ExprKind::Closure(_)

compiler/rustc_parse/src/parser/diagnostics.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1646,7 +1646,7 @@ impl<'a> Parser<'a> {
16461646
// Avoid knock-down errors as we don't know whether to interpret this as `foo().await?`
16471647
// or `foo()?.await` (the very reason we went with postfix syntax 😅).
16481648
ExprKind::Try(_) => ExprKind::Err,
1649-
_ => ExprKind::Await(expr),
1649+
_ => ExprKind::Await(expr, await_sp),
16501650
};
16511651
let expr = self.mk_expr(lo.to(sp), kind);
16521652
self.maybe_recover_from_bad_qpath(expr)

compiler/rustc_parse/src/parser/expr.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -859,7 +859,7 @@ impl<'a> Parser<'a> {
859859
ExprKind::Field(_, _) => "a field access",
860860
ExprKind::MethodCall(_) => "a method call",
861861
ExprKind::Call(_, _) => "a function call",
862-
ExprKind::Await(_) => "`.await`",
862+
ExprKind::Await(_, _) => "`.await`",
863863
ExprKind::Err => return Ok(with_postfix),
864864
_ => unreachable!("parse_dot_or_call_expr_with_ shouldn't produce this"),
865865
}
@@ -3256,7 +3256,7 @@ impl<'a> Parser<'a> {
32563256

32573257
fn mk_await_expr(&mut self, self_arg: P<Expr>, lo: Span) -> P<Expr> {
32583258
let span = lo.to(self.prev_token.span);
3259-
let await_expr = self.mk_expr(span, ExprKind::Await(self_arg));
3259+
let await_expr = self.mk_expr(span, ExprKind::Await(self_arg, self.prev_token.span));
32603260
self.recover_from_await_method_call();
32613261
await_expr
32623262
}

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

Lines changed: 45 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -1583,55 +1583,59 @@ impl<'tcx> TypeErrCtxtExt<'tcx> for TypeErrCtxt<'_, 'tcx> {
15831583
}
15841584

15851585
fn suggest_remove_await(&self, obligation: &PredicateObligation<'tcx>, err: &mut Diagnostic) {
1586-
let span = obligation.cause.span;
1587-
1588-
if let ObligationCauseCode::AwaitableExpr(hir_id) = obligation.cause.code().peel_derives() {
1589-
let hir = self.tcx.hir();
1590-
if let Some(hir::Node::Expr(expr)) = hir_id.and_then(|hir_id| hir.find(hir_id)) {
1591-
// FIXME: use `obligation.predicate.kind()...trait_ref.self_ty()` to see if we have `()`
1592-
// and if not maybe suggest doing something else? If we kept the expression around we
1593-
// could also check if it is an fn call (very likely) and suggest changing *that*, if
1594-
// it is from the local crate.
1586+
let hir = self.tcx.hir();
1587+
if let ObligationCauseCode::AwaitableExpr(Some(hir_id)) = obligation.cause.code().peel_derives()
1588+
&& let hir::Node::Expr(expr) = hir.get(*hir_id)
1589+
{
1590+
// FIXME: use `obligation.predicate.kind()...trait_ref.self_ty()` to see if we have `()`
1591+
// and if not maybe suggest doing something else? If we kept the expression around we
1592+
// could also check if it is an fn call (very likely) and suggest changing *that*, if
1593+
// it is from the local crate.
1594+
1595+
if let hir::Node::Expr(parent_expr) = hir.get_parent(*hir_id)
1596+
// Peel off the DesugaringKind from the span
1597+
&& let Some(desugar_parent_span) = parent_expr.span.parent_callsite()
1598+
{
15951599
err.span_suggestion(
1596-
span,
1600+
self.tcx.sess.source.shrink_to_hi().to(desugar_parent_span),
15971601
"remove the `.await`",
15981602
"",
15991603
Applicability::MachineApplicable,
16001604
);
1601-
// FIXME: account for associated `async fn`s.
1602-
if let hir::Expr { span, kind: hir::ExprKind::Call(base, _), .. } = expr {
1603-
if let ty::PredicateKind::Clause(ty::Clause::Trait(pred)) =
1604-
obligation.predicate.kind().skip_binder()
1605+
}
1606+
// FIXME: account for associated `async fn`s.
1607+
if let hir::Expr { span, kind: hir::ExprKind::Call(base, _), .. } = expr {
1608+
if let ty::PredicateKind::Clause(ty::Clause::Trait(pred)) =
1609+
obligation.predicate.kind().skip_binder()
1610+
{
1611+
err.span_label(*span, &format!("this call returns `{}`", pred.self_ty()));
1612+
}
1613+
if let Some(typeck_results) = &self.typeck_results
1614+
&& let ty = typeck_results.expr_ty_adjusted(base)
1615+
&& let ty::FnDef(def_id, _substs) = ty.kind()
1616+
&& let Some(hir::Node::Item(hir::Item { ident, span, vis_span, .. })) =
1617+
hir.get_if_local(*def_id)
16051618
{
1606-
err.span_label(*span, &format!("this call returns `{}`", pred.self_ty()));
1607-
}
1608-
if let Some(typeck_results) = &self.typeck_results
1609-
&& let ty = typeck_results.expr_ty_adjusted(base)
1610-
&& let ty::FnDef(def_id, _substs) = ty.kind()
1611-
&& let Some(hir::Node::Item(hir::Item { ident, span, vis_span, .. })) =
1612-
hir.get_if_local(*def_id)
1613-
{
1614-
let msg = format!(
1615-
"alternatively, consider making `fn {}` asynchronous",
1616-
ident
1619+
let msg = format!(
1620+
"alternatively, consider making `fn {}` asynchronous",
1621+
ident
1622+
);
1623+
if vis_span.is_empty() {
1624+
err.span_suggestion_verbose(
1625+
span.shrink_to_lo(),
1626+
&msg,
1627+
"async ",
1628+
Applicability::MaybeIncorrect,
1629+
);
1630+
} else {
1631+
err.span_suggestion_verbose(
1632+
vis_span.shrink_to_hi(),
1633+
&msg,
1634+
" async",
1635+
Applicability::MaybeIncorrect,
16171636
);
1618-
if vis_span.is_empty() {
1619-
err.span_suggestion_verbose(
1620-
span.shrink_to_lo(),
1621-
&msg,
1622-
"async ",
1623-
Applicability::MaybeIncorrect,
1624-
);
1625-
} else {
1626-
err.span_suggestion_verbose(
1627-
vis_span.shrink_to_hi(),
1628-
&msg,
1629-
" async",
1630-
Applicability::MaybeIncorrect,
1631-
);
1632-
}
16331637
}
1634-
}
1638+
}
16351639
}
16361640
}
16371641
}

src/tools/rustfmt/src/chains.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -232,7 +232,7 @@ impl ChainItemKind {
232232
let span = mk_sp(nested.span.hi(), field.span.hi());
233233
(kind, span)
234234
}
235-
ast::ExprKind::Await(ref nested) => {
235+
ast::ExprKind::Await(ref nested, _) => {
236236
let span = mk_sp(nested.span.hi(), expr.span.hi());
237237
(ChainItemKind::Await, span)
238238
}
@@ -459,7 +459,7 @@ impl Chain {
459459
ast::ExprKind::MethodCall(ref call) => Some(Self::convert_try(&call.receiver, context)),
460460
ast::ExprKind::Field(ref subexpr, _)
461461
| ast::ExprKind::Try(ref subexpr)
462-
| ast::ExprKind::Await(ref subexpr) => Some(Self::convert_try(subexpr, context)),
462+
| ast::ExprKind::Await(ref subexpr, _) => Some(Self::convert_try(subexpr, context)),
463463
_ => None,
464464
}
465465
}

src/tools/rustfmt/src/expr.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -218,7 +218,7 @@ pub(crate) fn format_expr(
218218
ast::ExprKind::Try(..)
219219
| ast::ExprKind::Field(..)
220220
| ast::ExprKind::MethodCall(..)
221-
| ast::ExprKind::Await(_) => rewrite_chain(expr, context, shape),
221+
| ast::ExprKind::Await(_, _) => rewrite_chain(expr, context, shape),
222222
ast::ExprKind::MacCall(ref mac) => {
223223
rewrite_macro(mac, None, context, shape, MacroPosition::Expression).or_else(|| {
224224
wrap_str(
@@ -1889,7 +1889,7 @@ impl<'ast> RhsAssignKind<'ast> {
18891889
ast::ExprKind::Try(..)
18901890
| ast::ExprKind::Field(..)
18911891
| ast::ExprKind::MethodCall(..)
1892-
| ast::ExprKind::Await(_)
1892+
| ast::ExprKind::Await(_, _)
18931893
)
18941894
}
18951895
_ => false,

0 commit comments

Comments
 (0)