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

Disallow char literals which should be escaped #9335

Merged
merged 1 commit into from
Sep 25, 2013
Merged
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
49 changes: 28 additions & 21 deletions src/libsyntax/parse/lexer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -747,27 +747,34 @@ fn next_token_inner(rdr: @mut StringReader) -> token::Token {
}

// Otherwise it is a character constant:
if c2 == '\\' {
// '\X' for some X must be a character constant:
let escaped = rdr.curr;
let escaped_pos = rdr.last_pos;
bump(rdr);
match escaped {
'n' => { c2 = '\n'; }
'r' => { c2 = '\r'; }
't' => { c2 = '\t'; }
'\\' => { c2 = '\\'; }
'\'' => { c2 = '\''; }
'"' => { c2 = '"'; }
'0' => { c2 = '\x00'; }
'x' => { c2 = scan_numeric_escape(rdr, 2u); }
'u' => { c2 = scan_numeric_escape(rdr, 4u); }
'U' => { c2 = scan_numeric_escape(rdr, 8u); }
c2 => {
fatal_span_char(rdr, escaped_pos, rdr.last_pos,
~"unknown character escape", c2);
}
match c2 {
'\\' => {
// '\X' for some X must be a character constant:
let escaped = rdr.curr;
let escaped_pos = rdr.last_pos;
bump(rdr);
match escaped {
'n' => { c2 = '\n'; }
'r' => { c2 = '\r'; }
't' => { c2 = '\t'; }
'\\' => { c2 = '\\'; }
'\'' => { c2 = '\''; }
'"' => { c2 = '"'; }
'0' => { c2 = '\x00'; }
'x' => { c2 = scan_numeric_escape(rdr, 2u); }
'u' => { c2 = scan_numeric_escape(rdr, 4u); }
'U' => { c2 = scan_numeric_escape(rdr, 8u); }
c2 => {
fatal_span_char(rdr, escaped_pos, rdr.last_pos,
~"unknown character escape", c2);
}
}
}
'\t' | '\n' | '\r' | '\'' => {
fatal_span_char(rdr, start, rdr.last_pos,
~"character constant must be escaped", c2);
}
_ => {}
}
if rdr.curr != '\'' {
fatal_span_verbose(rdr,
Expand Down Expand Up @@ -973,7 +980,7 @@ mod test {
}

#[test] fn character_escaped() {
let env = setup(@"'\n'");
let env = setup(@"'\\n'");
let TokenAndSpan {tok, sp: _} =
env.string_reader.next_token();
assert_eq!(tok, token::LIT_CHAR('\n' as u32));
Expand Down
15 changes: 15 additions & 0 deletions src/test/compile-fail/bad-char-literals.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
// Copyright 2013 The Rust Project Developers. See the COPYRIGHT
// file at the top-level directory of this distribution and at
// http://rust-lang.org/COPYRIGHT.
//
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
// option. This file may not be copied, modified, or distributed
// except according to those terms.

fn main() {
// these literals are just silly.
''';
//~^ ERROR: character constant must be escaped
}
16 changes: 16 additions & 0 deletions src/test/compile-fail/bad-char-literals2.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
// Copyright 2013 The Rust Project Developers. See the COPYRIGHT
// file at the top-level directory of this distribution and at
// http://rust-lang.org/COPYRIGHT.
//
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
// option. This file may not be copied, modified, or distributed
// except according to those terms.

fn main() {
// note that this is a literal "\n" byte
'
';
//~^^ ERROR: character constant must be escaped
}
15 changes: 15 additions & 0 deletions src/test/compile-fail/bad-char-literals3.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
// Copyright 2013 The Rust Project Developers. See the COPYRIGHT
// file at the top-level directory of this distribution and at
// http://rust-lang.org/COPYRIGHT.
//
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
// option. This file may not be copied, modified, or distributed
// except according to those terms.

fn main() {
// note that this is a literal "\r" byte
'';
//~^ ERROR: character constant must be escaped
}
Expand Down
15 changes: 15 additions & 0 deletions src/test/compile-fail/bad-char-literals4.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
// Copyright 2013 The Rust Project Developers. See the COPYRIGHT
// file at the top-level directory of this distribution and at
// http://rust-lang.org/COPYRIGHT.
//
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
// option. This file may not be copied, modified, or distributed
// except according to those terms.

fn main() {
// note that this is a literal tab character here
' ';
//~^ ERROR: character constant must be escaped
}