auto merge of #9335 : alexcrichton/rust/issue-7945, r=thestinger
As documented in issue #7945, these literal identifiers are all accepted by rust today, but they should probably be disallowed (especially `'''`). This changes all escapable sequences to being *required* to be escaped. Closes #7945 I wanted to write the tests with more exact spans, but I think #9308 will be fixing that?
This commit is contained in:
commit
512f7781fe
|
@ -747,27 +747,34 @@ fn next_token_inner(rdr: @mut StringReader) -> token::Token {
|
||||||
}
|
}
|
||||||
|
|
||||||
// Otherwise it is a character constant:
|
// Otherwise it is a character constant:
|
||||||
if c2 == '\\' {
|
match c2 {
|
||||||
// '\X' for some X must be a character constant:
|
'\\' => {
|
||||||
let escaped = rdr.curr;
|
// '\X' for some X must be a character constant:
|
||||||
let escaped_pos = rdr.last_pos;
|
let escaped = rdr.curr;
|
||||||
bump(rdr);
|
let escaped_pos = rdr.last_pos;
|
||||||
match escaped {
|
bump(rdr);
|
||||||
'n' => { c2 = '\n'; }
|
match escaped {
|
||||||
'r' => { c2 = '\r'; }
|
'n' => { c2 = '\n'; }
|
||||||
't' => { c2 = '\t'; }
|
'r' => { c2 = '\r'; }
|
||||||
'\\' => { c2 = '\\'; }
|
't' => { c2 = '\t'; }
|
||||||
'\'' => { c2 = '\''; }
|
'\\' => { c2 = '\\'; }
|
||||||
'"' => { c2 = '"'; }
|
'\'' => { c2 = '\''; }
|
||||||
'0' => { c2 = '\x00'; }
|
'"' => { c2 = '"'; }
|
||||||
'x' => { c2 = scan_numeric_escape(rdr, 2u); }
|
'0' => { c2 = '\x00'; }
|
||||||
'u' => { c2 = scan_numeric_escape(rdr, 4u); }
|
'x' => { c2 = scan_numeric_escape(rdr, 2u); }
|
||||||
'U' => { c2 = scan_numeric_escape(rdr, 8u); }
|
'u' => { c2 = scan_numeric_escape(rdr, 4u); }
|
||||||
c2 => {
|
'U' => { c2 = scan_numeric_escape(rdr, 8u); }
|
||||||
fatal_span_char(rdr, escaped_pos, rdr.last_pos,
|
c2 => {
|
||||||
~"unknown character escape", 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 != '\'' {
|
if rdr.curr != '\'' {
|
||||||
fatal_span_verbose(rdr,
|
fatal_span_verbose(rdr,
|
||||||
|
@ -973,7 +980,7 @@ mod test {
|
||||||
}
|
}
|
||||||
|
|
||||||
#[test] fn character_escaped() {
|
#[test] fn character_escaped() {
|
||||||
let env = setup(@"'\n'");
|
let env = setup(@"'\\n'");
|
||||||
let TokenAndSpan {tok, sp: _} =
|
let TokenAndSpan {tok, sp: _} =
|
||||||
env.string_reader.next_token();
|
env.string_reader.next_token();
|
||||||
assert_eq!(tok, token::LIT_CHAR('\n' as u32));
|
assert_eq!(tok, token::LIT_CHAR('\n' as u32));
|
||||||
|
|
|
@ -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
|
||||||
|
}
|
|
@ -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
|
||||||
|
}
|
|
@ -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
|
||||||
|
}
|
|
@ -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
|
||||||
|
}
|
Loading…
Reference in New Issue