Skip to content

Commit d4b44ba

Browse files
committed
fixup! Recover more expressions in patterns
1 parent d7454a2 commit d4b44ba

File tree

3 files changed

+144
-34
lines changed

3 files changed

+144
-34
lines changed

compiler/rustc_parse/src/parser/pat.rs

+20-9
Original file line numberDiff line numberDiff line change
@@ -346,7 +346,7 @@ impl<'a> Parser<'a> {
346346
/// 0..=1 + 2
347347
/// ^^^^^
348348
/// ```
349-
/// Only the end bound is spanned in this case, and this function have no idea if there were a `..=` before `pat_span`, hence the parameter.
349+
/// Only the end bound is spanned in this case, and this function has no idea if there was a `..=` before `pat_span`, hence the parameter.
350350
///
351351
/// This function returns `Some` if a trailing expression was recovered, and said expression's span.
352352
#[must_use = "the pattern must be discarded as `PatKind::Err` if this function returns Some"]
@@ -360,23 +360,34 @@ impl<'a> Parser<'a> {
360360
return None;
361361
}
362362

363-
// Returns `true` iff `token` is a `x.y` float.
364-
let is_two_tuple_indexes = |that: &Self, token: &Token| -> bool {
363+
// Returns `true` iff `token` is an unsuffixed integer.
364+
let is_one_tuple_index = |_: &Self, token: &Token| -> bool {
365365
use token::{Lit, LitKind};
366366

367-
let token::Literal(Lit { kind: LitKind::Float, symbol, suffix: None }) = token.kind
368-
else {
369-
return false;
370-
};
367+
matches!(
368+
token.kind,
369+
token::Literal(Lit { kind: LitKind::Integer, symbol: _, suffix: None })
370+
)
371+
};
372+
373+
// Returns `true` iff `token` is an unsuffixed `x.y` float.
374+
let is_two_tuple_indexes = |this: &Self, token: &Token| -> bool {
375+
use token::{Lit, LitKind};
371376

372-
matches!(that.break_up_float(symbol, token.span), DestructuredFloat::MiddleDot(..))
377+
if let token::Literal(Lit { kind: LitKind::Float, symbol, suffix: None }) = token.kind
378+
&& let DestructuredFloat::MiddleDot(..) = this.break_up_float(symbol, token.span)
379+
{
380+
true
381+
} else {
382+
false
383+
}
373384
};
374385

375386
// Check for `.hello` or `.0`.
376387
let has_dot_expr = self.check_noexpect(&token::Dot) // `.`
377388
&& self.look_ahead(1, |tok| {
378389
tok.is_ident() // `hello`
379-
|| tok.is_integer_lit() // `0`
390+
|| is_one_tuple_index(&self, &tok) // `0`
380391
|| is_two_tuple_indexes(&self, &tok) // `0.0`
381392
});
382393

tests/ui/parser/recover/recover-pat-exprs.rs

+17
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,23 @@ fn field_access() {
1212
{ let x.0e0; } //~ error: expected one of `:`, `;`, `=`, `@`, or `|`, found `.`
1313
{ let x.-0.0; } //~ error: expected one of `:`, `;`, `=`, `@`, or `|`, found `.`
1414
{ let x.-0; } //~ error: expected one of `:`, `;`, `=`, `@`, or `|`, found `.`
15+
16+
{ let x.0u32; } //~ error: expected one of `:`, `;`, `=`, `@`, or `|`, found `.`
17+
{ let x.0.0_f64; } //~ error: expected one of `:`, `;`, `=`, `@`, or `|`, found `.`
18+
}
19+
20+
// ArrayExpression
21+
fn array_indexing() {
22+
match 0 {
23+
x[0] => (), //~ error: expected a pattern, found an expression
24+
x[..] => (), //~ error: expected a pattern, found an expression
25+
}
26+
27+
{ let x[0, 1, 2]; } //~ error: expected one of `:`, `;`, `=`, `@`, or `|`, found `[`
28+
{ let x[0; 20]; } //~ error: expected one of `:`, `;`, `=`, `@`, or `|`, found `[`
29+
{ let x[]; } //~ error: expected one of `:`, `;`, `=`, `@`, or `|`, found `[`
30+
{ let (x[]); } //~ error: expected one of `)`, `,`, `@`, or `|`, found `[`
31+
//~^ missing `,`
1532
}
1633

1734
// MethodCallExpression, CallExpression, ErrorPropagationExpression

tests/ui/parser/recover/recover-pat-exprs.stderr

+107-25
Original file line numberDiff line numberDiff line change
@@ -133,8 +133,90 @@ error: expected one of `:`, `;`, `=`, `@`, or `|`, found `.`
133133
LL | { let x.-0; }
134134
| ^ expected one of `:`, `;`, `=`, `@`, or `|`
135135

136+
error: expected one of `:`, `;`, `=`, `@`, or `|`, found `.`
137+
--> $DIR/recover-pat-exprs.rs:16:12
138+
|
139+
LL | { let x.0u32; }
140+
| ^ expected one of `:`, `;`, `=`, `@`, or `|`
141+
142+
error: expected one of `:`, `;`, `=`, `@`, or `|`, found `.`
143+
--> $DIR/recover-pat-exprs.rs:17:12
144+
|
145+
LL | { let x.0.0_f64; }
146+
| ^ expected one of `:`, `;`, `=`, `@`, or `|`
147+
148+
error: expected a pattern, found an expression
149+
--> $DIR/recover-pat-exprs.rs:23:9
150+
|
151+
LL | x[0] => (),
152+
| ^^^^ arbitrary expressions are not allowed in patterns
153+
|
154+
help: check the value in an arm guard
155+
|
156+
LL | val if val == x[0] => (),
157+
| ~~~ ++++++++++++++
158+
help: extract the expression into a `const` and refer to it
159+
|
160+
LL + const VAL: _ = x[0];
161+
LL ~ match 0 {
162+
LL ~ VAL => (),
163+
|
164+
help: wrap the expression in a inline const (requires `#![feature(inline_const_pat)]`)
165+
|
166+
LL | const { x[0] } => (),
167+
| +++++++ +
168+
136169
error: expected a pattern, found an expression
137-
--> $DIR/recover-pat-exprs.rs:20:9
170+
--> $DIR/recover-pat-exprs.rs:24:9
171+
|
172+
LL | x[..] => (),
173+
| ^^^^^ arbitrary expressions are not allowed in patterns
174+
|
175+
help: check the value in an arm guard
176+
|
177+
LL | val if val == x[..] => (),
178+
| ~~~ +++++++++++++++
179+
help: extract the expression into a `const` and refer to it
180+
|
181+
LL + const VAL: _ = x[..];
182+
LL ~ match 0 {
183+
LL | x[0] => (),
184+
LL ~ VAL => (),
185+
|
186+
help: wrap the expression in a inline const (requires `#![feature(inline_const_pat)]`)
187+
|
188+
LL | const { x[..] } => (),
189+
| +++++++ +
190+
191+
error: expected one of `:`, `;`, `=`, `@`, or `|`, found `[`
192+
--> $DIR/recover-pat-exprs.rs:27:12
193+
|
194+
LL | { let x[0, 1, 2]; }
195+
| ^ expected one of `:`, `;`, `=`, `@`, or `|`
196+
197+
error: expected one of `:`, `;`, `=`, `@`, or `|`, found `[`
198+
--> $DIR/recover-pat-exprs.rs:28:12
199+
|
200+
LL | { let x[0; 20]; }
201+
| ^ expected one of `:`, `;`, `=`, `@`, or `|`
202+
203+
error: expected one of `:`, `;`, `=`, `@`, or `|`, found `[`
204+
--> $DIR/recover-pat-exprs.rs:29:12
205+
|
206+
LL | { let x[]; }
207+
| ^ expected one of `:`, `;`, `=`, `@`, or `|`
208+
209+
error: expected one of `)`, `,`, `@`, or `|`, found `[`
210+
--> $DIR/recover-pat-exprs.rs:30:13
211+
|
212+
LL | { let (x[]); }
213+
| ^
214+
| |
215+
| expected one of `)`, `,`, `@`, or `|`
216+
| help: missing `,`
217+
218+
error: expected a pattern, found an expression
219+
--> $DIR/recover-pat-exprs.rs:37:9
138220
|
139221
LL | x.f() => (),
140222
| ^^^^^ arbitrary expressions are not allowed in patterns
@@ -155,7 +237,7 @@ LL | const { x.f() } => (),
155237
| +++++++ +
156238

157239
error: expected a pattern, found an expression
158-
--> $DIR/recover-pat-exprs.rs:21:9
240+
--> $DIR/recover-pat-exprs.rs:38:9
159241
|
160242
LL | x._f() => (),
161243
| ^^^^^^ arbitrary expressions are not allowed in patterns
@@ -177,7 +259,7 @@ LL | const { x._f() } => (),
177259
| +++++++ +
178260

179261
error: expected a pattern, found an expression
180-
--> $DIR/recover-pat-exprs.rs:22:9
262+
--> $DIR/recover-pat-exprs.rs:39:9
181263
|
182264
LL | x? => (),
183265
| ^^ arbitrary expressions are not allowed in patterns
@@ -200,7 +282,7 @@ LL | const { x? } => (),
200282
| +++++++ +
201283

202284
error: expected a pattern, found an expression
203-
--> $DIR/recover-pat-exprs.rs:23:9
285+
--> $DIR/recover-pat-exprs.rs:40:9
204286
|
205287
LL | ().f() => (),
206288
| ^^^^^^ arbitrary expressions are not allowed in patterns
@@ -224,7 +306,7 @@ LL | const { ().f() } => (),
224306
| +++++++ +
225307

226308
error: expected a pattern, found an expression
227-
--> $DIR/recover-pat-exprs.rs:24:9
309+
--> $DIR/recover-pat-exprs.rs:41:9
228310
|
229311
LL | (0, x)?.f() => (),
230312
| ^^^^^^^^^^^ arbitrary expressions are not allowed in patterns
@@ -248,7 +330,7 @@ LL | const { (0, x)?.f() } => (),
248330
| +++++++ +
249331

250332
error: expected a pattern, found an expression
251-
--> $DIR/recover-pat-exprs.rs:25:9
333+
--> $DIR/recover-pat-exprs.rs:42:9
252334
|
253335
LL | x.f().g() => (),
254336
| ^^^^^^^^^ arbitrary expressions are not allowed in patterns
@@ -272,7 +354,7 @@ LL | const { x.f().g() } => (),
272354
| +++++++ +
273355

274356
error: expected a pattern, found an expression
275-
--> $DIR/recover-pat-exprs.rs:26:9
357+
--> $DIR/recover-pat-exprs.rs:43:9
276358
|
277359
LL | 0.f()?.g()?? => (),
278360
| ^^^^^^^^^^^^ arbitrary expressions are not allowed in patterns
@@ -296,7 +378,7 @@ LL | const { 0.f()?.g()?? } => (),
296378
| +++++++ +
297379

298380
error: expected a pattern, found an expression
299-
--> $DIR/recover-pat-exprs.rs:33:9
381+
--> $DIR/recover-pat-exprs.rs:50:9
300382
|
301383
LL | x as usize => (),
302384
| ^^^^^^^^^^ arbitrary expressions are not allowed in patterns
@@ -317,7 +399,7 @@ LL | const { x as usize } => (),
317399
| +++++++ +
318400

319401
error: expected a pattern, found an expression
320-
--> $DIR/recover-pat-exprs.rs:34:9
402+
--> $DIR/recover-pat-exprs.rs:51:9
321403
|
322404
LL | 0 as usize => (),
323405
| ^^^^^^^^^^ arbitrary expressions are not allowed in patterns
@@ -339,7 +421,7 @@ LL | const { 0 as usize } => (),
339421
| +++++++ +
340422

341423
error: expected a pattern, found an expression
342-
--> $DIR/recover-pat-exprs.rs:35:9
424+
--> $DIR/recover-pat-exprs.rs:52:9
343425
|
344426
LL | x.f().0.4 as f32 => (),
345427
| ^^^^^^^^^^^^^^^^ arbitrary expressions are not allowed in patterns
@@ -362,7 +444,7 @@ LL | const { x.f().0.4 as f32 } => (),
362444
| +++++++ +
363445

364446
error: expected a pattern, found an expression
365-
--> $DIR/recover-pat-exprs.rs:42:9
447+
--> $DIR/recover-pat-exprs.rs:59:9
366448
|
367449
LL | 1 + 1 => (),
368450
| ^^^^^ arbitrary expressions are not allowed in patterns
@@ -383,7 +465,7 @@ LL | const { 1 + 1 } => (),
383465
| +++++++ +
384466

385467
error: expected a pattern, found an expression
386-
--> $DIR/recover-pat-exprs.rs:43:9
468+
--> $DIR/recover-pat-exprs.rs:60:9
387469
|
388470
LL | (1 + 2) * 3 => (),
389471
| ^^^^^^^^^^^ arbitrary expressions are not allowed in patterns
@@ -405,7 +487,7 @@ LL | const { (1 + 2) * 3 } => (),
405487
| +++++++ +
406488

407489
error: left-hand side of `@` must be a binding
408-
--> $DIR/recover-pat-exprs.rs:56:9
490+
--> $DIR/recover-pat-exprs.rs:73:9
409491
|
410492
LL | x.sqrt() @ .. => (),
411493
| --------^^^--
@@ -416,13 +498,13 @@ LL | x.sqrt() @ .. => (),
416498
= note: bindings are `x`, `mut x`, `ref x`, and `ref mut x`
417499

418500
error: expected one of `)`, `,`, or `|`, found `+`
419-
--> $DIR/recover-pat-exprs.rs:70:12
501+
--> $DIR/recover-pat-exprs.rs:87:12
420502
|
421503
LL | (_ + 1) => (),
422504
| ^ expected one of `)`, `,`, or `|`
423505

424506
error: expected a pattern, found an expression
425-
--> $DIR/recover-pat-exprs.rs:54:9
507+
--> $DIR/recover-pat-exprs.rs:71:9
426508
|
427509
LL | u8::MAX.abs() => (),
428510
| ^^^^^^^^^^^^^ arbitrary expressions are not allowed in patterns
@@ -443,7 +525,7 @@ LL | const { u8::MAX.abs() } => (),
443525
| +++++++ +
444526

445527
error: expected a pattern, found an expression
446-
--> $DIR/recover-pat-exprs.rs:59:17
528+
--> $DIR/recover-pat-exprs.rs:76:17
447529
|
448530
LL | z @ w @ v.u() => (),
449531
| ^^^^^ arbitrary expressions are not allowed in patterns
@@ -467,7 +549,7 @@ LL | z @ w @ const { v.u() } => (),
467549
| +++++++ +
468550

469551
error: expected a pattern, found an expression
470-
--> $DIR/recover-pat-exprs.rs:61:9
552+
--> $DIR/recover-pat-exprs.rs:78:9
471553
|
472554
LL | y.ilog(3) => (),
473555
| ^^^^^^^^^ arbitrary expressions are not allowed in patterns
@@ -491,7 +573,7 @@ LL | const { y.ilog(3) } => (),
491573
| +++++++ +
492574

493575
error: expected a pattern, found an expression
494-
--> $DIR/recover-pat-exprs.rs:63:9
576+
--> $DIR/recover-pat-exprs.rs:80:9
495577
|
496578
LL | n + 1 => (),
497579
| ^^^^^ arbitrary expressions are not allowed in patterns
@@ -515,7 +597,7 @@ LL | const { n + 1 } => (),
515597
| +++++++ +
516598

517599
error: expected a pattern, found an expression
518-
--> $DIR/recover-pat-exprs.rs:65:10
600+
--> $DIR/recover-pat-exprs.rs:82:10
519601
|
520602
LL | ("".f() + 14 * 8) => (),
521603
| ^^^^^^^^^^^^^^^ arbitrary expressions are not allowed in patterns
@@ -539,7 +621,7 @@ LL | (const { "".f() + 14 * 8 }) => (),
539621
| +++++++ +
540622

541623
error: expected a pattern, found an expression
542-
--> $DIR/recover-pat-exprs.rs:68:9
624+
--> $DIR/recover-pat-exprs.rs:85:9
543625
|
544626
LL | f?() => (),
545627
| ^^^^ arbitrary expressions are not allowed in patterns
@@ -563,7 +645,7 @@ LL | const { f?() } => (),
563645
| +++++++ +
564646

565647
error: expected a pattern, found an expression
566-
--> $DIR/recover-pat-exprs.rs:74:9
648+
--> $DIR/recover-pat-exprs.rs:91:9
567649
|
568650
LL | let 1 + 1 = 2;
569651
| ^^^^^ arbitrary expressions are not allowed in patterns
@@ -575,7 +657,7 @@ LL + 1 + 1 = 2;
575657
|
576658

577659
error: expected one of `)`, `,`, `@`, or `|`, found `*`
578-
--> $DIR/recover-pat-exprs.rs:77:28
660+
--> $DIR/recover-pat-exprs.rs:94:28
579661
|
580662
LL | let b = matches!(x, (x * x | x.f()) | x[0]);
581663
| ^ expected one of `)`, `,`, `@`, or `|`
@@ -584,16 +666,16 @@ LL | let b = matches!(x, (x * x | x.f()) | x[0]);
584666
= note: while parsing argument for this `pat` macro fragment
585667

586668
error: expected a pattern, found an expression
587-
--> $DIR/recover-pat-exprs.rs:48:5
669+
--> $DIR/recover-pat-exprs.rs:65:5
588670
|
589671
LL | 1 + 2 * PI.cos() => 2,
590672
| ^^^^^^^^^^^^^^^^ arbitrary expressions are not allowed in patterns
591673

592674
error: expected a pattern, found an expression
593-
--> $DIR/recover-pat-exprs.rs:56:9
675+
--> $DIR/recover-pat-exprs.rs:73:9
594676
|
595677
LL | x.sqrt() @ .. => (),
596678
| ^^^^^^^^ arbitrary expressions are not allowed in patterns
597679

598-
error: aborting due to 32 previous errors
680+
error: aborting due to 40 previous errors
599681

0 commit comments

Comments
 (0)