Skip to content

Commit c89bff2

Browse files
authored
Rollup merge of #104199 - SarthakSingh31:issue-97417-1, r=cjgillot
Keep track of the start of the argument block of a closure This removes a call to `tcx.sess.source_map()` from [compiler/rustc_trait_selection/src/traits/error_reporting/mod.rs](https://github.com/rust-lang/rust/compare/master...SarthakSingh31:issue-97417-1?expand=1#diff-8406bbc0d0b43d84c91b1933305df896ecdba0d1f9269e6744f13d87a2ab268a) as required by #97417. VsCode automatically applied `rustfmt` to the files I edited under `src/tools`. I can undo that if its a problem. r? `@cjgillot`
2 parents 4bb1575 + 8f705e2 commit c89bff2

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
@@ -1312,8 +1312,10 @@ pub struct Closure {
13121312
pub movability: Movability,
13131313
pub fn_decl: P<FnDecl>,
13141314
pub body: P<Expr>,
1315-
/// The span of the argument block `|...|`.
1315+
/// The span of the declaration block: 'move |...| -> ...'
13161316
pub fn_decl_span: Span,
1317+
/// The span of the argument block `|...|`
1318+
pub fn_arg_span: Span,
13171319
}
13181320

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

compiler/rustc_ast/src/mut_visit.rs

+1
Original file line numberDiff line numberDiff line change
@@ -1368,6 +1368,7 @@ pub fn noop_visit_expr<T: MutVisitor>(
13681368
fn_decl,
13691369
body,
13701370
fn_decl_span,
1371+
fn_arg_span: _,
13711372
}) => {
13721373
vis.visit_closure_binder(binder);
13731374
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
}
@@ -642,6 +645,7 @@ impl<'hir> LoweringContext<'_, 'hir> {
642645
fn_decl,
643646
body,
644647
fn_decl_span: self.lower_span(span),
648+
fn_arg_span: None,
645649
movability: Some(hir::Movability::Static),
646650
});
647651

@@ -898,6 +902,7 @@ impl<'hir> LoweringContext<'_, 'hir> {
898902
decl: &FnDecl,
899903
body: &Expr,
900904
fn_decl_span: Span,
905+
fn_arg_span: Span,
901906
) -> hir::ExprKind<'hir> {
902907
let (binder_clause, generic_params) = self.lower_closure_binder(binder);
903908

@@ -928,6 +933,7 @@ impl<'hir> LoweringContext<'_, 'hir> {
928933
fn_decl,
929934
body: body_id,
930935
fn_decl_span: self.lower_span(fn_decl_span),
936+
fn_arg_span: Some(self.lower_span(fn_arg_span)),
931937
movability: generator_option,
932938
});
933939

@@ -984,6 +990,7 @@ impl<'hir> LoweringContext<'_, 'hir> {
984990
decl: &FnDecl,
985991
body: &Expr,
986992
fn_decl_span: Span,
993+
fn_arg_span: Span,
987994
) -> hir::ExprKind<'hir> {
988995
if let &ClosureBinder::For { span, .. } = binder {
989996
self.tcx.sess.emit_err(NotSupportedForLifetimeBinderAsyncClosure { span });
@@ -1038,6 +1045,7 @@ impl<'hir> LoweringContext<'_, 'hir> {
10381045
fn_decl,
10391046
body,
10401047
fn_decl_span: self.lower_span(fn_decl_span),
1048+
fn_arg_span: Some(self.lower_span(fn_arg_span)),
10411049
movability: None,
10421050
});
10431051
hir::ExprKind::Closure(c)

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

+1
Original file line numberDiff line numberDiff line change
@@ -402,6 +402,7 @@ impl<'a> State<'a> {
402402
fn_decl,
403403
body,
404404
fn_decl_span: _,
405+
fn_arg_span: _,
405406
}) => {
406407
self.print_closure_binder(binder);
407408
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
@@ -943,7 +943,10 @@ pub struct Closure<'hir> {
943943
pub bound_generic_params: &'hir [GenericParam<'hir>],
944944
pub fn_decl: &'hir FnDecl<'hir>,
945945
pub body: BodyId,
946+
/// The span of the declaration block: 'move |...| -> ...'
946947
pub fn_decl_span: Span,
948+
/// The span of the argument block `|...|`
949+
pub fn_arg_span: Option<Span>,
947950
pub movability: Option<Movability>,
948951
}
949952

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
@@ -456,10 +456,11 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
456456
.iter()
457457
.map(|ty| ArgKind::from_expected_ty(*ty, None))
458458
.collect();
459-
let (closure_span, found_args) = match self.get_fn_like_arguments(expr_map_node) {
460-
Some((sp, args)) => (Some(sp), args),
461-
None => (None, Vec::new()),
462-
};
459+
let (closure_span, closure_arg_span, found_args) =
460+
match self.get_fn_like_arguments(expr_map_node) {
461+
Some((sp, arg_sp, args)) => (Some(sp), arg_sp, args),
462+
None => (None, None, Vec::new()),
463+
};
463464
let expected_span =
464465
expected_sig.cause_span.unwrap_or_else(|| self.tcx.def_span(expr_def_id));
465466
self.report_arg_count_mismatch(
@@ -468,6 +469,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
468469
expected_args,
469470
found_args,
470471
true,
472+
closure_arg_span,
471473
)
472474
.emit();
473475

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
@@ -2060,7 +2060,7 @@ impl<'a> Parser<'a> {
20602060
};
20612061

