-
Notifications
You must be signed in to change notification settings - Fork 1.7k
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
fix: try to infer array type from slice pattern #19066
base: master
Are you sure you want to change the base?
fix: try to infer array type from slice pattern #19066
Conversation
rust-analyzer equivalent of rust-lang/rust#2827aa97
|
||
/// Determines whether we can infer the expected type in the slice pattern to be of type array. | ||
/// This is only possible if we're in an irrefutable pattern. If we were to allow this in refutable | ||
/// patterns we wouldn't e.g. report ambiguity in the following situation: |
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.
The compiler doesn't report ambiguity (https://play.rust-lang.org/?version=stable&mode=debug&edition=2021&gist=574bb654d0bd9905773ec8a82246c446), are you sure this logic is correct?
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.
That's copied from the corresponding rustc PR, though not sure why the text wasnt just copied verbatim https://github.com/rust-lang/rust/pull/113199/files#diff-9f26e4ec8d6ac64edbb3532a590592556b268b0e33f9fd4264d5d449aebbecf7R2061-R2090
crates/hir-ty/src/infer/pat.rs
Outdated
if let Some(decl_ctxt) = decl_ctxt { | ||
!decl_ctxt.has_else && matches!(decl_ctxt.origin, DeclOrigin::LocalDecl) | ||
} else { | ||
false | ||
} |
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.
if let Some(decl_ctxt) = decl_ctxt { | |
!decl_ctxt.has_else && matches!(decl_ctxt.origin, DeclOrigin::LocalDecl) | |
} else { | |
false | |
} | |
matches!(decl_ctxt, Some(DeclContext { origin: DeclOrigin::LocalDecl, has_else: true}) |
|
||
/// Determines whether we can infer the expected type in the slice pattern to be of type array. | ||
/// This is only possible if we're in an irrefutable pattern. If we were to allow this in refutable | ||
/// patterns we wouldn't e.g. report ambiguity in the following situation: |
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.
That's copied from the corresponding rustc PR, though not sure why the text wasnt just copied verbatim https://github.com/rust-lang/rust/pull/113199/files#diff-9f26e4ec8d6ac64edbb3532a590592556b268b0e33f9fd4264d5d449aebbecf7R2061-R2090
@@ -334,7 +334,7 @@ impl InferenceContext<'_> { | |||
ExprIsRead::No | |||
}; | |||
let input_ty = self.infer_expr(expr, &Expectation::none(), child_is_read); | |||
self.infer_top_pat(pat, &input_ty); | |||
self.infer_top_pat(pat, &input_ty, None); |
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.
While we have no use for it now, we should put a Some(DeclContext { origin: DeclOrigin::LetExpr }
here
fixes #16609