Skip to content

Commit 377c3c7

Browse files
committed
fix: incorrect error message for string literal suffixes
1 parent 9eb4a26 commit 377c3c7

File tree

8 files changed

+100
-41
lines changed

8 files changed

+100
-41
lines changed

compiler/rustc_parse/src/parser/expr.rs

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1561,7 +1561,14 @@ impl<'a> Parser<'a> {
15611561
fn parse_expr_lit(&mut self) -> PResult<'a, Box<Expr>> {
15621562
let lo = self.token.span;
15631563
match self.parse_opt_token_lit() {
1564-
Some((token_lit, _)) => {
1564+
Some((token_lit, span)) => {
1565+
// Check for invalid suffix on literals
1566+
if let Err(err) = ast::LitKind::from_token_lit(token_lit) {
1567+
let guar = report_lit_error(&self.psess, err, token_lit, span);
1568+
let token_lit = token::Lit::new(token::Err(guar), token_lit.symbol, None);
1569+
let expr = self.mk_expr(lo.to(self.prev_token.span), ExprKind::Lit(token_lit));
1570+
return self.maybe_recover_from_bad_qpath(expr);
1571+
}
15651572
let expr = self.mk_expr(lo.to(self.prev_token.span), ExprKind::Lit(token_lit));
15661573
self.maybe_recover_from_bad_qpath(expr)
15671574
}

tests/ui/parser/bad-lit-suffixes.stderr

Lines changed: 40 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -10,46 +10,6 @@ error: suffixes on string literals are invalid
1010
LL | "C"suffix
1111
| ^^^^^^^^^ invalid suffix `suffix`
1212

13-
error: suffixes on string literals are invalid
14-
--> $DIR/bad-lit-suffixes.rs:30:17
15-
|
16-
LL | #[rustc_dummy = "string"suffix]
17-
| ^^^^^^^^^^^^^^ invalid suffix `suffix`
18-
19-
error: suffixes on string literals are invalid
20-
--> $DIR/bad-lit-suffixes.rs:34:14
21-
|
22-
LL | #[must_use = "string"suffix]
23-
| ^^^^^^^^^^^^^^ invalid suffix `suffix`
24-
25-
error: suffixes on string literals are invalid
26-
--> $DIR/bad-lit-suffixes.rs:39:15
27-
|
28-
LL | #[link(name = "string"suffix)]
29-
| ^^^^^^^^^^^^^^ invalid suffix `suffix`
30-
31-
error: invalid suffix `suffix` for number literal
32-
--> $DIR/bad-lit-suffixes.rs:43:41
33-
|
34-
LL | #[rustc_layout_scalar_valid_range_start(0suffix)]
35-
| ^^^^^^^ invalid suffix `suffix`
36-
|
37-
= help: the suffix must be one of the numeric types (`u32`, `isize`, `f32`, etc.)
38-
39-
warning: `extern` declarations without an explicit ABI are deprecated
40-
--> $DIR/bad-lit-suffixes.rs:3:1
41-
|
42-
LL | extern
43-
| ^^^^^^ help: explicitly specify the "C" ABI: `extern "C"`
44-
|
45-
= note: `#[warn(missing_abi)]` on by default
46-
47-
warning: `extern` declarations without an explicit ABI are deprecated
48-
--> $DIR/bad-lit-suffixes.rs:7:1
49-
|
50-
LL | extern
51-
| ^^^^^^ help: explicitly specify the "C" ABI: `extern "C"`
52-
5313
error: suffixes on string literals are invalid
5414
--> $DIR/bad-lit-suffixes.rs:12:5
5515
|
@@ -150,6 +110,46 @@ LL | 1.0e10suffix;
150110
|
151111
= help: valid suffixes are `f32` and `f64`
152112

113+
error: suffixes on string literals are invalid
114+
--> $DIR/bad-lit-suffixes.rs:30:17
115+
|
116+
LL | #[rustc_dummy = "string"suffix]
117+
| ^^^^^^^^^^^^^^ invalid suffix `suffix`
118+
119+
error: suffixes on string literals are invalid
120+
--> $DIR/bad-lit-suffixes.rs:34:14
121+
|
122+
LL | #[must_use = "string"suffix]
123+
| ^^^^^^^^^^^^^^ invalid suffix `suffix`
124+
125+
error: suffixes on string literals are invalid
126+
--> $DIR/bad-lit-suffixes.rs:39:15
127+
|
128+
LL | #[link(name = "string"suffix)]
129+
| ^^^^^^^^^^^^^^ invalid suffix `suffix`
130+
131+
error: invalid suffix `suffix` for number literal
132+
--> $DIR/bad-lit-suffixes.rs:43:41
133+
|
134+
LL | #[rustc_layout_scalar_valid_range_start(0suffix)]
135+
| ^^^^^^^ invalid suffix `suffix`
136+
|
137+
= help: the suffix must be one of the numeric types (`u32`, `isize`, `f32`, etc.)
138+
139+
warning: `extern` declarations without an explicit ABI are deprecated
140+
--> $DIR/bad-lit-suffixes.rs:3:1
141+
|
142+
LL | extern
143+
| ^^^^^^ help: explicitly specify the "C" ABI: `extern "C"`
144+
|
145+
= note: `#[warn(missing_abi)]` on by default
146+
147+
warning: `extern` declarations without an explicit ABI are deprecated
148+
--> $DIR/bad-lit-suffixes.rs:7:1
149+
|
150+
LL | extern
151+
| ^^^^^^ help: explicitly specify the "C" ABI: `extern "C"`
152+
153153
error[E0539]: malformed `must_use` attribute input
154154
--> $DIR/bad-lit-suffixes.rs:34:1
155155
|
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
// Test for issue #144161: Wrong error for string literal suffix when stuck together
2+
// This tests the case where a raw string has an invalid suffix immediately followed by another string
3+
4+
fn main() {
5+
let s = r#" \\ "#r"\\ ";
6+
//~^ ERROR suffixes on string literals are invalid
7+
//~| ERROR expected one of `.`, `;`, `?`, `else`, or an operator
8+
}
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
error: suffixes on string literals are invalid
2+
--> $DIR/raw-string-suffix-invalid.rs:5:13
3+
|
4+
LL | let s = r#" \ "#r"\ ";
5+
| ^^^^^^^^^^ invalid suffix `r`
6+
7+
error: expected one of `.`, `;`, `?`, `else`, or an operator, found `"\ "`
8+
--> $DIR/raw-string-suffix-invalid.rs:5:23
9+
|
10+
LL | let s = r#" \ "#r"\ ";
11+
| ^^^^^ expected one of `.`, `;`, `?`, `else`, or an operator
12+
13+
error: aborting due to 2 previous errors
14+
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
// Test for raw string literals with invalid suffixes
2+
3+
fn main() {
4+
// Raw string literal with hash delimiters and suffix
5+
let s2 = r##"test"##suffix;
6+
//~^ ERROR suffixes on string literals are invalid
7+
}
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
error: suffixes on string literals are invalid
2+
--> $DIR/raw-string-suffix-long.rs:5:14
3+
|
4+
LL | let s2 = r##"test"##suffix;
5+
| ^^^^^^^^^^^^^^^^^ invalid suffix `suffix`
6+
7+
error: aborting due to 1 previous error
8+
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
// Test for raw string literal with word suffix
2+
3+
fn main() {
4+
// Raw string literal with word suffix
5+
let s3 = r#"test"#invalid;
6+
//~^ ERROR suffixes on string literals are invalid
7+
}
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
error: suffixes on string literals are invalid
2+
--> $DIR/raw-string-suffix-word.rs:5:14
3+
|
4+
LL | let s3 = r#"test"#invalid;
5+
| ^^^^^^^^^^^^^^^^ invalid suffix `invalid`
6+
7+
error: aborting due to 1 previous error
8+

0 commit comments

Comments
 (0)