about summary refs log tree commit diff
path: root/src
diff options
context:
space:
mode:
authorbors <bors@rust-lang.org>2017-06-06 02:56:17 +0000
committerbors <bors@rust-lang.org>2017-06-06 02:56:17 +0000
commite8626951583e2cac46417ec433254a862735c0cc (patch)
tree5f38928427f2d56ff470cc6d5dad93e3e420da0d /src
parentae7920153331f26f2bc144f62082e95e74c2ba31 (diff)
parent0b8c3de678065b82ae955b65192b7927b467f7a6 (diff)
Auto merge of #41990 - qnighy:disallow-underscore-suffix-for-string-like-literals, r=nikomatsakis
Disallow underscore suffix for string-like literals.

This patch turns string/bytestring/char/byte literals followed by an underscore, like `"Foo"_`, to an error.

`scan_optional_raw_name` will parse `_` as a valid raw name, but it will be rejected by the parser. I also considered just stopping parsing when the suffix is `_`, but in that case `"Foo"_` will be lexed as two valid tokens.

Fixes the latter half of #41723.
Diffstat (limited to 'src')
-rw-r--r--src/libsyntax/parse/lexer/mod.rs9
-rw-r--r--src/test/parse-fail/underscore-suffix-for-string.rs20
2 files changed, 29 insertions, 0 deletions
diff --git a/src/libsyntax/parse/lexer/mod.rs b/src/libsyntax/parse/lexer/mod.rs
index 0bcd4578518..e2656bea483 100644
--- a/src/libsyntax/parse/lexer/mod.rs
+++ b/src/libsyntax/parse/lexer/mod.rs
@@ -480,6 +480,15 @@ impl<'a> StringReader<'a> {
 
         self.with_str_from(start, |string| {
             if string == "_" {
+                self.sess.span_diagnostic
+                    .struct_span_warn(mk_sp(start, self.pos),
+                                      "underscore literal suffix is not allowed")
+                    .warn("this was previously accepted by the compiler but is \
+                          being phased out; it will become a hard error in \
+                          a future release!")
+                    .note("for more information, see issue #42326 \
+                          <https://github.com/rust-lang/rust/issues/42326>")
+                    .emit();
                 None
             } else {
                 Some(Symbol::intern(string))
diff --git a/src/test/parse-fail/underscore-suffix-for-string.rs b/src/test/parse-fail/underscore-suffix-for-string.rs
new file mode 100644
index 00000000000..05de5f8e194
--- /dev/null
+++ b/src/test/parse-fail/underscore-suffix-for-string.rs
@@ -0,0 +1,20 @@
+// Copyright 2017 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() {
+    let _ = "Foo"_;
+    //~^ WARNING underscore literal suffix is not allowed
+    //~| WARNING this was previously accepted
+    //~| NOTE issue #42326
+}
+
+FAIL
+//~^ ERROR
+//~| NOTE