Skip to content

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

+3-1
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

+1
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

+1
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

+8
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

+1
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

+3
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

+3
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

+1
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

+1
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

+6-4
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

compiler/rustc_middle/src/hir/map/mod.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -1022,7 +1022,7 @@ impl<'hir> Map<'hir> {
10221022
..
10231023
}) => {
10241024
// Ensure that the returned span has the item's SyntaxContext.
1025-
fn_decl_span.find_ancestor_in_same_ctxt(*span).unwrap_or(*span)
1025+
fn_decl_span.find_ancestor_inside(*span).unwrap_or(*span)
10261026
}
10271027
_ => self.span_with_body(hir_id),
10281028
};

compiler/rustc_parse/src/parser/expr.rs

+7-3
Original file line numberDiff line numberDiff line change
@@ -2057,7 +2057,7 @@ impl<'a> Parser<'a> {
20572057
};
20582058

20592059
let capture_clause = self.parse_capture_clause()?;
2060-
let fn_decl = self.parse_fn_block_decl()?;
2060+
let (fn_decl, fn_arg_span) = self.parse_fn_block_decl()?;
20612061
let decl_hi = self.prev_token.span;
20622062
let mut body = match fn_decl.output {
20632063
FnRetTy::Default(_) => {
@@ -2098,6 +2098,7 @@ impl<'a> Parser<'a> {
20982098
fn_decl,
20992099
body,
21002100
fn_decl_span: lo.to(decl_hi),
2101+
fn_arg_span,
21012102
})),
21022103
);
21032104

@@ -2126,7 +2127,9 @@ impl<'a> Parser<'a> {
21262127
}
21272128

21282129
/// Parses the `|arg, arg|` header of a closure.
2129-
fn parse_fn_block_decl(&mut self) -> PResult<'a, P<FnDecl>> {
2130+
fn parse_fn_block_decl(&mut self) -> PResult<'a, (P<FnDecl>, Span)> {
2131+
let arg_start = self.token.span.lo();
2132+
21302133
let inputs = if self.eat(&token::OrOr) {
21312134
Vec::new()
21322135
} else {
@@ -2142,10 +2145,11 @@ impl<'a> Parser<'a> {
21422145
self.expect_or()?;
21432146
args
21442147
};
2148+
let arg_span = self.prev_token.span.with_lo(arg_start);
21452149
let output =
21462150
self.parse_ret_ty(AllowPlus::Yes, RecoverQPath::Yes, RecoverReturnSign::Yes)?;
21472151

2148-
Ok(P(FnDecl { inputs, output }))
2152+
Ok((P(FnDecl { inputs, output }), arg_span))
21492153
}
21502154

21512155
/// Parses a parameter in a closure header (e.g., `|arg, arg|`).

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

