aboutsummaryrefslogtreecommitdiff
path: root/crates/hir_ty/src
diff options
context:
space:
mode:
Diffstat (limited to 'crates/hir_ty/src')
-rw-r--r--crates/hir_ty/src/consteval.rs3
-rw-r--r--crates/hir_ty/src/diagnostics/match_check/deconstruct_pat.rs9
-rw-r--r--crates/hir_ty/src/infer/coerce.rs8
-rw-r--r--crates/hir_ty/src/infer/expr.rs16
-rw-r--r--crates/hir_ty/src/infer/pat.rs9
-rw-r--r--crates/hir_ty/src/op.rs38
6 files changed, 33 insertions, 50 deletions
diff --git a/crates/hir_ty/src/consteval.rs b/crates/hir_ty/src/consteval.rs
index 6f0bf8f8c..ab1afce08 100644
--- a/crates/hir_ty/src/consteval.rs
+++ b/crates/hir_ty/src/consteval.rs
@@ -38,8 +38,7 @@ impl ConstExt for Const {
38// FIXME: support more than just evaluating literals 38// FIXME: support more than just evaluating literals
39pub fn eval_usize(expr: &Expr) -> Option<u64> { 39pub fn eval_usize(expr: &Expr) -> Option<u64> {
40 match expr { 40 match expr {
41 Expr::Literal(Literal::Uint(v, None)) 41 Expr::Literal(Literal::Uint(v, None | Some(BuiltinUint::Usize))) => (*v).try_into().ok(),
42 | Expr::Literal(Literal::Uint(v, Some(BuiltinUint::Usize))) => (*v).try_into().ok(),
43 _ => None, 42 _ => None,
44 } 43 }
45} 44}
diff --git a/crates/hir_ty/src/diagnostics/match_check/deconstruct_pat.rs b/crates/hir_ty/src/diagnostics/match_check/deconstruct_pat.rs
index 471cd4921..e3d640a79 100644
--- a/crates/hir_ty/src/diagnostics/match_check/deconstruct_pat.rs
+++ b/crates/hir_ty/src/diagnostics/match_check/deconstruct_pat.rs
@@ -84,10 +84,7 @@ impl IntRange {
84 #[inline] 84 #[inline]
85 fn is_integral(ty: &Ty) -> bool { 85 fn is_integral(ty: &Ty) -> bool {
86 match ty.kind(&Interner) { 86 match ty.kind(&Interner) {
87 TyKind::Scalar(Scalar::Char) 87 TyKind::Scalar(Scalar::Char | Scalar::Int(_) | Scalar::Uint(_) | Scalar::Bool) => true,
88 | TyKind::Scalar(Scalar::Int(_))
89 | TyKind::Scalar(Scalar::Uint(_))
90 | TyKind::Scalar(Scalar::Bool) => true,
91 _ => false, 88 _ => false,
92 } 89 }
93 } 90 }
@@ -381,7 +378,7 @@ impl Constructor {
381 // Wildcards cover anything 378 // Wildcards cover anything
382 (_, Wildcard) => true, 379 (_, Wildcard) => true,
383 // The missing ctors are not covered by anything in the matrix except wildcards. 380 // The missing ctors are not covered by anything in the matrix except wildcards.
384 (Missing, _) | (Wildcard, _) => false, 381 (Missing | Wildcard, _) => false,
385 382
386 (Single, Single) => true, 383 (Single, Single) => true,
387 (Variant(self_id), Variant(other_id)) => self_id == other_id, 384 (Variant(self_id), Variant(other_id)) => self_id == other_id,
@@ -523,7 +520,7 @@ impl SplitWildcard {
523 } 520 }
524 } 521 }
525 TyKind::Scalar(Scalar::Char) => unhandled(), 522 TyKind::Scalar(Scalar::Char) => unhandled(),
526 TyKind::Scalar(Scalar::Int(..)) | TyKind::Scalar(Scalar::Uint(..)) => unhandled(), 523 TyKind::Scalar(Scalar::Int(..) | Scalar::Uint(..)) => unhandled(),
527 TyKind::Never if !cx.feature_exhaustive_patterns() && !pcx.is_top_level => { 524 TyKind::Never if !cx.feature_exhaustive_patterns() && !pcx.is_top_level => {
528 smallvec![NonExhaustive] 525 smallvec![NonExhaustive]
529 } 526 }
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..4e4f6e5a4 100644
--- a/crates/hir_ty/src/infer/expr.rs
+++ b/crates/hir_ty/src/infer/expr.rs
@@ -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
diff --git a/crates/hir_ty/src/infer/pat.rs b/crates/hir_ty/src/infer/pat.rs
index 035f4ded6..58cb23e9d 100644
--- a/crates/hir_ty/src/infer/pat.rs
+++ b/crates/hir_ty/src/infer/pat.rs
@@ -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}
diff --git a/crates/hir_ty/src/op.rs b/crates/hir_ty/src/op.rs
index 0222de2bc..5ef6342d5 100644
--- a/crates/hir_ty/src/op.rs
+++ b/crates/hir_ty/src/op.rs
@@ -8,17 +8,15 @@ pub(super) fn binary_op_return_ty(op: BinaryOp, lhs_ty: Ty, rhs_ty: Ty) -> Ty {
8 match op { 8 match op {
9 BinaryOp::LogicOp(_) | BinaryOp::CmpOp(_) => TyKind::Scalar(Scalar::Bool).intern(&Interner), 9 BinaryOp::LogicOp(_) | BinaryOp::CmpOp(_) => TyKind::Scalar(Scalar::Bool).intern(&Interner),
10 BinaryOp::Assignment { .. } => TyBuilder::unit(), 10 BinaryOp::Assignment { .. } => TyBuilder::unit(),
11 BinaryOp::ArithOp(ArithOp::Shl) | BinaryOp::ArithOp(ArithOp::Shr) => { 11 BinaryOp::ArithOp(ArithOp::Shl | ArithOp::Shr) => {
12 // all integer combinations are valid here 12 // all integer combinations are valid here
13 if matches!( 13 if matches!(
14 lhs_ty.kind(&Interner), 14 lhs_ty.kind(&Interner),
15 TyKind::Scalar(Scalar::Int(_)) 15 TyKind::Scalar(Scalar::Int(_) | Scalar::Uint(_))
16 | TyKind::Scalar(Scalar::Uint(_))
17 | TyKind::InferenceVar(_, TyVariableKind::Integer) 16 | TyKind::InferenceVar(_, TyVariableKind::Integer)
18 ) && matches!( 17 ) && matches!(
19 rhs_ty.kind(&Interner), 18 rhs_ty.kind(&Interner),
20 TyKind::Scalar(Scalar::Int(_)) 19 TyKind::Scalar(Scalar::Int(_) | Scalar::Uint(_))
21 | TyKind::Scalar(Scalar::Uint(_))
22 | TyKind::InferenceVar(_, TyVariableKind::Integer) 20 | TyKind::InferenceVar(_, TyVariableKind::Integer)
23 ) { 21 ) {
24 lhs_ty 22 lhs_ty
@@ -32,15 +30,15 @@ pub(super) fn binary_op_return_ty(op: BinaryOp, lhs_ty: Ty, rhs_ty: Ty) -> Ty {
32 | (TyKind::Scalar(Scalar::Uint(_)), TyKind::Scalar(Scalar::Uint(_))) 30 | (TyKind::Scalar(Scalar::Uint(_)), TyKind::Scalar(Scalar::Uint(_)))
33 | (TyKind::Scalar(Scalar::Float(_)), TyKind::Scalar(Scalar::Float(_))) => rhs_ty, 31 | (TyKind::Scalar(Scalar::Float(_)), TyKind::Scalar(Scalar::Float(_))) => rhs_ty,
34 // ({int}, int) | ({int}, uint) 32 // ({int}, int) | ({int}, uint)
35 (TyKind::InferenceVar(_, TyVariableKind::Integer), TyKind::Scalar(Scalar::Int(_))) 33 (
36 | (TyKind::InferenceVar(_, TyVariableKind::Integer), TyKind::Scalar(Scalar::Uint(_))) => { 34 TyKind::InferenceVar(_, TyVariableKind::Integer),
37 rhs_ty 35 TyKind::Scalar(Scalar::Int(_) | Scalar::Uint(_)),
38 } 36 ) => rhs_ty,
39 // (int, {int}) | (uint, {int}) 37 // (int, {int}) | (uint, {int})
40 (TyKind::Scalar(Scalar::Int(_)), TyKind::InferenceVar(_, TyVariableKind::Integer)) 38 (
41 | (TyKind::Scalar(Scalar::Uint(_)), TyKind::InferenceVar(_, TyVariableKind::Integer)) => { 39 TyKind::Scalar(Scalar::Int(_) | Scalar::Uint(_)),
42 lhs_ty 40 TyKind::InferenceVar(_, TyVariableKind::Integer),
43 } 41 ) => lhs_ty,
44 // ({float} | float) 42 // ({float} | float)
45 (TyKind::InferenceVar(_, TyVariableKind::Float), TyKind::Scalar(Scalar::Float(_))) => { 43 (TyKind::InferenceVar(_, TyVariableKind::Float), TyKind::Scalar(Scalar::Float(_))) => {
46 rhs_ty 44 rhs_ty
@@ -69,21 +67,15 @@ pub(super) fn binary_op_rhs_expectation(op: BinaryOp, lhs_ty: Ty) -> Ty {
69 BinaryOp::Assignment { op: None } => lhs_ty, 67 BinaryOp::Assignment { op: None } => lhs_ty,
70 BinaryOp::CmpOp(CmpOp::Eq { .. }) => match lhs_ty.kind(&Interner) { 68 BinaryOp::CmpOp(CmpOp::Eq { .. }) => match lhs_ty.kind(&Interner) {
71 TyKind::Scalar(_) | TyKind::Str => lhs_ty, 69 TyKind::Scalar(_) | TyKind::Str => lhs_ty,
72 TyKind::InferenceVar(_, TyVariableKind::Integer) 70 TyKind::InferenceVar(_, TyVariableKind::Integer | TyVariableKind::Float) => lhs_ty,
73 | TyKind::InferenceVar(_, TyVariableKind::Float) => lhs_ty,
74 _ => TyKind::Error.intern(&Interner), 71 _ => TyKind::Error.intern(&Interner),
75 }, 72 },
76 BinaryOp::ArithOp(ArithOp::Shl) | BinaryOp::ArithOp(ArithOp::Shr) => { 73 BinaryOp::ArithOp(ArithOp::Shl | ArithOp::Shr) => TyKind::Error.intern(&Interner),
77 TyKind::Error.intern(&Interner)
78 }
79 BinaryOp::CmpOp(CmpOp::Ord { .. }) 74 BinaryOp::CmpOp(CmpOp::Ord { .. })
80 | BinaryOp::Assignment { op: Some(_) } 75 | BinaryOp::Assignment { op: Some(_) }
81 | BinaryOp::ArithOp(_) => match lhs_ty.kind(&Interner) { 76 | BinaryOp::ArithOp(_) => match lhs_ty.kind(&Interner) {
82 TyKind::Scalar(Scalar::Int(_)) 77 TyKind::Scalar(Scalar::Int(_) | Scalar::Uint(_) | Scalar::Float(_)) => lhs_ty,
83 | TyKind::Scalar(Scalar::Uint(_)) 78 TyKind::InferenceVar(_, TyVariableKind::Integer | TyVariableKind::Float) => lhs_ty,
84 | TyKind::Scalar(Scalar::Float(_)) => lhs_ty,
85 TyKind::InferenceVar(_, TyVariableKind::Integer)
86 | TyKind::InferenceVar(_, TyVariableKind::Float) => lhs_ty,
87 _ => TyKind::Error.intern(&Interner), 79 _ => TyKind::Error.intern(&Interner),
88 }, 80 },
89 } 81 }