aboutsummaryrefslogtreecommitdiff
path: root/crates/hir_ty/src/infer
diff options
context:
space:
mode:
authorLukas Wirth <[email protected]>2021-04-05 21:08:16 +0100
committerLukas Wirth <[email protected]>2021-04-06 09:45:30 +0100
commit96756f1b1df4729fd00ca96a59971b3997c91934 (patch)
tree1daa47d67081772ed1a61b0db13ff041337ddf8b /crates/hir_ty/src/infer
parent4bc8a018302d53951ae855ba57d07095a16ef182 (diff)
Add Lifetime to TyKind::Ref
Diffstat (limited to 'crates/hir_ty/src/infer')
-rw-r--r--crates/hir_ty/src/infer/coerce.rs6
-rw-r--r--crates/hir_ty/src/infer/expr.rs22
-rw-r--r--crates/hir_ty/src/infer/pat.rs20
-rw-r--r--crates/hir_ty/src/infer/unify.rs2
4 files changed, 32 insertions, 18 deletions
diff --git a/crates/hir_ty/src/infer/coerce.rs b/crates/hir_ty/src/infer/coerce.rs
index 32c273afc..d6c48870a 100644
--- a/crates/hir_ty/src/infer/coerce.rs
+++ b/crates/hir_ty/src/infer/coerce.rs
@@ -81,7 +81,7 @@ impl<'a> InferenceContext<'a> {
81 // `&T` -> `*const T` 81 // `&T` -> `*const T`
82 // `&mut T` -> `*mut T`/`*const T` 82 // `&mut T` -> `*mut T`/`*const T`
83 (TyKind::Ref(.., substs), &TyKind::Raw(m2 @ Mutability::Not, ..)) 83 (TyKind::Ref(.., substs), &TyKind::Raw(m2 @ Mutability::Not, ..))
84 | (TyKind::Ref(Mutability::Mut, substs), &TyKind::Raw(m2, ..)) => { 84 | (TyKind::Ref(Mutability::Mut, _, substs), &TyKind::Raw(m2, ..)) => {
85 from_ty = TyKind::Raw(m2, substs.clone()).intern(&Interner); 85 from_ty = TyKind::Raw(m2, substs.clone()).intern(&Interner);
86 } 86 }
87 87
@@ -111,7 +111,9 @@ impl<'a> InferenceContext<'a> {
111 // Auto Deref if cannot coerce 111 // Auto Deref if cannot coerce
112 match (from_ty.kind(&Interner), to_ty.kind(&Interner)) { 112 match (from_ty.kind(&Interner), to_ty.kind(&Interner)) {
113 // FIXME: DerefMut 113 // FIXME: DerefMut
114 (TyKind::Ref(_, st1), TyKind::Ref(_, st2)) => self.unify_autoderef_behind_ref(st1, st2), 114 (TyKind::Ref(.., st1), TyKind::Ref(.., st2)) => {
115 self.unify_autoderef_behind_ref(st1, st2)
116 }
115 117
116 // Otherwise, normal unify 118 // Otherwise, normal unify
117 _ => self.unify(&from_ty, to_ty), 119 _ => self.unify(&from_ty, to_ty),
diff --git a/crates/hir_ty/src/infer/expr.rs b/crates/hir_ty/src/infer/expr.rs
index ff564106b..77fb36332 100644
--- a/crates/hir_ty/src/infer/expr.rs
+++ b/crates/hir_ty/src/infer/expr.rs
@@ -23,7 +23,7 @@ use crate::{
23 traits::{chalk::from_chalk, FnTrait}, 23 traits::{chalk::from_chalk, FnTrait},
24 utils::{generics, variant_data, Generics}, 24 utils::{generics, variant_data, Generics},
25 AdtId, Binders, CallableDefId, FnPointer, FnSig, FnSubst, InEnvironment, Interner, 25 AdtId, Binders, CallableDefId, FnPointer, FnSig, FnSubst, InEnvironment, Interner,
26 ProjectionTyExt, Rawness, Scalar, Substitution, TraitRef, Ty, TyBuilder, TyKind, TypeWalk, 26 LifetimeData, ProjectionTyExt, Rawness, Scalar, Substitution, TraitRef, Ty, TyBuilder, TyKind,
27}; 27};
28 28
29use super::{ 29use super::{
@@ -527,7 +527,9 @@ impl<'a> InferenceContext<'a> {
527 let inner_ty = self.infer_expr_inner(*expr, &expectation); 527 let inner_ty = self.infer_expr_inner(*expr, &expectation);
528 match rawness { 528 match rawness {
529 Rawness::RawPtr => TyKind::Raw(mutability, inner_ty), 529 Rawness::RawPtr => TyKind::Raw(mutability, inner_ty),
530 Rawness::Ref => TyKind::Ref(mutability, inner_ty), 530 Rawness::Ref => {
531 TyKind::Ref(mutability, LifetimeData::Static.intern(&Interner), inner_ty)
532 }
531 } 533 }
532 .intern(&Interner) 534 .intern(&Interner)
533 } 535 }
@@ -730,13 +732,17 @@ impl<'a> InferenceContext<'a> {
730 } 732 }
731 Expr::Literal(lit) => match lit { 733 Expr::Literal(lit) => match lit {
732 Literal::Bool(..) => TyKind::Scalar(Scalar::Bool).intern(&Interner), 734 Literal::Bool(..) => TyKind::Scalar(Scalar::Bool).intern(&Interner),
733 Literal::String(..) => { 735 Literal::String(..) => TyKind::Ref(
734 TyKind::Ref(Mutability::Not, TyKind::Str.intern(&Interner)).intern(&Interner) 736 Mutability::Not,
735 } 737 LifetimeData::Static.intern(&Interner),
738 TyKind::Str.intern(&Interner),
739 )
740 .intern(&Interner),
736 Literal::ByteString(..) => { 741 Literal::ByteString(..) => {
737 let byte_type = TyKind::Scalar(Scalar::Uint(UintTy::U8)).intern(&Interner); 742 let byte_type = TyKind::Scalar(Scalar::Uint(UintTy::U8)).intern(&Interner);
738 let array_type = TyKind::Array(byte_type).intern(&Interner); 743 let array_type = TyKind::Array(byte_type).intern(&Interner);
739 TyKind::Ref(Mutability::Not, array_type).intern(&Interner) 744 TyKind::Ref(Mutability::Not, LifetimeData::Static.intern(&Interner), array_type)
745 .intern(&Interner)
740 } 746 }
741 Literal::Char(..) => TyKind::Scalar(Scalar::Char).intern(&Interner), 747 Literal::Char(..) => TyKind::Scalar(Scalar::Char).intern(&Interner),
742 Literal::Int(_v, ty) => match ty { 748 Literal::Int(_v, ty) => match ty {
@@ -872,7 +878,9 @@ impl<'a> InferenceContext<'a> {
872 // Apply autoref so the below unification works correctly 878 // Apply autoref so the below unification works correctly
873 // FIXME: return correct autorefs from lookup_method 879 // FIXME: return correct autorefs from lookup_method
874 let actual_receiver_ty = match expected_receiver_ty.as_reference() { 880 let actual_receiver_ty = match expected_receiver_ty.as_reference() {
875 Some((_, mutability)) => TyKind::Ref(mutability, derefed_receiver_ty).intern(&Interner), 881 Some((_, lifetime, mutability)) => {
882 TyKind::Ref(mutability, lifetime, derefed_receiver_ty).intern(&Interner)
883 }
876 _ => derefed_receiver_ty, 884 _ => derefed_receiver_ty,
877 }; 885 };
878 self.unify(&expected_receiver_ty, &actual_receiver_ty); 886 self.unify(&expected_receiver_ty, &actual_receiver_ty);
diff --git a/crates/hir_ty/src/infer/pat.rs b/crates/hir_ty/src/infer/pat.rs
index 252ae914a..b5e97cc8c 100644
--- a/crates/hir_ty/src/infer/pat.rs
+++ b/crates/hir_ty/src/infer/pat.rs
@@ -13,8 +13,8 @@ use hir_expand::name::Name;
13 13
14use super::{BindingMode, Expectation, InferenceContext}; 14use super::{BindingMode, Expectation, InferenceContext};
15use crate::{ 15use crate::{
16 lower::lower_to_chalk_mutability, utils::variant_data, Interner, Substitution, Ty, TyBuilder, 16 lower::lower_to_chalk_mutability, utils::variant_data, Interner, LifetimeData, Substitution,
17 TyKind, 17 Ty, TyBuilder, TyKind,
18}; 18};
19 19
20impl<'a> InferenceContext<'a> { 20impl<'a> InferenceContext<'a> {
@@ -104,7 +104,7 @@ impl<'a> InferenceContext<'a> {
104 let body = Arc::clone(&self.body); // avoid borrow checker problem 104 let body = Arc::clone(&self.body); // avoid borrow checker problem
105 105
106 if is_non_ref_pat(&body, pat) { 106 if is_non_ref_pat(&body, pat) {
107 while let Some((inner, mutability)) = expected.as_reference() { 107 while let Some((inner, _lifetime, mutability)) = expected.as_reference() {
108 expected = inner; 108 expected = inner;
109 default_bm = match default_bm { 109 default_bm = match default_bm {
110 BindingMode::Move => BindingMode::Ref(mutability), 110 BindingMode::Move => BindingMode::Ref(mutability),
@@ -162,7 +162,7 @@ impl<'a> InferenceContext<'a> {
162 Pat::Ref { pat, mutability } => { 162 Pat::Ref { pat, mutability } => {
163 let mutability = lower_to_chalk_mutability(*mutability); 163 let mutability = lower_to_chalk_mutability(*mutability);
164 let expectation = match expected.as_reference() { 164 let expectation = match expected.as_reference() {
165 Some((inner_ty, exp_mut)) => { 165 Some((inner_ty, _lifetime, exp_mut)) => {
166 if mutability != exp_mut { 166 if mutability != exp_mut {
167 // FIXME: emit type error? 167 // FIXME: emit type error?
168 } 168 }
@@ -171,7 +171,8 @@ impl<'a> InferenceContext<'a> {
171 _ => self.result.standard_types.unknown.clone(), 171 _ => self.result.standard_types.unknown.clone(),
172 }; 172 };
173 let subty = self.infer_pat(*pat, &expectation, default_bm); 173 let subty = self.infer_pat(*pat, &expectation, default_bm);
174 TyKind::Ref(mutability, subty).intern(&Interner) 174 TyKind::Ref(mutability, LifetimeData::Static.intern(&Interner), subty)
175 .intern(&Interner)
175 } 176 }
176 Pat::TupleStruct { path: p, args: subpats, ellipsis } => self.infer_tuple_struct_pat( 177 Pat::TupleStruct { path: p, args: subpats, ellipsis } => self.infer_tuple_struct_pat(
177 p.as_ref(), 178 p.as_ref(),
@@ -203,9 +204,12 @@ impl<'a> InferenceContext<'a> {
203 let inner_ty = self.insert_type_vars_shallow(inner_ty); 204 let inner_ty = self.insert_type_vars_shallow(inner_ty);
204 205
205 let bound_ty = match mode { 206 let bound_ty = match mode {
206 BindingMode::Ref(mutability) => { 207 BindingMode::Ref(mutability) => TyKind::Ref(
207 TyKind::Ref(mutability, inner_ty.clone()).intern(&Interner) 208 mutability,
208 } 209 LifetimeData::Static.intern(&Interner),
210 inner_ty.clone(),
211 )
212 .intern(&Interner),
209 BindingMode::Move => inner_ty.clone(), 213 BindingMode::Move => inner_ty.clone(),
210 }; 214 };
211 let bound_ty = self.resolve_ty_as_possible(bound_ty); 215 let bound_ty = self.resolve_ty_as_possible(bound_ty);
diff --git a/crates/hir_ty/src/infer/unify.rs b/crates/hir_ty/src/infer/unify.rs
index 2f9523325..c7878ebfd 100644
--- a/crates/hir_ty/src/infer/unify.rs
+++ b/crates/hir_ty/src/infer/unify.rs
@@ -317,7 +317,7 @@ impl InferenceTable {
317 | (TyKind::Closure(.., substs1), TyKind::Closure(.., substs2)) => { 317 | (TyKind::Closure(.., substs1), TyKind::Closure(.., substs2)) => {
318 self.unify_substs(substs1, substs2, depth + 1) 318 self.unify_substs(substs1, substs2, depth + 1)
319 } 319 }
320 (TyKind::Ref(_, ty1), TyKind::Ref(_, ty2)) 320 (TyKind::Ref(_, _, ty1), TyKind::Ref(_, _, ty2))
321 | (TyKind::Raw(_, ty1), TyKind::Raw(_, ty2)) 321 | (TyKind::Raw(_, ty1), TyKind::Raw(_, ty2))
322 | (TyKind::Array(ty1), TyKind::Array(ty2)) 322 | (TyKind::Array(ty1), TyKind::Array(ty2))
323 | (TyKind::Slice(ty1), TyKind::Slice(ty2)) => self.unify_inner(ty1, ty2, depth + 1), 323 | (TyKind::Slice(ty1), TyKind::Slice(ty2)) => self.unify_inner(ty1, ty2, depth + 1),