Skip to content
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
2 changes: 1 addition & 1 deletion src/libsyntax/parse/lexer/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1445,7 +1445,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::Char(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 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))),

// 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
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`.
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
;

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`.
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