Skip to content

Avoid ICE in macro's diagnostics #68633

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

Merged
merged 2 commits into from
Feb 1, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
40 changes: 23 additions & 17 deletions src/librustc_parse/parser/item.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1638,26 +1638,32 @@ impl<'a> Parser<'a> {
.span_to_snippet(self.prev_span)
.map(|s| s.ends_with(")") || s.ends_with("]"))
.unwrap_or(false);
let right_brace_span = if has_close_delim {
// it's safe to peel off one character only when it has the close delim
self.prev_span.with_lo(self.prev_span.hi() - BytePos(1))
} else {
self.prev_span.shrink_to_hi()
};

self.struct_span_err(
let mut err = self.struct_span_err(
self.prev_span,
"macros that expand to items must be delimited with braces or followed by a semicolon",
)
.multipart_suggestion(
"change the delimiters to curly braces",
vec![
(self.prev_span.with_hi(self.prev_span.lo() + BytePos(1)), "{".to_string()),
(right_brace_span, '}'.to_string()),
],
Applicability::MaybeIncorrect,
)
.span_suggestion(
);

// To avoid ICE, we shouldn't emit actual suggestions when it hasn't closing delims
if has_close_delim {
err.multipart_suggestion(
"change the delimiters to curly braces",
vec![
(self.prev_span.with_hi(self.prev_span.lo() + BytePos(1)), '{'.to_string()),
(self.prev_span.with_lo(self.prev_span.hi() - BytePos(1)), '}'.to_string()),
Copy link
Contributor

Choose a reason for hiding this comment

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

Feels like there's a missing Span method here (self.prev_span.shift_left(...))

Copy link
Member Author

Choose a reason for hiding this comment

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

I couldn't find the shift_left method: https://doc.rust-lang.org/nightly/nightly-rustc/rustc_span/struct.Span.html
Am I still missing something?

Copy link
Contributor

Choose a reason for hiding this comment

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

I'm saying that we could add a higher level operation to avoid dealing with primitives like this. :)

Copy link
Member Author

Choose a reason for hiding this comment

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

Ah, so we can add this tweak as a pub method of Span, right?

Copy link
Contributor

Choose a reason for hiding this comment

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

Yeah that would be the idea, if we think it's a good one. cc also @petrochenkov

Copy link
Member Author

@JohnTitor JohnTitor Jan 30, 2020

Choose a reason for hiding this comment

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

I feel it'd be nice that we'll do it as follow-up work, how about?

Copy link
Contributor

Choose a reason for hiding this comment

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

Works for me.

Copy link
Contributor

Choose a reason for hiding this comment

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

SourceMap::next_point let's you advance 1 char at a time (and is now unicode aware). We don't have a method to move left. It is in SourceMap instead of Span because you need to look at the underlying code for the span to be validated as not falling in the middle of a character.

Copy link
Contributor

Choose a reason for hiding this comment

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

Also, this might work better with shrink_to_hi and shrink_to_lo instead of with_*.

],
Applicability::MaybeIncorrect,
);
} else {
err.span_suggestion(
self.prev_span,
"change the delimiters to curly braces",
" { /* items */ }".to_string(),
Applicability::HasPlaceholders,
);
}

err.span_suggestion(
self.prev_span.shrink_to_hi(),
"add a semicolon",
';'.to_string(),
Expand Down
5 changes: 2 additions & 3 deletions src/test/ui/parser/issue-62524.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,8 @@ LL | | Ϥ,
|
help: change the delimiters to curly braces
|
LL | y!{
LL | Ϥ}
|
LL | y! { /* items */ }
| ^^^^^^^^^^^^^^^
help: add a semicolon
|
LL | Ϥ,;
Expand Down
Binary file added src/test/ui/parser/issue-68629.rs
Binary file not shown.
Binary file added src/test/ui/parser/issue-68629.stderr
Binary file not shown.
4 changes: 2 additions & 2 deletions src/test/ui/parser/mbe_missing_right_paren.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,8 @@ LL | macro_rules! abc(ؼ
|
help: change the delimiters to curly braces
|
LL | macro_rules! abc}
| ^ ^
LL | macro_rules! abc { /* items */ }
| ^^^^^^^^^^^^^^^
help: add a semicolon
|
LL | macro_rules! abc(ؼ;
Expand Down