From 35dd62e915905b6a93e2069a6d155cdf00cd254a Mon Sep 17 00:00:00 2001 From: Lukas Wirth Date: Tue, 24 Nov 2020 13:41:21 +0100 Subject: Properly infer tuple patterns when encountering ellipsis --- crates/hir_ty/src/infer/pat.rs | 25 +++++++++++++------- crates/hir_ty/src/tests/patterns.rs | 47 +++++++++++++++++++++++++++++++++++++ 2 files changed, 64 insertions(+), 8 deletions(-) (limited to 'crates') diff --git a/crates/hir_ty/src/infer/pat.rs b/crates/hir_ty/src/infer/pat.rs index cde2ab82b..b3fd74eab 100644 --- a/crates/hir_ty/src/infer/pat.rs +++ b/crates/hir_ty/src/infer/pat.rs @@ -111,20 +111,29 @@ impl<'a> InferenceContext<'a> { let expected = expected; let ty = match &body[pat] { - Pat::Tuple { ref args, .. } => { + Pat::Tuple { ref args, ellipsis } => { let expectations = match expected.as_tuple() { Some(parameters) => &*parameters.0, _ => &[], }; - let expectations_iter = expectations.iter().chain(repeat(&Ty::Unknown)); - let inner_tys = args - .iter() - .zip(expectations_iter) - .map(|(&pat, ty)| self.infer_pat(pat, ty, default_bm)) - .collect(); + let (pre, post) = match ellipsis { + &Some(idx) => args.split_at(idx), + None => (&args[..], &[][..]), + }; + let uncovered_range = pre.len()..expectations.len().saturating_sub(post.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()); + 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(post.iter().zip(expectations_iter).map(infer_pat)); - Ty::apply(TypeCtor::Tuple { cardinality: args.len() as u16 }, Substs(inner_tys)) + Ty::apply( + TypeCtor::Tuple { cardinality: inner_tys.len() as u16 }, + Substs(inner_tys.into()), + ) } Pat::Or(ref pats) => { if let Some((first_pat, rest)) = pats.split_first() { diff --git a/crates/hir_ty/src/tests/patterns.rs b/crates/hir_ty/src/tests/patterns.rs index 6a965ac4f..5776de280 100644 --- a/crates/hir_ty/src/tests/patterns.rs +++ b/crates/hir_ty/src/tests/patterns.rs @@ -679,3 +679,50 @@ fn box_pattern() { "#]], ); } + +#[test] +fn tuple_ellipsis_pattern() { + check_infer( + r#" +fn foo(tuple: (u8, i16, f32)) { + match tuple { + (.., b, c) => {}, + (a, .., c) => {}, + (a, b, ..) => {}, + (a, b) => {/*too short*/} + (a, b, c, d) => {/*too long*/} + _ => {} + } +}"#, + expect![[r#" + 7..12 'tuple': (u8, i16, f32) + 30..224 '{ ... } }': () + 36..222 'match ... }': () + 42..47 'tuple': (u8, i16, f32) + 58..68 '(.., b, c)': (u8, i16, f32) + 63..64 'b': i16 + 66..67 'c': f32 + 72..74 '{}': () + 84..94 '(a, .., c)': (u8, i16, f32) + 85..86 'a': u8 + 92..93 'c': f32 + 98..100 '{}': () + 110..120 '(a, b, ..)': (u8, i16, f32) + 111..112 'a': u8 + 114..115 'b': i16 + 124..126 '{}': () + 136..142 '(a, b)': (u8, i16, f32) + 137..138 'a': u8 + 140..141 'b': i16 + 146..161 '{/*too short*/}': () + 170..182 '(a, b, c, d)': (u8, i16, f32, {unknown}) + 171..172 'a': u8 + 174..175 'b': i16 + 177..178 'c': f32 + 180..181 'd': {unknown} + 186..200 '{/*too long*/}': () + 209..210 '_': (u8, i16, f32) + 214..216 '{}': () + "#]], + ); +} -- cgit v1.2.3