From 2de6eb7bc852d096d2f09b7efe2469317bd86c9c Mon Sep 17 00:00:00 2001 From: Jonas Schievink Date: Sat, 12 Sep 2020 21:15:00 +0200 Subject: Add box pattern test --- crates/hir_ty/src/tests/patterns.rs | 25 +++++++++++++++++++++++++ 1 file changed, 25 insertions(+) (limited to 'crates/hir_ty/src') diff --git a/crates/hir_ty/src/tests/patterns.rs b/crates/hir_ty/src/tests/patterns.rs index aeb191c79..6a965ac4f 100644 --- a/crates/hir_ty/src/tests/patterns.rs +++ b/crates/hir_ty/src/tests/patterns.rs @@ -654,3 +654,28 @@ fn slice_tail_pattern() { "#]], ); } + +#[test] +fn box_pattern() { + check_infer( + r#" + #[lang = "owned_box"] + pub struct Box(T); + + fn foo(params: Box) { + match params { + box integer => {} + } + } + "#, + expect![[r#" + 52..58 'params': Box + 70..124 '{ ... } }': () + 76..122 'match ... }': () + 82..88 'params': Box + 99..110 'box integer': Box + 103..110 'integer': i32 + 114..116 '{}': () + "#]], + ); +} -- cgit v1.2.3 From 07a704e31cf74deb28a15859b5adfe65ed709bf4 Mon Sep 17 00:00:00 2001 From: Jonas Schievink Date: Sat, 12 Sep 2020 21:18:57 +0200 Subject: Implement box pattern inference --- crates/hir_ty/src/infer/pat.rs | 14 +++++++++++++- 1 file changed, 13 insertions(+), 1 deletion(-) (limited to 'crates/hir_ty/src') diff --git a/crates/hir_ty/src/infer/pat.rs b/crates/hir_ty/src/infer/pat.rs index dde38bc39..cde2ab82b 100644 --- a/crates/hir_ty/src/infer/pat.rs +++ b/crates/hir_ty/src/infer/pat.rs @@ -209,6 +209,18 @@ impl<'a> InferenceContext<'a> { end_ty } Pat::Lit(expr) => self.infer_expr(*expr, &Expectation::has_type(expected.clone())), + Pat::Box { inner } => match self.resolve_boxed_box() { + Some(box_adt) => { + let inner_expected = match expected.as_adt() { + Some((adt, substs)) if adt == box_adt => substs.as_single(), + _ => &Ty::Unknown, + }; + + let inner_ty = self.infer_pat(*inner, inner_expected, default_bm); + Ty::apply_one(TypeCtor::Adt(box_adt), inner_ty) + } + None => Ty::Unknown, + }, Pat::Missing => Ty::Unknown, }; // use a new type variable if we got Ty::Unknown here @@ -236,6 +248,6 @@ fn is_non_ref_pat(body: &hir_def::body::Body, pat: PatId) -> bool { Expr::Literal(Literal::String(..)) => false, _ => true, }, - Pat::Wild | Pat::Bind { .. } | Pat::Ref { .. } | Pat::Missing => false, + Pat::Wild | Pat::Bind { .. } | Pat::Ref { .. } | Pat::Box { .. } | Pat::Missing => false, } } -- cgit v1.2.3