diff --git a/crates/hir-def/src/body/lower.rs b/crates/hir-def/src/body/lower.rs index f0cb39ec362c..88b7e63d5709 100644 --- a/crates/hir-def/src/body/lower.rs +++ b/crates/hir-def/src/body/lower.rs @@ -905,8 +905,11 @@ impl ExprCollector<'_> { } None => Pat::Missing, }, - // FIXME: implement - ast::Pat::RangePat(_) => Pat::Missing, + ast::Pat::RangePat(pat) => { + let start = pat.start().map(|pat| self.collect_pat(pat)); + let end = pat.end().map(|pat| self.collect_pat(pat)); + Pat::Range { start, end } + } }; let ptr = AstPtr::new(&pat); self.alloc_pat(pattern, Either::Left(ptr)) diff --git a/crates/hir-def/src/expr.rs b/crates/hir-def/src/expr.rs index 4dca8238880d..1d732083dc0f 100644 --- a/crates/hir-def/src/expr.rs +++ b/crates/hir-def/src/expr.rs @@ -375,7 +375,7 @@ pub enum Pat { Tuple { args: Box<[PatId]>, ellipsis: Option }, Or(Box<[PatId]>), Record { path: Option>, args: Box<[RecordFieldPat]>, ellipsis: bool }, - Range { start: ExprId, end: ExprId }, + Range { start: Option, end: Option }, Slice { prefix: Box<[PatId]>, slice: Option, suffix: Box<[PatId]> }, Path(Box), Lit(ExprId), diff --git a/crates/hir-ty/src/infer/pat.rs b/crates/hir-ty/src/infer/pat.rs index 600b82ca4140..d157532497e0 100644 --- a/crates/hir-ty/src/infer/pat.rs +++ b/crates/hir-ty/src/infer/pat.rs @@ -258,8 +258,11 @@ impl<'a> InferenceContext<'a> { } Pat::Wild => expected.clone(), Pat::Range { start, end } => { - let start_ty = self.infer_expr(*start, &Expectation::has_type(expected.clone())); - self.infer_expr(*end, &Expectation::has_type(start_ty)) + let start_ty = start.map(|pat| self.infer_pat(pat, &expected, default_bm)); + let end_ty = end.as_ref().map(|&pat| { + self.infer_pat(pat, start_ty.as_ref().unwrap_or(&expected), default_bm) + }); + end_ty.unwrap_or_else(|| expected.clone()) } Pat::Lit(expr) => self.infer_expr(*expr, &Expectation::has_type(expected.clone())), Pat::Box { inner } => match self.resolve_boxed_box() { diff --git a/crates/hir-ty/src/tests/patterns.rs b/crates/hir-ty/src/tests/patterns.rs index acdd8f50efb8..cc443b74ef6e 100644 --- a/crates/hir-ty/src/tests/patterns.rs +++ b/crates/hir-ty/src/tests/patterns.rs @@ -172,12 +172,20 @@ fn infer_range_pattern() { 17..75 '{ ...2 {} }': () 23..45 'if let...u32 {}': () 26..42 'let 1....= 2u32': bool + 30..31 '1': u32 + 30..31 '1': u32 30..35 '1..76': u32 + 33..35 '76': u32 + 33..35 '76': u32 38..42 '2u32': u32 43..45 '{}': () 50..73 'if let...u32 {}': () 53..70 'let 1....= 2u32': bool + 57..58 '1': u32 + 57..58 '1': u32 57..63 '1..=76': u32 + 61..63 '76': u32 + 61..63 '76': u32 66..70 '2u32': u32 71..73 '{}': () "#]],