Skip to content

Commit 7d05157

Browse files
authored
Rollup merge of #127058 - compiler-errors:tighten-async-spans, r=oli-obk
Tighten `fn_decl_span` for async blocks Tightens the span of `async {}` blocks in diagnostics, and subsequently async closures and async fns, by actually setting the `fn_decl_span` correctly. This is kinda a follow-up on #125078, but it fixes the problem in a more general way. I think the diagnostics are significantly improved, since we no longer have a bunch of overlapping spans. I'll point out one caveat where I think the diagnostic may get a bit more confusing, but where I don't think it matters. r? ``@estebank`` or ``@oli-obk`` or someone else on wg-diag or compiler i dont really care lol
2 parents 613c5d5 + 789ee88 commit 7d05157

File tree

43 files changed

+173
-204
lines changed

Some content is hidden

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

43 files changed

+173
-204
lines changed

compiler/rustc_ast/src/ast.rs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1454,7 +1454,10 @@ pub enum ExprKind {
14541454
Block(P<Block>, Option<Label>),
14551455
/// An `async` block (`async move { ... }`),
14561456
/// or a `gen` block (`gen move { ... }`)
1457-
Gen(CaptureBy, P<Block>, GenBlockKind),
1457+
///
1458+
/// The span is the "decl", which is the header before the body `{ }`
1459+
/// including the `asyng`/`gen` keywords and possibly `move`.
1460+
Gen(CaptureBy, P<Block>, GenBlockKind, Span),
14581461
/// An await expression (`my_future.await`). Span is of await keyword.
14591462
Await(P<Expr>, Span),
14601463

compiler/rustc_ast/src/mut_visit.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1528,8 +1528,9 @@ pub fn noop_visit_expr<T: MutVisitor>(
15281528
visit_opt(label, |label| vis.visit_label(label));
15291529
vis.visit_block(blk);
15301530
}
1531-
ExprKind::Gen(_capture_by, body, _kind) => {
1531+
ExprKind::Gen(_capture_by, body, _kind, decl_span) => {
15321532
vis.visit_block(body);
1533+
vis.visit_span(decl_span);
15331534
}
15341535
ExprKind::Await(expr, await_kw_span) => {
15351536
vis.visit_expr(expr);

compiler/rustc_ast/src/visit.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1122,7 +1122,7 @@ pub fn walk_expr<'a, V: Visitor<'a>>(visitor: &mut V, expression: &'a Expr) -> V
11221122
visit_opt!(visitor, visit_label, opt_label);
11231123
try_visit!(visitor.visit_block(block));
11241124
}
1125-
ExprKind::Gen(_capt, body, _kind) => try_visit!(visitor.visit_block(body)),
1125+
ExprKind::Gen(_capt, body, _kind, _decl_span) => try_visit!(visitor.visit_block(body)),
11261126
ExprKind::Await(expr, _span) => try_visit!(visitor.visit_expr(expr)),
11271127
ExprKind::Assign(lhs, rhs, _span) => {
11281128
try_visit!(visitor.visit_expr(lhs));

compiler/rustc_ast_lowering/src/expr.rs

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -226,7 +226,7 @@ impl<'hir> LoweringContext<'_, 'hir> {
226226
*fn_arg_span,
227227
),
228228
},
229-
ExprKind::Gen(capture_clause, block, genblock_kind) => {
229+
ExprKind::Gen(capture_clause, block, genblock_kind, decl_span) => {
230230
let desugaring_kind = match genblock_kind {
231231
GenBlockKind::Async => hir::CoroutineDesugaring::Async,
232232
GenBlockKind::Gen => hir::CoroutineDesugaring::Gen,
@@ -236,6 +236,7 @@ impl<'hir> LoweringContext<'_, 'hir> {
236236
*capture_clause,
237237
e.id,
238238
None,
239+
*decl_span,
239240
e.span,
240241
desugaring_kind,
241242
hir::CoroutineSource::Block,
@@ -615,6 +616,7 @@ impl<'hir> LoweringContext<'_, 'hir> {
615616
capture_clause: CaptureBy,
616617
closure_node_id: NodeId,
617618
return_ty: Option<hir::FnRetTy<'hir>>,
619+
fn_decl_span: Span,
618620
span: Span,
619621
desugaring_kind: hir::CoroutineDesugaring,
620622
coroutine_source: hir::CoroutineSource,
@@ -691,7 +693,7 @@ impl<'hir> LoweringContext<'_, 'hir> {
691693
bound_generic_params: &[],
692694
fn_decl,
693695
body,
694-
fn_decl_span: self.lower_span(span),
696+
fn_decl_span: self.lower_span(fn_decl_span),
695697
fn_arg_span: None,
696698
kind: hir::ClosureKind::Coroutine(coroutine_kind),
697699
constness: hir::Constness::NotConst,
@@ -1082,6 +1084,7 @@ impl<'hir> LoweringContext<'_, 'hir> {
10821084
let (parameters, expr) = this.lower_coroutine_body_with_moved_arguments(
10831085
&inner_decl,
10841086
|this| this.with_new_scopes(fn_decl_span, |this| this.lower_expr_mut(body)),
1087+
fn_decl_span,
10851088
body.span,
10861089
coroutine_kind,
10871090
hir::CoroutineSource::Closure,

compiler/rustc_ast_lowering/src/item.rs

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -212,6 +212,7 @@ impl<'hir> LoweringContext<'_, 'hir> {
212212
// declaration (decl), not the return types.
213213
let coroutine_kind = header.coroutine_kind;
214214
let body_id = this.lower_maybe_coroutine_body(
215+
*fn_sig_span,
215216
span,
216217
hir_id,
217218
decl,
@@ -819,6 +820,7 @@ impl<'hir> LoweringContext<'_, 'hir> {
819820
}
820821
AssocItemKind::Fn(box Fn { sig, generics, body: Some(body), .. }) => {
821822
let body_id = self.lower_maybe_coroutine_body(
823+
sig.span,
822824
i.span,
823825
hir_id,
824826
&sig.decl,
@@ -942,6 +944,7 @@ impl<'hir> LoweringContext<'_, 'hir> {
942944
),
943945
AssocItemKind::Fn(box Fn { sig, generics, body, .. }) => {
944946
let body_id = self.lower_maybe_coroutine_body(
947+
sig.span,
945948
i.span,
946949
hir_id,
947950
&sig.decl,
@@ -1140,6 +1143,7 @@ impl<'hir> LoweringContext<'_, 'hir> {
11401143
/// `gen {}` block as appropriate.
11411144
fn lower_maybe_coroutine_body(
11421145
&mut self,
1146+
fn_decl_span: Span,
11431147
span: Span,
11441148
fn_id: hir::HirId,
11451149
decl: &FnDecl,
@@ -1153,6 +1157,7 @@ impl<'hir> LoweringContext<'_, 'hir> {
11531157
let (parameters, expr) = this.lower_coroutine_body_with_moved_arguments(
11541158
decl,
11551159
|this| this.lower_block_expr(body),
1160+
fn_decl_span,
11561161
body.span,
11571162
coroutine_kind,
11581163
hir::CoroutineSource::Fn,
@@ -1174,6 +1179,7 @@ impl<'hir> LoweringContext<'_, 'hir> {
11741179
&mut self,
11751180
decl: &FnDecl,
11761181
lower_body: impl FnOnce(&mut LoweringContext<'_, 'hir>) -> hir::Expr<'hir>,
1182+
fn_decl_span: Span,
11771183
body_span: Span,
11781184
coroutine_kind: CoroutineKind,
11791185
coroutine_source: hir::CoroutineSource,
@@ -1344,13 +1350,6 @@ impl<'hir> LoweringContext<'_, 'hir> {
13441350
};
13451351
let closure_id = coroutine_kind.closure_id();
13461352

1347-
let span = if let FnRetTy::Default(span) = decl.output
1348-
&& matches!(coroutine_source, rustc_hir::CoroutineSource::Closure)
1349-
{
1350-
body_span.with_lo(span.lo())
1351-
} else {
1352-
body_span
1353-
};
13541353
let coroutine_expr = self.make_desugared_coroutine_expr(
13551354
// The default capture mode here is by-ref. Later on during upvar analysis,
13561355
// we will force the captured arguments to by-move, but for async closures,
@@ -1359,7 +1358,8 @@ impl<'hir> LoweringContext<'_, 'hir> {
13591358
CaptureBy::Ref,
13601359
closure_id,
13611360
None,
1362-
span,
1361+
fn_decl_span,
1362+
body_span,
13631363
desugaring_kind,
13641364
coroutine_source,
13651365
mkbody,

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -540,7 +540,7 @@ impl<'a> State<'a> {
540540
self.ibox(0);
541541
self.print_block_with_attrs(blk, attrs);
542542
}
543-
ast::ExprKind::Gen(capture_clause, blk, kind) => {
543+
ast::ExprKind::Gen(capture_clause, blk, kind, _decl_span) => {
544544
self.word_nbsp(kind.modifier());
545545
self.print_capture_clause(*capture_clause);
546546
// cbox/ibox in analogy to the `ExprKind::Block` arm above

compiler/rustc_builtin_macros/src/assert/context.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -298,7 +298,7 @@ impl<'cx, 'a> Context<'cx, 'a> {
298298
// sync with the `rfc-2011-nicer-assert-messages/all-expr-kinds.rs` test.
299299
ExprKind::Assign(_, _, _)
300300
| ExprKind::AssignOp(_, _, _)
301-
| ExprKind::Gen(_, _, _)
301+
| ExprKind::Gen(_, _, _, _)
302302
| ExprKind::Await(_, _)
303303
| ExprKind::Block(_, _)
304304
| ExprKind::Break(_, _)

compiler/rustc_parse/src/parser/expr.rs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3432,8 +3432,9 @@ impl<'a> Parser<'a> {
34323432
}
34333433
}
34343434
let capture_clause = self.parse_capture_clause()?;
3435+
let decl_span = lo.to(self.prev_token.span);
34353436
let (attrs, body) = self.parse_inner_attrs_and_block()?;
3436-
let kind = ExprKind::Gen(capture_clause, body, kind);
3437+
let kind = ExprKind::Gen(capture_clause, body, kind, decl_span);
34373438
Ok(self.mk_expr_with_attrs(lo.to(self.prev_token.span), kind, attrs))
34383439
}
34393440

@@ -4022,7 +4023,7 @@ impl MutVisitor for CondChecker<'_> {
40224023
| ExprKind::Match(_, _, _)
40234024
| ExprKind::Closure(_)
40244025
| ExprKind::Block(_, _)
4025-
| ExprKind::Gen(_, _, _)
4026+
| ExprKind::Gen(_, _, _, _)
40264027
| ExprKind::TryBlock(_)
40274028
| ExprKind::Underscore
40284029
| ExprKind::Path(_, _)

compiler/rustc_resolve/src/def_collector.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -334,7 +334,7 @@ impl<'a, 'b, 'tcx> visit::Visitor<'a> for DefCollector<'a, 'b, 'tcx> {
334334
None => closure_def,
335335
}
336336
}
337-
ExprKind::Gen(_, _, _) => {
337+
ExprKind::Gen(_, _, _, _) => {
338338
self.create_def(expr.id, kw::Empty, DefKind::Closure, expr.span)
339339
}
340340
ExprKind::ConstBlock(ref constant) => {

src/tools/clippy/clippy_lints/src/suspicious_operation_groupings.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -549,7 +549,7 @@ fn ident_difference_expr_with_base_location(
549549
| (Assign(_, _, _), Assign(_, _, _))
550550
| (TryBlock(_), TryBlock(_))
551551
| (Await(_, _), Await(_, _))
552-
| (Gen(_, _, _), Gen(_, _, _))
552+
| (Gen(_, _, _, _), Gen(_, _, _, _))
553553
| (Block(_, _), Block(_, _))
554554
| (Closure(_), Closure(_))
555555
| (Match(_, _, _), Match(_, _, _))

0 commit comments

Comments
 (0)