Skip to content
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 9 commits into from
Jan 27, 2016

Conversation

fhahn
Copy link
Contributor

@fhahn fhahn commented Jan 21, 2016

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:

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.

@rust-highfive
Copy link
Collaborator

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);
Copy link
Member

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.)

@fhahn
Copy link
Contributor Author

fhahn commented Jan 23, 2016

@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 unprintable span I could find (I've created testcases for all issues fixed). Just updating the span of the outermost expression seesm fine, because that way errors in inner expressions are reported at the macro call site (as seen here), which is nice.

But this PR currently only updates the spans of NtExpr arguments. The spans of other non terminals should probably be updated as well, but I am not sure if it should be done in this PR or in a separate one?

@fhahn
Copy link
Contributor Author

fhahn commented Jan 23, 2016

I'm pretty sure this PR also closes #25353, but the link to the example is not valid any more.

@pnkfelix
Copy link
Member

The spans of other non terminals should probably be updated as well, but I am not sure if it should be done in this PR or in a separate one?

I am fine with keeping this PR focused on just the NtExprs,

@fhahn
Copy link
Contributor Author

fhahn commented Jan 24, 2016

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 Interpolated token (instead of the span of the transcribed expression, which points to the point of the macro invocation). This way, there should be no unprintable spans.

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 Interpolated to decide which span to use. For now this PR does this by saving it in last_token, but we do not need to know what was inside this token, so I could add another field to the parsers (like last_token_interpolated: bool).

@fhahn fhahn force-pushed the macro-ice branch 2 times, most recently from cdf6e38 to 26a9c32 Compare January 24, 2016 23:02
@@ -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());
Copy link
Member

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?

Copy link
Contributor Author

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

@bors
Copy link
Contributor

bors commented Jan 26, 2016

☔ The latest upstream changes (presumably #31065) made this pull request unmergeable. Please resolve the merge conflicts.

@fhahn fhahn force-pushed the macro-ice branch 2 times, most recently from 95a0b50 to 94ed73c Compare January 26, 2016 10:46
@fhahn
Copy link
Contributor Author

fhahn commented Jan 26, 2016

@pnkfelix I've pushed a new commit which adds a macro interpolated_or_expr_span which takes an expr to parse an expr as argument and returns the resulting expr and either the span of the expression or the interpolated token.

@@ -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 {
Copy link
Member

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.

Copy link
Contributor Author

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.

Copy link
Member

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.)

Copy link
Contributor Author

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.

Copy link
Contributor Author

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.

@pnkfelix
Copy link
Member

@fhahn okay hang in there I think we are converging on something I will be very happy with. :)

@pnkfelix
Copy link
Member

@fhahn r=me after you add the //~ NOTE annotations to the test cases (see #31089 (comment) )

@pnkfelix
Copy link
Member

@fhahn (also, travis seems to signalled a make tidy failure.)

@pnkfelix
Copy link
Member

@bors r+ ecb7b01

@bors
Copy link
Contributor

bors commented Jan 27, 2016

⌛ Testing commit ecb7b01 with merge 8256c47...

bors added a commit that referenced this pull request Jan 27, 2016
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.
@bors bors merged commit ecb7b01 into rust-lang:master Jan 27, 2016
@fhahn fhahn deleted the macro-ice branch January 27, 2016 17:26
@itaibn
Copy link

itaibn commented Feb 14, 2016

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?

@fhahn
Copy link
Contributor Author

fhahn commented Feb 14, 2016

This PR fixed a couple of unprintable span errors related to expaneded macro arguments. What's the problamtic code?

@fhahn
Copy link
Contributor Author

fhahn commented Feb 14, 2016

Anyways, I think opening an issue would be a good idea, so we do not loose track of the problem.

@itaibn
Copy link

itaibn commented Feb 14, 2016

Alright, I opened a new issue for the error I found.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

ICE: "unprintable span" with macro and two files, no backtrace
5 participants