Skip to content

Commit 135fca9

Browse files
committed
Revert tests::patterns::infer_pattern
And apply requested changes
1 parent dd46120 commit 135fca9

File tree

4 files changed

+35
-37
lines changed

4 files changed

+35
-37
lines changed

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

+6-3
Original file line numberDiff line numberDiff line change
@@ -334,7 +334,11 @@ impl InferenceContext<'_> {
334334
ExprIsRead::No
335335
};
336336
let input_ty = self.infer_expr(expr, &Expectation::none(), child_is_read);
337-
self.infer_top_pat(pat, &input_ty, None);
337+
self.infer_top_pat(
338+
pat,
339+
&input_ty,
340+
Some(DeclContext { origin: DeclOrigin::LetExpr }),
341+
);
338342
self.result.standard_types.bool_.clone()
339343
}
340344
Expr::Block { statements, tail, label, id } => {
@@ -1633,8 +1637,7 @@ impl InferenceContext<'_> {
16331637
};
16341638

16351639
let decl = DeclContext {
1636-
has_else: else_branch.is_some(),
1637-
origin: DeclOrigin::LocalDecl,
1640+
origin: DeclOrigin::LocalDecl { has_else: else_branch.is_some() },
16381641
};
16391642

16401643
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

+5-6
Original file line numberDiff line numberDiff line change
@@ -1052,18 +1052,17 @@ pub fn known_const_to_ast(
10521052

10531053
#[derive(Debug, Copy, Clone)]
10541054
pub(crate) enum DeclOrigin {
1055-
// from an `if let` expression
10561055
LetExpr,
1057-
// from `let x = ..`
1058-
LocalDecl,
1056+
/// from `let x = ..`
1057+
LocalDecl {
1058+
has_else: bool,
1059+
},
10591060
}
10601061

10611062
/// Provides context for checking patterns in declarations. More specifically this
10621063
/// allows us to infer array types if the pattern is irrefutable and allows us to infer
1063-
/// the size of the array. See issue #76342.
1064+
/// the size of the array. See issue rust-lang/rust#76342.
10641065
#[derive(Debug, Copy, Clone)]
10651066
pub(crate) struct DeclContext {
1066-
// whether we're in a let-else context
1067-
pub(crate) has_else: bool,
10681067
pub(crate) origin: DeclOrigin,
10691068
}

crates/hir-ty/src/tests/patterns.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -67,9 +67,9 @@ fn infer_pattern() {
6767
143..144 'e': {unknown}
6868
157..204 'if let... }': ()
6969
160..175 'let [val] = opt': bool
70-
164..169 '[val]': [{unknown}; 1]
70+
164..169 '[val]': [{unknown}]
7171
165..168 'val': {unknown}
72-
172..175 'opt': [{unknown}; 1]
72+
172..175 'opt': [{unknown}]
7373
176..204 '{ ... }': ()
7474
190..191 'h': {unknown}
7575
194..197 'val': {unknown}

0 commit comments

Comments
 (0)