Skip to content

Commit d82fd9e

Browse files
committed
Auto merge of #62643 - estebank:parse-recovery-type-errs, r=petrochenkov
Do not emit type errors after parse error in last statement of block When recovering from a parse error inside a block, do not emit type errors generating on that block's recovered return expression. Fix #57383.
2 parents 83e4eed + 8259a2d commit d82fd9e

File tree

9 files changed

+42
-61
lines changed

9 files changed

+42
-61
lines changed

src/libsyntax/parse/parser.rs

+4-2
Original file line numberDiff line numberDiff line change
@@ -4675,6 +4675,9 @@ impl<'a> Parser<'a> {
46754675
{
46764676
e.emit();
46774677
self.recover_stmt();
4678+
// Don't complain about type errors in body tail after parse error (#57383).
4679+
let sp = expr.span.to(self.prev_span);
4680+
stmt.node = StmtKind::Expr(DummyResult::raw_expr(sp, true));
46784681
}
46794682
}
46804683
}
@@ -4692,8 +4695,7 @@ impl<'a> Parser<'a> {
46924695
if self.eat(&token::Semi) {
46934696
stmt = stmt.add_trailing_semicolon();
46944697
}
4695-
4696-
stmt.span = stmt.span.with_hi(self.prev_span.hi());
4698+
stmt.span = stmt.span.to(self.prev_span);
46974699
Ok(Some(stmt))
46984700
}
46994701

src/test/ui/obsolete-in-place/bad.rs

-1
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@
33
fn foo() {
44
let (x, y) = (0, 0);
55
x <- y; //~ ERROR expected one of
6-
//~^ ERROR mismatched types
76
}
87

