aboutsummaryrefslogtreecommitdiff
path: root/crates/hir_ty/src/infer
diff options
context:
space:
mode:
Diffstat (limited to 'crates/hir_ty/src/infer')
-rw-r--r--crates/hir_ty/src/infer/coerce.rs8
-rw-r--r--crates/hir_ty/src/infer/expr.rs26
-rw-r--r--crates/hir_ty/src/infer/pat.rs11
3 files changed, 21 insertions, 24 deletions
diff --git a/crates/hir_ty/src/infer/coerce.rs b/crates/hir_ty/src/infer/coerce.rs
index 4b7f31521..7be914451 100644
--- a/crates/hir_ty/src/infer/coerce.rs
+++ b/crates/hir_ty/src/infer/coerce.rs
@@ -47,10 +47,7 @@ impl<'a> InferenceContext<'a> {
47 // pointers to have a chance at getting a match. See 47 // pointers to have a chance at getting a match. See
48 // https://github.com/rust-lang/rust/blob/7b805396bf46dce972692a6846ce2ad8481c5f85/src/librustc_typeck/check/coercion.rs#L877-L916 48 // https://github.com/rust-lang/rust/blob/7b805396bf46dce972692a6846ce2ad8481c5f85/src/librustc_typeck/check/coercion.rs#L877-L916
49 let sig = match (ty1.kind(&Interner), ty2.kind(&Interner)) { 49 let sig = match (ty1.kind(&Interner), ty2.kind(&Interner)) {
50 (TyKind::FnDef(..), TyKind::FnDef(..)) 50 (TyKind::FnDef(..) | TyKind::Closure(..), TyKind::FnDef(..) | TyKind::Closure(..)) => {
51 | (TyKind::Closure(..), TyKind::FnDef(..))
52 | (TyKind::FnDef(..), TyKind::Closure(..))
53 | (TyKind::Closure(..), TyKind::Closure(..)) => {
54 // FIXME: we're ignoring safety here. To be more correct, if we have one FnDef and one Closure, 51 // FIXME: we're ignoring safety here. To be more correct, if we have one FnDef and one Closure,
55 // we should be coercing the closure to a fn pointer of the safety of the FnDef 52 // we should be coercing the closure to a fn pointer of the safety of the FnDef
56 cov_mark::hit!(coerce_fn_reification); 53 cov_mark::hit!(coerce_fn_reification);
@@ -448,8 +445,7 @@ fn safe_to_unsafe_fn_ty(fn_ty: FnPointer) -> FnPointer {
448 445
449fn coerce_mutabilities(from: Mutability, to: Mutability) -> Result<(), TypeError> { 446fn coerce_mutabilities(from: Mutability, to: Mutability) -> Result<(), TypeError> {
450 match (from, to) { 447 match (from, to) {
451 (Mutability::Mut, Mutability::Mut) 448 (Mutability::Mut, Mutability::Mut | Mutability::Not)
452 | (Mutability::Mut, Mutability::Not)
453 | (Mutability::Not, Mutability::Not) => Ok(()), 449 | (Mutability::Not, Mutability::Not) => Ok(()),
454 (Mutability::Not, Mutability::Mut) => Err(TypeError), 450 (Mutability::Not, Mutability::Mut) => Err(TypeError),
455 } 451 }
diff --git a/crates/hir_ty/src/infer/expr.rs b/crates/hir_ty/src/infer/expr.rs
index 5ea2e5934..c3a5b979f 100644
--- a/crates/hir_ty/src/infer/expr.rs
+++ b/crates/hir_ty/src/infer/expr.rs
@@ -367,7 +367,7 @@ impl<'a> InferenceContext<'a> {
367 Expr::Path(p) => { 367 Expr::Path(p) => {
368 // FIXME this could be more efficient... 368 // FIXME this could be more efficient...
369 let resolver = resolver_for_expr(self.db.upcast(), self.owner, tgt_expr); 369 let resolver = resolver_for_expr(self.db.upcast(), self.owner, tgt_expr);
370 self.infer_path(&resolver, p, tgt_expr.into()).unwrap_or(self.err_ty()) 370 self.infer_path(&resolver, p, tgt_expr.into()).unwrap_or_else(|| self.err_ty())
371 } 371 }
372 Expr::Continue { .. } => TyKind::Never.intern(&Interner), 372 Expr::Continue { .. } => TyKind::Never.intern(&Interner),
373 Expr::Break { expr, label } => { 373 Expr::Break { expr, label } => {
@@ -511,7 +511,7 @@ impl<'a> InferenceContext<'a> {
511 _ => None, 511 _ => None,
512 } 512 }
513 }) 513 })
514 .unwrap_or(self.err_ty()); 514 .unwrap_or_else(|| self.err_ty());
515 let ty = self.insert_type_vars(ty); 515 let ty = self.insert_type_vars(ty);
516 self.normalize_associated_types_in(ty) 516 self.normalize_associated_types_in(ty)
517 } 517 }
@@ -593,11 +593,11 @@ impl<'a> InferenceContext<'a> {
593 UnaryOp::Neg => { 593 UnaryOp::Neg => {
594 match inner_ty.kind(&Interner) { 594 match inner_ty.kind(&Interner) {
595 // Fast path for builtins 595 // Fast path for builtins
596 TyKind::Scalar(Scalar::Int(_)) 596 TyKind::Scalar(Scalar::Int(_) | Scalar::Uint(_) | Scalar::Float(_))
597 | TyKind::Scalar(Scalar::Uint(_)) 597 | TyKind::InferenceVar(
598 | TyKind::Scalar(Scalar::Float(_)) 598 _,
599 | TyKind::InferenceVar(_, TyVariableKind::Integer) 599 TyVariableKind::Integer | TyVariableKind::Float,
600 | TyKind::InferenceVar(_, TyVariableKind::Float) => inner_ty, 600 ) => inner_ty,
601 // Otherwise we resolve via the std::ops::Neg trait 601 // Otherwise we resolve via the std::ops::Neg trait
602 _ => self 602 _ => self
603 .resolve_associated_type(inner_ty, self.resolve_ops_neg_output()), 603 .resolve_associated_type(inner_ty, self.resolve_ops_neg_output()),
@@ -606,9 +606,7 @@ impl<'a> InferenceContext<'a> {
606 UnaryOp::Not => { 606 UnaryOp::Not => {
607 match inner_ty.kind(&Interner) { 607 match inner_ty.kind(&Interner) {
608 // Fast path for builtins 608 // Fast path for builtins
609 TyKind::Scalar(Scalar::Bool) 609 TyKind::Scalar(Scalar::Bool | Scalar::Int(_) | Scalar::Uint(_))
610 | TyKind::Scalar(Scalar::Int(_))
611 | TyKind::Scalar(Scalar::Uint(_))
612 | TyKind::InferenceVar(_, TyVariableKind::Integer) => inner_ty, 610 | TyKind::InferenceVar(_, TyVariableKind::Integer) => inner_ty,
613 // Otherwise we resolve via the std::ops::Not trait 611 // Otherwise we resolve via the std::ops::Not trait
614 _ => self 612 _ => self
@@ -735,7 +733,7 @@ impl<'a> InferenceContext<'a> {
735 Expr::Array(array) => { 733 Expr::Array(array) => {
736 let elem_ty = 734 let elem_ty =
737 match expected.to_option(&mut self.table).as_ref().map(|t| t.kind(&Interner)) { 735 match expected.to_option(&mut self.table).as_ref().map(|t| t.kind(&Interner)) {
738 Some(TyKind::Array(st, _)) | Some(TyKind::Slice(st)) => st.clone(), 736 Some(TyKind::Array(st, _) | TyKind::Slice(st)) => st.clone(),
739 _ => self.table.new_type_var(), 737 _ => self.table.new_type_var(),
740 }; 738 };
741 739
@@ -820,8 +818,10 @@ impl<'a> InferenceContext<'a> {
820 for stmt in statements { 818 for stmt in statements {
821 match stmt { 819 match stmt {
822 Statement::Let { pat, type_ref, initializer } => { 820 Statement::Let { pat, type_ref, initializer } => {
823 let decl_ty = 821 let decl_ty = type_ref
824 type_ref.as_ref().map(|tr| self.make_ty(tr)).unwrap_or(self.err_ty()); 822 .as_ref()
823 .map(|tr| self.make_ty(tr))
824 .unwrap_or_else(|| self.err_ty());
825 825
826 // Always use the declared type when specified 826 // Always use the declared type when specified
827 let mut ty = decl_ty.clone(); 827 let mut ty = decl_ty.clone();
diff --git a/crates/hir_ty/src/infer/pat.rs b/crates/hir_ty/src/infer/pat.rs
index 035f4ded6..c79ed91ea 100644
--- a/crates/hir_ty/src/infer/pat.rs
+++ b/crates/hir_ty/src/infer/pat.rs
@@ -192,7 +192,7 @@ impl<'a> InferenceContext<'a> {
192 Pat::Path(path) => { 192 Pat::Path(path) => {
193 // FIXME use correct resolver for the surrounding expression 193 // FIXME use correct resolver for the surrounding expression
194 let resolver = self.resolver.clone(); 194 let resolver = self.resolver.clone();
195 self.infer_path(&resolver, path, pat.into()).unwrap_or(self.err_ty()) 195 self.infer_path(&resolver, path, pat.into()).unwrap_or_else(|| self.err_ty())
196 } 196 }
197 Pat::Bind { mode, name: _, subpat } => { 197 Pat::Bind { mode, name: _, subpat } => {
198 let mode = if mode == &BindingAnnotation::Unannotated { 198 let mode = if mode == &BindingAnnotation::Unannotated {
@@ -297,10 +297,11 @@ fn is_non_ref_pat(body: &hir_def::body::Body, pat: PatId) -> bool {
297 Expr::Literal(Literal::String(..)) => false, 297 Expr::Literal(Literal::String(..)) => false,
298 _ => true, 298 _ => true,
299 }, 299 },
300 Pat::Bind { mode: BindingAnnotation::Mutable, subpat: Some(subpat), .. } 300 Pat::Bind {
301 | Pat::Bind { mode: BindingAnnotation::Unannotated, subpat: Some(subpat), .. } => { 301 mode: BindingAnnotation::Mutable | BindingAnnotation::Unannotated,
302 is_non_ref_pat(body, *subpat) 302 subpat: Some(subpat),
303 } 303 ..
304 } => is_non_ref_pat(body, *subpat),
304 Pat::Wild | Pat::Bind { .. } | Pat::Ref { .. } | Pat::Box { .. } | Pat::Missing => false, 305 Pat::Wild | Pat::Bind { .. } | Pat::Ref { .. } | Pat::Box { .. } | Pat::Missing => false,
305 } 306 }
306} 307}