]> git.lizzy.rs Git - rust.git/commitdiff
Display better snippet for invalid char literal
authorGreg Chapple <gregchapple1@gmail.com>
Thu, 7 Jan 2016 16:12:28 +0000 (16:12 +0000)
committerGreg Chapple <gregchapple1@gmail.com>
Thu, 14 Jan 2016 17:34:42 +0000 (17:34 +0000)
Given this code:

    fn main() {
        let _ = 'abcd';
    }

The compiler would give a message like:

    error: character literal may only contain one codepoint: ';
    let _ = 'abcd';
                 ^~

With this change, the message now displays:

    error: character literal may only contain one codepoint: 'abcd'
    let _ = 'abcd'
            ^~~~~~

Fixes #30033

src/libsyntax/parse/lexer/mod.rs
src/test/parse-fail/lex-bad-char-literals-1.rs [new file with mode: 0644]
src/test/parse-fail/lex-bad-char-literals-2.rs [new file with mode: 0644]
src/test/parse-fail/lex-bad-char-literals-3.rs [new file with mode: 0644]
src/test/parse-fail/lex-bad-char-literals-4.rs [new file with mode: 0644]
src/test/parse-fail/lex-bad-char-literals-5.rs [new file with mode: 0644]
src/test/parse-fail/lex-bad-char-literals.rs [deleted file]

index 1402b7888dd10e7089733a6176f70f841b6645bc..3183dfbd954f57dff4642fe8dceffb231e7c39f9 100644 (file)
@@ -1195,6 +1195,7 @@ fn next_token_inner(&mut self) -> token::Token {
             }
             '\'' => {
                 // Either a character constant 'a' OR a lifetime name 'abc
+                let start_with_quote = self.last_pos;
                 self.bump();
                 let start = self.last_pos;
 
@@ -1208,6 +1209,14 @@ fn next_token_inner(&mut self) -> token::Token {
                     while ident_continue(self.curr) {
                         self.bump();
                     }
+                    // lifetimes shouldn't end with a single quote
+                    // if we find one, then this is an invalid character literal
+                    if self.curr_is('\'') {
+                        panic!(self.fatal_span_verbose(
+                               start_with_quote, self.pos,
+                               String::from("character literal may only contain one codepoint")));
+
+                    }
 
                     // Include the leading `'` in the real identifier, for macro
                     // expansion purposes. See #12512 for the gory details of why
@@ -1233,26 +1242,22 @@ fn next_token_inner(&mut self) -> token::Token {
                        !keyword_checking_token.is_keyword(token::keywords::Static) {
                         self.err_span_(start, last_bpos, "invalid lifetime name");
                     }
+
                     return token::Lifetime(ident);
                 }
 
-                // Otherwise it is a character constant:
                 let valid = self.scan_char_or_byte(start,
                                                    c2,
                                                    // ascii_only =
                                                    false,
                                                    '\'');
+
                 if !self.curr_is('\'') {
-                    let last_bpos = self.last_pos;
-                    panic!(self.fatal_span_verbose(// Byte offsetting here is okay because the
-                                                   // character before position `start` is an
-                                                   // ascii single quote.
-                                                   start - BytePos(1),
-                                                   last_bpos,
-
-                                                   String::from("character literal may only \
-                                                                 contain one codepoint")));
+                    panic!(self.fatal_span_verbose(
+                           start_with_quote, self.last_pos,
+                           String::from("character literal may only contain one codepoint")));
                 }
+
                 let id = if valid {
                     self.name_from(start)
                 } else {
diff --git a/src/test/parse-fail/lex-bad-char-literals-1.rs b/src/test/parse-fail/lex-bad-char-literals-1.rs
new file mode 100644 (file)
index 0000000..7e22a11
--- /dev/null
@@ -0,0 +1,27 @@
+// Copyright 2016 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.
+
+// compile-flags: -Z parse-only
+static c3: char =
+    '\x1' //~ ERROR: numeric character escape is too short
+;
+
+static s: &'static str =
+    "\x1" //~ ERROR: numeric character escape is too short
+;
+
+static c: char =
+    '\●' //~ ERROR: unknown character escape
+;
+
+static s: &'static str =
+    "\●" //~ ERROR: unknown character escape
+;
+
diff --git a/src/test/parse-fail/lex-bad-char-literals-2.rs b/src/test/parse-fail/lex-bad-char-literals-2.rs
new file mode 100644 (file)
index 0000000..8bd6808
--- /dev/null
@@ -0,0 +1,17 @@
+// Copyright 2016 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.
+
+// compile-flags: -Z parse-only
+
+// 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'
+;
+
diff --git a/src/test/parse-fail/lex-bad-char-literals-3.rs b/src/test/parse-fail/lex-bad-char-literals-3.rs
new file mode 100644 (file)
index 0000000..92432dc
--- /dev/null
@@ -0,0 +1,16 @@
+// Copyright 2016 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.
+
+// compile-flags: -Z parse-only
+
+// 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: '●
+;
diff --git a/src/test/parse-fail/lex-bad-char-literals-4.rs b/src/test/parse-fail/lex-bad-char-literals-4.rs
new file mode 100644 (file)
index 0000000..b230e62
--- /dev/null
@@ -0,0 +1,16 @@
+// Copyright 2016 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.
+
+// compile-flags: -Z parse-only
+//
+// 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: '●
+;
diff --git a/src/test/parse-fail/lex-bad-char-literals-5.rs b/src/test/parse-fail/lex-bad-char-literals-5.rs
new file mode 100644 (file)
index 0000000..5259175
--- /dev/null
@@ -0,0 +1,16 @@
+// Copyright 2016 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.
+
+// compile-flags: -Z parse-only
+//
+// 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: '\x10
+;
diff --git a/src/test/parse-fail/lex-bad-char-literals.rs b/src/test/parse-fail/lex-bad-char-literals.rs
deleted file mode 100644 (file)
index 6335632..0000000
+++ /dev/null
@@ -1,32 +0,0 @@
-// 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.
-
-// compile-flags: -Z parse-only
-static c3: char =
-    '\x1' //~ ERROR: numeric character escape is too short
-;
-
-static s: &'static str =
-    "\x1" //~ ERROR: numeric character escape is too short
-;
-
-static c: char =
-    '\●' //~ ERROR: unknown character escape
-;
-
-static s: &'static str =
-    "\●" //~ ERROR: unknown character escape
-;
-
-// THIS MUST BE LAST, since it kills the lexer
-
-static c: char =
-    '●  //~ ERROR: character literal may only contain one codepoint
-;