From 50a42fe5134fd8dbd5bc6df110654a31ee059a62 Mon Sep 17 00:00:00 2001 From: Guillaume Gomez Date: Wed, 10 Jun 2020 11:53:57 +0200 Subject: [PATCH 1/2] Create new error code E0762 for unterminated char literals --- src/librustc_error_codes/error_codes.rs | 1 + src/librustc_error_codes/error_codes/E0762.md | 13 +++++++++++++ src/librustc_parse/lexer/mod.rs | 10 +++++++++- 3 files changed, 23 insertions(+), 1 deletion(-) create mode 100644 src/librustc_error_codes/error_codes/E0762.md diff --git a/src/librustc_error_codes/error_codes.rs b/src/librustc_error_codes/error_codes.rs index ec5b3251e68..285242647b3 100644 --- a/src/librustc_error_codes/error_codes.rs +++ b/src/librustc_error_codes/error_codes.rs @@ -440,6 +440,7 @@ E0754: include_str!("./error_codes/E0754.md"), E0758: include_str!("./error_codes/E0758.md"), E0760: include_str!("./error_codes/E0760.md"), E0761: include_str!("./error_codes/E0761.md"), +E0762: include_str!("./error_codes/E0762.md"), ; // E0006, // merged with E0005 // E0008, // cannot bind by-move into a pattern guard diff --git a/src/librustc_error_codes/error_codes/E0762.md b/src/librustc_error_codes/error_codes/E0762.md new file mode 100644 index 00000000000..b01ded4a866 --- /dev/null +++ b/src/librustc_error_codes/error_codes/E0762.md @@ -0,0 +1,13 @@ +A character literal wasn't ended with a quote. + +Erroneous code example: + +```compile_fail,E0762 +static C: char = '●; // error! +``` + +To fix this error, add the missing quote: + +``` +static C: char = '●'; // ok! +``` diff --git a/src/librustc_parse/lexer/mod.rs b/src/librustc_parse/lexer/mod.rs index 9bc6a50acad..d135f713478 100644 --- a/src/librustc_parse/lexer/mod.rs +++ b/src/librustc_parse/lexer/mod.rs @@ -325,7 +325,15 @@ impl<'a> StringReader<'a> { let (lit_kind, mode, prefix_len, postfix_len) = match kind { rustc_lexer::LiteralKind::Char { terminated } => { if !terminated { - self.fatal_span_(start, suffix_start, "unterminated character literal").raise() + self.sess + .span_diagnostic + .struct_span_fatal_with_code( + self.mk_sp(start, suffix_start), + "unterminated character literal", + error_code!(E0762), + ) + .emit(); + FatalError.raise(); } (token::Char, Mode::Char, 1, 1) // ' ' } From 7bd87cfac2ca4c2c19b04018774b1fb1a9b39e48 Mon Sep 17 00:00:00 2001 From: Guillaume Gomez Date: Wed, 10 Jun 2020 11:55:50 +0200 Subject: [PATCH 2/2] Add tests for E0762 --- src/test/ui/parser/lex-bad-char-literals-4.stderr | 3 ++- src/test/ui/parser/lex-bad-char-literals-7.stderr | 3 ++- 2 files changed, 4 insertions(+), 2 deletions(-) diff --git a/src/test/ui/parser/lex-bad-char-literals-4.stderr b/src/test/ui/parser/lex-bad-char-literals-4.stderr index 8f8f806f6cf..fec4421c48a 100644 --- a/src/test/ui/parser/lex-bad-char-literals-4.stderr +++ b/src/test/ui/parser/lex-bad-char-literals-4.stderr @@ -1,4 +1,4 @@ -error: unterminated character literal +error[E0762]: unterminated character literal --> $DIR/lex-bad-char-literals-4.rs:4:5 | LL | '● @@ -6,3 +6,4 @@ LL | '● error: aborting due to previous error +For more information about this error, try `rustc --explain E0762`. diff --git a/src/test/ui/parser/lex-bad-char-literals-7.stderr b/src/test/ui/parser/lex-bad-char-literals-7.stderr index ee9aa869352..70ee8087b51 100644 --- a/src/test/ui/parser/lex-bad-char-literals-7.stderr +++ b/src/test/ui/parser/lex-bad-char-literals-7.stderr @@ -10,7 +10,7 @@ error: empty unicode escape (must have at least 1 hex digit) LL | let _: char = '\u{}'; | ^^^^ -error: unterminated character literal +error[E0762]: unterminated character literal --> $DIR/lex-bad-char-literals-7.rs:11:13 | LL | let _ = ' hello // here's a comment @@ -18,3 +18,4 @@ LL | let _ = ' hello // here's a comment error: aborting due to 3 previous errors +For more information about this error, try `rustc --explain E0762`.