Skip to content

Adjust close delim span #65966

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

Closed
wants to merge 4 commits into from
Closed
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
22 changes: 22 additions & 0 deletions src/libsyntax/parse/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -135,6 +135,28 @@ fn string_to_tts_1() {
})
}

#[test]
fn string_to_tts_2() {
with_default_globals(|| {
let tts = string_to_stream("fn main(\u{063c}".to_string());

let expected = TokenStream::new(vec![
TokenTree::token(token::Ident(kw::Fn, false), sp(0, 2)).into(),
TokenTree::token(token::Ident(Name::intern("main"), false), sp(3, 7)).into(),
TokenTree::Delimited(
DelimSpan::from_pair(sp(7, 8), sp(10, 10)),
token::DelimToken::Paren,
TokenStream::new(vec![
TokenTree::token(token::Ident(Name::intern("\u{063c}"), false),
sp(8, 10)).into(),
]).into(),
).into()
]);

assert_eq!(tts, expected);
})
}

#[test] fn parse_use() {
with_default_globals(|| {
let use_s = "use foo::bar::baz;";
Expand Down
3 changes: 2 additions & 1 deletion src/libsyntax/tokenstream.rs
Original file line number Diff line number Diff line change
Expand Up @@ -115,14 +115,15 @@ impl TokenTree {
let open_span = if span.is_dummy() {
span
} else {
assert!(span.lo() != span.hi());
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.

Expand Down
4 changes: 2 additions & 2 deletions src/test/ui/parser/issue-62973.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -33,13 +33,13 @@ LL | )
|

error: expected one of `.`, `?`, `{`, or an operator, found `}`
--> $DIR/issue-62973.rs:8:1
--> $DIR/issue-62973.rs:8:2
|
LL | fn p() { match s { v, E { [) {) }
| ----- while parsing this match expression
LL |
LL |
| ^ expected one of `.`, `?`, `{`, or an operator here
| ^ expected one of `.`, `?`, `{`, or an operator here

error: incorrect close delimiter: `)`
--> $DIR/issue-62973.rs:6:28
Expand Down
8 changes: 4 additions & 4 deletions src/test/ui/parser/issue-63135.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -23,16 +23,16 @@ LL | fn i(n{...,f #
| `..` must be at the end and cannot have a trailing comma

error: expected `[`, found `}`
--> $DIR/issue-63135.rs:3:15
--> $DIR/issue-63135.rs:3:16
|
LL | fn i(n{...,f #
| ^ expected `[`
| ^ expected `[`

error: expected one of `:` or `|`, found `)`
--> $DIR/issue-63135.rs:3:15
--> $DIR/issue-63135.rs:3:16
|
LL | fn i(n{...,f #
| ^ expected one of `:` or `|` here
| ^ expected one of `:` or `|` here

error: aborting due to 5 previous errors

3 changes: 3 additions & 0 deletions src/test/ui/parser/missing_right_paren.rs
Original file line number Diff line number Diff line change
@@ -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.

17 changes: 17 additions & 0 deletions src/test/ui/parser/missing_right_paren.stderr
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
error: this file contains an un-closed delimiter
--> $DIR/missing_right_paren.rs:3:11
|
LL | fn main((ؼ
| -- ^
| ||
| |un-closed delimiter
| un-closed delimiter

error: expected one of `:` or `|`, found `)`
--> $DIR/missing_right_paren.rs:3:11
|
LL | fn main((ؼ
| ^ expected one of `:` or `|` here

error: aborting due to 2 previous errors