-
Notifications
You must be signed in to change notification settings - Fork 2.1k
feat(diagnostics): add handler for base expression in struct pattern #22206
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
Closed
Changes from all commits
Commits
Show all changes
2 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
65 changes: 65 additions & 0 deletions
65
crates/ide-diagnostics/src/handlers/base_expr_in_struct_pattern.rs
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,65 @@ | ||
| use crate::{Diagnostic, DiagnosticCode, DiagnosticsContext}; | ||
|
|
||
| // Diagnostic: base-expr-in-struct-pattern | ||
| // | ||
| // This diagnostic is triggered if a struct pattern contains a base expression (`..base`). | ||
| pub(crate) fn base_expr_in_struct_pattern( | ||
| ctx: &DiagnosticsContext<'_>, | ||
| d: &hir::BaseExprInStructPattern, | ||
| ) -> Diagnostic { | ||
| Diagnostic::new_with_syntax_node_ptr( | ||
| ctx, | ||
| DiagnosticCode::SyntaxError, | ||
| "base expressions aren't allowed in struct patterns", | ||
| d.node.map(Into::into), | ||
| ) | ||
| .stable() | ||
| } | ||
|
|
||
| #[cfg(test)] | ||
| mod tests { | ||
| use crate::tests::check_diagnostics; | ||
|
|
||
| #[test] | ||
| fn spread_variable() { | ||
| check_diagnostics( | ||
| r#" | ||
| struct Foo { bar: u32, baz: u32 } | ||
| fn test(f: Foo, g: Foo) { | ||
| if let Foo { ..g } = f {} | ||
| // ^ error: base expressions aren't allowed in struct patterns | ||
| if let Foo { bar: 0, ..g } = f {} | ||
| // ^ error: base expressions aren't allowed in struct patterns | ||
| if let Foo { bar: 0, baz: 0, ..g } = f {} | ||
| // ^ error: base expressions aren't allowed in struct patterns | ||
| } | ||
| "#, | ||
| ); | ||
| } | ||
|
|
||
| #[test] | ||
| fn spread_default() { | ||
| check_diagnostics( | ||
| r#" | ||
| struct Foo { bar: u32, baz: u32 } | ||
| fn test(f: Foo) { | ||
| if let Foo { ..Default::default() } = f {} | ||
| // ^^^^^^^^^^^^^^^^^^ error: base expressions aren't allowed in struct patterns | ||
| } | ||
| "#, | ||
| ); | ||
| } | ||
|
|
||
| #[test] | ||
| fn spread_struct() { | ||
| check_diagnostics( | ||
| r#" | ||
| struct Foo { bar: u32, baz: u32 } | ||
| fn test(f: Foo) { | ||
| if let Foo { ..Foo { bar: 0, baz: 0 } } = f {} | ||
| // ^^^^^^^^^^^^^^^^^^^^^^ error: base expressions aren't allowed in struct patterns | ||
| } | ||
| "#, | ||
| ); | ||
| } | ||
| } | ||
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.
Oh, it turns out I've been running the wrong tests while working on this... But I'm actually not sure what to do with these extraneous errors:
It also looks like no other handlers are for syntax errors, so nowhere to draw inspiration from..
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.
I think that if we already emit syntax errors here, we don't need another error.
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.
fwiw the error from rustc is not quite ideal (imo):
but I don't have a strong opinion on this.
In this case, I'd open a V2 PR which updates the fixme comment to say something like "we wanted to emit a diagnostic on this but decided to rely on rustc instead, since this is a syntax error"
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.
@ada4a
I don't think the point is to rely on
rustc. What happens right now is: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 @lnicola, I don't see where you're getting at with this
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.
By not adding the diagnostic, we're not relying on
rustc, we're relying on our parser error (expected COMMA).