20622062
let capture_clause = self.parse_capture_clause()?;
2063-
let fn_decl = self.parse_fn_block_decl()?;
2063+
let (fn_decl, fn_arg_span) = self.parse_fn_block_decl()?;
20642064
let decl_hi = self.prev_token.span;
20652065
let mut body = match fn_decl.output {
20662066
FnRetTy::Default(_) => {
@@ -2101,6 +2101,7 @@ impl<'a> Parser<'a> {
21012101
fn_decl,
21022102
body,
21032103
fn_decl_span: lo.to(decl_hi),
2104+
fn_arg_span,
21042105
})),
21052106
);
21062107

@@ -2129,7 +2130,9 @@ impl<'a> Parser<'a> {
21292130
}
21302131

21312132
/// Parses the `|arg, arg|` header of a closure.
2132-
fn parse_fn_block_decl(&mut self) -> PResult<'a, P<FnDecl>> {
2133+
fn parse_fn_block_decl(&mut self) -> PResult<'a, (P<FnDecl>, Span)> {
2134+
let arg_start = self.token.span.lo();
2135+
21332136
let inputs = if self.eat(&token::OrOr) {
21342137
Vec::new()
21352138
} else {
@@ -2145,10 +2148,11 @@ impl<'a> Parser<'a> {
21452148
self.expect_or()?;
21462149
args
21472150
};
2151+
let arg_span = self.prev_token.span.with_lo(arg_start);
21482152
let output =
21492153
self.parse_ret_ty(AllowPlus::Yes, RecoverQPath::Yes, RecoverReturnSign::Yes)?;
21502154

2151-
Ok(P(FnDecl { inputs, output }))
2155+
Ok((P(FnDecl { inputs, output }), arg_span))
21522156
}
21532157

21542158
/// 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
@@ -71,7 +71,7 @@ pub trait InferCtxtExt<'tcx> {
7171
/// returns a span and `ArgKind` information that describes the
7272
/// arguments it expects. This can be supplied to
7373
/// `report_arg_count_mismatch`.
74-
fn get_fn_like_arguments(&self, node: Node<'_>) -> Option<(Span, Vec<ArgKind>)>;
74+
fn get_fn_like_arguments(&self, node: Node<'_>) -> Option<(Span, Option<Span>, Vec<ArgKind>)>;
7575

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

8889
/// Checks if the type implements one of `Fn`, `FnMut`, or `FnOnce`
@@ -135,15 +136,16 @@ impl<'tcx> InferCtxtExt<'tcx> for InferCtxt<'tcx> {
135136
/// returns a span and `ArgKind` information that describes the
136137
/// arguments it expects. This can be supplied to
137138
/// `report_arg_count_mismatch`.
138-
fn get_fn_like_arguments(&self, node: Node<'_>) -> Option<(Span, Vec<ArgKind>)> {
139+
fn get_fn_like_arguments(&self, node: Node<'_>) -> Option<(Span, Option<Span>, Vec<ArgKind>)> {
139140
let sm = self.tcx.sess.source_map();
140141
let hir = self.tcx.hir();
141142
Some(match node {
142143
Node::Expr(&hir::Expr {
143-
kind: hir::ExprKind::Closure(&hir::Closure { body, fn_decl_span, .. }),
144+
kind: hir::ExprKind::Closure(&hir::Closure { body, fn_decl_span, fn_arg_span, .. }),
144145
..
145146
}) => (
146147
fn_decl_span,
148+
fn_arg_span,
147149
hir.body(body)
148150
.params
149151
.iter()
@@ -174,6 +176,7 @@ impl<'tcx> InferCtxtExt<'tcx> for InferCtxt<'tcx> {
174176
kind: hir::TraitItemKind::Fn(ref sig, _), ..
175177
}) => (
176178
sig.span,
179+
None,
177180
sig.decl
178181
.inputs
179182
.iter()
@@ -188,7 +191,7 @@ impl<'tcx> InferCtxtExt<'tcx> for InferCtxt<'tcx> {
188191
),
189192
Node::Ctor(ref variant_data) => {
190193
let span = variant_data.ctor_hir_id().map_or(DUMMY_SP, |id| hir.span(id));
191-
(span, vec![ArgKind::empty(); variant_data.fields().len()])
194+
(span, None, vec![ArgKind::empty(); variant_data.fields().len()])
192195
}
193196
_ => panic!("non-FnLike node found: {:?}", node),
194197
})
@@ -204,6 +207,7 @@ impl<'tcx> InferCtxtExt<'tcx> for InferCtxt<'tcx> {
204207
expected_args: Vec<ArgKind>,
205208
found_args: Vec<ArgKind>,
206209
is_closure: bool,
210+
closure_arg_span: Option<Span>,
207211
) -> DiagnosticBuilder<'tcx, ErrorGuaranteed> {
208212
let kind = if is_closure { "closure" } else { "function" };
209213

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

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

12631257
self.report_arg_count_mismatch(
12641258
span,
12651259
closure_span,
12661260
expected,
12671261
found,
12681262
found_trait_ty.is_closure(),
1263+
closure_arg_span,
12691264
)
12701265
}
12711266
}

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)