From a8f4699f4fb12e4ff817256855cca2a5c542ed44 Mon Sep 17 00:00:00 2001 From: Guanqun Lu Date: Thu, 17 Oct 2019 16:54:18 +0800 Subject: [PATCH 1/4] don't slice a span whose length is 0 --- src/libsyntax/tokenstream.rs | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/src/libsyntax/tokenstream.rs b/src/libsyntax/tokenstream.rs index 0ff1c26bac2b2..7e11f3d1fbcda 100644 --- a/src/libsyntax/tokenstream.rs +++ b/src/libsyntax/tokenstream.rs @@ -115,6 +115,7 @@ 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) @@ -122,7 +123,7 @@ impl TokenTree { /// 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)) From 690ff2a5e3a51ae7d399073a1a21218e5da42fba Mon Sep 17 00:00:00 2001 From: Guanqun Lu Date: Thu, 17 Oct 2019 23:47:28 +0800 Subject: [PATCH 2/4] add unit tests --- src/libsyntax/parse/tests.rs | 22 +++++++++++++++++++ src/test/ui/parser/missing_right_paren.rs | 3 +++ src/test/ui/parser/missing_right_paren.stderr | 17 ++++++++++++++ 3 files changed, 42 insertions(+) create mode 100644 src/test/ui/parser/missing_right_paren.rs create mode 100644 src/test/ui/parser/missing_right_paren.stderr diff --git a/src/libsyntax/parse/tests.rs b/src/libsyntax/parse/tests.rs index 3bdb9227b4edd..ee960e8f773b4 100644 --- a/src/libsyntax/parse/tests.rs +++ b/src/libsyntax/parse/tests.rs @@ -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;"; diff --git a/src/test/ui/parser/missing_right_paren.rs b/src/test/ui/parser/missing_right_paren.rs new file mode 100644 index 0000000000000..4f7c5eea1d183 --- /dev/null +++ b/src/test/ui/parser/missing_right_paren.rs @@ -0,0 +1,3 @@ +// ignore-tidy-trailing-newlines +// error-pattern: aborting due to 2 previous errors +fn main((ؼ \ No newline at end of file diff --git a/src/test/ui/parser/missing_right_paren.stderr b/src/test/ui/parser/missing_right_paren.stderr new file mode 100644 index 0000000000000..fc75b031e76a8 --- /dev/null +++ b/src/test/ui/parser/missing_right_paren.stderr @@ -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 + From c6ba7d69ed690c65296d2a933e55e61a12f98b91 Mon Sep 17 00:00:00 2001 From: Guanqun Lu Date: Wed, 30 Oct 2019 22:46:03 +0800 Subject: [PATCH 3/4] fixup existing unit tests --- src/test/ui/parser/issue-62973.stderr | 2 +- src/test/ui/parser/issue-63135.stderr | 4 ++-- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/src/test/ui/parser/issue-62973.stderr b/src/test/ui/parser/issue-62973.stderr index 141076bf6b638..57eb2ef1ca02f 100644 --- a/src/test/ui/parser/issue-62973.stderr +++ b/src/test/ui/parser/issue-62973.stderr @@ -39,7 +39,7 @@ 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 diff --git a/src/test/ui/parser/issue-63135.stderr b/src/test/ui/parser/issue-63135.stderr index a077ad454a9df..fd58d985fc39e 100644 --- a/src/test/ui/parser/issue-63135.stderr +++ b/src/test/ui/parser/issue-63135.stderr @@ -26,13 +26,13 @@ error: expected `[`, found `}` --> $DIR/issue-63135.rs:3:15 | LL | fn i(n{...,f # - | ^ expected `[` + | ^ expected `[` error: expected one of `:` or `|`, found `)` --> $DIR/issue-63135.rs:3:15 | LL | fn i(n{...,f # - | ^ expected one of `:` or `|` here + | ^ expected one of `:` or `|` here error: aborting due to 5 previous errors From 3a4d6d26f5ffc482480356896eba6fccc2760ab4 Mon Sep 17 00:00:00 2001 From: Guanqun Lu Date: Thu, 31 Oct 2019 12:08:39 +0800 Subject: [PATCH 4/4] fixup the unit tests --- src/test/ui/parser/issue-62973.stderr | 2 +- src/test/ui/parser/issue-63135.stderr | 4 ++-- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/src/test/ui/parser/issue-62973.stderr b/src/test/ui/parser/issue-62973.stderr index 57eb2ef1ca02f..a1b711140a780 100644 --- a/src/test/ui/parser/issue-62973.stderr +++ b/src/test/ui/parser/issue-62973.stderr @@ -33,7 +33,7 @@ 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 diff --git a/src/test/ui/parser/issue-63135.stderr b/src/test/ui/parser/issue-63135.stderr index fd58d985fc39e..8e8087978a366 100644 --- a/src/test/ui/parser/issue-63135.stderr +++ b/src/test/ui/parser/issue-63135.stderr @@ -23,13 +23,13 @@ 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 `[` 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