From 0f257df31a1c7684f8d1824a4b0e776236d23a0e Mon Sep 17 00:00:00 2001 From: Seo Sanghyeon Date: Tue, 17 May 2016 20:37:07 +0900 Subject: [PATCH 1/4] Preserve span when lowering ExprKind::Paren --- src/librustc/hir/lowering.rs | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/src/librustc/hir/lowering.rs b/src/librustc/hir/lowering.rs index 0c3c190064b..ea47b0924bc 100644 --- a/src/librustc/hir/lowering.rs +++ b/src/librustc/hir/lowering.rs @@ -1260,8 +1260,9 @@ impl<'a> LoweringContext<'a> { maybe_expr.as_ref().map(|x| self.lower_expr(x))) } ExprKind::Paren(ref ex) => { - // merge attributes into the inner expression. return self.lower_expr(ex).map(|mut ex| { + ex.span = e.span; + // merge attributes into the inner expression. ex.attrs.update(|attrs| { attrs.prepend(e.attrs.clone()) }); From bc4c67dd0aaab66ba6a5579bc62c993a73688876 Mon Sep 17 00:00:00 2001 From: Seo Sanghyeon Date: Wed, 18 May 2016 22:22:32 +0900 Subject: [PATCH 2/4] Fix code suggestion in test --- src/test/compile-fail/issue-2392.rs | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/test/compile-fail/issue-2392.rs b/src/test/compile-fail/issue-2392.rs index 47d50eb9d53..790b774bd21 100644 --- a/src/test/compile-fail/issue-2392.rs +++ b/src/test/compile-fail/issue-2392.rs @@ -81,11 +81,11 @@ impl FuncContainerOuter { fn run(&self) { unsafe { (*self.container).f1(1); //~ ERROR no method named `f1` found - //~^ NOTE use `(*self.container.f1)(...)` + //~^ NOTE use `((*self.container).f1)(...)` (*self.container).f2(1); //~ ERROR no method named `f2` found - //~^ NOTE use `(*self.container.f2)(...)` + //~^ NOTE use `((*self.container).f2)(...)` (*self.container).f3(1); //~ ERROR no method named `f3` found - //~^ NOTE use `(*self.container.f3)(...)` + //~^ NOTE use `((*self.container).f3)(...)` } } } From cdbf01570fb8947cecb6c3e7dc16ecaf104b51b6 Mon Sep 17 00:00:00 2001 From: Seo Sanghyeon Date: Wed, 18 May 2016 22:24:33 +0900 Subject: [PATCH 3/4] Remove unnecessary parens in macro --- src/libcore/macros.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/libcore/macros.rs b/src/libcore/macros.rs index ad90b447508..a40608b0762 100644 --- a/src/libcore/macros.rs +++ b/src/libcore/macros.rs @@ -86,7 +86,7 @@ macro_rules! assert { #[stable(feature = "rust1", since = "1.0.0")] macro_rules! assert_eq { ($left:expr , $right:expr) => ({ - match (&($left), &($right)) { + match (&$left, &$right) { (left_val, right_val) => { if !(*left_val == *right_val) { panic!("assertion failed: `(left == right)` \ From edf1773fb7b56b39e68411d87c86f870697b997b Mon Sep 17 00:00:00 2001 From: Seo Sanghyeon Date: Thu, 19 May 2016 00:03:00 +0900 Subject: [PATCH 4/4] 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 ea47b0924bc..5b539439435 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 00000000000..8ed5050f3de --- /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 +}