Skip to content

Commit ce3bd29

Browse files
committed
Handle case of incomplete local ty more gracefully
When encountering a local binding with a type that isn't completed, the parser will reach a `=` token. When this happen, consider the type "complete" as far as the parser is concerned to avoid further errors being emitted by parse recovery logic.
1 parent 23744c8 commit ce3bd29

File tree

3 files changed

+20
-55
lines changed

3 files changed

+20
-55
lines changed

src/librustc_parse/parser/stmt.rs

+10-4
Original file line numberDiff line numberDiff line change
@@ -162,13 +162,19 @@ impl<'a> Parser<'a> {
162162
match self.parse_ty() {
163163
Ok(ty) => (None, Some(ty)),
164164
Err(mut err) => {
165-
// Rewind to before attempting to parse the type and continue parsing.
166-
let parser_snapshot_after_type =
167-
mem::replace(self, parser_snapshot_before_type);
168165
if let Ok(snip) = self.span_to_snippet(pat.span) {
169166
err.span_label(pat.span, format!("while parsing the type for `{}`", snip));
170167
}
171-
(Some((parser_snapshot_after_type, colon_sp, err)), None)
168+
let err = if self.check(&token::Eq) {
169+
err.emit();
170+
None
171+
} else {
172+
// Rewind to before attempting to parse the type and continue parsing.
173+
let parser_snapshot_after_type =
174+
mem::replace(self, parser_snapshot_before_type);
175+
Some((parser_snapshot_after_type, colon_sp, err))
176+
};
177+
(err, None)
172178
}
173179
}
174180
} else {

src/test/ui/issues/issue-34334.rs

+1-6
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,6 @@
11
fn main () {
22
let sr: Vec<(u32, _, _) = vec![];
33
//~^ ERROR expected one of `,` or `>`, found `=`
4-
//~| ERROR expected value, found struct `Vec`
5-
//~| ERROR mismatched types
6-
//~| ERROR invalid left-hand side of assignment
7-
//~| ERROR expected expression, found reserved identifier `_`
8-
//~| ERROR expected expression, found reserved identifier `_`
94
let sr2: Vec<(u32, _, _)> = sr.iter().map(|(faction, th_sender, th_receiver)| {}).collect();
10-
//~^ ERROR no method named `iter` found
5+
//~^ ERROR a value of type `std::vec::Vec<(u32, _, _)>` cannot be built
116
}

src/test/ui/issues/issue-34334.stderr

+9-45
Original file line numberDiff line numberDiff line change
@@ -1,55 +1,19 @@
1-
error: expected expression, found reserved identifier `_`
2-
--> $DIR/issue-34334.rs:2:23
3-
|
4-
LL | let sr: Vec<(u32, _, _) = vec![];
5-
| ^ expected expression
6-
7-
error: expected expression, found reserved identifier `_`
8-
--> $DIR/issue-34334.rs:2:26
9-
|
10-
LL | let sr: Vec<(u32, _, _) = vec![];
11-
| ^ expected expression
12-
131
error: expected one of `,` or `>`, found `=`
142
--> $DIR/issue-34334.rs:2:29
153
|
164
LL | let sr: Vec<(u32, _, _) = vec![];
17-
| --- ^ expected one of `,` or `>`
18-
| | |
19-
| | help: use `=` if you meant to assign
5+
| -- ^ expected one of `,` or `>`
6+
| |
207
| while parsing the type for `sr`
218

22-
error[E0423]: expected value, found struct `Vec`
23-
--> $DIR/issue-34334.rs:2:13
24-
|
25-
LL | let sr: Vec<(u32, _, _) = vec![];
26-
| ^^^ help: use struct literal syntax instead: `Vec { buf: val, len: val }`
27-
28-
error[E0308]: mismatched types
29-
--> $DIR/issue-34334.rs:2:31
30-
|
31-
LL | let sr: Vec<(u32, _, _) = vec![];
32-
| ^^^^^^ expected `bool`, found struct `std::vec::Vec`
33-
|
34-
= note: expected type `bool`
35-
found struct `std::vec::Vec<_>`
36-
= note: this error originates in a macro (in Nightly builds, run with -Z macro-backtrace for more info)
37-
38-
error[E0070]: invalid left-hand side of assignment
39-
--> $DIR/issue-34334.rs:2:29
40-
|
41-
LL | let sr: Vec<(u32, _, _) = vec![];
42-
| --------------- ^
43-
| |
44-
| cannot assign to this expression
45-
46-
error[E0599]: no method named `iter` found for unit type `()` in the current scope
47-
--> $DIR/issue-34334.rs:9:36
9+
error[E0277]: a value of type `std::vec::Vec<(u32, _, _)>` cannot be built from an iterator over elements of type `()`
10+
--> $DIR/issue-34334.rs:4:87
4811
|
4912
LL | let sr2: Vec<(u32, _, _)> = sr.iter().map(|(faction, th_sender, th_receiver)| {}).collect();
50-
| ^^^^ method not found in `()`
13+
| ^^^^^^^ value of type `std::vec::Vec<(u32, _, _)>` cannot be built from `std::iter::Iterator<Item=()>`
14+
|
15+
= help: the trait `std::iter::FromIterator<()>` is not implemented for `std::vec::Vec<(u32, _, _)>`
5116

52-
error: aborting due to 7 previous errors
17+
error: aborting due to 2 previous errors
5318

54-
Some errors have detailed explanations: E0070, E0308, E0423, E0599.
55-
For more information about an error, try `rustc --explain E0070`.
19+
For more information about this error, try `rustc --explain E0277`.

0 commit comments

Comments
 (0)