Skip to content

Commit dbbe336

Browse files
committed
Ensure 'let mut ;' where ':pat' is banned.
1 parent f908aa9 commit dbbe336

File tree

3 files changed

+27
-1
lines changed

3 files changed

+27
-1
lines changed

src/libsyntax/parse/parser/pat.rs

+9
Original file line numberDiff line numberDiff line change
@@ -384,6 +384,7 @@ impl<'a> Parser<'a> {
384384
})
385385
}
386386

387+
/// Parse a mutable binding with the `mut` token already eaten.
387388
fn parse_pat_ident_mut(&mut self) -> PResult<'a, PatKind> {
388389
let mut_span = self.prev_span;
389390

@@ -393,6 +394,14 @@ impl<'a> Parser<'a> {
393394

394395
self.recover_additional_muts();
395396

397+
// Make sure we don't allow e.g. `let mut $p;` where `$p:pat`.
398+
if let token::Interpolated(ref nt) = self.token.kind {
399+
if let token::NtPat(_) = **nt {
400+
self.expected_ident_found().emit();
401+
}
402+
}
403+
404+
// Parse the pattern we hope to be an identifier.
396405
let mut pat = self.parse_pat(Some("identifier"))?;
397406

398407
// Add `mut` to any binding in the parsed pattern.

src/test/ui/parser/mut-patterns.rs

+8
Original file line numberDiff line numberDiff line change
@@ -32,4 +32,12 @@ pub fn main() {
3232
let mut W(mut a, W(b, W(ref c, W(d, B { box f }))))
3333
//~^ ERROR `mut` must be attached to each individual binding
3434
= W(0, W(1, W(2, W(3, B { f: Box::new(4u8) }))));
35+
36+
// Make sure we don't accidentally allow `mut $p` where `$p:pat`.
37+
macro_rules! foo {
38+
($p:pat) => {
39+
let mut $p = 0; //~ ERROR expected identifier, found `x`
40+
}
41+
}
42+
foo!(x);
3543
}

src/test/ui/parser/mut-patterns.stderr

+10-1
Original file line numberDiff line numberDiff line change
@@ -64,5 +64,14 @@ error: `mut` must be attached to each individual binding
6464
LL | let mut W(mut a, W(b, W(ref c, W(d, B { box f }))))
6565
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: add `mut` to each binding: `W(mut a, W(mut b, W(ref c, W(mut d, B { box mut f }))))`
6666

67-
error: aborting due to 9 previous errors
67+
error: expected identifier, found `x`
68+
--> $DIR/mut-patterns.rs:39:21
69+
|
70+
LL | let mut $p = 0;
71+
| ^^ expected identifier
72+
...
73+
LL | foo!(x);
74+
| -------- in this macro invocation
75+
76+
error: aborting due to 10 previous errors
6877

0 commit comments

Comments
 (0)