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

Implement new literal type Err #57651

Merged
merged 14 commits into from
Jan 20, 2019
1 change: 1 addition & 0 deletions src/librustc/ich/impls_syntax.rs
Original file line number Diff line number Diff line change
Expand Up @@ -329,6 +329,7 @@ fn hash_token<'a, 'gcx, W: StableHasherResult>(
match *lit {
token::Lit::Byte(val) |
token::Lit::Char(val) |
token::Lit::Err(val) |
token::Lit::Integer(val) |
token::Lit::Float(val) |
token::Lit::Str_(val) |
Expand Down
2 changes: 1 addition & 1 deletion src/librustdoc/html/highlight.rs
Original file line number Diff line number Diff line change
Expand Up @@ -296,7 +296,7 @@ impl<'a> Classifier<'a> {
token::Literal(lit, _suf) => {
match lit {
// Text literals.
token::Byte(..) | token::Char(..) |
token::Byte(..) | token::Char(..) | token::Err(..) |
token::ByteStr(..) | token::ByteStrRaw(..) |
token::Str_(..) | token::StrRaw(..) => Class::String,

Expand Down
1 change: 1 addition & 0 deletions src/libsyntax/ext/quote.rs
Original file line number Diff line number Diff line change
Expand Up @@ -646,6 +646,7 @@ fn expr_mk_token(cx: &ExtCtxt, sp: Span, tok: &token::Token) -> P<ast::Expr> {

token::Literal(token::Byte(i), suf) => return mk_lit!("Byte", suf, i),
token::Literal(token::Char(i), suf) => return mk_lit!("Char", suf, i),
token::Literal(token::Err(_i), _suf) => return cx.expr(sp, ast::ExprKind::Err),
token::Literal(token::Integer(i), suf) => return mk_lit!("Integer", suf, i),
token::Literal(token::Float(i), suf) => return mk_lit!("Float", suf, i),
token::Literal(token::Str_(i), suf) => return mk_lit!("Str_", suf, i),
Expand Down
14 changes: 8 additions & 6 deletions src/libsyntax/parse/lexer/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1408,9 +1408,10 @@ impl<'a> StringReader<'a> {
// lifetimes shouldn't end with a single quote
estebank marked this conversation as resolved.
Show resolved Hide resolved
// if we find one, then this is an invalid character literal
if self.ch_is('\'') {
self.fatal_span_verbose(start_with_quote, self.next_pos,
String::from("character literal may only contain one codepoint"))
.raise();
self.err_span_(start_with_quote, self.next_pos,
"character literal may only contain one codepoint");
self.bump();
return Ok(token::Literal(token::Err(Symbol::intern("??")), None))

}

Expand Down Expand Up @@ -1445,7 +1446,7 @@ impl<'a> StringReader<'a> {
format!("\"{}\"", &self.src[start..end]),
Applicability::MachineApplicable
).emit();
return Ok(token::Literal(token::Str_(Symbol::intern("??")), None))
return Ok(token::Literal(token::Err(Symbol::intern("??")), None))
}
if self.ch_is('\n') || self.is_eof() || self.ch_is('/') {
// Only attempt to infer single line string literals. If we encounter
Expand All @@ -1455,8 +1456,9 @@ impl<'a> StringReader<'a> {
}
estebank marked this conversation as resolved.
Show resolved Hide resolved
}

self.fatal_span_verbose(start_with_quote, pos,
String::from("character literal may only contain one codepoint")).raise();
self.err_span_(start_with_quote, pos,
"character literal may only contain one codepoint");
self.bump();
Copy link
Contributor

Choose a reason for hiding this comment

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

This bump also looks suspect. I'm trying a change locally, but I believe there's a high likelihood that removing this self.bump() will fix the problem with let x = 'asdf //~^ ERROR '.

Copy link
Contributor

Choose a reason for hiding this comment

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

Ok, upon further review, just revert this one change and let's fatally fail on this case. The code is beyond meaninful recovery at this point. We'll keep the current behavior for unterminated char literals, but recover in all other cases.

Copy link
Contributor

Choose a reason for hiding this comment

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

@JohnTitor I believe that with these changes we'll be as close as we can get quickly to the "ideal" output:

estebank@63ad0a4

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'm grateful for your support! Following your commit, I added some commits.

}

let id = if valid {
Expand Down
1 change: 1 addition & 0 deletions src/libsyntax/parse/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -466,6 +466,7 @@ crate fn lit_token(lit: token::Lit, suf: Option<Symbol>, diag: Option<(Span, &Ha
match lit {
token::Byte(i) => (true, Some(LitKind::Byte(byte_lit(&i.as_str()).0))),
token::Char(i) => (true, Some(LitKind::Char(char_lit(&i.as_str(), diag).0))),
token::Err(i) => (true, Some(LitKind::Char(char_lit(&i.as_str(), diag).0))),
estebank marked this conversation as resolved.
Show resolved Hide resolved

// There are some valid suffixes for integer and float literals,
// so all the handling is done internally.
Expand Down
2 changes: 2 additions & 0 deletions src/libsyntax/parse/token.rs
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,7 @@ impl DelimToken {
pub enum Lit {
Byte(ast::Name),
Char(ast::Name),
Err(ast::Name),
Integer(ast::Name),
Float(ast::Name),
Str_(ast::Name),
Expand All @@ -73,6 +74,7 @@ impl Lit {
match *self {
Byte(_) => "byte literal",
Char(_) => "char literal",
Err(_) => "invalid literal",
Integer(_) => "integer literal",
Float(_) => "float literal",
Str_(_) | StrRaw(..) => "string literal",
Expand Down
1 change: 1 addition & 0 deletions src/libsyntax/print/pprust.rs
Original file line number Diff line number Diff line change
Expand Up @@ -224,6 +224,7 @@ pub fn token_to_string(tok: &Token) -> String {
let mut out = match lit {
token::Byte(b) => format!("b'{}'", b),
token::Char(c) => format!("'{}'", c),
token::Err(c) => format!("'{}'", c),
token::Float(c) |
token::Integer(c) => c.to_string(),
token::Str_(s) => format!("\"{}\"", s),
Expand Down
2 changes: 1 addition & 1 deletion src/test/ui/parser/lex-bad-char-literals-2.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// This test needs to the last one appearing in this file as it kills the parser
static c: char =
'nope' //~ ERROR: character literal may only contain one codepoint: 'nope'
'nope' //~ ERROR: character literal may only contain one codepoint
;
9 changes: 7 additions & 2 deletions src/test/ui/parser/lex-bad-char-literals-2.stderr
Original file line number Diff line number Diff line change
@@ -1,8 +1,13 @@
error: character literal may only contain one codepoint: 'nope'
error: character literal may only contain one codepoint
--> $DIR/lex-bad-char-literals-2.rs:3:5
|
LL | 'nope' //~ ERROR: character literal may only contain one codepoint: 'nope'
| ^^^^^^

error: aborting due to previous error
error[E0601]: `main` function not found in crate `lex_bad_char_literals_2`
|
= note: consider adding a `main` function to `$DIR/lex-bad-char-literals-2.rs`

error: aborting due to 2 previous errors

For more information about this error, try `rustc --explain E0601`.
6 changes: 4 additions & 2 deletions src/test/ui/parser/lex-bad-char-literals-3.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
// This test needs to the last one appearing in this file as it kills the parser
static c: char =
'●●' //~ ERROR: character literal may only contain one codepoint
//~| ERROR: mismatched types
;

fn main() {}
fn main() {
let ch: &str = '●●'; //~ ERROR: character literal may only contain one codepoint
//~^ ERROR: mismatched types
}
22 changes: 16 additions & 6 deletions src/test/ui/parser/lex-bad-char-literals-3.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -8,15 +8,25 @@ help: if you meant to write a `str` literal, use double quotes
LL | "●●" //~ ERROR: character literal may only contain one codepoint
| ^^^^

error: character literal may only contain one codepoint
--> $DIR/lex-bad-char-literals-3.rs:7:20
|
LL | let ch: &str = '●●'; //~ ERROR: character literal may only contain one codepoint
| ^^^^
help: if you meant to write a `str` literal, use double quotes
|
LL | let ch: &str = "●●"; //~ ERROR: character literal may only contain one codepoint
| ^^^^

error[E0308]: mismatched types
--> $DIR/lex-bad-char-literals-3.rs:3:5
--> $DIR/lex-bad-char-literals-3.rs:7:20
|
LL | '●●' //~ ERROR: character literal may only contain one codepoint
| ^^^^ expected char, found reference
LL | let ch: &str = '●●'; //~ ERROR: character literal may only contain one codepoint
| ^^^^ expected &str, found char
|
= note: expected type `char`
found type `&'static str`
= note: expected type `&str`
found type `char`

error: aborting due to 2 previous errors
error: aborting due to 3 previous errors

For more information about this error, try `rustc --explain E0308`.
2 changes: 1 addition & 1 deletion src/test/ui/parser/lex-bad-char-literals-4.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
//
// This test needs to the last one appearing in this file as it kills the parser
static c: char =
'● //~ ERROR: character literal may only contain one codepoint: '●
'● //~ ERROR: character literal may only contain one codepoint
;
16 changes: 14 additions & 2 deletions src/test/ui/parser/lex-bad-char-literals-4.stderr
Original file line number Diff line number Diff line change
@@ -1,8 +1,20 @@
error: character literal may only contain one codepoint: '●
error: character literal may only contain one codepoint
--> $DIR/lex-bad-char-literals-4.rs:4:5
|
LL | '● //~ ERROR: character literal may only contain one codepoint: '●
| ^^

error: aborting due to previous error
error: character literal may only contain one codepoint
--> $DIR/lex-bad-char-literals-4.rs:4:70
|
LL | '● //~ ERROR: character literal may only contain one codepoint: '●
| ^^

error: expected one of `.`, `;`, `?`, or an operator, found `~`
--> $DIR/lex-bad-char-literals-4.rs:4:11
|
LL | '● //~ ERROR: character literal may only contain one codepoint: '●
| ^ expected one of `.`, `;`, `?`, or an operator here

error: aborting due to 3 previous errors

6 changes: 4 additions & 2 deletions src/test/ui/parser/lex-bad-char-literals-5.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,9 @@
// This test needs to the last one appearing in this file as it kills the parser
static c: char =
'\x10\x10' //~ ERROR: character literal may only contain one codepoint
//~| ERROR: mismatched types
;
estebank marked this conversation as resolved.
Show resolved Hide resolved

fn main() {}
fn main() {
let ch: &str = '\x10\x10'; //~ ERROR: character literal may only contain one codepoint
//~^ ERROR: mismatched types
}
22 changes: 16 additions & 6 deletions src/test/ui/parser/lex-bad-char-literals-5.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -8,15 +8,25 @@ help: if you meant to write a `str` literal, use double quotes
LL | "/x10/x10" //~ ERROR: character literal may only contain one codepoint
| ^^^^^^^^^^

error: character literal may only contain one codepoint
--> $DIR/lex-bad-char-literals-5.rs:8:20
|
LL | let ch: &str = '/x10/x10'; //~ ERROR: character literal may only contain one codepoint
| ^^^^^^^^^^
help: if you meant to write a `str` literal, use double quotes
|
LL | let ch: &str = "/x10/x10"; //~ ERROR: character literal may only contain one codepoint
| ^^^^^^^^^^

error[E0308]: mismatched types
--> $DIR/lex-bad-char-literals-5.rs:4:5
--> $DIR/lex-bad-char-literals-5.rs:8:20
|
LL | '/x10/x10' //~ ERROR: character literal may only contain one codepoint
| ^^^^^^^^^^ expected char, found reference
LL | let ch: &str = '/x10/x10'; //~ ERROR: character literal may only contain one codepoint
| ^^^^^^^^^^ expected &str, found char
|
= note: expected type `char`
found type `&'static str`
= note: expected type `&str`
found type `char`

error: aborting due to 2 previous errors
error: aborting due to 3 previous errors

For more information about this error, try `rustc --explain E0308`.
12 changes: 12 additions & 0 deletions src/test/ui/parser/lex-bad-char-literals-6.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
fn main() {
let x: &str = 'ab'; //~ ERROR: character literal may only contain one codepoint
//~^ ERROR: mismatched types
let y: char = 'cd'; //~ ERROR: character literal may only contain one codepoint
let z = 'ef'; //~ ERROR: character literal may only contain one codepoint

if x == y {} //~ ERROR: can't compare `&str` with `char`
if y == z {} // no error here
if x == z {} //~ ERROR: can't compare `&str` with `char`

let a: usize = ""; //~ ERROR: mismatched types
}
56 changes: 56 additions & 0 deletions src/test/ui/parser/lex-bad-char-literals-6.stderr
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
error: character literal may only contain one codepoint
--> $DIR/lex-bad-char-literals-6.rs:2:19
|
LL | let x: &str = 'ab'; //~ ERROR: character literal may only contain one codepoint
| ^^^^

error: character literal may only contain one codepoint
--> $DIR/lex-bad-char-literals-6.rs:3:19
|
LL | let y: char = 'cd'; //~ ERROR: character literal may only contain one codepoint
| ^^^^

error: character literal may only contain one codepoint
--> $DIR/lex-bad-char-literals-6.rs:4:13
|
LL | let z = 'ef'; //~ ERROR: character literal may only contain one codepoint
| ^^^^

error[E0308]: mismatched types
--> $DIR/lex-bad-char-literals-6.rs:2:19
|
LL | let x: &str = 'ab'; //~ ERROR: character literal may only contain one codepoint
| ^^^^ expected &str, found char
|
= note: expected type `&str`
found type `char`

error[E0277]: can't compare `&str` with `char`
--> $DIR/lex-bad-char-literals-6.rs:6:10
|
LL | if x == y {} // no error here
| ^^ no implementation for `&str == char`
|
= help: the trait `std::cmp::PartialEq<char>` is not implemented for `&str`

error[E0308]: mismatched types
--> $DIR/lex-bad-char-literals-6.rs:10:20
|
LL | let a: usize = ""; // type error here to confirm we got past the parser
| ^^ expected usize, found reference
|
= note: expected type `usize`
found type `&'static str`

error[E0277]: can't compare `&str` with `char`
--> $DIR/lex-bad-char-literals-6.rs:8:10
|
LL | if x == z {} // no error here
| ^^ no implementation for `&str == char`
|
= help: the trait `std::cmp::PartialEq<char>` is not implemented for `&str`

error: aborting due to 7 previous errors

Some errors occurred: E0277, E0308.
For more information about an error, try `rustc --explain E0277`.
4 changes: 2 additions & 2 deletions src/test/ui/str/str-as-char.fixed
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
// run-rustfix

fn main() {
println!("●●");
//~^ ERROR character literal may only contain one codepoint
println!("{}", "●●"); //~ ERROR character literal may only contain one codepoint
//~^ ERROR format argument must be a string literal
}
4 changes: 2 additions & 2 deletions src/test/ui/str/str-as-char.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
// run-rustfix

fn main() {
println!('●●');
//~^ ERROR character literal may only contain one codepoint
println!('●●'); //~ ERROR character literal may only contain one codepoint
//~^ ERROR format argument must be a string literal
}
16 changes: 13 additions & 3 deletions src/test/ui/str/str-as-char.stderr
Original file line number Diff line number Diff line change
@@ -1,12 +1,22 @@
error: character literal may only contain one codepoint
--> $DIR/str-as-char.rs:4:14
|
LL | println!('●●');
LL | println!('●●'); //~ ERROR character literal may only contain one codepoint
| ^^^^
help: if you meant to write a `str` literal, use double quotes
|
LL | println!("●●");
LL | println!("●●"); //~ ERROR character literal may only contain one codepoint
| ^^^^

error: aborting due to previous error
error: format argument must be a string literal
--> $DIR/str-as-char.rs:4:14
|
LL | println!('●●'); //~ ERROR character literal may only contain one codepoint
| ^^^^
help: you might be missing a string literal to format with
|
LL | println!("{}", '●●'); //~ ERROR character literal may only contain one codepoint
| ^^^^^

error: aborting due to 2 previous errors