Skip to content

Commit a65937e

Browse files
committed
Silence resolve errors if there were parse errors
Fix rust-lang#74863.
1 parent 93e62a2 commit a65937e

File tree

67 files changed

+71
-44
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

67 files changed

+71
-44
lines changed

compiler/rustc_parse/src/parser/item.rs

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -152,15 +152,21 @@ impl<'a> Parser<'a> {
152152
let lo = self.token.span;
153153
let vis = self.parse_visibility(FollowedByType::No)?;
154154
let mut def = self.parse_defaultness();
155-
let kind = self.parse_item_kind(
155+
let kind = match self.parse_item_kind(
156156
&mut attrs,
157157
mac_allowed,
158158
lo,
159159
&vis,
160160
&mut def,
161161
fn_parse_mode,
162162
Case::Sensitive,
163-
)?;
163+
) {
164+
Ok(kind) => kind,
165+
Err(err) => {
166+
self.sess.parse_errors_encountered.set(true);
167+
return Err(err);
168+
}
169+
};
164170
if let Some((ident, kind)) = kind {
165171
self.error_on_unconsumed_default(def, &kind);
166172
let span = lo.to(self.prev_token.span);
@@ -726,6 +732,7 @@ impl<'a> Parser<'a> {
726732
let is_let = self.token.is_keyword(kw::Let);
727733

728734
let mut err = self.struct_span_err(non_item_span, "non-item in item list");
735+
self.sess.parse_errors_encountered.set(true);
729736
self.consume_block(Delimiter::Brace, ConsumeClosingDelim::Yes);
730737
if is_let {
731738
err.span_suggestion(
@@ -752,6 +759,7 @@ impl<'a> Parser<'a> {
752759
}
753760
Ok(Some(item)) => items.extend(item),
754761
Err(mut err) => {
762+
self.sess.parse_errors_encountered.set(true);
755763
self.consume_block(Delimiter::Brace, ConsumeClosingDelim::Yes);
756764
err.span_label(open_brace_span, "while parsing this item list starting here")
757765
.span_label(self.prev_token.span, "the item list ends here")
@@ -1618,6 +1626,7 @@ impl<'a> Parser<'a> {
16181626
while self.token != token::CloseDelim(Delimiter::Brace) {
16191627
let field = self.parse_field_def(adt_ty).map_err(|e| {
16201628
self.consume_block(Delimiter::Brace, ConsumeClosingDelim::No);
1629+
self.sess.parse_errors_encountered.set(true);
16211630
recovered = true;
16221631
e
16231632
});

compiler/rustc_parse/src/parser/mod.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1459,6 +1459,7 @@ pub(crate) fn make_unclosed_delims_error(
14591459
if let Some(sp) = unmatched.unclosed_span {
14601460
spans.push(sp);
14611461
};
1462+
sess.parse_errors_encountered.set(true);
14621463
let err = MismatchedClosingDelimiter {
14631464
spans,
14641465
delimiter: pprust::token_kind_to_string(&token::CloseDelim(found_delim)).to_string(),

compiler/rustc_parse/src/parser/pat.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -870,6 +870,7 @@ impl<'a> Parser<'a> {
870870
let (fields, etc) = self.parse_pat_fields().unwrap_or_else(|mut e| {
871871
e.span_label(path.span, "while parsing the fields for this pattern");
872872
e.emit();
873+
self.sess.parse_errors_encountered.set(true);
873874
self.recover_stmt();
874875
(ThinVec::new(), true)
875876
});
@@ -1104,6 +1105,7 @@ impl<'a> Parser<'a> {
11041105
}
11051106
}
11061107
}
1108+
self.sess.parse_errors_encountered.set(true);
11071109
err.emit();
11081110
}
11091111
Ok((fields, etc))

compiler/rustc_resolve/src/late/diagnostics.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -430,6 +430,10 @@ impl<'a: 'ast, 'ast, 'tcx> LateResolutionVisitor<'a, '_, 'ast, 'tcx> {
430430
base_error.msg.clone(),
431431
code,
432432
);
433+
if self.r.tcx.sess.parse_sess.parse_errors_encountered.get() {
434+
// We've encountered parse errors that can cause resolution errors, so we silence them.
435+
err.delay_as_bug();
436+
}
433437

434438
self.suggest_swapping_misplaced_self_ty_and_trait(&mut err, source, res, base_error.span);
435439

compiler/rustc_session/src/parse.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ use rustc_span::source_map::{FilePathMapping, SourceMap};
2323
use rustc_span::{Span, Symbol};
2424

2525
use rustc_ast::attr::AttrIdGenerator;
26+
use std::cell::Cell;
2627
use std::str;
2728

2829
/// The set of keys (and, optionally, values) that define the compilation
@@ -222,6 +223,8 @@ pub struct ParseSess {
222223
pub proc_macro_quoted_spans: AppendOnlyVec<Span>,
223224
/// Used to generate new `AttrId`s. Every `AttrId` is unique.
224225
pub attr_id_generator: AttrIdGenerator,
226+
/// Whether any parse errors have occurred. Used to silence resolution errors.
227+
pub parse_errors_encountered: Cell<bool>,
225228
}
226229

227230
impl ParseSess {
@@ -252,6 +255,7 @@ impl ParseSess {
252255
assume_incomplete_release: false,
253256
proc_macro_quoted_spans: Default::default(),
254257
attr_id_generator: AttrIdGenerator::new(),
258+
parse_errors_encountered: Cell::new(false),
255259
}
256260
}
257261

tests/ui/associated-consts/issue-105330.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ pub trait TraitWAssocConst {
33
}
44
pub struct Demo {}
55

6-
impl TraitWAssocConst for impl Demo { //~ ERROR E0404
6+
impl TraitWAssocConst for impl Demo {
77
//~^ ERROR E0562
88
pubconst A: str = 32; //~ ERROR expected one of
99
}

tests/ui/associated-consts/issue-105330.stderr

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -9,12 +9,6 @@ LL | pubconst A: str = 32;
99
LL | }
1010
| - the item list ends here
1111

12-
error[E0404]: expected trait, found struct `Demo`
13-
--> $DIR/issue-105330.rs:6:32
14-
|
15-
LL | impl TraitWAssocConst for impl Demo {
16-
| ^^^^ not a trait
17-
1812
error[E0658]: associated const equality is incomplete
1913
--> $DIR/issue-105330.rs:11:28
2014
|
@@ -117,7 +111,7 @@ note: required by a bound in `foo`
117111
LL | fn foo<A: TraitWAssocConst<A=32>>() {
118112
| ^^^^ required by this bound in `foo`
119113

120-
error: aborting due to 11 previous errors
114+
error: aborting due to 10 previous errors
121115

122-
Some errors have detailed explanations: E0131, E0271, E0277, E0404, E0562, E0618, E0658.
116+
Some errors have detailed explanations: E0131, E0271, E0277, E0562, E0618, E0658.
123117
For more information about an error, try `rustc --explain E0131`.

tests/ui/parser/fn-field-parse-error-ice.rs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@ struct Baz {
44
inner : dyn fn ()
55
//~^ ERROR expected `,`, or `}`, found keyword `fn`
66
//~| ERROR expected identifier, found keyword `fn`
7-
//~| ERROR cannot find type `dyn` in this scope
87
}
98

109
fn main() {}

tests/ui/parser/fn-field-parse-error-ice.stderr

Lines changed: 1 addition & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -17,12 +17,5 @@ help: escape `fn` to use it as an identifier
1717
LL | inner : dyn r#fn ()
1818
| ++
1919

20-
error[E0412]: cannot find type `dyn` in this scope
21-
--> $DIR/fn-field-parse-error-ice.rs:4:13
22-
|
23-
LL | inner : dyn fn ()
24-
| ^^^ not found in this scope
25-
26-
error: aborting due to 3 previous errors
20+
error: aborting due to 2 previous errors
2721

28-
For more information about this error, try `rustc --explain E0412`.
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
struct Website {
2+
url: String,
3+
title: Option<String>,
4+
}
5+
6+
fn main() {
7+
let website = Website {
8+
url: "http://www.example.com".into(),
9+
title: Some("Example Domain".into()),
10+
};
11+
12+
if let Website { url, Some(title) } = website { //~ ERROR expected `,`
13+
println!("[{}]({})", title, url);
14+
}
15+
if let Website { url, #, title } = website { //~ ERROR expected one of `!` or `[`, found `,`
16+
println!("[{}]({})", title, url);
17+
}
18+
}
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
error: expected `,`
2+
--> $DIR/avoid-resolve-errors-caused-by-parse-error.rs:12:31
3+
|
4+
LL | if let Website { url, Some(title) } = website {
5+
| ------- ^
6+
| |
7+
| while parsing the fields for this pattern
8+
9+
error: expected one of `!` or `[`, found `,`
10+
--> $DIR/avoid-resolve-errors-caused-by-parse-error.rs:15:28
11+
|
12+
LL | if let Website { url, #, title } = website {
13+
| ------- ^ expected one of `!` or `[`
14+
| |
15+
| while parsing the fields for this pattern
16+
17+
error: aborting due to 2 previous errors
18+

tests/ui/parser/recover-fn-ptr-with-generics.rs renamed to tests/ui/parser/recover/recover-fn-ptr-with-generics.rs

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,6 @@ fn main() {
44

55
type Identity = fn<T>(T) -> T;
66
//~^ ERROR function pointer types may not have generic parameters
7-
//~| ERROR cannot find type `T` in this scope
8-
//~| ERROR cannot find type `T` in this scope
97

108
let _: fn<const N: usize, 'e, Q, 'f>();
119
//~^ ERROR function pointer types may not have generic parameters

tests/ui/parser/recover-fn-ptr-with-generics.stderr renamed to tests/ui/parser/recover/recover-fn-ptr-with-generics.stderr

Lines changed: 9 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ LL | type Identity = fn<T>(T) -> T;
1717
| ^^^
1818

1919
error: function pointer types may not have generic parameters
20-
--> $DIR/recover-fn-ptr-with-generics.rs:10:14
20+
--> $DIR/recover-fn-ptr-with-generics.rs:8:14
2121
|
2222
LL | let _: fn<const N: usize, 'e, Q, 'f>();
2323
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^
@@ -29,7 +29,7 @@ LL + let _: for<'e, 'f> fn();
2929
|
3030

3131
error: function pointer types may not have generic parameters
32-
--> $DIR/recover-fn-ptr-with-generics.rs:13:26
32+
--> $DIR/recover-fn-ptr-with-generics.rs:11:26
3333
|
3434
LL | let _: for<'outer> fn<'inner>();
3535
| ^^^^^^^^
@@ -41,7 +41,7 @@ LL + let _: for<'outer, 'inner> fn();
4141
|
4242

4343
error: function pointer types may not have generic parameters
44-
--> $DIR/recover-fn-ptr-with-generics.rs:16:20
44+
--> $DIR/recover-fn-ptr-with-generics.rs:14:20
4545
|
4646
LL | let _: for<> fn<'r>();
4747
| ^^^^
@@ -53,13 +53,13 @@ LL + let _: for<'r> fn();
5353
|
5454

5555
error: function pointer types may not have generic parameters
56-
--> $DIR/recover-fn-ptr-with-generics.rs:19:18
56+
--> $DIR/recover-fn-ptr-with-generics.rs:17:18
5757
|
5858
LL | type Hmm = fn<>();
5959
| ^^
6060

6161
error: function pointer types may not have generic parameters
62-
--> $DIR/recover-fn-ptr-with-generics.rs:22:21
62+
--> $DIR/recover-fn-ptr-with-generics.rs:20:21
6363
|
6464
LL | let _: extern fn<'a: 'static>();
6565
| ^^^^^^^^^^^^^
@@ -71,7 +71,7 @@ LL + let _: for<'a> extern fn();
7171
|
7272

7373
error: function pointer types may not have generic parameters
74-
--> $DIR/recover-fn-ptr-with-generics.rs:26:35
74+
--> $DIR/recover-fn-ptr-with-generics.rs:24:35
7575
|
7676
LL | let _: for<'any> extern "C" fn<'u>();
7777
| ^^^^
@@ -83,29 +83,16 @@ LL + let _: for<'any, 'u> extern "C" fn();
8383
|
8484

8585
error: expected identifier, found `>`
86-
--> $DIR/recover-fn-ptr-with-generics.rs:29:32
86+
--> $DIR/recover-fn-ptr-with-generics.rs:27:32
8787
|
8888
LL | type QuiteBroken = fn<const>();
8989
| ^ expected identifier
9090

91-
error[E0412]: cannot find type `T` in this scope
92-
--> $DIR/recover-fn-ptr-with-generics.rs:5:27
93-
|
94-
LL | type Identity = fn<T>(T) -> T;
95-
| ^ not found in this scope
96-
97-
error[E0412]: cannot find type `T` in this scope
98-
--> $DIR/recover-fn-ptr-with-generics.rs:5:33
99-
|
100-
LL | type Identity = fn<T>(T) -> T;
101-
| ^ not found in this scope
102-
10391
error: lifetime bounds cannot be used in this context
104-
--> $DIR/recover-fn-ptr-with-generics.rs:22:26
92+
--> $DIR/recover-fn-ptr-with-generics.rs:20:26
10593
|
10694
LL | let _: extern fn<'a: 'static>();
10795
| ^^^^^^^
10896

109-
error: aborting due to 12 previous errors
97+
error: aborting due to 10 previous errors
11098

111-
For more information about this error, try `rustc --explain E0412`.

0 commit comments

Comments
 (0)