Skip to content

Commit e862695

Browse files
committed
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.
2 parents ae79201 + 0b8c3de commit e862695

File tree

2 files changed

+29
-0
lines changed

2 files changed

+29
-0
lines changed

src/libsyntax/parse/lexer/mod.rs

+9
Original file line numberDiff line numberDiff line change
@@ -480,6 +480,15 @@ impl<'a> StringReader<'a> {
480480

481481
self.with_str_from(start, |string| {
482482
if string == "_" {
483+
self.sess.span_diagnostic
484+
.struct_span_warn(mk_sp(start, self.pos),
485+
"underscore literal suffix is not allowed")
486+
.warn("this was previously accepted by the compiler but is \
487+
being phased out; it will become a hard error in \
488+
a future release!")
489+
.note("for more information, see issue #42326 \
490+
<https://github.com/rust-lang/rust/issues/42326>")
491+
.emit();
483492
None
484493
} else {
485494
Some(Symbol::intern(string))
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
// Copyright 2017 The Rust Project Developers. See the COPYRIGHT
2+
// file at the top-level directory of this distribution and at
3+
// http://rust-lang.org/COPYRIGHT.
4+
//
5+
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
6+
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
7+
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
8+
// option. This file may not be copied, modified, or distributed
9+
// except according to those terms.
10+
11+
fn main() {
12+
let _ = "Foo"_;
13+
//~^ WARNING underscore literal suffix is not allowed
14+
//~| WARNING this was previously accepted
15+
//~| NOTE issue #42326
16+
}
17+
18+
FAIL
19+
//~^ ERROR
20+
//~| NOTE

0 commit comments

Comments
 (0)