-
Notifications
You must be signed in to change notification settings - Fork 2.1k
feat: add diagnostic for E0422 #22393
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
Open
WeiTheShinobi
wants to merge
4
commits into
rust-lang:master
Choose a base branch
from
WeiTheShinobi:e0422
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,79 @@ | ||
| use either::Either; | ||
| use syntax::{AstNode, ast}; | ||
|
|
||
| use crate::{Diagnostic, DiagnosticCode, DiagnosticsContext, adjusted_display_range}; | ||
|
|
||
| // Diagnostic: unresolved-variant | ||
| // | ||
| // This diagnostic is triggered if the struct, variant, or union type referred to by a record expression does not exist in the current scope. | ||
| pub(crate) fn unresolved_variant( | ||
| ctx: &DiagnosticsContext<'_, '_>, | ||
| d: &hir::UnresolvedVariant, | ||
| ) -> Diagnostic { | ||
| Diagnostic::new( | ||
| DiagnosticCode::RustcHardError("E0422"), | ||
| "cannot find struct, variant or union type in this scope".to_owned(), | ||
| adjusted_display_range(ctx, d.node, &|node| match node { | ||
| Either::Left(ast::Expr::RecordExpr(it)) => it.path().map(|p| p.syntax().text_range()), | ||
| Either::Right(ast::Pat::RecordPat(it)) => it.path().map(|p| p.syntax().text_range()), | ||
| Either::Right(ast::Pat::TupleStructPat(it)) => { | ||
| it.path().map(|p| p.syntax().text_range()) | ||
| } | ||
| _ => None, | ||
| }), | ||
| ) | ||
| .stable() | ||
| } | ||
|
|
||
| #[cfg(test)] | ||
| mod tests { | ||
| use crate::tests::check_diagnostics; | ||
|
|
||
| #[test] | ||
| fn unresolved_record_expr() { | ||
| check_diagnostics( | ||
| r#" | ||
| fn main() { | ||
| let _ = DoesNotExist { x: 1, y: 2 }; | ||
| // ^^^^^^^^^^^^ error: cannot find struct, variant or union type in this scope | ||
| } | ||
| "#, | ||
| ); | ||
| } | ||
|
|
||
| #[test] | ||
| fn unresolved_record_pat() { | ||
| check_diagnostics( | ||
| r#" | ||
| fn main() { | ||
| struct Exist { x: i32, y: i32 } | ||
| let a = Exist { x: 1, y: 2 }; | ||
| match a { | ||
| Exist { .. } => {} | ||
| DoesNotExist { .. } => {} | ||
| // ^^^^^^^^^^^^ error: cannot find struct, variant or union type in this scope | ||
| _ => {} | ||
| } | ||
| } | ||
| "#, | ||
| ); | ||
| } | ||
|
|
||
| #[test] | ||
| fn unresolved_tuple_struct_pat() { | ||
| check_diagnostics( | ||
| r#" | ||
| fn main() { | ||
| struct Tuple(i32, i32); | ||
| let t = Tuple(1, 2); | ||
| match t { | ||
| Tuple( .. ) => {} | ||
| DoesNotExist( .. ) => {} | ||
| // ^^^^^^^^^^^^ error: cannot find struct, variant or union type in this scope | ||
| _ => {} | ||
| } | ||
| } | ||
| "#, | ||
| ); | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Sorry about this, but IMO this is still not the right place. We should emit an error in
resolve_path_in_X(), and I don't know if we should do it yet.View changes since the review
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Also there are more cases in this method after resolution.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I found that if I emit diagnostics in
resolve_path_in_value_nsandresolve_path_in_type_ns, different call sites need different error code (for example, anotherresolve_path_in_type_nscall site needE0425rather thanE0422). I feel like there is no proper place for now.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
First, the error codes are not set in stone. The code organization is more important. Second, we can pass this information from the outside.
But as I said, I am not sure we should do this yet.