From 0a780c0ab3869d92fb56ae3b2ddc7636fb169314 Mon Sep 17 00:00:00 2001 From: Lukas Wirth Date: Wed, 23 Dec 2020 12:15:38 +0100 Subject: Implement const pat inference --- crates/hir_ty/src/infer/pat.rs | 6 +++++- crates/hir_ty/src/tests/patterns.rs | 30 ++++++++++++++++++++++++++++++ 2 files changed, 35 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 b70ec55eb..d974f805b 100644 --- a/crates/hir_ty/src/infer/pat.rs +++ b/crates/hir_ty/src/infer/pat.rs @@ -243,6 +243,9 @@ impl<'a> InferenceContext<'a> { } None => Ty::Unknown, }, + Pat::ConstBlock(expr) => { + self.infer_expr(*expr, &Expectation::has_type(expected.clone())) + } Pat::Missing => Ty::Unknown, }; // use a new type variable if we got Ty::Unknown here @@ -264,8 +267,9 @@ fn is_non_ref_pat(body: &hir_def::body::Body, pat: PatId) -> bool { | Pat::Range { .. } | Pat::Slice { .. } => true, Pat::Or(pats) => pats.iter().all(|p| is_non_ref_pat(body, *p)), - // FIXME: Path/Lit might actually evaluate to ref, but inference is unimplemented. + // FIXME: ConstBlock/Path/Lit might actually evaluate to ref, but inference is unimplemented. Pat::Path(..) => true, + Pat::ConstBlock(..) => true, Pat::Lit(expr) => match body[*expr] { Expr::Literal(Literal::String(..)) => false, _ => true, diff --git a/crates/hir_ty/src/tests/patterns.rs b/crates/hir_ty/src/tests/patterns.rs index 5a5f48fd0..2053d8f56 100644 --- a/crates/hir_ty/src/tests/patterns.rs +++ b/crates/hir_ty/src/tests/patterns.rs @@ -774,3 +774,33 @@ fn foo(tuple: Tuple) { "#]], ); } + +#[test] +fn const_block_pattern() { + check_infer( + r#" +struct Foo(usize); +fn foo(foo: Foo) { + match foo { + const { Foo(15 + 32) } => {}, + _ => {} + } +}"#, + expect![[r#" + 26..29 'foo': Foo + 36..115 '{ ... } }': () + 42..113 'match ... }': () + 48..51 'foo': Foo + 62..84 'const ... 32) }': Foo + 68..84 '{ Foo(... 32) }': Foo + 70..73 'Foo': Foo(usize) -> Foo + 70..82 'Foo(15 + 32)': Foo + 74..76 '15': usize + 74..81 '15 + 32': usize + 79..81 '32': usize + 88..90 '{}': () + 100..101 '_': Foo + 105..107 '{}': () + "#]], + ); +} -- cgit v1.2.3