Skip to content

Commit dba6fc3

Browse files
committedNov 6, 2022
Make underscore_literal_suffix a hard error.
It's been a warning for 5.5 years. Time to make it a hard error. Closes #42326.
1 parent d726c84 commit dba6fc3

File tree

4 files changed

+28
-23
lines changed

4 files changed

+28
-23
lines changed
 

‎compiler/rustc_lexer/src/lib.rs

+6-3
Original file line numberDiff line numberDiff line change
@@ -88,7 +88,9 @@ pub enum TokenKind {
8888
/// tokens.
8989
UnknownPrefix,
9090

91-
/// Examples: `"12_u8"`, `"1.0e-40"`, `b"123`.
91+
/// Examples: `12u8`, `1.0e-40`, `b"123"`. Note that `_` is an invalid
92+
/// suffix, but may be present here on string and float literals. Users of
93+
/// this type will need to check for and reject that case.
9294
///
9395
/// See [LiteralKind] for more details.
9496
Literal { kind: LiteralKind, suffix_start: u32 },
@@ -840,12 +842,13 @@ impl Cursor<'_> {
840842
self.eat_decimal_digits()
841843
}
842844

843-
// Eats the suffix of the literal, e.g. "_u8".
845+
// Eats the suffix of the literal, e.g. "u8".
844846
fn eat_literal_suffix(&mut self) {
845847
self.eat_identifier();
846848
}
847849

848-
// Eats the identifier.
850+
// Eats the identifier. Note: succeeds on `_`, which isn't a valid
851+
// identifer.
849852
fn eat_identifier(&mut self) {
850853
if !is_id_start(self.first()) {
851854
return;

‎compiler/rustc_parse/src/lexer/mod.rs

+1-11
Original file line numberDiff line numberDiff line change
@@ -175,20 +175,10 @@ impl<'a> StringReader<'a> {
175175
if string == "_" {
176176
self.sess
177177
.span_diagnostic
178-
.struct_span_warn(
178+
.struct_span_err(
179179
self.mk_sp(suffix_start, self.pos),
180180
"underscore literal suffix is not allowed",
181181
)
182-
.warn(
183-
"this was previously accepted by the compiler but is \
184-
being phased out; it will become a hard error in \
185-
a future release!",
186-
)
187-
.note(
188-
"see issue #42326 \
189-
<https://github.com/rust-lang/rust/issues/42326> \
190-
for more information",
191-
)
192182
.emit();
193183
None
194184
} else {
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,17 @@
1-
// check-pass
1+
macro_rules! sink {
2+
($tt:tt) => {()}
3+
}
24

35
fn main() {
46
let _ = "Foo"_;
5-
//~^ WARNING underscore literal suffix is not allowed
6-
//~| WARNING this was previously accepted
7-
//~| NOTE issue #42326
7+
//~^ ERROR underscore literal suffix is not allowed
8+
9+
// This is ok, because `__` is a valid identifier and the macro consumes it
10+
// before proper parsing happens.
11+
let _ = sink!("Foo"__);
12+
13+
// This is not ok, even as an input to a macro, because the `_` suffix is
14+
// never allowed.
15+
sink!("Foo"_);
16+
//~^ ERROR underscore literal suffix is not allowed
817
}
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,14 @@
1-
warning: underscore literal suffix is not allowed
2-
--> $DIR/underscore-suffix-for-string.rs:4:18
1+
error: underscore literal suffix is not allowed
2+
--> $DIR/underscore-suffix-for-string.rs:6:18
33
|
44
LL | let _ = "Foo"_;
55
| ^
6+
7+
error: underscore literal suffix is not allowed
8+
--> $DIR/underscore-suffix-for-string.rs:15:16
69
|
7-
= warning: this was previously accepted by the compiler but is being phased out; it will become a hard error in a future release!
8-
= note: see issue #42326 <https://github.com/rust-lang/rust/issues/42326> for more information
10+
LL | sink!("Foo"_);
11+
| ^
912

10-
warning: 1 warning emitted
13+
error: aborting due to 2 previous errors
1114

0 commit comments

Comments
 (0)
Please sign in to comment.