From ecd10f04cea34352ff240b0462d40cd1d95a4e0f Mon Sep 17 00:00:00 2001 From: Niko Matsakis Date: Wed, 20 Apr 2016 14:44:07 -0400 Subject: thread tighter span for closures around Track the span corresponding to the `|...|` part of the closure. --- src/libsyntax/ext/build.rs | 37 +++++++++++++++++++++++++++++-------- src/libsyntax/ext/expand.rs | 13 ++++++++----- 2 files changed, 37 insertions(+), 13 deletions(-) (limited to 'src/libsyntax/ext') diff --git a/src/libsyntax/ext/build.rs b/src/libsyntax/ext/build.rs index a4e5b68277d..8ebf4863af5 100644 --- a/src/libsyntax/ext/build.rs +++ b/src/libsyntax/ext/build.rs @@ -194,10 +194,14 @@ pub trait AstBuilder { cond: P, then: P, els: Option>) -> P; fn expr_loop(&self, span: Span, block: P) -> P; - fn lambda_fn_decl(&self, span: Span, - fn_decl: P, blk: P) -> P; + fn lambda_fn_decl(&self, + span: Span, + fn_decl: P, + blk: P, + fn_decl_span: Span) + -> P; - fn lambda(&self, span: Span, ids: Vec , blk: P) -> P; + fn lambda(&self, span: Span, ids: Vec, blk: P) -> P; fn lambda0(&self, span: Span, blk: P) -> P; fn lambda1(&self, span: Span, blk: P, ident: ast::Ident) -> P; @@ -894,17 +898,34 @@ impl<'a> AstBuilder for ExtCtxt<'a> { self.expr(span, ast::ExprKind::Loop(block, None)) } - fn lambda_fn_decl(&self, span: Span, - fn_decl: P, blk: P) -> P { - self.expr(span, ast::ExprKind::Closure(ast::CaptureBy::Ref, fn_decl, blk)) + fn lambda_fn_decl(&self, + span: Span, + fn_decl: P, + blk: P, + fn_decl_span: Span) // span of the `|...|` part + -> P { + self.expr(span, ast::ExprKind::Closure(ast::CaptureBy::Ref, + fn_decl, + blk, + fn_decl_span)) } - fn lambda(&self, span: Span, ids: Vec, blk: P) -> P { + + fn lambda(&self, + span: Span, + ids: Vec, + blk: P) + -> P { let fn_decl = self.fn_decl( ids.iter().map(|id| self.arg(span, *id, self.ty_infer(span))).collect(), self.ty_infer(span)); - self.expr(span, ast::ExprKind::Closure(ast::CaptureBy::Ref, fn_decl, blk)) + // FIXME -- We are using `span` as the span of the `|...|` + // part of the lambda, but it probably (maybe?) corresponds to + // the entire lambda body. Probably we should extend the API + // here, but that's not entirely clear. + self.expr(span, ast::ExprKind::Closure(ast::CaptureBy::Ref, fn_decl, blk, span)) } + fn lambda0(&self, span: Span, blk: P) -> P { self.lambda(span, Vec::new(), blk) } diff --git a/src/libsyntax/ext/expand.rs b/src/libsyntax/ext/expand.rs index cd7b0fcfb00..38132eb42ee 100644 --- a/src/libsyntax/ext/expand.rs +++ b/src/libsyntax/ext/expand.rs @@ -149,14 +149,17 @@ pub fn expand_expr(e: P, fld: &mut MacroExpander) -> P { fld.cx.expr(span, il).with_attrs(fold_thin_attrs(attrs, fld)) } - ast::ExprKind::Closure(capture_clause, fn_decl, block) => { + ast::ExprKind::Closure(capture_clause, fn_decl, block, fn_decl_span) => { let (rewritten_fn_decl, rewritten_block) = expand_and_rename_fn_decl_and_block(fn_decl, block, fld); let new_node = ast::ExprKind::Closure(capture_clause, - rewritten_fn_decl, - rewritten_block); - P(ast::Expr{id:id, node: new_node, span: fld.new_span(span), - attrs: fold_thin_attrs(attrs, fld)}) + rewritten_fn_decl, + rewritten_block, + fld.new_span(fn_decl_span)); + P(ast::Expr{ id:id, + node: new_node, + span: fld.new_span(span), + attrs: fold_thin_attrs(attrs, fld) }) } _ => { -- cgit 1.4.1-3-g733a5