+15-20
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,7 @@ pub trait InferCtxtExt<'tcx> {
7070
/// returns a span and `ArgKind` information that describes the
7171
/// arguments it expects. This can be supplied to
7272
/// `report_arg_count_mismatch`.
73-
fn get_fn_like_arguments(&self, node: Node<'_>) -> Option<(Span, Vec<ArgKind>)>;
73+
fn get_fn_like_arguments(&self, node: Node<'_>) -> Option<(Span, Option<Span>, Vec<ArgKind>)>;
7474

7575
/// Reports an error when the number of arguments needed by a
7676
/// trait match doesn't match the number that the expression
@@ -82,6 +82,7 @@ pub trait InferCtxtExt<'tcx> {
8282
expected_args: Vec<ArgKind>,
8383
found_args: Vec<ArgKind>,
8484
is_closure: bool,
85+
closure_pipe_span: Option<Span>,
8586
) -> DiagnosticBuilder<'tcx, ErrorGuaranteed>;
8687

8788
/// Checks if the type implements one of `Fn`, `FnMut`, or `FnOnce`
@@ -134,15 +135,16 @@ impl<'tcx> InferCtxtExt<'tcx> for InferCtxt<'tcx> {
134135
/// returns a span and `ArgKind` information that describes the
135136
/// arguments it expects. This can be supplied to
136137
/// `report_arg_count_mismatch`.
137-
fn get_fn_like_arguments(&self, node: Node<'_>) -> Option<(Span, Vec<ArgKind>)> {
138+
fn get_fn_like_arguments(&self, node: Node<'_>) -> Option<(Span, Option<Span>, Vec<ArgKind>)> {
138139
let sm = self.tcx.sess.source_map();
139140
let hir = self.tcx.hir();
140141
Some(match node {
141142
Node::Expr(&hir::Expr {
142-
kind: hir::ExprKind::Closure(&hir::Closure { body, fn_decl_span, .. }),
143+
kind: hir::ExprKind::Closure(&hir::Closure { body, fn_decl_span, fn_arg_span, .. }),
143144
..
144145
}) => (
145146
fn_decl_span,
147+
fn_arg_span,
146148
hir.body(body)
147149
.params
148150
.iter()
@@ -173,6 +175,7 @@ impl<'tcx> InferCtxtExt<'tcx> for InferCtxt<'tcx> {
173175
kind: hir::TraitItemKind::Fn(ref sig, _), ..
174176
}) => (
175177
sig.span,
178+
None,
176179
sig.decl
177180
.inputs
178181
.iter()
@@ -187,7 +190,7 @@ impl<'tcx> InferCtxtExt<'tcx> for InferCtxt<'tcx> {
187190
),
188191
Node::Ctor(ref variant_data) => {
189192
let span = variant_data.ctor_hir_id().map_or(DUMMY_SP, |id| hir.span(id));
190-
(span, vec![ArgKind::empty(); variant_data.fields().len()])
193+
(span, None, vec![ArgKind::empty(); variant_data.fields().len()])
191194
}
192195
_ => panic!("non-FnLike node found: {:?}", node),
193196
})
@@ -203,6 +206,7 @@ impl<'tcx> InferCtxtExt<'tcx> for InferCtxt<'tcx> {
203206
expected_args: Vec<ArgKind>,
204207
found_args: Vec<ArgKind>,
205208
is_closure: bool,
209+
closure_arg_span: Option<Span>,
206210
) -> DiagnosticBuilder<'tcx, ErrorGuaranteed> {
207211
let kind = if is_closure { "closure" } else { "function" };
208212

@@ -240,24 +244,13 @@ impl<'tcx> InferCtxtExt<'tcx> for InferCtxt<'tcx> {
240244
if let Some(found_span) = found_span {
241245
err.span_label(found_span, format!("takes {}", found_str));
242246

243-
// move |_| { ... }
244-
// ^^^^^^^^-- def_span
245-
//
246-
// move |_| { ... }
247-
// ^^^^^-- prefix
248-
let prefix_span = self.tcx.sess.source_map().span_until_non_whitespace(found_span);
249-
// move |_| { ... }
250-
// ^^^-- pipe_span
251-
let pipe_span =
252-
if let Some(span) = found_span.trim_start(prefix_span) { span } else { found_span };
253-
254247
// Suggest to take and ignore the arguments with expected_args_length `_`s if
255248
// found arguments is empty (assume the user just wants to ignore args in this case).
256249
// For example, if `expected_args_length` is 2, suggest `|_, _|`.
257250
if found_args.is_empty() && is_closure {
258251
let underscores = vec!["_"; expected_args.len()].join(", ");
259252
err.span_suggestion_verbose(
260-
pipe_span,
253+
closure_arg_span.unwrap_or(found_span),
261254
&format!(
262255
"consider changing the closure to take and ignore the expected argument{}",
263256
pluralize!(expected_args.len())
@@ -1251,20 +1244,22 @@ impl<'tcx> TypeErrCtxtExt<'tcx> for TypeErrCtxt<'_, 'tcx> {
12511244
obligation.cause.code(),
12521245
)
12531246
} else {
1254-
let (closure_span, found) = found_did
1247+
let (closure_span, closure_arg_span, found) = found_did
12551248
.and_then(|did| {
12561249
let node = self.tcx.hir().get_if_local(did)?;
1257-
let (found_span, found) = self.get_fn_like_arguments(node)?;
1258-
Some((Some(found_span), found))
1250+
let (found_span, closure_arg_span, found) =
1251+
self.get_fn_like_arguments(node)?;
1252+
Some((Some(found_span), closure_arg_span, found))
12591253
})
1260-
.unwrap_or((found_span, found));
1254+
.unwrap_or((found_span, None, found));
12611255

12621256
self.report_arg_count_mismatch(
12631257
span,
12641258
closure_span,
12651259
expected,
12661260
found,
12671261
found_trait_ty.is_closure(),
1262+
closure_arg_span,
12681263
)
12691264
}
12701265
}

src/test/ui-fulldeps/pprust-expr-roundtrip.rs

+1
Original file line numberDiff line numberDiff line change
@@ -126,6 +126,7 @@ fn iter_exprs(depth: usize, f: &mut dyn FnMut(P<Expr>)) {
126126
fn_decl: decl.clone(),
127127
body: e,
128128
fn_decl_span: DUMMY_SP,
129+
fn_arg_span: DUMMY_SP,
129130
})))
130131
});
131132
}

src/tools/rustfmt/src/closures.rs

+1
Original file line numberDiff line numberDiff line change
@@ -335,6 +335,7 @@ pub(crate) fn rewrite_last_closure(
335335
ref fn_decl,
336336
ref body,
337337
fn_decl_span: _,
338+
fn_arg_span: _,
338339
} = **closure;
339340
let body = match body.kind {
340341
ast::ExprKind::Block(ref block, _)

0 commit comments

Comments
 (0)