Skip to content
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

Open
wants to merge 7 commits into
base: master
Choose a base branch
from

Conversation

alibektas
Copy link
Member

fixes #16609

@rustbot rustbot added the S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. label Jan 29, 2025

/// 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:
Copy link
Contributor

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?

Copy link
Member

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

Comment on lines 635 to 639
if let Some(decl_ctxt) = decl_ctxt {
!decl_ctxt.has_else && matches!(decl_ctxt.origin, DeclOrigin::LocalDecl)
} else {
false
}
Copy link
Member

Choose a reason for hiding this comment

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

Suggested change
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:
Copy link
Member

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/lib.rs Outdated Show resolved Hide resolved
crates/hir-ty/src/lib.rs Outdated Show resolved Hide resolved
crates/hir-ty/src/lib.rs Outdated Show resolved Hide resolved
crates/hir-ty/src/infer/pat.rs Outdated Show resolved Hide resolved
@@ -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);
Copy link
Member

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
S-waiting-on-review Status: Awaiting review from the assignee but also interested parties.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Inference failure since slice patterns does not serve as type expections
4 participants