-
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
Conversation
r? @pnkfelix (rust_highfive has picked a reviewer for you, use r? to override) |
@@ -181,6 +187,7 @@ pub fn tt_next_token(r: &mut TtReader) -> TokenAndSpan { | |||
tok: r.cur_tok.clone(), | |||
sp: r.cur_span.clone(), | |||
}; | |||
//println!("RET {:?} {:?}", r.cur_tok, r.cur_span); |
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.
please remove these commented out println's
... (or if you think they have long term value, then turn them into uncommented debug!
invocations.)
@pnkfelix Thanks for the feedback and sorry for opening the PR with those problems. Ran out of time and wanted to start the PR to get some feedback on the general approach. Now I finally had time to update those tests and think the approach as it is at the moment already gets us pretty far. It fixes all issues with But this PR currently only updates the spans of |
I'm pretty sure this PR also closes #25353, but the link to the example is not valid any more. |
I am fine with keeping this PR focused on just the NtExprs, |
I've updated the PR again. They way how the spans are handled changed completely from the first version. The idea is as follows: Transcribed expressions keep their original spans so errors that only involve this expressions are displayed at the macro invocation. When building spans for expressions inside a macro definition that contain transcribed expressions, the parsers uses the span of the There are already a couple of test cases which illustrate where errors are displayed with this change, but this scheme required small changes to multiple parts of the parser, so I probably should add a couple of more, covering all cases. This change may have a slight impact on the parser performance. We need to know if the previous token was |
cdf6e38
to
26a9c32
Compare
@@ -2782,13 +2802,13 @@ impl<'a> Parser<'a> { | |||
} | |||
// Special cases: | |||
if op == AssocOp::As { | |||
let rhs = try!(self.parse_ty()); | |||
lhs = self.mk_expr(lhs.span.lo, rhs.span.hi, | |||
let rhs = try!(self.parse_ty()); |
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.
The indent is weird here, 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.
Fixed in my latest commit
☔ The latest upstream changes (presumably #31065) made this pull request unmergeable. Please resolve the merge conflicts. |
closes rust-lang#29084 closes rust-lang#28308 closes rust-lang#25385 closes rust-lang#28288 closes rust-lang#31011 closes rust-lang#26480 closes rust-lang#26093 closes rust-lang#26094 closes rust-lang#25386 closes rust-lang#26237 closes rust-lang#25793
95a0b50
to
94ed73c
Compare
@pnkfelix I've pushed a new commit which adds a macro |
@@ -233,6 +233,21 @@ macro_rules! maybe_whole { | |||
) | |||
} | |||
|
|||
/// Uses $parse_expr to parse an expression and returns the span of the interpolated | |||
/// token or the span of the parsed expression, if it was not interpolated | |||
macro_rules! interpolated_or_expr_span { |
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.
this makes me feel better about the pattern here. (I still worry people may not know when to call this, but at least now it's easy to see and swap in.)
AFAICT, the reason you made this a macro is due to the use of try!
in the given $parse_expr.
I would prefer you make this a fn
that returns Result, and shift the trys at the call sites.
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.
It's a macro so it can be used with different methods for parsing expressions. In particular it is once used with Parser::parse_bottom_expr and also with Parser::parse_prefix_expr.
I've update the macro to return Result
, which should have the same effect making it a fn
, at least with respect to the try!
(I hope). A similar fn
would have to take a closure which is probably slightly less ergomatic. However if you still think a fn
would be better, I'd be happy to change that.
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.
Your argument still doesn't make sense to me.
Both Parser::parse_bottom_expr
and Parser::parse_prefix_expr
have the same return type, so its not like you are using the macro to inject some kind of duck-typing...
To be clear: You have a macro here that takes in two expressions as input ($p
and $parse_expr
). It unconditionally evaluates both expressions at the outset. (There's a second evaluation of $p
but I don't think that was a deliberate choice.)
So, why wouldn't this work:
impl<'a> Parser<'a> {
/// Uses $parse_expr to parse an expression and returns the span of the interpolated
/// token or the span of the parsed expression, if it was not interpolated
fn interpolated_or_expr_span(&self, expr: PResult<'a, P<Expr>>) -> PResult<'a, (Span, P<Expr>)> {
let is_interpolated = self.token.is_interpolated();
expr.map(|e| {
if is_interpolated {
(self.last_span, e)
} else {
(e.span, e)
}
})
}
}
(And then update the macro calls to call this method instead.)
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.
Yeah, that makes sense. I've pushed another commit that turns it into a function.
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 now remembered to original reason for that being a macro. As it was, interpolated_or_expr_span
used the current token to check if it is interpolated, which means the parse functions has to be called after that check.
But by using last_token
it works as expected. I pushed another commit however, which avoids storing (and cloning) interpolated tokens and instead added a Parser.last_token_interpolated
flag.
@fhahn okay hang in there I think we are converging on something I will be very happy with. :) |
@fhahn r=me after you add the |
@fhahn (also, travis seems to signalled a |
This is a work in progress PR that potentially should fix #29084, #28308, #25385, #28288, #31011. I think this may also adresse parts of #2887. The problem in this issues seems to be that when transcribing macro arguments, we just clone the argument Nonterminal, which still has to original spans. This leads to the unprintable spans. One solution would be to update the spans of the inserted argument to match the argument in the macro definition. So for [this testcase](https://github.com/rust-lang/rust/compare/master...fhahn:macro-ice?expand=1#diff-f7def7420c51621640707b6337726876R2) the error message would be displayed in the macro definition: src/test/compile-fail/issue-31011.rs:4:12: 4:22 error: attempted access of field `trace` on type `&T`, but no field with that name was found src/test/compile-fail/issue-31011.rs:4 if $ctx.trace { Currently I've added a very simple `update_span` function, which updates the span of the outer-most expression of a `NtExpr`, but this `update_span` function should be updated to handle all Nonterminals. But I'm pretty new to the macro system and would like to check if this approach makes sense, before doing that.
I found a piece of code that emits a "unprintable span" compiler error. It looks like it's probably a duplicate of this issue, but I don't know for sure. Should I submit a bug report? |
This PR fixed a couple of unprintable span errors related to expaneded macro arguments. What's the problamtic code? |
Anyways, I think opening an issue would be a good idea, so we do not loose track of the problem. |
Alright, I opened a new issue for the error I found. |
This is a work in progress PR that potentially should fix #29084, #28308, #25385, #28288, #31011. I think this may also adresse parts of #2887.
The problem in this issues seems to be that when transcribing macro arguments, we just clone the argument Nonterminal, which still has to original spans. This leads to the unprintable spans. One solution would be to update the spans of the inserted argument to match the argument in the macro definition. So for this testcase the error message would be displayed in the macro definition:
Currently I've added a very simple
update_span
function, which updates the span of the outer-most expression of aNtExpr
, but thisupdate_span
function should be updated to handle all Nonterminals. But I'm pretty new to the macro system and would like to check if this approach makes sense, before doing that.