-
Notifications
You must be signed in to change notification settings - Fork 12.8k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Update expression span when transcribing macro args #31089
Merged
Merged
Changes from all commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
b285ebc
Update expression span when transcribing macro args
fhahn 877ed0d
Update tests
fhahn 20edb36
Set span for interpolated tokens correctly
fhahn 1bde18d
Use interpolated token span when building spans for bigger expressions
fhahn 2bc8f4f
Add interpolated_or_expr_span macro and pass lo to newly added parse_…
fhahn 9d8c64b
Push try! to call site of interpolated_or_expr_span!
fhahn 47bfd8c
Turn interpolated_or_expr_span into a function
fhahn e533ed9
Avoid storing interolated token in Parser.last_token
fhahn ecb7b01
Add NOTE test annotations
fhahn File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
// 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. | ||
|
||
|
||
macro_rules! foo { | ||
($e:expr) => { $e.foo() } | ||
//~^ ERROR no method named `foo` found for type `i32` in the current scope | ||
} | ||
|
||
fn main() { | ||
let a = 1i32; | ||
foo!(a); | ||
//~^ NOTE in this expansion of foo! | ||
|
||
foo!(1i32.foo()); | ||
//~^ ERROR no method named `foo` found for type `i32` in the current scope | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
// 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. | ||
|
||
mod stuff { | ||
pub struct Item { | ||
c_object: Box<CObj>, | ||
} | ||
pub struct CObj { | ||
name: Option<String>, | ||
} | ||
impl Item { | ||
pub fn new() -> Item { | ||
Item { | ||
c_object: Box::new(CObj { name: None }), | ||
} | ||
} | ||
} | ||
} | ||
|
||
macro_rules! check_ptr_exist { | ||
($var:expr, $member:ident) => ( | ||
(*$var.c_object).$member.is_some() | ||
//~^ ERROR field `name` of struct `stuff::CObj` is private | ||
//~^^ ERROR field `c_object` of struct `stuff::Item` is private | ||
); | ||
} | ||
|
||
fn main() { | ||
let item = stuff::Item::new(); | ||
println!("{}", check_ptr_exist!(item, name)); | ||
//~^ NOTE in this expansion of check_ptr_exist! | ||
//~^^ NOTE in this expansion of check_ptr_exist! | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
// 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. | ||
|
||
macro_rules! width( | ||
($this:expr) => { | ||
$this.width.unwrap() | ||
//~^ ERROR cannot use `self.width` because it was mutably borrowed | ||
} | ||
); | ||
|
||
struct HasInfo { | ||
width: Option<usize> | ||
} | ||
|
||
impl HasInfo { | ||
fn get_size(&mut self, n: usize) -> usize { | ||
n | ||
} | ||
|
||
fn get_other(&mut self) -> usize { | ||
self.get_size(width!(self)) | ||
//~^ NOTE in this expansion of width! | ||
} | ||
} | ||
|
||
fn main() {} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
// 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. | ||
|
||
macro_rules! not_an_lvalue { | ||
($thing:expr) => { | ||
$thing = 42; | ||
//~^ ERROR invalid left-hand side expression | ||
} | ||
} | ||
|
||
fn main() { | ||
not_an_lvalue!(99); | ||
//~^ NOTE in this expansion of not_an_lvalue! | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Looking at this test I worry whether your approach is not ideal; in practice wouldn't it be a strict improvement to point the user at the macro invocation in a case like this? otherwise important context is missing, no?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I agree, even with this change, the error messages are not as good as they could be. In most cases it would be really helpful to have the information to what a macro variable has been expanded to get the complete picture, e.g. by adding a note:
"$e" was expanded to "a"
, which highlights the parameter in the macro invocation.I think it still makes sense to highlight the error inside the macro, because that's what causes the error. At the invocation site, we just pass what the macro rule expects (an arbitrary expression).
I also think the ideal place to highlight the error depends on whether the underlying problem is in the macro defintion (there's a bug in the macro) or at with the invocation (an unsupported expression was passed). If we would know that the problem is with the invocation, then we could just display at the invocation site, like:
But I think there's no way to know that for sure.
So in my opinion including more detailed information about expanded macro arguments would be the most flexible option. This would probably require substantial changes to the error reporting however.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Sure, but ... we display a macro-backtrace, and one can walk up that to inspect the macro definition, right?
Maybe I need to try out the patch myself. If you are printing out the macro backtraces in the error messages here, and those backtraces do start at the macro invocation site, then perhaps I have nothing to worry about: the compiler will guess at which site to report in its span, but if the backtrace includes everything starting at the invocation site, then the user can always use the backtrace to get the invocation site.
(But this is assuming that the backtrace is that clever, which I suspect it is not, since I think it just uses the provided span and the links therein, which means we won't have the invocation site here, right?)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
As it is now, the invocation site is displayed with a note, e.g. for the test case the relevant output would be:
So it includes a backtrace to the invocation site. But in order to find out which parameter
$e
is expanded to one would have to look at the macro definition.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Hmm okay then.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
One last thing then: can you add the expected
//~ NOTE
annotations to the macro invocation sites in these test cases?That will make it easier for other people reviewing the tests to see the overall picture of the error output we expect, without having to run the compiler on the test by hand.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yep, done.