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

Adjust close delim span #65966

Closed
wants to merge 4 commits into from

Conversation

guanqun
Copy link
Contributor

@guanqun guanqun commented Oct 30, 2019

Fixes #62524

The only thing that's not quite perfect is that I need to update two existing unit tests... If I remove the newline in these two files, then the "^" position looks reasonable. Any hints how to adjust that? Or do you think this fix is a good start? (at least it fixes the ICE).

@rust-highfive
Copy link
Collaborator

r? @eddyb

(rust_highfive has picked a reviewer for you, use r? to override)

@rust-highfive rust-highfive added the S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. label Oct 30, 2019
@guanqun
Copy link
Contributor Author

guanqun commented Oct 30, 2019

also add @estebank for review.

@@ -0,0 +1,3 @@
// ignore-tidy-trailing-newlines
// error-pattern: aborting due to 2 previous errors
fn main((ؼ
Copy link
Contributor Author

@guanqun guanqun Oct 30, 2019

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

note that this file needs "no-newline" otherwise it won't crash in previous version.

Should we create some mechanism in the code to verify that this file has no new line at the end of file?

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 notice that some editors would try to add a new line by accident.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Add this as a comment to the test on a line right before fn main( so that people reading the file understand why the ignore rule is there.

@eddyb
Copy link
Member

eddyb commented Oct 30, 2019

r? @petrochenkov cc @dtolnay

@rust-highfive rust-highfive assigned petrochenkov and unassigned eddyb Oct 30, 2019
@rust-highfive

This comment has been minimized.

@estebank
Copy link
Contributor

Please run ./x.py test src/test/ui --stage 1 --bless to update the stderr files that are left over with discrepancies. After that, r=me as the change looks very reasonable, unless anyone else wants to take a look.

span.with_hi(span.lo() + BytePos(delim.len() as u32))
};
TokenTree::token(token::OpenDelim(delim), open_span)
}

/// Returns the closing delimiter as a token tree.
pub fn close_tt(span: Span, delim: DelimToken) -> TokenTree {
let close_span = if span.is_dummy() {
let close_span = if span.is_dummy() || span.lo() == span.hi() {
span
} else {
span.with_lo(span.hi() - BytePos(delim.len() as u32))
Copy link
Contributor

@petrochenkov petrochenkov Oct 30, 2019

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This may be trickier than it may look on the first glance.
If span is arbitrary (e.g. created by a proc macro), then span + 1/span - 1 can always step into something bad in the source map.
The only way to know for sure is to check whether span_to_source returns an error or not, but that's expensive.

If we want to check this without accessing the source map, then we need to understand when exactly this span is used for indexing into it (only when reporting "un-closed delimiter" or somewhere else?) and whether it can contain spans produced by proc macros at that point, or only lexer produced spans.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

the tokenstream would insert an un-closed delimiter with a span whose lo equals hi. I'm not sure about proc macros, haven't got time into that part yet.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@petrochenkov I think it would be reasonable to commit this PR with a FIXME comment pretty much containing the same text in your comment, as well as a ticket for that work. Although this won't catch every single ICE that could be caused by proc_macro span shenaningans, it will catch one that is relatively easy to hit. We could also add a check for whether the span has a macro backtrace and also use span directly in those cases.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'd rather add some asserts instead of the FIXME, it's quite possible that this is only called with spans from lexer, will try to look into this tomorrow.

I'd also inline open_tt and close_tt into the caller since they are only called once and cannot work with arbitrary arguments.

@guanqun
Copy link
Contributor Author

guanqun commented Oct 31, 2019

Per estebank's comment,

r? @estebank

@petrochenkov petrochenkov self-assigned this Nov 3, 2019
@petrochenkov
Copy link
Contributor

Superseded by #66054.

@guanqun guanqun deleted the adjust-close-delim-span branch November 3, 2019 14:31
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
S-waiting-on-review Status: Awaiting review from the assignee but also interested parties.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

compiler panic: "byte index 10 is not a char boundary"
5 participants