98
fn main() {

src/test/ui/obsolete-in-place/bad.stderr

+2-15
Original file line numberDiff line numberDiff line change
@@ -5,23 +5,10 @@ LL | x <- y;
55
| ^^ expected one of 8 possible tokens here
66

77
error: expected expression, found keyword `in`
8-
--> $DIR/bad.rs:11:5
8+
--> $DIR/bad.rs:10:5
99
|
1010
LL | in(foo) { bar };
1111
| ^^ expected expression
1212

13-
error[E0308]: mismatched types
14-
--> $DIR/bad.rs:5:5
15-
|
16-
LL | fn foo() {
17-
| - possibly return type missing here?
18-
LL | let (x, y) = (0, 0);
19-
LL | x <- y;
20-
| ^ expected (), found integer
21-
|
22-
= note: expected type `()`
23-
found type `{integer}`
24-
25-
error: aborting due to 3 previous errors
13+
error: aborting due to 2 previous errors
2614

27-
For more information about this error, try `rustc --explain E0308`.

src/test/ui/parser/issue-19096.rs

+7-2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,10 @@
1-
fn main() {
1+
fn main() { // we don't complain about the return type being `{integer}`
22
let t = (42, 42);
33
t.0::<isize>; //~ ERROR expected one of `.`, `;`, `?`, `}`, or an operator, found `::`
4-
//~| ERROR mismatched types
4+
}
5+
6+
fn foo() -> usize { // we don't complain about the return type being unit
7+
let t = (42, 42);
8+
t.0::<isize>; //~ ERROR expected one of `.`, `;`, `?`, `}`, or an operator, found `::`
9+
42;
510
}

src/test/ui/parser/issue-19096.stderr

+3-10
Original file line numberDiff line numberDiff line change
@@ -4,18 +4,11 @@ error: expected one of `.`, `;`, `?`, `}`, or an operator, found `::`
44
LL | t.0::<isize>;
55
| ^^ expected one of `.`, `;`, `?`, `}`, or an operator here
66

7-
error[E0308]: mismatched types
8-
--> $DIR/issue-19096.rs:3:5
7+
error: expected one of `.`, `;`, `?`, `}`, or an operator, found `::`
8+
--> $DIR/issue-19096.rs:8:8
99
|
10-
LL | fn main() {
11-
| - expected `()` because of default return type
12-
LL | let t = (42, 42);
1310
LL | t.0::<isize>;
14-
| ^^^ expected (), found integer
15-
|
16-
= note: expected type `()`
17-
found type `{integer}`
11+
| ^^ expected one of `.`, `;`, `?`, `}`, or an operator here
1812

1913
error: aborting due to 2 previous errors
2014

21-
For more information about this error, try `rustc --explain E0308`.
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,25 @@
11
fn test_if() {
22
r#if true { } //~ ERROR found `true`
3-
//~| ERROR cannot find value `if` in this scope
43
}
54

65
fn test_struct() {
76
r#struct Test; //~ ERROR found `Test`
8-
//~| ERROR cannot find value `struct` in this scope
97
}
108

119
fn test_union() {
1210
r#union Test; //~ ERROR found `Test`
13-
//~| ERROR cannot find value `union` in this scope
11+
}
12+
13+
fn test_if_2() {
14+
let _ = r#if; //~ ERROR cannot find value `if` in this scope
15+
}
16+
17+
fn test_struct_2() {
18+
let _ = r#struct; //~ ERROR cannot find value `struct` in this scope
19+
}
20+
21+
fn test_union_2() {
22+
let _ = r#union; //~ ERROR cannot find value `union` in this scope
1423
}
1524

1625
fn main() {}

src/test/ui/parser/raw/raw-literal-keywords.stderr

+11-11
Original file line numberDiff line numberDiff line change
@@ -5,34 +5,34 @@ LL | r#if true { }
55
| ^^^^ expected one of 8 possible tokens here
66

77
error: expected one of `!`, `.`, `::`, `;`, `?`, `{`, `}`, or an operator, found `Test`
8-
--> $DIR/raw-literal-keywords.rs:7:14
8+
--> $DIR/raw-literal-keywords.rs:6:14
99
|
1010
LL | r#struct Test;
1111
| ^^^^ expected one of 8 possible tokens here
1212

1313
error: expected one of `!`, `.`, `::`, `;`, `?`, `{`, `}`, or an operator, found `Test`
14-
--> $DIR/raw-literal-keywords.rs:12:13
14+
--> $DIR/raw-literal-keywords.rs:10:13
1515
|
1616
LL | r#union Test;
1717
| ^^^^ expected one of 8 possible tokens here
1818

1919
error[E0425]: cannot find value `if` in this scope
20-
--> $DIR/raw-literal-keywords.rs:2:5
20+
--> $DIR/raw-literal-keywords.rs:14:13
2121
|
22-
LL | r#if true { }
23-
| ^^^^ not found in this scope
22+
LL | let _ = r#if;
23+
| ^^^^ not found in this scope
2424

2525
error[E0425]: cannot find value `struct` in this scope
26-
--> $DIR/raw-literal-keywords.rs:7:5
26+
--> $DIR/raw-literal-keywords.rs:18:13
2727
|
28-
LL | r#struct Test;
29-
| ^^^^^^^^ not found in this scope
28+
LL | let _ = r#struct;
29+
| ^^^^^^^^ not found in this scope
3030

3131
error[E0425]: cannot find value `union` in this scope
32-
--> $DIR/raw-literal-keywords.rs:12:5
32+
--> $DIR/raw-literal-keywords.rs:22:13
3333
|
34-
LL | r#union Test;
35-
| ^^^^^^^ not found in this scope
34+
LL | let _ = r#union;
35+
| ^^^^^^^ not found in this scope
3636

3737
error: aborting due to 6 previous errors
3838

src/test/ui/resolve/token-error-correct-3.rs

-1
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,6 @@ pub mod raw {
1515
callback(path.as_ref();
1616
//~^ ERROR expected one of
1717
fs::create_dir_all(path.as_ref()).map(|()| true)
18-
//~^ ERROR mismatched types
1918
} else {
2019
//~^ ERROR expected one of `.`, `;`, `?`, `}`, or an operator, found `)`
2120
Ok(false);

src/test/ui/resolve/token-error-correct-3.stderr

+3-16
Original file line numberDiff line numberDiff line change
@@ -7,11 +7,10 @@ LL | callback(path.as_ref();
77
| unclosed delimiter
88

99
error: expected one of `.`, `;`, `?`, `}`, or an operator, found `)`
10-
--> $DIR/token-error-correct-3.rs:19:9
10+
--> $DIR/token-error-correct-3.rs:18:9
1111
|
1212
LL | fs::create_dir_all(path.as_ref()).map(|()| true)
1313
| - expected one of `.`, `;`, `?`, `}`, or an operator here
14-
LL |
1514
LL | } else {
1615
| ^ unexpected token
1716

@@ -21,18 +20,6 @@ error[E0425]: cannot find function `is_directory` in this scope
2120
LL | if !is_directory(path.as_ref()) {
2221
| ^^^^^^^^^^^^ not found in this scope
2322

24-
error[E0308]: mismatched types
25-
--> $DIR/token-error-correct-3.rs:17:13
26-
|
27-
LL | fs::create_dir_all(path.as_ref()).map(|()| true)
28-
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^- help: try adding a semicolon: `;`
29-
| |
30-
| expected (), found enum `std::result::Result`
31-
|
32-
= note: expected type `()`
33-
found type `std::result::Result<bool, std::io::Error>`
34-
35-
error: aborting due to 4 previous errors
23+
error: aborting due to 3 previous errors
3624

37-
Some errors have detailed explanations: E0308, E0425.
38-
For more information about an error, try `rustc --explain E0308`.
25+
For more information about this error, try `rustc --explain E0425`.

0 commit comments

Comments
 (0)