Skip to content
This repository was archived by the owner on May 28, 2025. It is now read-only.

Commit 8f705e2

Browse files
Keep track of the start of the argument block of a closure
1 parent 872631d commit 8f705e2

File tree

15 files changed

+53
-29
lines changed

15 files changed

+53
-29
lines changed

compiler/rustc_ast/src/ast.rs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1280,8 +1280,10 @@ pub struct Closure {
12801280
pub movability: Movability,
12811281
pub fn_decl: P<FnDecl>,
12821282
pub body: P<Expr>,
1283-
/// The span of the argument block `|...|`.
1283+
/// The span of the declaration block: 'move |...| -> ...'
12841284
pub fn_decl_span: Span,
1285+
/// The span of the argument block `|...|`
1286+
pub fn_arg_span: Span,
12851287
}
12861288

12871289
/// Limit types of a range (inclusive or exclusive)

compiler/rustc_ast/src/mut_visit.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1371,6 +1371,7 @@ pub fn noop_visit_expr<T: MutVisitor>(
13711371
fn_decl,
13721372
body,
13731373
fn_decl_span,
1374+
fn_arg_span: _,
13741375
}) => {
13751376
vis.visit_closure_binder(binder);
13761377
vis.visit_asyncness(asyncness);

compiler/rustc_ast/src/visit.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -840,6 +840,7 @@ pub fn walk_expr<'a, V: Visitor<'a>>(visitor: &mut V, expression: &'a Expr) {
840840
fn_decl,
841841
body,
842842
fn_decl_span: _,
843+
fn_arg_span: _,
843844
}) => {
844845
visitor.visit_fn(FnKind::Closure(binder, fn_decl, body), expression.span, expression.id)
845846
}

compiler/rustc_ast_lowering/src/expr.rs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -176,6 +176,7 @@ impl<'hir> LoweringContext<'_, 'hir> {
176176
fn_decl,
177177
body,
178178
fn_decl_span,
179+
fn_arg_span,
179180
}) => {
180181
if let Async::Yes { closure_id, .. } = asyncness {
181182
self.lower_expr_async_closure(
@@ -186,6 +187,7 @@ impl<'hir> LoweringContext<'_, 'hir> {
186187
fn_decl,
187188
body,
188189
*fn_decl_span,
190+
*fn_arg_span,
189191
)
190192
} else {
191193
self.lower_expr_closure(
@@ -196,6 +198,7 @@ impl<'hir> LoweringContext<'_, 'hir> {
196198
fn_decl,
197199
body,
198200
*fn_decl_span,
201+
*fn_arg_span,
199202
)
200203
}
201204
}
@@ -639,6 +642,7 @@ impl<'hir> LoweringContext<'_, 'hir> {
639642
fn_decl,
640643
body,
641644
fn_decl_span: self.lower_span(span),
645+
fn_arg_span: None,
642646
movability: Some(hir::Movability::Static),
643647
});
644648

@@ -887,6 +891,7 @@ impl<'hir> LoweringContext<'_, 'hir> {
887891
decl: &FnDecl,
888892
body: &Expr,
889893
fn_decl_span: Span,
894+
fn_arg_span: Span,
890895
) -> hir::ExprKind<'hir> {
891896
let (binder_clause, generic_params) = self.lower_closure_binder(binder);
892897

@@ -917,6 +922,7 @@ impl<'hir> LoweringContext<'_, 'hir> {
917922
fn_decl,
918923
body: body_id,
919924
fn_decl_span: self.lower_span(fn_decl_span),
925+
fn_arg_span: Some(self.lower_span(fn_arg_span)),
920926
movability: generator_option,
921927
});
922928

@@ -973,6 +979,7 @@ impl<'hir> LoweringContext<'_, 'hir> {
973979
decl: &FnDecl,
974980
body: &Expr,
975981
fn_decl_span: Span,
982+
fn_arg_span: Span,
976983
) -> hir::ExprKind<'hir> {
977984
if let &ClosureBinder::For { span, .. } = binder {
978985
self.tcx.sess.emit_err(NotSupportedForLifetimeBinderAsyncClosure { span });
@@ -1027,6 +1034,7 @@ impl<'hir> LoweringContext<'_, 'hir> {
10271034
fn_decl,
10281035
body,
10291036
fn_decl_span: self.lower_span(fn_decl_span),
1037+
fn_arg_span: Some(self.lower_span(fn_arg_span)),
10301038
movability: None,
10311039
});
10321040
hir::ExprKind::Closure(c)

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -409,6 +409,7 @@ impl<'a> State<'a> {
409409
ref fn_decl,
410410
ref body,
411411
fn_decl_span: _,
412+
fn_arg_span: _,
412413
}) => {
413414
self.print_closure_binder(binder);
414415
self.print_movability(movability);

compiler/rustc_expand/src/build.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -539,6 +539,9 @@ impl<'a> ExtCtxt<'a> {
539539
fn_decl,
540540
body,
541541
fn_decl_span: span,
542+
// FIXME(SarthakSingh31): This points to the start of the declaration block and
543+
// not the span of the argument block.
544+
fn_arg_span: span,
542545
})),
543546
)
544547
}

compiler/rustc_hir/src/hir.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -928,7 +928,10 @@ pub struct Closure<'hir> {
928928
pub bound_generic_params: &'hir [GenericParam<'hir>],
929929
pub fn_decl: &'hir FnDecl<'hir>,
930930
pub body: BodyId,
931+
/// The span of the declaration block: 'move |...| -> ...'
931932
pub fn_decl_span: Span,
933+
/// The span of the argument block `|...|`
934+
pub fn_arg_span: Option<Span>,
932935
pub movability: Option<Movability>,
933936
}
934937

compiler/rustc_hir/src/intravisit.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -740,6 +740,7 @@ pub fn walk_expr<'v, V: Visitor<'v>>(visitor: &mut V, expression: &'v Expr<'v>)
740740
body,
741741
capture_clause: _,
742742
fn_decl_span: _,
743+
fn_arg_span: _,
743744
movability: _,
744745
}) => {
745746
walk_list!(visitor, visit_generic_param, bound_generic_params);

compiler/rustc_hir_pretty/src/lib.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1480,6 +1480,7 @@ impl<'a> State<'a> {
14801480
fn_decl,
14811481
body,
14821482
fn_decl_span: _,
1483+
fn_arg_span: _,
14831484
movability: _,
14841485
def_id: _,
14851486
}) => {

compiler/rustc_hir_typeck/src/closure.rs

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -457,10 +457,11 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
457457
.iter()
458458
.map(|ty| ArgKind::from_expected_ty(*ty, None))
459459
.collect();
460-
let (closure_span, found_args) = match self.get_fn_like_arguments(expr_map_node) {
461-
Some((sp, args)) => (Some(sp), args),
462-
None => (None, Vec::new()),
463-
};
460+
let (closure_span, closure_arg_span, found_args) =
461+
match self.get_fn_like_arguments(expr_map_node) {
462+
Some((sp, arg_sp, args)) => (Some(sp), arg_sp, args),
463+
None => (None, None, Vec::new()),
464+
};
464465
let expected_span =
465466
expected_sig.cause_span.unwrap_or_else(|| self.tcx.def_span(expr_def_id));
466467
self.report_arg_count_mismatch(
@@ -469,6 +470,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
469470
expected_args,
470471
found_args,
471472
true,
473+
closure_arg_span,
472474
)
473475
.emit();
474476

0 commit comments

Comments
 (0)