Skip to content

Commit

Permalink
Be smart about span of parenthesized expression in macro
Browse files Browse the repository at this point in the history
  • Loading branch information
sanxiyn committed May 18, 2016
1 parent cdbf015 commit edf1773
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 1 deletion.
5 changes: 4 additions & 1 deletion src/librustc/hir/lowering.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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())
Expand Down
31 changes: 31 additions & 0 deletions src/test/compile-fail/paren-span.rs
Original file line number Diff line number Diff line change
@@ -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 <LICENSE-APACHE or
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, 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
}

0 comments on commit edf1773

Please sign in to comment.