From edf1773fb7b56b39e68411d87c86f870697b997b Mon Sep 17 00:00:00 2001 From: Seo Sanghyeon Date: Thu, 19 May 2016 00:03:00 +0900 Subject: [PATCH] Be smart about span of parenthesized expression in macro --- src/librustc/hir/lowering.rs | 5 ++++- src/test/compile-fail/paren-span.rs | 31 +++++++++++++++++++++++++++++ 2 files changed, 35 insertions(+), 1 deletion(-) create mode 100644 src/test/compile-fail/paren-span.rs diff --git a/src/librustc/hir/lowering.rs b/src/librustc/hir/lowering.rs index ea47b0924bc01..5b539439435e7 100644 --- a/src/librustc/hir/lowering.rs +++ b/src/librustc/hir/lowering.rs @@ -1261,7 +1261,10 @@ impl<'a> LoweringContext<'a> { } ExprKind::Paren(ref ex) => { return self.lower_expr(ex).map(|mut ex| { - ex.span = e.span; + // include parens in span, but only if it is a super-span. + if e.span.contains(ex.span) { + ex.span = e.span; + } // merge attributes into the inner expression. ex.attrs.update(|attrs| { attrs.prepend(e.attrs.clone()) diff --git a/src/test/compile-fail/paren-span.rs b/src/test/compile-fail/paren-span.rs new file mode 100644 index 0000000000000..8ed5050f3de3c --- /dev/null +++ b/src/test/compile-fail/paren-span.rs @@ -0,0 +1,31 @@ +// Copyright 2016 The Rust Project Developers. See the COPYRIGHT +// file at the top-level directory of this distribution and at +// http://rust-lang.org/COPYRIGHT. +// +// Licensed under the Apache License, Version 2.0 or the MIT license +// , at your +// option. This file may not be copied, modified, or distributed +// except according to those terms. + +// Be smart about span of parenthesized expression in macro. + +macro_rules! paren { + ($e:expr) => (($e)) + // ^^^^ do not highlight here +} + +mod m { + pub struct S { + x: i32 + } + pub fn make() -> S { + S { x: 0 } + } +} + +fn main() { + let s = m::make(); + paren!(s.x); //~ ERROR field `x` of struct `m::S` is private + // ^^^ highlight here +}