Skip to content

Commit 17ba11d

Browse files
committed
Rollup merge of rust-lang#33125 - Manishearth:closure-span, r=Manishearth
Track the span corresponding to the `|...|` part of the closure. lifted from rust-lang#32756 cc rust-lang#31645 libsyntax-[breaking change]
2 parents 8d0dd78 + ecd10f0 commit 17ba11d

File tree

29 files changed

+87
-53
lines changed

29 files changed

+87
-53
lines changed

src/librustc/hir/fold.rs

+3-2
Original file line numberDiff line numberDiff line change
@@ -1060,10 +1060,11 @@ pub fn noop_fold_expr<T: Folder>(Expr { id, node, span, attrs }: Expr, folder: &
10601060
arms.move_map(|x| folder.fold_arm(x)),
10611061
source)
10621062
}
1063-
ExprClosure(capture_clause, decl, body) => {
1063+
ExprClosure(capture_clause, decl, body, fn_decl_span) => {
10641064
ExprClosure(capture_clause,
10651065
folder.fold_fn_decl(decl),
1066-
folder.fold_block(body))
1066+
folder.fold_block(body),
1067+
folder.new_span(fn_decl_span))
10671068
}
10681069
ExprBlock(blk) => ExprBlock(folder.fold_block(blk)),
10691070
ExprAssign(el, er) => {

src/librustc/hir/intravisit.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -785,7 +785,7 @@ pub fn walk_expr<'v, V: Visitor<'v>>(visitor: &mut V, expression: &'v Expr) {
785785
visitor.visit_expr(subexpression);
786786
walk_list!(visitor, visit_arm, arms);
787787
}
788-
ExprClosure(_, ref function_declaration, ref body) => {
788+
ExprClosure(_, ref function_declaration, ref body, _fn_decl_span) => {
789789
visitor.visit_fn(FnKind::Closure(expression.attrs.as_attr_slice()),
790790
function_declaration,
791791
body,

src/librustc/hir/lowering.rs

+3-2
Original file line numberDiff line numberDiff line change
@@ -1260,11 +1260,12 @@ pub fn lower_expr(lctx: &LoweringContext, e: &Expr) -> P<hir::Expr> {
12601260
arms.iter().map(|x| lower_arm(lctx, x)).collect(),
12611261
hir::MatchSource::Normal)
12621262
}
1263-
ExprKind::Closure(capture_clause, ref decl, ref body) => {
1263+
ExprKind::Closure(capture_clause, ref decl, ref body, fn_decl_span) => {
12641264
lctx.with_parent_def(e.id, || {
12651265
hir::ExprClosure(lower_capture_clause(lctx, capture_clause),
12661266
lower_fn_decl(lctx, decl),
1267-
lower_block(lctx, body))
1267+
lower_block(lctx, body),
1268+
fn_decl_span)
12681269
})
12691270
}
12701271
ExprKind::Block(ref blk) => hir::ExprBlock(lower_block(lctx, blk)),

src/librustc/hir/map/blocks.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -250,7 +250,7 @@ impl<'a> FnLikeNode<'a> {
250250
}
251251
}
252252
map::NodeExpr(e) => match e.node {
253-
ast::ExprClosure(_, ref decl, ref block) =>
253+
ast::ExprClosure(_, ref decl, ref block, _fn_decl_span) =>
254254
closure(ClosureParts::new(&decl,
255255
&block,
256256
e.id,

src/librustc/hir/mod.rs

+4-2
Original file line numberDiff line numberDiff line change
@@ -949,8 +949,10 @@ pub enum Expr_ {
949949
/// A `match` block, with a source that indicates whether or not it is
950950
/// the result of a desugaring, and if so, which kind.
951951
ExprMatch(P<Expr>, HirVec<Arm>, MatchSource),
952-
/// A closure (for example, `move |a, b, c| {a + b + c}`)
953-
ExprClosure(CaptureClause, P<FnDecl>, P<Block>),
952+
/// A closure (for example, `move |a, b, c| {a + b + c}`).
953+
///
954+
/// The final span is the span of the argument block `|...|`
955+
ExprClosure(CaptureClause, P<FnDecl>, P<Block>, Span),
954956
/// A block (`{ ... }`)
955957
ExprBlock(P<Block>),
956958

src/librustc/hir/print.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -1392,7 +1392,7 @@ impl<'a> State<'a> {
13921392
}
13931393
self.bclose_(expr.span, indent_unit)?;
13941394
}
1395-
hir::ExprClosure(capture_clause, ref decl, ref body) => {
1395+
hir::ExprClosure(capture_clause, ref decl, ref body, _fn_decl_span) => {
13961396
self.print_capture_clause(capture_clause)?;
13971397

13981398
self.print_fn_block_args(&decl)?;

src/librustc/middle/expr_use_visitor.rs

+5-5
Original file line numberDiff line numberDiff line change
@@ -537,8 +537,8 @@ impl<'d,'t,'a,'tcx> ExprUseVisitor<'d,'t,'a,'tcx> {
537537
self.consume_expr(&count);
538538
}
539539

540-
hir::ExprClosure(..) => {
541-
self.walk_captures(expr)
540+
hir::ExprClosure(_, _, _, fn_decl_span) => {
541+
self.walk_captures(expr, fn_decl_span)
542542
}
543543

544544
hir::ExprBox(ref base) => {
@@ -1142,7 +1142,7 @@ impl<'d,'t,'a,'tcx> ExprUseVisitor<'d,'t,'a,'tcx> {
11421142
}));
11431143
}
11441144

1145-
fn walk_captures(&mut self, closure_expr: &hir::Expr) {
1145+
fn walk_captures(&mut self, closure_expr: &hir::Expr, fn_decl_span: Span) {
11461146
debug!("walk_captures({:?})", closure_expr);
11471147

11481148
self.tcx().with_freevars(closure_expr.id, |freevars| {
@@ -1152,7 +1152,7 @@ impl<'d,'t,'a,'tcx> ExprUseVisitor<'d,'t,'a,'tcx> {
11521152
closure_expr_id: closure_expr.id };
11531153
let upvar_capture = self.typer.upvar_capture(upvar_id).unwrap();
11541154
let cmt_var = return_if_err!(self.cat_captured_var(closure_expr.id,
1155-
closure_expr.span,
1155+
fn_decl_span,
11561156
freevar.def));
11571157
match upvar_capture {
11581158
ty::UpvarCapture::ByValue => {
@@ -1161,7 +1161,7 @@ impl<'d,'t,'a,'tcx> ExprUseVisitor<'d,'t,'a,'tcx> {
11611161
}
11621162
ty::UpvarCapture::ByRef(upvar_borrow) => {
11631163
self.delegate.borrow(closure_expr.id,
1164-
closure_expr.span,
1164+
fn_decl_span,
11651165
cmt_var,
11661166
upvar_borrow.region,
11671167
upvar_borrow.kind,

src/librustc/middle/liveness.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -948,7 +948,7 @@ impl<'a, 'tcx> Liveness<'a, 'tcx> {
948948
self.propagate_through_expr(&e, succ)
949949
}
950950

951-
hir::ExprClosure(_, _, ref blk) => {
951+
hir::ExprClosure(_, _, ref blk, _) => {
952952
debug!("{} is an ExprClosure",
953953
expr_to_string(expr));
954954

src/librustc/middle/mem_categorization.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -728,7 +728,7 @@ impl<'t, 'a,'tcx> MemCategorizationContext<'t, 'a, 'tcx> {
728728
};
729729

730730
match fn_expr.node {
731-
hir::ExprClosure(_, _, ref body) => body.id,
731+
hir::ExprClosure(_, _, ref body, _) => body.id,
732732
_ => bug!()
733733
}
734734
};

src/librustc_borrowck/borrowck/mod.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -415,7 +415,7 @@ pub fn closure_to_block(closure_id: ast::NodeId,
415415
tcx: &TyCtxt) -> ast::NodeId {
416416
match tcx.map.get(closure_id) {
417417
hir_map::NodeExpr(expr) => match expr.node {
418-
hir::ExprClosure(_, _, ref block) => {
418+
hir::ExprClosure(_, _, ref block, _) => {
419419
block.id
420420
}
421421
_ => {

src/librustc_mir/hair/cx/expr.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -725,7 +725,7 @@ fn convert_var<'a, 'tcx: 'a>(cx: &mut Cx<'a, 'tcx>,
725725
let body_id = match cx.tcx.map.find(closure_expr_id) {
726726
Some(map::NodeExpr(expr)) => {
727727
match expr.node {
728-
hir::ExprClosure(_, _, ref body) => body.id,
728+
hir::ExprClosure(_, _, ref body, _) => body.id,
729729
_ => {
730730
span_bug!(expr.span, "closure expr is not a closure expr");
731731
}

src/librustc_passes/loops.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ impl<'a, 'v> Visitor<'v> for CheckLoopVisitor<'a> {
4848
hir::ExprLoop(ref b, _) => {
4949
self.with_context(Loop, |v| v.visit_block(&b));
5050
}
51-
hir::ExprClosure(_, _, ref b) => {
51+
hir::ExprClosure(_, _, ref b, _) => {
5252
self.with_context(Closure, |v| v.visit_block(&b));
5353
}
5454
hir::ExprBreak(_) => self.require_loop("break", e.span),

src/librustc_save_analysis/dump_visitor.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -1258,7 +1258,7 @@ impl<'v, 'l, 'tcx: 'l, 'll, D: Dump +'ll> Visitor<'v> for DumpVisitor<'l, 'tcx,
12581258
ty),
12591259
}
12601260
}
1261-
ast::ExprKind::Closure(_, ref decl, ref body) => {
1261+
ast::ExprKind::Closure(_, ref decl, ref body, _fn_decl_span) => {
12621262
let mut id = String::from("$");
12631263
id.push_str(&ex.id.to_string());
12641264
self.process_formals(&decl.inputs, &id);

src/librustc_trans/base.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -1339,7 +1339,7 @@ fn build_cfg(tcx: &TyCtxt, id: ast::NodeId) -> (ast::NodeId, Option<cfg::CFG>) {
13391339
}
13401340
Some(hir_map::NodeExpr(e)) => {
13411341
match e.node {
1342-
hir::ExprClosure(_, _, ref blk) => blk,
1342+
hir::ExprClosure(_, _, ref blk, _) => blk,
13431343
_ => bug!("unexpected expr variant in has_nested_returns"),
13441344
}
13451345
}

src/librustc_trans/consts.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -990,7 +990,7 @@ fn const_expr_unadjusted<'a, 'tcx>(cx: &CrateContext<'a, 'tcx>,
990990
None => C_nil(cx),
991991
}
992992
},
993-
hir::ExprClosure(_, ref decl, ref body) => {
993+
hir::ExprClosure(_, ref decl, ref body, _) => {
994994
match ety.sty {
995995
ty::TyClosure(def_id, ref substs) => {
996996
closure::trans_closure_expr(closure::Dest::Ignore(cx),

src/librustc_trans/debuginfo/create_scope_map.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -479,7 +479,7 @@ fn walk_expr(cx: &CrateContext,
479479
})
480480
}
481481

482-
hir::ExprClosure(_, ref decl, ref block) => {
482+
hir::ExprClosure(_, ref decl, ref block, _) => {
483483
with_new_scope(cx,
484484
block.span,
485485
scope_stack,

src/librustc_trans/expr.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -1118,7 +1118,7 @@ fn trans_rvalue_dps_unadjusted<'blk, 'tcx>(bcx: Block<'blk, 'tcx>,
11181118
hir::ExprVec(..) | hir::ExprRepeat(..) => {
11191119
tvec::trans_fixed_vstore(bcx, expr, dest)
11201120
}
1121-
hir::ExprClosure(_, ref decl, ref body) => {
1121+
hir::ExprClosure(_, ref decl, ref body, _) => {
11221122
let dest = match dest {
11231123
SaveIn(lldest) => closure::Dest::SaveIn(bcx, lldest),
11241124
Ignore => closure::Dest::Ignore(bcx.ccx())

src/librustc_typeck/check/mod.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -3530,7 +3530,7 @@ fn check_expr_with_expectation_and_lvalue_pref<'a, 'tcx>(fcx: &FnCtxt<'a, 'tcx>,
35303530
hir::ExprMatch(ref discrim, ref arms, match_src) => {
35313531
_match::check_match(fcx, expr, &discrim, arms, expected, match_src);
35323532
}
3533-
hir::ExprClosure(capture, ref decl, ref body) => {
3533+
hir::ExprClosure(capture, ref decl, ref body, _) => {
35343534
closure::check_expr_closure(fcx, expr, capture, &decl, &body, expected);
35353535
}
35363536
hir::ExprBlock(ref b) => {

src/librustc_typeck/check/regionck.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -782,7 +782,7 @@ fn visit_expr(rcx: &mut Rcx, expr: &hir::Expr) {
782782
intravisit::walk_expr(rcx, expr);
783783
}
784784

785-
hir::ExprClosure(_, _, ref body) => {
785+
hir::ExprClosure(_, _, ref body, _) => {
786786
check_expr_fn_block(rcx, expr, &body);
787787
}
788788

src/librustc_typeck/check/upvar.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -98,7 +98,7 @@ struct SeedBorrowKind<'a,'tcx:'a> {
9898
impl<'a, 'tcx, 'v> Visitor<'v> for SeedBorrowKind<'a, 'tcx> {
9999
fn visit_expr(&mut self, expr: &hir::Expr) {
100100
match expr.node {
101-
hir::ExprClosure(cc, _, ref body) => {
101+
hir::ExprClosure(cc, _, ref body, _) => {
102102
self.check_closure(expr, cc, &body);
103103
}
104104

src/librustc_typeck/check/writeback.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -156,7 +156,7 @@ impl<'cx, 'tcx, 'v> Visitor<'v> for WritebackCx<'cx, 'tcx> {
156156
self.visit_method_map_entry(ResolvingExpr(e.span),
157157
MethodCall::expr(e.id));
158158

159-
if let hir::ExprClosure(_, ref decl, _) = e.node {
159+
if let hir::ExprClosure(_, ref decl, _, _) = e.node {
160160
for input in &decl.inputs {
161161
self.visit_node_id(ResolvingExpr(e.span), input.id);
162162
}

src/libstd/collections/hash/map.rs

+3-3
Original file line numberDiff line numberDiff line change
@@ -830,7 +830,7 @@ impl<K, V, S> HashMap<K, V, S>
830830
/// }
831831
/// ```
832832
#[stable(feature = "rust1", since = "1.0.0")]
833-
pub fn keys<'a>(&'a self) -> Keys<'a, K, V> {
833+
pub fn keys(&self) -> Keys<K, V> {
834834
Keys { inner: self.iter() }
835835
}
836836

@@ -852,7 +852,7 @@ impl<K, V, S> HashMap<K, V, S>
852852
/// }
853853
/// ```
854854
#[stable(feature = "rust1", since = "1.0.0")]
855-
pub fn values<'a>(&'a self) -> Values<'a, K, V> {
855+
pub fn values(&self) -> Values<K, V> {
856856
Values { inner: self.iter() }
857857
}
858858

@@ -880,7 +880,7 @@ impl<K, V, S> HashMap<K, V, S>
880880
/// }
881881
/// ```
882882
#[unstable(feature = "map_values_mut", reason = "recently added", issue = "32551")]
883-
pub fn values_mut<'a>(&'a mut self) -> ValuesMut<'a, K, V> {
883+
pub fn values_mut<'a>(&'a mut self) -> ValuesMut<K, V> {
884884
ValuesMut { inner: self.iter_mut() }
885885
}
886886

src/libsyntax/ast.rs

+3-1
Original file line numberDiff line numberDiff line change
@@ -986,7 +986,9 @@ pub enum ExprKind {
986986
/// A `match` block.
987987
Match(P<Expr>, Vec<Arm>),
988988
/// A closure (for example, `move |a, b, c| {a + b + c}`)
989-
Closure(CaptureBy, P<FnDecl>, P<Block>),
989+
///
990+
/// The final span is the span of the argument block `|...|`
991+
Closure(CaptureBy, P<FnDecl>, P<Block>, Span),
990992
/// A block (`{ ... }`)
991993
Block(P<Block>),
992994

src/libsyntax/ext/build.rs

+29-8
Original file line numberDiff line numberDiff line change
@@ -194,10 +194,14 @@ pub trait AstBuilder {
194194
cond: P<ast::Expr>, then: P<ast::Expr>, els: Option<P<ast::Expr>>) -> P<ast::Expr>;
195195
fn expr_loop(&self, span: Span, block: P<ast::Block>) -> P<ast::Expr>;
196196

197-
fn lambda_fn_decl(&self, span: Span,
198-
fn_decl: P<ast::FnDecl>, blk: P<ast::Block>) -> P<ast::Expr>;
197+
fn lambda_fn_decl(&self,
198+
span: Span,
199+
fn_decl: P<ast::FnDecl>,
200+
blk: P<ast::Block>,
201+
fn_decl_span: Span)
202+
-> P<ast::Expr>;
199203

200-
fn lambda(&self, span: Span, ids: Vec<ast::Ident> , blk: P<ast::Block>) -> P<ast::Expr>;
204+
fn lambda(&self, span: Span, ids: Vec<ast::Ident>, blk: P<ast::Block>) -> P<ast::Expr>;
201205
fn lambda0(&self, span: Span, blk: P<ast::Block>) -> P<ast::Expr>;
202206
fn lambda1(&self, span: Span, blk: P<ast::Block>, ident: ast::Ident) -> P<ast::Expr>;
203207

@@ -894,17 +898,34 @@ impl<'a> AstBuilder for ExtCtxt<'a> {
894898
self.expr(span, ast::ExprKind::Loop(block, None))
895899
}
896900

897-
fn lambda_fn_decl(&self, span: Span,
898-
fn_decl: P<ast::FnDecl>, blk: P<ast::Block>) -> P<ast::Expr> {
899-
self.expr(span, ast::ExprKind::Closure(ast::CaptureBy::Ref, fn_decl, blk))
901+
fn lambda_fn_decl(&self,
902+
span: Span,
903+
fn_decl: P<ast::FnDecl>,
904+
blk: P<ast::Block>,
905+
fn_decl_span: Span) // span of the `|...|` part
906+
-> P<ast::Expr> {
907+
self.expr(span, ast::ExprKind::Closure(ast::CaptureBy::Ref,
908+
fn_decl,
909+
blk,
910+
fn_decl_span))
900911
}
901-
fn lambda(&self, span: Span, ids: Vec<ast::Ident>, blk: P<ast::Block>) -> P<ast::Expr> {
912+
913+
fn lambda(&self,
914+
span: Span,
915+
ids: Vec<ast::Ident>,
916+
blk: P<ast::Block>)
917+
-> P<ast::Expr> {
902918
let fn_decl = self.fn_decl(
903919
ids.iter().map(|id| self.arg(span, *id, self.ty_infer(span))).collect(),
904920
self.ty_infer(span));
905921

906-
self.expr(span, ast::ExprKind::Closure(ast::CaptureBy::Ref, fn_decl, blk))
922+
// FIXME -- We are using `span` as the span of the `|...|`
923+
// part of the lambda, but it probably (maybe?) corresponds to
924+
// the entire lambda body. Probably we should extend the API
925+
// here, but that's not entirely clear.
926+
self.expr(span, ast::ExprKind::Closure(ast::CaptureBy::Ref, fn_decl, blk, span))
907927
}
928+
908929
fn lambda0(&self, span: Span, blk: P<ast::Block>) -> P<ast::Expr> {
909930
self.lambda(span, Vec::new(), blk)
910931
}

src/libsyntax/ext/expand.rs

+8-5
Original file line numberDiff line numberDiff line change
@@ -149,14 +149,17 @@ pub fn expand_expr(e: P<ast::Expr>, fld: &mut MacroExpander) -> P<ast::Expr> {
149149
fld.cx.expr(span, il).with_attrs(fold_thin_attrs(attrs, fld))
150150
}
151151

152-
ast::ExprKind::Closure(capture_clause, fn_decl, block) => {
152+
ast::ExprKind::Closure(capture_clause, fn_decl, block, fn_decl_span) => {
153153
let (rewritten_fn_decl, rewritten_block)
154154
= expand_and_rename_fn_decl_and_block(fn_decl, block, fld);
155155
let new_node = ast::ExprKind::Closure(capture_clause,
156-
rewritten_fn_decl,
157-
rewritten_block);
158-
P(ast::Expr{id:id, node: new_node, span: fld.new_span(span),
159-
attrs: fold_thin_attrs(attrs, fld)})
156+
rewritten_fn_decl,
157+
rewritten_block,
158+
fld.new_span(fn_decl_span));
159+
P(ast::Expr{ id:id,
160+
node: new_node,
161+
span: fld.new_span(span),
162+
attrs: fold_thin_attrs(attrs, fld) })
160163
}
161164

162165
_ => {

src/libsyntax/fold.rs

+4-3
Original file line numberDiff line numberDiff line change
@@ -1241,10 +1241,11 @@ pub fn noop_fold_expr<T: Folder>(Expr {id, node, span, attrs}: Expr, folder: &mu
12411241
ExprKind::Match(folder.fold_expr(expr),
12421242
arms.move_map(|x| folder.fold_arm(x)))
12431243
}
1244-
ExprKind::Closure(capture_clause, decl, body) => {
1244+
ExprKind::Closure(capture_clause, decl, body, span) => {
12451245
ExprKind::Closure(capture_clause,
1246-
folder.fold_fn_decl(decl),
1247-
folder.fold_block(body))
1246+
folder.fold_fn_decl(decl),
1247+
folder.fold_block(body),
1248+
folder.new_span(span))
12481249
}
12491250
ExprKind::Block(blk) => ExprKind::Block(folder.fold_block(blk)),
12501251
ExprKind::Assign(el, er) => {

src/libsyntax/parse/parser.rs

+6-3
Original file line numberDiff line numberDiff line change
@@ -3225,13 +3225,15 @@ impl<'a> Parser<'a> {
32253225
Ok(self.mk_expr(lo, hi, ExprKind::IfLet(pat, expr, thn, els), attrs))
32263226
}
32273227

3228-
// `|args| expr`
3229-
pub fn parse_lambda_expr(&mut self, lo: BytePos,
3228+
// `move |args| expr`
3229+
pub fn parse_lambda_expr(&mut self,
3230+
lo: BytePos,
32303231
capture_clause: CaptureBy,
32313232
attrs: ThinAttributes)
32323233
-> PResult<'a, P<Expr>>
32333234
{
32343235
let decl = self.parse_fn_block_decl()?;
3236+
let decl_hi = self.last_span.hi;
32353237
let body = match decl.output {
32363238
FunctionRetTy::Default(_) => {
32373239
// If no explicit return type is given, parse any
@@ -3255,7 +3257,8 @@ impl<'a> Parser<'a> {
32553257
Ok(self.mk_expr(
32563258
lo,
32573259
body.span.hi,
3258-
ExprKind::Closure(capture_clause, decl, body), attrs))
3260+
ExprKind::Closure(capture_clause, decl, body, mk_sp(lo, decl_hi)),
3261+
attrs))
32593262
}
32603263

32613264
// `else` token already eaten

0 commit comments

Comments
 (0)