Skip to content

Silence resolve errors if there were parse errors #116904

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 11 additions & 2 deletions compiler/rustc_parse/src/parser/item.rs
Original file line number Diff line number Diff line change
Expand Up @@ -152,15 +152,21 @@ impl<'a> Parser<'a> {
let lo = self.token.span;
let vis = self.parse_visibility(FollowedByType::No)?;
let mut def = self.parse_defaultness();
let kind = self.parse_item_kind(
let kind = match self.parse_item_kind(
&mut attrs,
mac_allowed,
lo,
&vis,
&mut def,
fn_parse_mode,
Case::Sensitive,
)?;
) {
Ok(kind) => kind,
Err(err) => {
self.sess.parse_errors_encountered.set(true);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This seems way more flow-dependent than just checking if we've emitted any session errors.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

For example, this doesn't protect against erroneous recoveries that don't result in a bubbled up Err value.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah, I experimented with silencing all resolve errors if there were any parse errors, but in practice it wasn't an improvement. The only place where the silencing really comes in handy is when parsing patterns fails, and the silencing should be only within that scope, not all resolve errors.

return Err(err);
}
};
if let Some((ident, kind)) = kind {
self.error_on_unconsumed_default(def, &kind);
let span = lo.to(self.prev_token.span);
Expand Down Expand Up @@ -726,6 +732,7 @@ impl<'a> Parser<'a> {
let is_let = self.token.is_keyword(kw::Let);

let mut err = self.struct_span_err(non_item_span, "non-item in item list");
self.sess.parse_errors_encountered.set(true);
self.consume_block(Delimiter::Brace, ConsumeClosingDelim::Yes);
if is_let {
err.span_suggestion(
Expand All @@ -752,6 +759,7 @@ impl<'a> Parser<'a> {
}
Ok(Some(item)) => items.extend(item),
Err(mut err) => {
self.sess.parse_errors_encountered.set(true);
self.consume_block(Delimiter::Brace, ConsumeClosingDelim::Yes);
err.span_label(open_brace_span, "while parsing this item list starting here")
.span_label(self.prev_token.span, "the item list ends here")
Expand Down Expand Up @@ -1618,6 +1626,7 @@ impl<'a> Parser<'a> {
while self.token != token::CloseDelim(Delimiter::Brace) {
let field = self.parse_field_def(adt_ty).map_err(|e| {
self.consume_block(Delimiter::Brace, ConsumeClosingDelim::No);
self.sess.parse_errors_encountered.set(true);
recovered = true;
e
});
Expand Down
1 change: 1 addition & 0 deletions compiler/rustc_parse/src/parser/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1459,6 +1459,7 @@ pub(crate) fn make_unclosed_delims_error(
if let Some(sp) = unmatched.unclosed_span {
spans.push(sp);
};
sess.parse_errors_encountered.set(true);
let err = MismatchedClosingDelimiter {
spans,
delimiter: pprust::token_kind_to_string(&token::CloseDelim(found_delim)).to_string(),
Expand Down
32 changes: 30 additions & 2 deletions compiler/rustc_parse/src/parser/pat.rs
Original file line number Diff line number Diff line change
Expand Up @@ -870,6 +870,7 @@ impl<'a> Parser<'a> {
let (fields, etc) = self.parse_pat_fields().unwrap_or_else(|mut e| {
e.span_label(path.span, "while parsing the fields for this pattern");
e.emit();
self.sess.parse_errors_encountered.set(true);
self.recover_stmt();
(ThinVec::new(), true)
});
Expand Down Expand Up @@ -963,12 +964,38 @@ impl<'a> Parser<'a> {
return Err(err);
}
};
let prev = self.prev_token.span;
let lo = self.token.span;

// check that a comma comes after every field
if !ate_comma {
let err = ExpectedCommaAfterPatternField { span: self.token.span }
.into_diagnostic(&self.sess.span_diagnostic);
let mut snapshot = self.create_snapshot_for_diagnostic();
let err = match snapshot.parse_pat_allow_top_alt(
None,
RecoverComma::No,
RecoverColon::No,
CommaRecoveryMode::EitherTupleOrPipe,
) {
Ok(pat) => {
// We've got `Struct { Some(foo) }` where the user forgot the field ident.
let span = prev.to(pat.span);
let mut err =
self.struct_span_err(span, "missing field name before pattern");
err.span_label(span, "this pattern needs to be preceeded by a field name");
err.span_suggestion_verbose(
prev.shrink_to_lo(),
"you might have meant to add a field",
"field_name: ".to_string(),
Applicability::HasPlaceholders,
);
err
}
Err(err) => {
err.cancel();
ExpectedCommaAfterPatternField { span: self.token.span }
.into_diagnostic(&self.sess.span_diagnostic)
}
};
if let Some(mut delayed) = delayed_err {
delayed.emit();
}
Expand Down Expand Up @@ -1104,6 +1131,7 @@ impl<'a> Parser<'a> {
}
}
}
self.sess.parse_errors_encountered.set(true);
err.emit();
}
Ok((fields, etc))
Expand Down
4 changes: 4 additions & 0 deletions compiler/rustc_resolve/src/late/diagnostics.rs
Original file line number Diff line number Diff line change
Expand Up @@ -430,6 +430,10 @@ impl<'a: 'ast, 'ast, 'tcx> LateResolutionVisitor<'a, '_, 'ast, 'tcx> {
base_error.msg.clone(),
code,
);
if self.r.tcx.sess.parse_sess.parse_errors_encountered.get() {
// We've encountered parse errors that can cause resolution errors, so we silence them.
err.delay_as_bug();
}

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

Expand Down
4 changes: 4 additions & 0 deletions compiler/rustc_session/src/parse.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ use rustc_span::source_map::{FilePathMapping, SourceMap};
use rustc_span::{Span, Symbol};

use rustc_ast::attr::AttrIdGenerator;
use std::cell::Cell;
use std::str;

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

impl ParseSess {
Expand Down Expand Up @@ -252,6 +255,7 @@ impl ParseSess {
assume_incomplete_release: false,
proc_macro_quoted_spans: Default::default(),
attr_id_generator: AttrIdGenerator::new(),
parse_errors_encountered: Cell::new(false),
}
}

Expand Down
2 changes: 1 addition & 1 deletion tests/ui/associated-consts/issue-105330.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ pub trait TraitWAssocConst {
}
pub struct Demo {}

impl TraitWAssocConst for impl Demo { //~ ERROR E0404
impl TraitWAssocConst for impl Demo {
//~^ ERROR E0562
pubconst A: str = 32; //~ ERROR expected one of
}
Expand Down
10 changes: 2 additions & 8 deletions tests/ui/associated-consts/issue-105330.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,6 @@ LL | pubconst A: str = 32;
LL | }
| - the item list ends here

error[E0404]: expected trait, found struct `Demo`
--> $DIR/issue-105330.rs:6:32
|
LL | impl TraitWAssocConst for impl Demo {
| ^^^^ not a trait

error[E0658]: associated const equality is incomplete
--> $DIR/issue-105330.rs:11:28
|
Expand Down Expand Up @@ -117,7 +111,7 @@ note: required by a bound in `foo`
LL | fn foo<A: TraitWAssocConst<A=32>>() {
| ^^^^ required by this bound in `foo`

error: aborting due to 11 previous errors
error: aborting due to 10 previous errors

Some errors have detailed explanations: E0131, E0271, E0277, E0404, E0562, E0618, E0658.
Some errors have detailed explanations: E0131, E0271, E0277, E0562, E0618, E0658.
For more information about an error, try `rustc --explain E0131`.
1 change: 0 additions & 1 deletion tests/ui/parser/fn-field-parse-error-ice.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ struct Baz {
inner : dyn fn ()
//~^ ERROR expected `,`, or `}`, found keyword `fn`
//~| ERROR expected identifier, found keyword `fn`
//~| ERROR cannot find type `dyn` in this scope
}

fn main() {}
9 changes: 1 addition & 8 deletions tests/ui/parser/fn-field-parse-error-ice.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -17,12 +17,5 @@ help: escape `fn` to use it as an identifier
LL | inner : dyn r#fn ()
| ++

error[E0412]: cannot find type `dyn` in this scope
--> $DIR/fn-field-parse-error-ice.rs:4:13
|
LL | inner : dyn fn ()
| ^^^ not found in this scope

error: aborting due to 3 previous errors
error: aborting due to 2 previous errors

For more information about this error, try `rustc --explain E0412`.
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
struct Website {
url: String,
title: Option<String>,
}

fn main() {
let website = Website {
url: "http://www.example.com".into(),
title: Some("Example Domain".into()),
};

if let Website { url, Some(title) } = website { //~ ERROR missing field name before pattern
println!("[{}]({})", title, url);
}
if let Website { url, #, title } = website { //~ ERROR expected one of `!` or `[`, found `,`
println!("[{}]({})", title, url);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
error: missing field name before pattern
--> $DIR/avoid-resolve-errors-caused-by-parse-error.rs:12:27
|
LL | if let Website { url, Some(title) } = website {
| ------- ^^^^^^^^^^^ this pattern needs to be preceeded by a field name
| |
| while parsing the fields for this pattern
|
help: you might have meant to add a field
|
LL | if let Website { url, field_name: Some(title) } = website {
| +++++++++++

error: expected one of `!` or `[`, found `,`
--> $DIR/avoid-resolve-errors-caused-by-parse-error.rs:15:28
|
LL | if let Website { url, #, title } = website {
| ------- ^ expected one of `!` or `[`
| |
| while parsing the fields for this pattern

error: aborting due to 2 previous errors

Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,6 @@ fn main() {

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

let _: fn<const N: usize, 'e, Q, 'f>();
//~^ ERROR function pointer types may not have generic parameters
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ LL | type Identity = fn<T>(T) -> T;
| ^^^

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

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

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

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

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

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

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

error[E0412]: cannot find type `T` in this scope
--> $DIR/recover-fn-ptr-with-generics.rs:5:27
|
LL | type Identity = fn<T>(T) -> T;
| ^ not found in this scope

error[E0412]: cannot find type `T` in this scope
--> $DIR/recover-fn-ptr-with-generics.rs:5:33
|
LL | type Identity = fn<T>(T) -> T;
| ^ not found in this scope

error: lifetime bounds cannot be used in this context
--> $DIR/recover-fn-ptr-with-generics.rs:22:26
--> $DIR/recover-fn-ptr-with-generics.rs:20:26
|
LL | let _: extern fn<'a: 'static>();
| ^^^^^^^

error: aborting due to 12 previous errors
error: aborting due to 10 previous errors

For more information about this error, try `rustc --explain E0412`.
2 changes: 1 addition & 1 deletion tests/ui/resolve/issue-54379.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ fn main() {

match thing {
MyStruct { .., Some(_) } => {},
//~^ ERROR expected `,`
//~^ ERROR missing field name before pattern
//~| ERROR expected `}`, found `,`
_ => {}
}
Expand Down
11 changes: 8 additions & 3 deletions tests/ui/resolve/issue-54379.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,18 @@ LL | MyStruct { .., Some(_) } => {},
| | expected `}`
| `..` must be at the end and cannot have a trailing comma

error: expected `,`
--> $DIR/issue-54379.rs:9:28
error: missing field name before pattern
--> $DIR/issue-54379.rs:9:24
|
LL | MyStruct { .., Some(_) } => {},
| -------- ^
| -------- ^^^^^^^ this pattern needs to be preceeded by a field name
| |
| while parsing the fields for this pattern
|
help: you might have meant to add a field
|
LL | MyStruct { .., field_name: Some(_) } => {},
| +++++++++++

error: aborting due to 2 previous errors