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

Merged
merged 5 commits into from
Feb 3, 2025

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

@Veykril
Copy link
Member

Veykril commented Feb 3, 2025

Looks good to me now 👍
Could you squash the history / give the last two commits a bit more descriptive names?

And apply requested changes
@alibektas alibektas force-pushed the slice_pattern_type_inference branch from 97255d1 to 135fca9 Compare February 3, 2025 11:23
@Veykril Veykril added this pull request to the merge queue Feb 3, 2025
Merged via the queue into rust-lang:master with commit 13c17db Feb 3, 2025
9 checks passed
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