diff --git a/crates/hir_ty/src/infer/pat.rs b/crates/hir_ty/src/infer/pat.rs index b3fd74eab70..b70ec55eb5c 100644 --- a/crates/hir_ty/src/infer/pat.rs +++ b/crates/hir_ty/src/infer/pat.rs @@ -23,6 +23,7 @@ fn infer_tuple_struct_pat( expected: &Ty, default_bm: BindingMode, id: PatId, + ellipsis: Option, ) -> Ty { let (ty, def) = self.resolve_variant(path); let var_data = def.map(|it| variant_data(self.db.upcast(), it)); @@ -34,8 +35,15 @@ fn infer_tuple_struct_pat( let substs = ty.substs().unwrap_or_else(Substs::empty); let field_tys = def.map(|it| self.db.field_types(it)).unwrap_or_default(); + let (pre, post) = match ellipsis { + Some(idx) => subpats.split_at(idx), + None => (&subpats[..], &[][..]), + }; + let post_idx_offset = field_tys.iter().count() - post.len(); - for (i, &subpat) in subpats.iter().enumerate() { + let pre_iter = pre.iter().enumerate(); + let post_iter = (post_idx_offset..).zip(post.iter()); + for (i, &subpat) in pre_iter.chain(post_iter) { let expected_ty = var_data .as_ref() .and_then(|d| d.field(&Name::new_tuple_field(i))) @@ -111,23 +119,23 @@ pub(super) fn infer_pat( let expected = expected; let ty = match &body[pat] { - Pat::Tuple { ref args, ellipsis } => { + &Pat::Tuple { ref args, ellipsis } => { let expectations = match expected.as_tuple() { Some(parameters) => &*parameters.0, _ => &[], }; let (pre, post) = match ellipsis { - &Some(idx) => args.split_at(idx), + Some(idx) => args.split_at(idx), None => (&args[..], &[][..]), }; - let uncovered_range = pre.len()..expectations.len().saturating_sub(post.len()); + let n_uncovered_patterns = expectations.len().saturating_sub(args.len()); let mut expectations_iter = expectations.iter().chain(repeat(&Ty::Unknown)); let mut infer_pat = |(&pat, ty)| self.infer_pat(pat, ty, default_bm); - let mut inner_tys = Vec::with_capacity(expectations.len()); + let mut inner_tys = Vec::with_capacity(n_uncovered_patterns + args.len()); inner_tys.extend(pre.iter().zip(expectations_iter.by_ref()).map(&mut infer_pat)); - inner_tys.extend(expectations_iter.by_ref().take(uncovered_range.len()).cloned()); + inner_tys.extend(expectations_iter.by_ref().take(n_uncovered_patterns).cloned()); inner_tys.extend(post.iter().zip(expectations_iter).map(infer_pat)); Ty::apply( @@ -159,9 +167,14 @@ pub(super) fn infer_pat( let subty = self.infer_pat(*pat, expectation, default_bm); Ty::apply_one(TypeCtor::Ref(*mutability), subty) } - Pat::TupleStruct { path: p, args: subpats, .. } => { - self.infer_tuple_struct_pat(p.as_ref(), subpats, expected, default_bm, pat) - } + Pat::TupleStruct { path: p, args: subpats, ellipsis } => self.infer_tuple_struct_pat( + p.as_ref(), + subpats, + expected, + default_bm, + pat, + *ellipsis, + ), Pat::Record { path: p, args: fields, ellipsis: _ } => { self.infer_record_pat(p.as_ref(), fields, expected, default_bm, pat) } diff --git a/crates/hir_ty/src/tests/patterns.rs b/crates/hir_ty/src/tests/patterns.rs index 5776de28079..5a5f48fd05b 100644 --- a/crates/hir_ty/src/tests/patterns.rs +++ b/crates/hir_ty/src/tests/patterns.rs @@ -726,3 +726,51 @@ fn foo(tuple: (u8, i16, f32)) { "#]], ); } + +#[test] +fn tuple_struct_ellipsis_pattern() { + check_infer( + r#" +struct Tuple(u8, i16, f32); +fn foo(tuple: Tuple) { + match tuple { + Tuple(.., b, c) => {}, + Tuple(a, .., c) => {}, + Tuple(a, b, ..) => {}, + Tuple(a, b) => {/*too short*/} + Tuple(a, b, c, d) => {/*too long*/} + _ => {} + } +}"#, + expect![[r#" + 35..40 'tuple': Tuple + 49..268 '{ ... } }': () + 55..266 'match ... }': () + 61..66 'tuple': Tuple + 77..92 'Tuple(.., b, c)': Tuple + 87..88 'b': i16 + 90..91 'c': f32 + 96..98 '{}': () + 108..123 'Tuple(a, .., c)': Tuple + 114..115 'a': u8 + 121..122 'c': f32 + 127..129 '{}': () + 139..154 'Tuple(a, b, ..)': Tuple + 145..146 'a': u8 + 148..149 'b': i16 + 158..160 '{}': () + 170..181 'Tuple(a, b)': Tuple + 176..177 'a': u8 + 179..180 'b': i16 + 185..200 '{/*too short*/}': () + 209..226 'Tuple(... c, d)': Tuple + 215..216 'a': u8 + 218..219 'b': i16 + 221..222 'c': f32 + 224..225 'd': {unknown} + 230..244 '{/*too long*/}': () + 253..254 '_': Tuple + 258..260 '{}': () + "#]], + ); +}