Skip to content

Commit 5dc8dfa

Browse files
Rollup merge of rust-lang#33683 - sanxiyn:paren-span, r=nikomatsakis
Preserve span when lowering ExprKind::Paren Fix rust-lang#33681.
2 parents f22c5aa + edf1773 commit 5dc8dfa

File tree

4 files changed

+40
-5
lines changed

4 files changed

+40
-5
lines changed

src/libcore/macros.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -86,7 +86,7 @@ macro_rules! assert {
8686
#[stable(feature = "rust1", since = "1.0.0")]
8787
macro_rules! assert_eq {
8888
($left:expr , $right:expr) => ({
89-
match (&($left), &($right)) {
89+
match (&$left, &$right) {
9090
(left_val, right_val) => {
9191
if !(*left_val == *right_val) {
9292
panic!("assertion failed: `(left == right)` \

src/librustc/hir/lowering.rs

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1286,8 +1286,12 @@ impl<'a> LoweringContext<'a> {
12861286
maybe_expr.as_ref().map(|x| self.lower_expr(x)))
12871287
}
12881288
ExprKind::Paren(ref ex) => {
1289-
// merge attributes into the inner expression.
12901289
return self.lower_expr(ex).map(|mut ex| {
1290+
// include parens in span, but only if it is a super-span.
1291+
if e.span.contains(ex.span) {
1292+
ex.span = e.span;
1293+
}
1294+
// merge attributes into the inner expression.
12911295
ex.attrs.update(|attrs| {
12921296
attrs.prepend(e.attrs.clone())
12931297
});

src/test/compile-fail/issue-2392.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -81,11 +81,11 @@ impl FuncContainerOuter {
8181
fn run(&self) {
8282
unsafe {
8383
(*self.container).f1(1); //~ ERROR no method named `f1` found
84-
//~^ NOTE use `(*self.container.f1)(...)`
84+
//~^ NOTE use `((*self.container).f1)(...)`
8585
(*self.container).f2(1); //~ ERROR no method named `f2` found
86-
//~^ NOTE use `(*self.container.f2)(...)`
86+
//~^ NOTE use `((*self.container).f2)(...)`
8787
(*self.container).f3(1); //~ ERROR no method named `f3` found
88-
//~^ NOTE use `(*self.container.f3)(...)`
88+
//~^ NOTE use `((*self.container).f3)(...)`
8989
}
9090
}
9191
}

src/test/compile-fail/paren-span.rs

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
// Copyright 2016 The Rust Project Developers. See the COPYRIGHT
2+
// file at the top-level directory of this distribution and at
3+
// http://rust-lang.org/COPYRIGHT.
4+
//
5+
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
6+
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
7+
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
8+
// option. This file may not be copied, modified, or distributed
9+
// except according to those terms.
10+
11+
// Be smart about span of parenthesized expression in macro.
12+
13+
macro_rules! paren {
14+
($e:expr) => (($e))
15+
// ^^^^ do not highlight here
16+
}
17+
18+
mod m {
19+
pub struct S {
20+
x: i32
21+
}
22+
pub fn make() -> S {
23+
S { x: 0 }
24+
}
25+
}
26+
27+
fn main() {
28+
let s = m::make();
29+
paren!(s.x); //~ ERROR field `x` of struct `m::S` is private
30+
// ^^^ highlight here
31+
}

0 commit comments

Comments
 (0)