From 2bd9f0f0205e03f95ab83e8f579742618b97a609 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Lauren=C8=9Biu=20Nicola?= Date: Wed, 24 Jun 2020 12:57:28 +0300 Subject: Fix string literal inference in match --- crates/ra_hir_ty/src/infer/pat.rs | 33 ++++++++++++++++++++------------- 1 file changed, 20 insertions(+), 13 deletions(-) (limited to 'crates/ra_hir_ty/src/infer') diff --git a/crates/ra_hir_ty/src/infer/pat.rs b/crates/ra_hir_ty/src/infer/pat.rs index 4006f595d..23de2bd6b 100644 --- a/crates/ra_hir_ty/src/infer/pat.rs +++ b/crates/ra_hir_ty/src/infer/pat.rs @@ -4,7 +4,7 @@ use std::iter::repeat; use std::sync::Arc; use hir_def::{ - expr::{BindingAnnotation, Pat, PatId, RecordFieldPat}, + expr::{BindingAnnotation, Expr, Literal, Pat, PatId, RecordFieldPat}, path::Path, type_ref::Mutability, FieldId, @@ -90,18 +90,7 @@ impl<'a> InferenceContext<'a> { ) -> Ty { let body = Arc::clone(&self.body); // avoid borrow checker problem - let is_non_ref_pat = match &body[pat] { - Pat::Tuple { .. } - | Pat::Or(..) - | Pat::TupleStruct { .. } - | Pat::Record { .. } - | Pat::Range { .. } - | Pat::Slice { .. } => true, - // FIXME: Path/Lit might actually evaluate to ref, but inference is unimplemented. - Pat::Path(..) | Pat::Lit(..) => true, - Pat::Wild | Pat::Bind { .. } | Pat::Ref { .. } | Pat::Missing => false, - }; - if is_non_ref_pat { + if is_non_ref_pat(&body, pat) { while let Some((inner, mutability)) = expected.as_reference() { expected = inner; default_bm = match default_bm { @@ -227,3 +216,21 @@ impl<'a> InferenceContext<'a> { ty } } + +fn is_non_ref_pat(body: &hir_def::body::Body, pat: PatId) -> bool { + match &body[pat] { + Pat::Tuple { .. } + | Pat::TupleStruct { .. } + | Pat::Record { .. } + | 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. + Pat::Path(..) => true, + Pat::Lit(expr) => match body[*expr] { + Expr::Literal(Literal::String(..)) => false, + _ => true, + }, + Pat::Wild | Pat::Bind { .. } | Pat::Ref { .. } | Pat::Missing => false, + } +} -- cgit v1.2.3