Skip to content

Commit c0f6fd4

Browse files
committed
v2
1 parent ba2da96 commit c0f6fd4

File tree

3 files changed

+25
-37
lines changed

3 files changed

+25
-37
lines changed

crates/hir-ty/src/infer/expr.rs

+1-2
Original file line numberDiff line numberDiff line change
@@ -1633,8 +1633,7 @@ impl InferenceContext<'_> {
16331633
};
16341634

16351635
let decl = DeclContext {
1636-
has_else: else_branch.is_some(),
1637-
origin: DeclOrigin::LocalDecl,
1636+
origin: DeclOrigin::LocalDecl { has_else: else_branch.is_some() },
16381637
};
16391638

16401639
this.infer_top_pat(*pat, &ty, Some(decl));

crates/hir-ty/src/infer/pat.rs

+22-26
Original file line numberDiff line numberDiff line change
@@ -498,7 +498,7 @@ impl InferenceContext<'_> {
498498

499499
// If `expected` is an infer ty, we try to equate it to an array if the given pattern
500500
// allows it. See issue #16609
501-
if self.decl_allows_array_type_infer(decl) && expected.is_ty_var() {
501+
if self.pat_is_irrefutable(decl) && expected.is_ty_var() {
502502
if let Some(resolved_array_ty) =
503503
self.try_resolve_slice_ty_to_array_ty(prefix, suffix, slice)
504504
{
@@ -601,42 +601,38 @@ impl InferenceContext<'_> {
601601
Some(array_ty)
602602
}
603603

604-
/// Determines whether we can infer the expected type in the slice pattern to be of type array.
604+
/// Used to determine whether we can infer the expected type in the slice pattern to be of type array.
605605
/// This is only possible if we're in an irrefutable pattern. If we were to allow this in refutable
606606
/// patterns we wouldn't e.g. report ambiguity in the following situation:
607607
///
608608
/// ```ignore(rust)
609-
/// struct Zeroes;
610-
/// const ARR: [usize; 2] = [0; 2];
611-
/// const ARR2: [usize; 2] = [2; 2];
609+
/// struct Zeroes;
610+
/// const ARR: [usize; 2] = [0; 2];
611+
/// const ARR2: [usize; 2] = [2; 2];
612612
///
613-
/// impl Into<&'static [usize; 2]> for Zeroes {
614-
/// fn into(self) -> &'static [usize; 2] {
613+
/// impl Into<&'static [usize; 2]> for Zeroes {
614+
/// fn into(self) -> &'static [usize; 2] {
615615
/// &ARR
616-
/// }
617-
/// }
616+
/// }
617+
/// }
618618
///
619-
/// impl Into<&'static [usize]> for Zeroes {
620-
/// fn into(self) -> &'static [usize] {
621-
/// &ARR2
622-
/// }
623-
/// }
619+
/// impl Into<&'static [usize]> for Zeroes {
620+
/// fn into(self) -> &'static [usize] {
621+
/// &ARR2
622+
/// }
623+
/// }
624624
///
625-
/// fn main() {
626-
/// let &[a, b]: &[usize] = Zeroes.into() else {
627-
/// ..
628-
/// };
629-
/// }
625+
/// fn main() {
626+
/// let &[a, b]: &[usize] = Zeroes.into() else {
627+
/// ..
628+
/// };
629+
/// }
630630
/// ```
631631
///
632632
/// If we're in an irrefutable pattern we prefer the array impl candidate given that
633-
/// the slice impl candidate would be be rejected anyway (if no ambiguity existed).
634-
fn decl_allows_array_type_infer(&self, decl_ctxt: Option<DeclContext>) -> bool {
635-
if let Some(decl_ctxt) = decl_ctxt {
636-
!decl_ctxt.has_else && matches!(decl_ctxt.origin, DeclOrigin::LocalDecl)
637-
} else {
638-
false
639-
}
633+
/// the slice impl candidate would be rejected anyway (if no ambiguity existed).
634+
fn pat_is_irrefutable(&self, decl_ctxt: Option<DeclContext>) -> bool {
635+
matches!(decl_ctxt, Some(DeclContext { origin: DeclOrigin::LocalDecl { has_else: false } }))
640636
}
641637
}
642638

crates/hir-ty/src/lib.rs

+2-9
Original file line numberDiff line numberDiff line change
@@ -1052,21 +1052,14 @@ pub fn known_const_to_ast(
10521052

10531053
#[derive(Debug, Copy, Clone)]
10541054
pub(crate) enum DeclOrigin {
1055-
// from an `if let` expression
1056-
// Due to the limited use of `DeclOrigin` it has so far been
1057-
// necessary to have the `LetExpr` variant. However, to have the
1058-
// identical struct to its rustc counterpart, we keep this variant here.
1059-
// LetExpr,
10601055
/// from `let x = ..`
1061-
LocalDecl,
1056+
LocalDecl { has_else: bool },
10621057
}
10631058

10641059
/// Provides context for checking patterns in declarations. More specifically this
10651060
/// allows us to infer array types if the pattern is irrefutable and allows us to infer
1066-
/// the size of the array. See issue #76342.
1061+
/// the size of the array. See issue rust-lang/rust#76342.
10671062
#[derive(Debug, Copy, Clone)]
10681063
pub(crate) struct DeclContext {
1069-
// whether we're in a let-else context
1070-
pub(crate) has_else: bool,
10711064
pub(crate) origin: DeclOrigin,
10721065
}

0 commit comments

Comments
 (0)