aboutsummaryrefslogtreecommitdiff
path: root/crates/ra_hir_ty
diff options
context:
space:
mode:
Diffstat (limited to 'crates/ra_hir_ty')
-rw-r--r--crates/ra_hir_ty/Cargo.toml5
-rw-r--r--crates/ra_hir_ty/src/db.rs5
-rw-r--r--crates/ra_hir_ty/src/diagnostics.rs4
-rw-r--r--crates/ra_hir_ty/src/infer.rs11
-rw-r--r--crates/ra_hir_ty/src/infer/coerce.rs4
-rw-r--r--crates/ra_hir_ty/src/infer/expr.rs98
-rw-r--r--crates/ra_hir_ty/src/lib.rs24
-rw-r--r--crates/ra_hir_ty/src/tests/coercion.rs17
-rw-r--r--crates/ra_hir_ty/src/tests/patterns.rs50
-rw-r--r--crates/ra_hir_ty/src/tests/simple.rs76
-rw-r--r--crates/ra_hir_ty/src/tests/traits.rs118
-rw-r--r--crates/ra_hir_ty/src/traits/builtin.rs3
-rw-r--r--crates/ra_hir_ty/src/traits/chalk.rs87
-rw-r--r--crates/ra_hir_ty/src/traits/chalk/interner.rs14
-rw-r--r--crates/ra_hir_ty/src/traits/chalk/mapping.rs93
-rw-r--r--crates/ra_hir_ty/src/traits/chalk/tls.rs18
16 files changed, 505 insertions, 122 deletions
diff --git a/crates/ra_hir_ty/Cargo.toml b/crates/ra_hir_ty/Cargo.toml
index b2de7fa34..4b8dcdc07 100644
--- a/crates/ra_hir_ty/Cargo.toml
+++ b/crates/ra_hir_ty/Cargo.toml
@@ -27,9 +27,8 @@ test_utils = { path = "../test_utils" }
27 27
28scoped-tls = "1" 28scoped-tls = "1"
29 29
30chalk-solve = { git = "https://github.com/rust-lang/chalk.git", rev = "eaab84b394007d1bed15f5470409a6ea02900a96" } 30chalk-solve = { git = "https://github.com/rust-lang/chalk.git", rev = "329b7f3fdd2431ed6f6778cde53f22374c7d094c" }
31chalk-rust-ir = { git = "https://github.com/rust-lang/chalk.git", rev = "eaab84b394007d1bed15f5470409a6ea02900a96" } 31chalk-ir = { git = "https://github.com/rust-lang/chalk.git", rev = "329b7f3fdd2431ed6f6778cde53f22374c7d094c" }
32chalk-ir = { git = "https://github.com/rust-lang/chalk.git", rev = "eaab84b394007d1bed15f5470409a6ea02900a96" }
33 32
34[dev-dependencies] 33[dev-dependencies]
35insta = "0.16.0" 34insta = "0.16.0"
diff --git a/crates/ra_hir_ty/src/db.rs b/crates/ra_hir_ty/src/db.rs
index dfc6c7dd6..0a8bb24ac 100644
--- a/crates/ra_hir_ty/src/db.rs
+++ b/crates/ra_hir_ty/src/db.rs
@@ -76,6 +76,8 @@ pub trait HirDatabase: DefDatabase + Upcast<dyn DefDatabase> {
76 #[salsa::interned] 76 #[salsa::interned]
77 fn intern_type_ctor(&self, type_ctor: TypeCtor) -> crate::TypeCtorId; 77 fn intern_type_ctor(&self, type_ctor: TypeCtor) -> crate::TypeCtorId;
78 #[salsa::interned] 78 #[salsa::interned]
79 fn intern_callable_def(&self, callable_def: CallableDef) -> crate::CallableDefId;
80 #[salsa::interned]
79 fn intern_type_param_id(&self, param_id: TypeParamId) -> GlobalTypeParamId; 81 fn intern_type_param_id(&self, param_id: TypeParamId) -> GlobalTypeParamId;
80 #[salsa::interned] 82 #[salsa::interned]
81 fn intern_chalk_impl(&self, impl_: Impl) -> crate::traits::GlobalImplId; 83 fn intern_chalk_impl(&self, impl_: Impl) -> crate::traits::GlobalImplId;
@@ -94,6 +96,9 @@ pub trait HirDatabase: DefDatabase + Upcast<dyn DefDatabase> {
94 #[salsa::invoke(crate::traits::chalk::impl_datum_query)] 96 #[salsa::invoke(crate::traits::chalk::impl_datum_query)]
95 fn impl_datum(&self, krate: CrateId, impl_id: chalk::ImplId) -> Arc<chalk::ImplDatum>; 97 fn impl_datum(&self, krate: CrateId, impl_id: chalk::ImplId) -> Arc<chalk::ImplDatum>;
96 98
99 #[salsa::invoke(crate::traits::chalk::fn_def_datum_query)]
100 fn fn_def_datum(&self, krate: CrateId, fn_def_id: chalk::FnDefId) -> Arc<chalk::FnDefDatum>;
101
97 #[salsa::invoke(crate::traits::chalk::associated_ty_value_query)] 102 #[salsa::invoke(crate::traits::chalk::associated_ty_value_query)]
98 fn associated_ty_value( 103 fn associated_ty_value(
99 &self, 104 &self,
diff --git a/crates/ra_hir_ty/src/diagnostics.rs b/crates/ra_hir_ty/src/diagnostics.rs
index 41ac70272..2c7298714 100644
--- a/crates/ra_hir_ty/src/diagnostics.rs
+++ b/crates/ra_hir_ty/src/diagnostics.rs
@@ -40,7 +40,7 @@ impl Diagnostic for MissingFields {
40 fn message(&self) -> String { 40 fn message(&self) -> String {
41 let mut buf = String::from("Missing structure fields:\n"); 41 let mut buf = String::from("Missing structure fields:\n");
42 for field in &self.missed_fields { 42 for field in &self.missed_fields {
43 format_to!(buf, "- {}", field); 43 format_to!(buf, "- {}\n", field);
44 } 44 }
45 buf 45 buf
46 } 46 }
@@ -73,7 +73,7 @@ impl Diagnostic for MissingPatFields {
73 fn message(&self) -> String { 73 fn message(&self) -> String {
74 let mut buf = String::from("Missing structure fields:\n"); 74 let mut buf = String::from("Missing structure fields:\n");
75 for field in &self.missed_fields { 75 for field in &self.missed_fields {
76 format_to!(buf, "- {}", field); 76 format_to!(buf, "- {}\n", field);
77 } 77 }
78 buf 78 buf
79 } 79 }
diff --git a/crates/ra_hir_ty/src/infer.rs b/crates/ra_hir_ty/src/infer.rs
index 957d6e0b5..dc77e88e5 100644
--- a/crates/ra_hir_ty/src/infer.rs
+++ b/crates/ra_hir_ty/src/infer.rs
@@ -219,6 +219,17 @@ struct InferenceContext<'a> {
219struct BreakableContext { 219struct BreakableContext {
220 pub may_break: bool, 220 pub may_break: bool,
221 pub break_ty: Ty, 221 pub break_ty: Ty,
222 pub label: Option<name::Name>,
223}
224
225fn find_breakable<'c>(
226 ctxs: &'c mut [BreakableContext],
227 label: Option<&name::Name>,
228) -> Option<&'c mut BreakableContext> {
229 match label {
230 Some(_) => ctxs.iter_mut().rev().find(|ctx| ctx.label.as_ref() == label),
231 None => ctxs.last_mut(),
232 }
222} 233}
223 234
224impl<'a> InferenceContext<'a> { 235impl<'a> InferenceContext<'a> {
diff --git a/crates/ra_hir_ty/src/infer/coerce.rs b/crates/ra_hir_ty/src/infer/coerce.rs
index 2ee9adb16..32c7c57cd 100644
--- a/crates/ra_hir_ty/src/infer/coerce.rs
+++ b/crates/ra_hir_ty/src/infer/coerce.rs
@@ -45,9 +45,7 @@ impl<'a> InferenceContext<'a> {
45 self.coerce_merge_branch(&ptr_ty1, &ptr_ty2) 45 self.coerce_merge_branch(&ptr_ty1, &ptr_ty2)
46 } else { 46 } else {
47 mark::hit!(coerce_merge_fail_fallback); 47 mark::hit!(coerce_merge_fail_fallback);
48 // For incompatible types, we use the latter one as result 48 ty1.clone()
49 // to be better recovery for `if` without `else`.
50 ty2.clone()
51 } 49 }
52 } 50 }
53 } 51 }
diff --git a/crates/ra_hir_ty/src/infer/expr.rs b/crates/ra_hir_ty/src/infer/expr.rs
index b28724f0e..4a98e2deb 100644
--- a/crates/ra_hir_ty/src/infer/expr.rs
+++ b/crates/ra_hir_ty/src/infer/expr.rs
@@ -17,13 +17,13 @@ use crate::{
17 autoderef, method_resolution, op, 17 autoderef, method_resolution, op,
18 traits::InEnvironment, 18 traits::InEnvironment,
19 utils::{generics, variant_data, Generics}, 19 utils::{generics, variant_data, Generics},
20 ApplicationTy, Binders, CallableDef, InferTy, IntTy, Mutability, Obligation, Substs, TraitRef, 20 ApplicationTy, Binders, CallableDef, InferTy, IntTy, Mutability, Obligation, Rawness, Substs,
21 Ty, TypeCtor, Uncertain, 21 TraitRef, Ty, TypeCtor, Uncertain,
22}; 22};
23 23
24use super::{ 24use super::{
25 BindingMode, BreakableContext, Diverges, Expectation, InferenceContext, InferenceDiagnostic, 25 find_breakable, BindingMode, BreakableContext, Diverges, Expectation, InferenceContext,
26 TypeMismatch, 26 InferenceDiagnostic, TypeMismatch,
27}; 27};
28 28
29impl<'a> InferenceContext<'a> { 29impl<'a> InferenceContext<'a> {
@@ -86,16 +86,20 @@ impl<'a> InferenceContext<'a> {
86 86
87 self.coerce_merge_branch(&then_ty, &else_ty) 87 self.coerce_merge_branch(&then_ty, &else_ty)
88 } 88 }
89 Expr::Block { statements, tail } => self.infer_block(statements, *tail, expected), 89 Expr::Block { statements, tail, .. } => {
90 // FIXME: Breakable block inference
91 self.infer_block(statements, *tail, expected)
92 }
90 Expr::TryBlock { body } => { 93 Expr::TryBlock { body } => {
91 let _inner = self.infer_expr(*body, expected); 94 let _inner = self.infer_expr(*body, expected);
92 // FIXME should be std::result::Result<{inner}, _> 95 // FIXME should be std::result::Result<{inner}, _>
93 Ty::Unknown 96 Ty::Unknown
94 } 97 }
95 Expr::Loop { body } => { 98 Expr::Loop { body, label } => {
96 self.breakables.push(BreakableContext { 99 self.breakables.push(BreakableContext {
97 may_break: false, 100 may_break: false,
98 break_ty: self.table.new_type_var(), 101 break_ty: self.table.new_type_var(),
102 label: label.clone(),
99 }); 103 });
100 self.infer_expr(*body, &Expectation::has_type(Ty::unit())); 104 self.infer_expr(*body, &Expectation::has_type(Ty::unit()));
101 105
@@ -110,8 +114,12 @@ impl<'a> InferenceContext<'a> {
110 Ty::simple(TypeCtor::Never) 114 Ty::simple(TypeCtor::Never)
111 } 115 }
112 } 116 }
113 Expr::While { condition, body } => { 117 Expr::While { condition, body, label } => {
114 self.breakables.push(BreakableContext { may_break: false, break_ty: Ty::Unknown }); 118 self.breakables.push(BreakableContext {
119 may_break: false,
120 break_ty: Ty::Unknown,
121 label: label.clone(),
122 });
115 // while let is desugared to a match loop, so this is always simple while 123 // while let is desugared to a match loop, so this is always simple while
116 self.infer_expr(*condition, &Expectation::has_type(Ty::simple(TypeCtor::Bool))); 124 self.infer_expr(*condition, &Expectation::has_type(Ty::simple(TypeCtor::Bool)));
117 self.infer_expr(*body, &Expectation::has_type(Ty::unit())); 125 self.infer_expr(*body, &Expectation::has_type(Ty::unit()));
@@ -120,10 +128,14 @@ impl<'a> InferenceContext<'a> {
120 self.diverges = Diverges::Maybe; 128 self.diverges = Diverges::Maybe;
121 Ty::unit() 129 Ty::unit()
122 } 130 }
123 Expr::For { iterable, body, pat } => { 131 Expr::For { iterable, body, pat, label } => {
124 let iterable_ty = self.infer_expr(*iterable, &Expectation::none()); 132 let iterable_ty = self.infer_expr(*iterable, &Expectation::none());
125 133
126 self.breakables.push(BreakableContext { may_break: false, break_ty: Ty::Unknown }); 134 self.breakables.push(BreakableContext {
135 may_break: false,
136 break_ty: Ty::Unknown,
137 label: label.clone(),
138 });
127 let pat_ty = 139 let pat_ty =
128 self.resolve_associated_type(iterable_ty, self.resolve_into_iter_item()); 140 self.resolve_associated_type(iterable_ty, self.resolve_into_iter_item());
129 141
@@ -140,13 +152,13 @@ impl<'a> InferenceContext<'a> {
140 152
141 let mut sig_tys = Vec::new(); 153 let mut sig_tys = Vec::new();
142 154
143 for (arg_pat, arg_type) in args.iter().zip(arg_types.iter()) { 155 // collect explicitly written argument types
144 let expected = if let Some(type_ref) = arg_type { 156 for arg_type in arg_types.iter() {
157 let arg_ty = if let Some(type_ref) = arg_type {
145 self.make_ty(type_ref) 158 self.make_ty(type_ref)
146 } else { 159 } else {
147 Ty::Unknown 160 self.table.new_type_var()
148 }; 161 };
149 let arg_ty = self.infer_pat(*arg_pat, &expected, BindingMode::default());
150 sig_tys.push(arg_ty); 162 sig_tys.push(arg_ty);
151 } 163 }
152 164
@@ -158,7 +170,7 @@ impl<'a> InferenceContext<'a> {
158 sig_tys.push(ret_ty.clone()); 170 sig_tys.push(ret_ty.clone());
159 let sig_ty = Ty::apply( 171 let sig_ty = Ty::apply(
160 TypeCtor::FnPtr { num_args: sig_tys.len() as u16 - 1 }, 172 TypeCtor::FnPtr { num_args: sig_tys.len() as u16 - 1 },
161 Substs(sig_tys.into()), 173 Substs(sig_tys.clone().into()),
162 ); 174 );
163 let closure_ty = 175 let closure_ty =
164 Ty::apply_one(TypeCtor::Closure { def: self.owner, expr: tgt_expr }, sig_ty); 176 Ty::apply_one(TypeCtor::Closure { def: self.owner, expr: tgt_expr }, sig_ty);
@@ -168,6 +180,12 @@ impl<'a> InferenceContext<'a> {
168 // infer the body. 180 // infer the body.
169 self.coerce(&closure_ty, &expected.ty); 181 self.coerce(&closure_ty, &expected.ty);
170 182
183 // Now go through the argument patterns
184 for (arg_pat, arg_ty) in args.iter().zip(sig_tys) {
185 let resolved = self.resolve_ty_as_possible(arg_ty);
186 self.infer_pat(*arg_pat, &resolved, BindingMode::default());
187 }
188
171 let prev_diverges = mem::replace(&mut self.diverges, Diverges::Maybe); 189 let prev_diverges = mem::replace(&mut self.diverges, Diverges::Maybe);
172 let prev_ret_ty = mem::replace(&mut self.return_ty, ret_ty.clone()); 190 let prev_ret_ty = mem::replace(&mut self.return_ty, ret_ty.clone());
173 191
@@ -230,23 +248,24 @@ impl<'a> InferenceContext<'a> {
230 let resolver = resolver_for_expr(self.db.upcast(), self.owner, tgt_expr); 248 let resolver = resolver_for_expr(self.db.upcast(), self.owner, tgt_expr);
231 self.infer_path(&resolver, p, tgt_expr.into()).unwrap_or(Ty::Unknown) 249 self.infer_path(&resolver, p, tgt_expr.into()).unwrap_or(Ty::Unknown)
232 } 250 }
233 Expr::Continue => Ty::simple(TypeCtor::Never), 251 Expr::Continue { .. } => Ty::simple(TypeCtor::Never),
234 Expr::Break { expr } => { 252 Expr::Break { expr, label } => {
235 let val_ty = if let Some(expr) = expr { 253 let val_ty = if let Some(expr) = expr {
236 self.infer_expr(*expr, &Expectation::none()) 254 self.infer_expr(*expr, &Expectation::none())
237 } else { 255 } else {
238 Ty::unit() 256 Ty::unit()
239 }; 257 };
240 258
241 let last_ty = if let Some(ctxt) = self.breakables.last() { 259 let last_ty =
242 ctxt.break_ty.clone() 260 if let Some(ctxt) = find_breakable(&mut self.breakables, label.as_ref()) {
243 } else { 261 ctxt.break_ty.clone()
244 Ty::Unknown 262 } else {
245 }; 263 Ty::Unknown
264 };
246 265
247 let merged_type = self.coerce_merge_branch(&last_ty, &val_ty); 266 let merged_type = self.coerce_merge_branch(&last_ty, &val_ty);
248 267
249 if let Some(ctxt) = self.breakables.last_mut() { 268 if let Some(ctxt) = find_breakable(&mut self.breakables, label.as_ref()) {
250 ctxt.break_ty = merged_type; 269 ctxt.break_ty = merged_type;
251 ctxt.may_break = true; 270 ctxt.may_break = true;
252 } else { 271 } else {
@@ -350,19 +369,28 @@ impl<'a> InferenceContext<'a> {
350 // FIXME check the cast... 369 // FIXME check the cast...
351 cast_ty 370 cast_ty
352 } 371 }
353 Expr::Ref { expr, mutability } => { 372 Expr::Ref { expr, rawness, mutability } => {
354 let expectation = 373 let expectation = if let Some((exp_inner, exp_rawness, exp_mutability)) =
355 if let Some((exp_inner, exp_mutability)) = &expected.ty.as_reference() { 374 &expected.ty.as_reference_or_ptr()
356 if *exp_mutability == Mutability::Mut && *mutability == Mutability::Shared { 375 {
357 // FIXME: throw type error - expected mut reference but found shared ref, 376 if *exp_mutability == Mutability::Mut && *mutability == Mutability::Shared {
358 // which cannot be coerced 377 // FIXME: throw type error - expected mut reference but found shared ref,
359 } 378 // which cannot be coerced
360 Expectation::rvalue_hint(Ty::clone(exp_inner)) 379 }
361 } else { 380 if *exp_rawness == Rawness::Ref && *rawness == Rawness::RawPtr {
362 Expectation::none() 381 // FIXME: throw type error - expected reference but found ptr,
363 }; 382 // which cannot be coerced
383 }
384 Expectation::rvalue_hint(Ty::clone(exp_inner))
385 } else {
386 Expectation::none()
387 };
364 let inner_ty = self.infer_expr_inner(*expr, &expectation); 388 let inner_ty = self.infer_expr_inner(*expr, &expectation);
365 Ty::apply_one(TypeCtor::Ref(*mutability), inner_ty) 389 let ty = match rawness {
390 Rawness::RawPtr => TypeCtor::RawPtr(*mutability),
391 Rawness::Ref => TypeCtor::Ref(*mutability),
392 };
393 Ty::apply_one(ty, inner_ty)
366 } 394 }
367 Expr::Box { expr } => { 395 Expr::Box { expr } => {
368 let inner_ty = self.infer_expr_inner(*expr, &Expectation::none()); 396 let inner_ty = self.infer_expr_inner(*expr, &Expectation::none());
diff --git a/crates/ra_hir_ty/src/lib.rs b/crates/ra_hir_ty/src/lib.rs
index e91c9be04..9fa8d3bdc 100644
--- a/crates/ra_hir_ty/src/lib.rs
+++ b/crates/ra_hir_ty/src/lib.rs
@@ -49,8 +49,10 @@ use std::sync::Arc;
49use std::{iter, mem}; 49use std::{iter, mem};
50 50
51use hir_def::{ 51use hir_def::{
52 expr::ExprId, type_ref::Mutability, AdtId, AssocContainerId, DefWithBodyId, GenericDefId, 52 expr::ExprId,
53 HasModule, Lookup, TraitId, TypeAliasId, TypeParamId, 53 type_ref::{Mutability, Rawness},
54 AdtId, AssocContainerId, DefWithBodyId, GenericDefId, HasModule, Lookup, TraitId, TypeAliasId,
55 TypeParamId,
54}; 56};
55use ra_db::{impl_intern_key, salsa, CrateId}; 57use ra_db::{impl_intern_key, salsa, CrateId};
56 58
@@ -159,6 +161,12 @@ pub enum TypeCtor {
159pub struct TypeCtorId(salsa::InternId); 161pub struct TypeCtorId(salsa::InternId);
160impl_intern_key!(TypeCtorId); 162impl_intern_key!(TypeCtorId);
161 163
164/// This exists just for Chalk, because Chalk just has a single `FnDefId` where
165/// we have different IDs for struct and enum variant constructors.
166#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Ord, PartialOrd)]
167pub struct CallableDefId(salsa::InternId);
168impl_intern_key!(CallableDefId);
169
162impl TypeCtor { 170impl TypeCtor {
163 pub fn num_ty_params(self, db: &dyn HirDatabase) -> usize { 171 pub fn num_ty_params(self, db: &dyn HirDatabase) -> usize {
164 match self { 172 match self {
@@ -703,6 +711,18 @@ impl Ty {
703 } 711 }
704 } 712 }
705 713
714 pub fn as_reference_or_ptr(&self) -> Option<(&Ty, Rawness, Mutability)> {
715 match self {
716 Ty::Apply(ApplicationTy { ctor: TypeCtor::Ref(mutability), parameters }) => {
717 Some((parameters.as_single(), Rawness::Ref, *mutability))
718 }
719 Ty::Apply(ApplicationTy { ctor: TypeCtor::RawPtr(mutability), parameters }) => {
720 Some((parameters.as_single(), Rawness::RawPtr, *mutability))
721 }
722 _ => None,
723 }
724 }
725
706 pub fn strip_references(&self) -> &Ty { 726 pub fn strip_references(&self) -> &Ty {
707 let mut t: &Ty = self; 727 let mut t: &Ty = self;
708 728
diff --git a/crates/ra_hir_ty/src/tests/coercion.rs b/crates/ra_hir_ty/src/tests/coercion.rs
index 2cc4f4bf9..6f777ed8c 100644
--- a/crates/ra_hir_ty/src/tests/coercion.rs
+++ b/crates/ra_hir_ty/src/tests/coercion.rs
@@ -116,15 +116,20 @@ fn infer_let_stmt_coerce() {
116 assert_snapshot!( 116 assert_snapshot!(
117 infer(r#" 117 infer(r#"
118fn test() { 118fn test() {
119 let x: &[i32] = &[1]; 119 let x: &[isize] = &[1];
120 let x: *const [isize] = &[1];
120} 121}
121"#), 122"#),
122 @r###" 123 @r###"
123 11..40 '{ ...[1]; }': () 124 11..76 '{ ...[1]; }': ()
124 21..22 'x': &[i32] 125 21..22 'x': &[isize]
125 33..37 '&[1]': &[i32; _] 126 35..39 '&[1]': &[isize; _]
126 34..37 '[1]': [i32; _] 127 36..39 '[1]': [isize; _]
127 35..36 '1': i32 128 37..38 '1': isize
129 49..50 'x': *const [isize]
130 69..73 '&[1]': &[isize; _]
131 70..73 '[1]': [isize; _]
132 71..72 '1': isize
128 "###); 133 "###);
129} 134}
130 135
diff --git a/crates/ra_hir_ty/src/tests/patterns.rs b/crates/ra_hir_ty/src/tests/patterns.rs
index 0c5f972a2..fe62587c0 100644
--- a/crates/ra_hir_ty/src/tests/patterns.rs
+++ b/crates/ra_hir_ty/src/tests/patterns.rs
@@ -520,3 +520,53 @@ fn main() {
520 105..107 '()': () 520 105..107 '()': ()
521 ") 521 ")
522} 522}
523
524#[test]
525fn match_ergonomics_in_closure_params() {
526 assert_snapshot!(
527 infer(r#"
528#[lang = "fn_once"]
529trait FnOnce<Args> {
530 type Output;
531}
532
533fn foo<T, U, F: FnOnce(T) -> U>(t: T, f: F) -> U { loop {} }
534
535fn test() {
536 foo(&(1, "a"), |&(x, y)| x); // normal, no match ergonomics
537 foo(&(1, "a"), |(x, y)| x);
538}
539"#),
540 @r###"
541 94..95 't': T
542 100..101 'f': F
543 111..122 '{ loop {} }': U
544 113..120 'loop {}': !
545 118..120 '{}': ()
546 134..233 '{ ... x); }': ()
547 140..143 'foo': fn foo<&(i32, &str), i32, |&(i32, &str)| -> i32>(&(i32, &str), |&(i32, &str)| -> i32) -> i32
548 140..167 'foo(&(...y)| x)': i32
549 144..153 '&(1, "a")': &(i32, &str)
550 145..153 '(1, "a")': (i32, &str)
551 146..147 '1': i32
552 149..152 '"a"': &str
553 155..166 '|&(x, y)| x': |&(i32, &str)| -> i32
554 156..163 '&(x, y)': &(i32, &str)
555 157..163 '(x, y)': (i32, &str)
556 158..159 'x': i32
557 161..162 'y': &str
558 165..166 'x': i32
559 204..207 'foo': fn foo<&(i32, &str), &i32, |&(i32, &str)| -> &i32>(&(i32, &str), |&(i32, &str)| -> &i32) -> &i32
560 204..230 'foo(&(...y)| x)': &i32
561 208..217 '&(1, "a")': &(i32, &str)
562 209..217 '(1, "a")': (i32, &str)
563 210..211 '1': i32
564 213..216 '"a"': &str
565 219..229 '|(x, y)| x': |&(i32, &str)| -> &i32
566 220..226 '(x, y)': (i32, &str)
567 221..222 'x': &i32
568 224..225 'y': &&str
569 228..229 'x': &i32
570 "###
571 );
572}
diff --git a/crates/ra_hir_ty/src/tests/simple.rs b/crates/ra_hir_ty/src/tests/simple.rs
index fd2208af2..88309157b 100644
--- a/crates/ra_hir_ty/src/tests/simple.rs
+++ b/crates/ra_hir_ty/src/tests/simple.rs
@@ -385,6 +385,26 @@ fn test(a: &u32, b: &mut u32, c: *const u32, d: *mut u32) {
385} 385}
386 386
387#[test] 387#[test]
388fn infer_raw_ref() {
389 assert_snapshot!(
390 infer(r#"
391fn test(a: i32) {
392 &raw mut a;
393 &raw const a;
394}
395"#),
396 @r###"
397 9..10 'a': i32
398 17..54 '{ ...t a; }': ()
399 23..33 '&raw mut a': *mut i32
400 32..33 'a': i32
401 39..51 '&raw const a': *const i32
402 50..51 'a': i32
403 "###
404 );
405}
406
407#[test]
388fn infer_literals() { 408fn infer_literals() {
389 assert_snapshot!( 409 assert_snapshot!(
390 infer(r##" 410 infer(r##"
@@ -937,7 +957,7 @@ fn main(foo: Foo) {
937 51..107 'if tru... }': () 957 51..107 'if tru... }': ()
938 54..58 'true': bool 958 54..58 'true': bool
939 59..67 '{ }': () 959 59..67 '{ }': ()
940 73..107 'if fal... }': () 960 73..107 'if fal... }': i32
941 76..81 'false': bool 961 76..81 'false': bool
942 82..107 '{ ... }': i32 962 82..107 '{ ... }': i32
943 92..95 'foo': Foo 963 92..95 'foo': Foo
@@ -1923,3 +1943,57 @@ fn test() {
1923 "### 1943 "###
1924 ); 1944 );
1925} 1945}
1946
1947#[test]
1948fn infer_labelled_break_with_val() {
1949 assert_snapshot!(
1950 infer(r#"
1951fn foo() {
1952 let _x = || 'outer: loop {
1953 let inner = 'inner: loop {
1954 let i = Default::default();
1955 if (break 'outer i) {
1956 loop { break 'inner 5i8; };
1957 } else if true {
1958 break 'inner 6;
1959 }
1960 break 7;
1961 };
1962 break inner < 8;
1963 };
1964}
1965"#),
1966 @r###"
1967 10..336 '{ ... }; }': ()
1968 20..22 '_x': || -> bool
1969 25..333 '|| 'ou... }': || -> bool
1970 28..333 ''outer... }': bool
1971 41..333 '{ ... }': ()
1972 55..60 'inner': i8
1973 63..301 ''inner... }': i8
1974 76..301 '{ ... }': ()
1975 94..95 'i': bool
1976 98..114 'Defaul...efault': {unknown}
1977 98..116 'Defaul...ault()': bool
1978 130..270 'if (br... }': ()
1979 134..148 'break 'outer i': !
1980 147..148 'i': bool
1981 150..209 '{ ... }': ()
1982 168..194 'loop {...5i8; }': !
1983 173..194 '{ brea...5i8; }': ()
1984 175..191 'break ...er 5i8': !
1985 188..191 '5i8': i8
1986 215..270 'if tru... }': ()
1987 218..222 'true': bool
1988 223..270 '{ ... }': ()
1989 241..255 'break 'inner 6': !
1990 254..255 '6': i8
1991 283..290 'break 7': !
1992 289..290 '7': i8
1993 311..326 'break inner < 8': !
1994 317..322 'inner': i8
1995 317..326 'inner < 8': bool
1996 325..326 '8': i8
1997 "###
1998 );
1999}
diff --git a/crates/ra_hir_ty/src/tests/traits.rs b/crates/ra_hir_ty/src/tests/traits.rs
index 6826610cb..e8778d419 100644
--- a/crates/ra_hir_ty/src/tests/traits.rs
+++ b/crates/ra_hir_ty/src/tests/traits.rs
@@ -2644,6 +2644,79 @@ fn test() {
2644} 2644}
2645 2645
2646#[test] 2646#[test]
2647fn builtin_fn_def_copy() {
2648 assert_snapshot!(
2649 infer_with_mismatches(r#"
2650#[lang = "copy"]
2651trait Copy {}
2652
2653fn foo() {}
2654fn bar<T: Copy>(T) -> T {}
2655struct Struct(usize);
2656enum Enum { Variant(usize) }
2657
2658trait Test { fn test(&self) -> bool; }
2659impl<T: Copy> Test for T {}
2660
2661fn test() {
2662 foo.test();
2663 bar.test();
2664 Struct.test();
2665 Enum::Variant.test();
2666}
2667"#, true),
2668 @r###"
2669 42..44 '{}': ()
2670 61..62 'T': {unknown}
2671 69..71 '{}': ()
2672 69..71: expected T, got ()
2673 146..150 'self': &Self
2674 202..282 '{ ...t(); }': ()
2675 208..211 'foo': fn foo()
2676 208..218 'foo.test()': bool
2677 224..227 'bar': fn bar<{unknown}>({unknown}) -> {unknown}
2678 224..234 'bar.test()': bool
2679 240..246 'Struct': Struct(usize) -> Struct
2680 240..253 'Struct.test()': bool
2681 259..272 'Enum::Variant': Variant(usize) -> Enum
2682 259..279 'Enum::...test()': bool
2683 "###
2684 );
2685}
2686
2687#[test]
2688fn builtin_fn_ptr_copy() {
2689 assert_snapshot!(
2690 infer_with_mismatches(r#"
2691#[lang = "copy"]
2692trait Copy {}
2693
2694trait Test { fn test(&self) -> bool; }
2695impl<T: Copy> Test for T {}
2696
2697fn test(f1: fn(), f2: fn(usize) -> u8, f3: fn(u8, u8) -> &u8) {
2698 f1.test();
2699 f2.test();
2700 f3.test();
2701}
2702"#, true),
2703 @r###"
2704 55..59 'self': &Self
2705 109..111 'f1': fn()
2706 119..121 'f2': fn(usize) -> u8
2707 140..142 'f3': fn(u8, u8) -> &u8
2708 163..211 '{ ...t(); }': ()
2709 169..171 'f1': fn()
2710 169..178 'f1.test()': bool
2711 184..186 'f2': fn(usize) -> u8
2712 184..193 'f2.test()': bool
2713 199..201 'f3': fn(u8, u8) -> &u8
2714 199..208 'f3.test()': bool
2715 "###
2716 );
2717}
2718
2719#[test]
2647fn builtin_sized() { 2720fn builtin_sized() {
2648 assert_snapshot!( 2721 assert_snapshot!(
2649 infer_with_mismatches(r#" 2722 infer_with_mismatches(r#"
@@ -2680,3 +2753,48 @@ fn test() {
2680 "### 2753 "###
2681 ); 2754 );
2682} 2755}
2756
2757#[test]
2758fn integer_range_iterate() {
2759 let t = type_at(
2760 r#"
2761//- /main.rs crate:main deps:std
2762fn test() {
2763 for x in 0..100 { x<|>; }
2764}
2765
2766//- /std.rs crate:std
2767pub mod ops {
2768 pub struct Range<Idx> {
2769 pub start: Idx,
2770 pub end: Idx,
2771 }
2772}
2773
2774pub mod iter {
2775 pub trait Iterator {
2776 type Item;
2777 }
2778
2779 pub trait IntoIterator {
2780 type Item;
2781 type IntoIter: Iterator<Item = Self::Item>;
2782 }
2783
2784 impl<T> IntoIterator for T where T: Iterator {
2785 type Item = <T as Iterator>::Item;
2786 type IntoIter = Self;
2787 }
2788}
2789
2790trait Step {}
2791impl Step for i32 {}
2792impl Step for i64 {}
2793
2794impl<A: Step> iter::Iterator for ops::Range<A> {
2795 type Item = A;
2796}
2797"#,
2798 );
2799 assert_eq!(t, "i32");
2800}
diff --git a/crates/ra_hir_ty/src/traits/builtin.rs b/crates/ra_hir_ty/src/traits/builtin.rs
index ccab246bf..88a422d2c 100644
--- a/crates/ra_hir_ty/src/traits/builtin.rs
+++ b/crates/ra_hir_ty/src/traits/builtin.rs
@@ -290,8 +290,7 @@ fn trait_object_unsize_impl_datum(
290 let self_trait_ref = TraitRef { trait_, substs: self_substs }; 290 let self_trait_ref = TraitRef { trait_, substs: self_substs };
291 let where_clauses = vec![GenericPredicate::Implemented(self_trait_ref)]; 291 let where_clauses = vec![GenericPredicate::Implemented(self_trait_ref)];
292 292
293 let impl_substs = 293 let impl_substs = Substs::builder(2).push(self_ty).push(Ty::Dyn(target_bounds.into())).build();
294 Substs::builder(2).push(self_ty).push(Ty::Dyn(target_bounds.clone().into())).build();
295 294
296 let trait_ref = TraitRef { trait_: unsize_trait, substs: impl_substs }; 295 let trait_ref = TraitRef { trait_: unsize_trait, substs: impl_substs };
297 296
diff --git a/crates/ra_hir_ty/src/traits/chalk.rs b/crates/ra_hir_ty/src/traits/chalk.rs
index e2f2a9ccb..61de3cc30 100644
--- a/crates/ra_hir_ty/src/traits/chalk.rs
+++ b/crates/ra_hir_ty/src/traits/chalk.rs
@@ -4,6 +4,7 @@ use std::sync::Arc;
4use log::debug; 4use log::debug;
5 5
6use chalk_ir::{fold::shift::Shift, GenericArg, TypeName}; 6use chalk_ir::{fold::shift::Shift, GenericArg, TypeName};
7use chalk_solve::rust_ir::{self, WellKnownTrait};
7 8
8use hir_def::{ 9use hir_def::{
9 lang_item::{lang_attr, LangItemTarget}, 10 lang_item::{lang_attr, LangItemTarget},
@@ -14,9 +15,8 @@ use ra_db::{salsa::InternKey, CrateId};
14use super::{builtin, AssocTyValue, ChalkContext, Impl}; 15use super::{builtin, AssocTyValue, ChalkContext, Impl};
15use crate::{ 16use crate::{
16 db::HirDatabase, display::HirDisplay, method_resolution::TyFingerprint, utils::generics, 17 db::HirDatabase, display::HirDisplay, method_resolution::TyFingerprint, utils::generics,
17 DebruijnIndex, GenericPredicate, Substs, Ty, TypeCtor, 18 CallableDef, DebruijnIndex, GenericPredicate, Substs, Ty, TypeCtor,
18}; 19};
19use chalk_rust_ir::WellKnownTrait;
20use mapping::{convert_where_clauses, generic_predicate_to_inline_bound, make_binders}; 20use mapping::{convert_where_clauses, generic_predicate_to_inline_bound, make_binders};
21 21
22pub use self::interner::*; 22pub use self::interner::*;
@@ -54,10 +54,9 @@ impl<'a> chalk_solve::RustIrDatabase<Interner> for ChalkContext<'a> {
54 54
55 fn fn_def_datum( 55 fn fn_def_datum(
56 &self, 56 &self,
57 _fn_def_id: chalk_ir::FnDefId<Interner>, 57 fn_def_id: chalk_ir::FnDefId<Interner>,
58 ) -> Arc<chalk_rust_ir::FnDefDatum<Interner>> { 58 ) -> Arc<rust_ir::FnDefDatum<Interner>> {
59 // We don't yet provide any FnDefs to Chalk 59 self.db.fn_def_datum(self.krate, fn_def_id)
60 unimplemented!()
61 } 60 }
62 61
63 fn impls_for_trait( 62 fn impls_for_trait(
@@ -113,7 +112,7 @@ impl<'a> chalk_solve::RustIrDatabase<Interner> for ChalkContext<'a> {
113 } 112 }
114 fn well_known_trait_id( 113 fn well_known_trait_id(
115 &self, 114 &self,
116 well_known_trait: chalk_rust_ir::WellKnownTrait, 115 well_known_trait: rust_ir::WellKnownTrait,
117 ) -> Option<chalk_ir::TraitId<Interner>> { 116 ) -> Option<chalk_ir::TraitId<Interner>> {
118 let lang_attr = lang_attr_from_well_known_trait(well_known_trait); 117 let lang_attr = lang_attr_from_well_known_trait(well_known_trait);
119 let lang_items = self.db.crate_lang_items(self.krate); 118 let lang_items = self.db.crate_lang_items(self.krate);
@@ -134,13 +133,13 @@ impl<'a> chalk_solve::RustIrDatabase<Interner> for ChalkContext<'a> {
134 fn opaque_ty_data( 133 fn opaque_ty_data(
135 &self, 134 &self,
136 _id: chalk_ir::OpaqueTyId<Interner>, 135 _id: chalk_ir::OpaqueTyId<Interner>,
137 ) -> Arc<chalk_rust_ir::OpaqueTyDatum<Interner>> { 136 ) -> Arc<rust_ir::OpaqueTyDatum<Interner>> {
138 unimplemented!() 137 unimplemented!()
139 } 138 }
140 139
141 fn force_impl_for( 140 fn force_impl_for(
142 &self, 141 &self,
143 _well_known: chalk_rust_ir::WellKnownTrait, 142 _well_known: rust_ir::WellKnownTrait,
144 _ty: &chalk_ir::TyData<Interner>, 143 _ty: &chalk_ir::TyData<Interner>,
145 ) -> Option<bool> { 144 ) -> Option<bool> {
146 // this method is mostly for rustc 145 // this method is mostly for rustc
@@ -151,6 +150,10 @@ impl<'a> chalk_solve::RustIrDatabase<Interner> for ChalkContext<'a> {
151 // FIXME: implement actual object safety 150 // FIXME: implement actual object safety
152 true 151 true
153 } 152 }
153
154 fn hidden_opaque_type(&self, _id: chalk_ir::OpaqueTyId<Interner>) -> chalk_ir::Ty<Interner> {
155 Ty::Unknown.to_chalk(self.db)
156 }
154} 157}
155 158
156pub(crate) fn program_clauses_for_chalk_env_query( 159pub(crate) fn program_clauses_for_chalk_env_query(
@@ -189,7 +192,7 @@ pub(crate) fn associated_ty_data_query(
189 .collect(); 192 .collect();
190 193
191 let where_clauses = convert_where_clauses(db, type_alias.into(), &bound_vars); 194 let where_clauses = convert_where_clauses(db, type_alias.into(), &bound_vars);
192 let bound_data = chalk_rust_ir::AssociatedTyDatumBound { bounds, where_clauses }; 195 let bound_data = rust_ir::AssociatedTyDatumBound { bounds, where_clauses };
193 let datum = AssociatedTyDatum { 196 let datum = AssociatedTyDatum {
194 trait_id: trait_.to_chalk(db), 197 trait_id: trait_.to_chalk(db),
195 id, 198 id,
@@ -210,7 +213,7 @@ pub(crate) fn trait_datum_query(
210 debug!("trait {:?} = {:?}", trait_id, trait_data.name); 213 debug!("trait {:?} = {:?}", trait_id, trait_data.name);
211 let generic_params = generics(db.upcast(), trait_.into()); 214 let generic_params = generics(db.upcast(), trait_.into());
212 let bound_vars = Substs::bound_vars(&generic_params, DebruijnIndex::INNERMOST); 215 let bound_vars = Substs::bound_vars(&generic_params, DebruijnIndex::INNERMOST);
213 let flags = chalk_rust_ir::TraitFlags { 216 let flags = rust_ir::TraitFlags {
214 auto: trait_data.auto, 217 auto: trait_data.auto,
215 upstream: trait_.lookup(db.upcast()).container.module(db.upcast()).krate != krate, 218 upstream: trait_.lookup(db.upcast()).container.module(db.upcast()).krate != krate,
216 non_enumerable: true, 219 non_enumerable: true,
@@ -222,7 +225,7 @@ pub(crate) fn trait_datum_query(
222 let where_clauses = convert_where_clauses(db, trait_.into(), &bound_vars); 225 let where_clauses = convert_where_clauses(db, trait_.into(), &bound_vars);
223 let associated_ty_ids = 226 let associated_ty_ids =
224 trait_data.associated_types().map(|type_alias| type_alias.to_chalk(db)).collect(); 227 trait_data.associated_types().map(|type_alias| type_alias.to_chalk(db)).collect();
225 let trait_datum_bound = chalk_rust_ir::TraitDatumBound { where_clauses }; 228 let trait_datum_bound = rust_ir::TraitDatumBound { where_clauses };
226 let well_known = 229 let well_known =
227 lang_attr(db.upcast(), trait_).and_then(|name| well_known_trait_from_lang_attr(&name)); 230 lang_attr(db.upcast(), trait_).and_then(|name| well_known_trait_from_lang_attr(&name));
228 let trait_datum = TraitDatum { 231 let trait_datum = TraitDatum {
@@ -272,12 +275,12 @@ pub(crate) fn struct_datum_query(
272 convert_where_clauses(db, generic_def, &bound_vars) 275 convert_where_clauses(db, generic_def, &bound_vars)
273 }) 276 })
274 .unwrap_or_else(Vec::new); 277 .unwrap_or_else(Vec::new);
275 let flags = chalk_rust_ir::AdtFlags { 278 let flags = rust_ir::AdtFlags {
276 upstream, 279 upstream,
277 // FIXME set fundamental flag correctly 280 // FIXME set fundamental flag correctly
278 fundamental: false, 281 fundamental: false,
279 }; 282 };
280 let struct_datum_bound = chalk_rust_ir::AdtDatumBound { 283 let struct_datum_bound = rust_ir::AdtDatumBound {
281 fields: Vec::new(), // FIXME add fields (only relevant for auto traits) 284 fields: Vec::new(), // FIXME add fields (only relevant for auto traits)
282 where_clauses, 285 where_clauses,
283 }; 286 };
@@ -317,9 +320,9 @@ fn impl_def_datum(
317 let bound_vars = Substs::bound_vars(&generic_params, DebruijnIndex::INNERMOST); 320 let bound_vars = Substs::bound_vars(&generic_params, DebruijnIndex::INNERMOST);
318 let trait_ = trait_ref.trait_; 321 let trait_ = trait_ref.trait_;
319 let impl_type = if impl_id.lookup(db.upcast()).container.module(db.upcast()).krate == krate { 322 let impl_type = if impl_id.lookup(db.upcast()).container.module(db.upcast()).krate == krate {
320 chalk_rust_ir::ImplType::Local 323 rust_ir::ImplType::Local
321 } else { 324 } else {
322 chalk_rust_ir::ImplType::External 325 rust_ir::ImplType::External
323 }; 326 };
324 let where_clauses = convert_where_clauses(db, impl_id.into(), &bound_vars); 327 let where_clauses = convert_where_clauses(db, impl_id.into(), &bound_vars);
325 let negative = impl_data.is_negative; 328 let negative = impl_data.is_negative;
@@ -332,13 +335,9 @@ fn impl_def_datum(
332 ); 335 );
333 let trait_ref = trait_ref.to_chalk(db); 336 let trait_ref = trait_ref.to_chalk(db);
334 337
335 let polarity = if negative { 338 let polarity = if negative { rust_ir::Polarity::Negative } else { rust_ir::Polarity::Positive };
336 chalk_rust_ir::Polarity::Negative
337 } else {
338 chalk_rust_ir::Polarity::Positive
339 };
340 339
341 let impl_datum_bound = chalk_rust_ir::ImplDatumBound { trait_ref, where_clauses }; 340 let impl_datum_bound = rust_ir::ImplDatumBound { trait_ref, where_clauses };
342 let trait_data = db.trait_data(trait_); 341 let trait_data = db.trait_data(trait_);
343 let associated_ty_value_ids = impl_data 342 let associated_ty_value_ids = impl_data
344 .items 343 .items
@@ -396,8 +395,8 @@ fn type_alias_associated_ty_value(
396 .associated_type_by_name(&type_alias_data.name) 395 .associated_type_by_name(&type_alias_data.name)
397 .expect("assoc ty value should not exist"); // validated when building the impl data as well 396 .expect("assoc ty value should not exist"); // validated when building the impl data as well
398 let ty = db.ty(type_alias.into()); 397 let ty = db.ty(type_alias.into());
399 let value_bound = chalk_rust_ir::AssociatedTyValueBound { ty: ty.value.to_chalk(db) }; 398 let value_bound = rust_ir::AssociatedTyValueBound { ty: ty.value.to_chalk(db) };
400 let value = chalk_rust_ir::AssociatedTyValue { 399 let value = rust_ir::AssociatedTyValue {
401 impl_id: Impl::ImplDef(impl_id).to_chalk(db), 400 impl_id: Impl::ImplDef(impl_id).to_chalk(db),
402 associated_ty_id: assoc_ty.to_chalk(db), 401 associated_ty_id: assoc_ty.to_chalk(db),
403 value: make_binders(value_bound, ty.num_binders), 402 value: make_binders(value_bound, ty.num_binders),
@@ -405,6 +404,26 @@ fn type_alias_associated_ty_value(
405 Arc::new(value) 404 Arc::new(value)
406} 405}
407 406
407pub(crate) fn fn_def_datum_query(
408 db: &dyn HirDatabase,
409 _krate: CrateId,
410 fn_def_id: FnDefId,
411) -> Arc<FnDefDatum> {
412 let callable_def: CallableDef = from_chalk(db, fn_def_id);
413 let generic_params = generics(db.upcast(), callable_def.into());
414 let sig = db.callable_item_signature(callable_def);
415 let bound_vars = Substs::bound_vars(&generic_params, DebruijnIndex::INNERMOST);
416 let where_clauses = convert_where_clauses(db, callable_def.into(), &bound_vars);
417 let bound = rust_ir::FnDefDatumBound {
418 // Note: Chalk doesn't actually use this information yet as far as I am aware, but we provide it anyway
419 argument_types: sig.value.params().iter().map(|ty| ty.clone().to_chalk(db)).collect(),
420 return_type: sig.value.ret().clone().to_chalk(db),
421 where_clauses,
422 };
423 let datum = FnDefDatum { id: fn_def_id, binders: make_binders(bound, sig.num_binders) };
424 Arc::new(datum)
425}
426
408impl From<AdtId> for crate::TypeCtorId { 427impl From<AdtId> for crate::TypeCtorId {
409 fn from(struct_id: AdtId) -> Self { 428 fn from(struct_id: AdtId) -> Self {
410 struct_id.0 429 struct_id.0
@@ -417,6 +436,18 @@ impl From<crate::TypeCtorId> for AdtId {
417 } 436 }
418} 437}
419 438
439impl From<FnDefId> for crate::CallableDefId {
440 fn from(fn_def_id: FnDefId) -> Self {
441 InternKey::from_intern_id(fn_def_id.0)
442 }
443}
444
445impl From<crate::CallableDefId> for FnDefId {
446 fn from(callable_def_id: crate::CallableDefId) -> Self {
447 chalk_ir::FnDefId(callable_def_id.as_intern_id())
448 }
449}
450
420impl From<ImplId> for crate::traits::GlobalImplId { 451impl From<ImplId> for crate::traits::GlobalImplId {
421 fn from(impl_id: ImplId) -> Self { 452 fn from(impl_id: ImplId) -> Self {
422 InternKey::from_intern_id(impl_id.0) 453 InternKey::from_intern_id(impl_id.0)
@@ -429,14 +460,14 @@ impl From<crate::traits::GlobalImplId> for ImplId {
429 } 460 }
430} 461}
431 462
432impl From<chalk_rust_ir::AssociatedTyValueId<Interner>> for crate::traits::AssocTyValueId { 463impl From<rust_ir::AssociatedTyValueId<Interner>> for crate::traits::AssocTyValueId {
433 fn from(id: chalk_rust_ir::AssociatedTyValueId<Interner>) -> Self { 464 fn from(id: rust_ir::AssociatedTyValueId<Interner>) -> Self {
434 Self::from_intern_id(id.0) 465 Self::from_intern_id(id.0)
435 } 466 }
436} 467}
437 468
438impl From<crate::traits::AssocTyValueId> for chalk_rust_ir::AssociatedTyValueId<Interner> { 469impl From<crate::traits::AssocTyValueId> for rust_ir::AssociatedTyValueId<Interner> {
439 fn from(assoc_ty_value_id: crate::traits::AssocTyValueId) -> Self { 470 fn from(assoc_ty_value_id: crate::traits::AssocTyValueId) -> Self {
440 chalk_rust_ir::AssociatedTyValueId(assoc_ty_value_id.as_intern_id()) 471 rust_ir::AssociatedTyValueId(assoc_ty_value_id.as_intern_id())
441 } 472 }
442} 473}
diff --git a/crates/ra_hir_ty/src/traits/chalk/interner.rs b/crates/ra_hir_ty/src/traits/chalk/interner.rs
index 060372819..e27074ba6 100644
--- a/crates/ra_hir_ty/src/traits/chalk/interner.rs
+++ b/crates/ra_hir_ty/src/traits/chalk/interner.rs
@@ -11,15 +11,17 @@ use std::{fmt, sync::Arc};
11pub struct Interner; 11pub struct Interner;
12 12
13pub type AssocTypeId = chalk_ir::AssocTypeId<Interner>; 13pub type AssocTypeId = chalk_ir::AssocTypeId<Interner>;
14pub type AssociatedTyDatum = chalk_rust_ir::AssociatedTyDatum<Interner>; 14pub type AssociatedTyDatum = chalk_solve::rust_ir::AssociatedTyDatum<Interner>;
15pub type TraitId = chalk_ir::TraitId<Interner>; 15pub type TraitId = chalk_ir::TraitId<Interner>;
16pub type TraitDatum = chalk_rust_ir::TraitDatum<Interner>; 16pub type TraitDatum = chalk_solve::rust_ir::TraitDatum<Interner>;
17pub type AdtId = chalk_ir::AdtId<Interner>; 17pub type AdtId = chalk_ir::AdtId<Interner>;
18pub type StructDatum = chalk_rust_ir::AdtDatum<Interner>; 18pub type StructDatum = chalk_solve::rust_ir::AdtDatum<Interner>;
19pub type ImplId = chalk_ir::ImplId<Interner>; 19pub type ImplId = chalk_ir::ImplId<Interner>;
20pub type ImplDatum = chalk_rust_ir::ImplDatum<Interner>; 20pub type ImplDatum = chalk_solve::rust_ir::ImplDatum<Interner>;
21pub type AssociatedTyValueId = chalk_rust_ir::AssociatedTyValueId<Interner>; 21pub type AssociatedTyValueId = chalk_solve::rust_ir::AssociatedTyValueId<Interner>;
22pub type AssociatedTyValue = chalk_rust_ir::AssociatedTyValue<Interner>; 22pub type AssociatedTyValue = chalk_solve::rust_ir::AssociatedTyValue<Interner>;
23pub type FnDefId = chalk_ir::FnDefId<Interner>;
24pub type FnDefDatum = chalk_solve::rust_ir::FnDefDatum<Interner>;
23 25
24impl chalk_ir::interner::Interner for Interner { 26impl chalk_ir::interner::Interner for Interner {
25 type InternedType = Box<chalk_ir::TyData<Self>>; // FIXME use Arc? 27 type InternedType = Box<chalk_ir::TyData<Self>>; // FIXME use Arc?
diff --git a/crates/ra_hir_ty/src/traits/chalk/mapping.rs b/crates/ra_hir_ty/src/traits/chalk/mapping.rs
index a83d82fd8..5f6daf842 100644
--- a/crates/ra_hir_ty/src/traits/chalk/mapping.rs
+++ b/crates/ra_hir_ty/src/traits/chalk/mapping.rs
@@ -7,6 +7,7 @@ use chalk_ir::{
7 cast::Cast, fold::shift::Shift, interner::HasInterner, PlaceholderIndex, Scalar, TypeName, 7 cast::Cast, fold::shift::Shift, interner::HasInterner, PlaceholderIndex, Scalar, TypeName,
8 UniverseIndex, 8 UniverseIndex,
9}; 9};
10use chalk_solve::rust_ir;
10 11
11use hir_def::{type_ref::Mutability, AssocContainerId, GenericDefId, Lookup, TypeAliasId}; 12use hir_def::{type_ref::Mutability, AssocContainerId, GenericDefId, Lookup, TypeAliasId};
12use ra_db::salsa::InternKey; 13use ra_db::salsa::InternKey;
@@ -15,8 +16,8 @@ use crate::{
15 db::HirDatabase, 16 db::HirDatabase,
16 primitive::{FloatBitness, FloatTy, IntBitness, IntTy, Signedness, Uncertain}, 17 primitive::{FloatBitness, FloatTy, IntBitness, IntTy, Signedness, Uncertain},
17 traits::{builtin, AssocTyValue, Canonical, Impl, Obligation}, 18 traits::{builtin, AssocTyValue, Canonical, Impl, Obligation},
18 ApplicationTy, GenericPredicate, InEnvironment, ProjectionPredicate, ProjectionTy, Substs, 19 ApplicationTy, CallableDef, GenericPredicate, InEnvironment, ProjectionPredicate, ProjectionTy,
19 TraitEnvironment, TraitRef, Ty, TypeCtor, 20 Substs, TraitEnvironment, TraitRef, Ty, TypeCtor,
20}; 21};
21 22
22use super::interner::*; 23use super::interner::*;
@@ -26,14 +27,19 @@ impl ToChalk for Ty {
26 type Chalk = chalk_ir::Ty<Interner>; 27 type Chalk = chalk_ir::Ty<Interner>;
27 fn to_chalk(self, db: &dyn HirDatabase) -> chalk_ir::Ty<Interner> { 28 fn to_chalk(self, db: &dyn HirDatabase) -> chalk_ir::Ty<Interner> {
28 match self { 29 match self {
29 Ty::Apply(apply_ty) => { 30 Ty::Apply(apply_ty) => match apply_ty.ctor {
30 if let TypeCtor::Ref(m) = apply_ty.ctor { 31 TypeCtor::Ref(m) => ref_to_chalk(db, m, apply_ty.parameters),
31 return ref_to_chalk(db, m, apply_ty.parameters); 32 TypeCtor::FnPtr { num_args: _ } => {
33 let substitution = apply_ty.parameters.to_chalk(db).shifted_in(&Interner);
34 chalk_ir::TyData::Function(chalk_ir::Fn { num_binders: 0, substitution })
35 .intern(&Interner)
32 } 36 }
33 let name = apply_ty.ctor.to_chalk(db); 37 _ => {
34 let substitution = apply_ty.parameters.to_chalk(db); 38 let name = apply_ty.ctor.to_chalk(db);
35 chalk_ir::ApplicationTy { name, substitution }.cast(&Interner).intern(&Interner) 39 let substitution = apply_ty.parameters.to_chalk(db);
36 } 40 chalk_ir::ApplicationTy { name, substitution }.cast(&Interner).intern(&Interner)
41 }
42 },
37 Ty::Projection(proj_ty) => { 43 Ty::Projection(proj_ty) => {
38 let associated_ty_id = proj_ty.associated_ty.to_chalk(db); 44 let associated_ty_id = proj_ty.associated_ty.to_chalk(db);
39 let substitution = proj_ty.parameters.to_chalk(db); 45 let substitution = proj_ty.parameters.to_chalk(db);
@@ -93,9 +99,15 @@ impl ToChalk for Ty {
93 Ty::Projection(ProjectionTy { associated_ty, parameters }) 99 Ty::Projection(ProjectionTy { associated_ty, parameters })
94 } 100 }
95 chalk_ir::TyData::Alias(chalk_ir::AliasTy::Opaque(_)) => unimplemented!(), 101 chalk_ir::TyData::Alias(chalk_ir::AliasTy::Opaque(_)) => unimplemented!(),
96 chalk_ir::TyData::Function(_) => unimplemented!(), 102 chalk_ir::TyData::Function(chalk_ir::Fn { num_binders: _, substitution }) => {
103 let parameters: Substs = from_chalk(db, substitution);
104 Ty::Apply(ApplicationTy {
105 ctor: TypeCtor::FnPtr { num_args: (parameters.len() - 1) as u16 },
106 parameters,
107 })
108 }
97 chalk_ir::TyData::BoundVar(idx) => Ty::Bound(idx), 109 chalk_ir::TyData::BoundVar(idx) => Ty::Bound(idx),
98 chalk_ir::TyData::InferenceVar(_iv) => Ty::Unknown, 110 chalk_ir::TyData::InferenceVar(_iv, _kind) => Ty::Unknown,
99 chalk_ir::TyData::Dyn(where_clauses) => { 111 chalk_ir::TyData::Dyn(where_clauses) => {
100 assert_eq!(where_clauses.bounds.binders.len(&Interner), 1); 112 assert_eq!(where_clauses.bounds.binders.len(&Interner), 1);
101 let predicates = where_clauses 113 let predicates = where_clauses
@@ -217,13 +229,17 @@ impl ToChalk for TypeCtor {
217 TypeCtor::Slice => TypeName::Slice, 229 TypeCtor::Slice => TypeName::Slice,
218 TypeCtor::Ref(mutability) => TypeName::Ref(mutability.to_chalk(db)), 230 TypeCtor::Ref(mutability) => TypeName::Ref(mutability.to_chalk(db)),
219 TypeCtor::Str => TypeName::Str, 231 TypeCtor::Str => TypeName::Str,
232 TypeCtor::FnDef(callable_def) => {
233 let id = callable_def.to_chalk(db);
234 TypeName::FnDef(id)
235 }
236 TypeCtor::Never => TypeName::Never,
237
220 TypeCtor::Int(Uncertain::Unknown) 238 TypeCtor::Int(Uncertain::Unknown)
221 | TypeCtor::Float(Uncertain::Unknown) 239 | TypeCtor::Float(Uncertain::Unknown)
222 | TypeCtor::Adt(_) 240 | TypeCtor::Adt(_)
223 | TypeCtor::Array 241 | TypeCtor::Array
224 | TypeCtor::FnDef(_)
225 | TypeCtor::FnPtr { .. } 242 | TypeCtor::FnPtr { .. }
226 | TypeCtor::Never
227 | TypeCtor::Closure { .. } => { 243 | TypeCtor::Closure { .. } => {
228 // other TypeCtors get interned and turned into a chalk StructId 244 // other TypeCtors get interned and turned into a chalk StructId
229 let struct_id = db.intern_type_ctor(self).into(); 245 let struct_id = db.intern_type_ctor(self).into();
@@ -259,10 +275,14 @@ impl ToChalk for TypeCtor {
259 TypeName::Slice => TypeCtor::Slice, 275 TypeName::Slice => TypeCtor::Slice,
260 TypeName::Ref(mutability) => TypeCtor::Ref(from_chalk(db, mutability)), 276 TypeName::Ref(mutability) => TypeCtor::Ref(from_chalk(db, mutability)),
261 TypeName::Str => TypeCtor::Str, 277 TypeName::Str => TypeCtor::Str,
278 TypeName::Never => TypeCtor::Never,
262 279
263 TypeName::FnDef(_) => unreachable!(), 280 TypeName::FnDef(fn_def_id) => {
281 let callable_def = from_chalk(db, fn_def_id);
282 TypeCtor::FnDef(callable_def)
283 }
264 284
265 TypeName::Error => { 285 TypeName::Array | TypeName::Error => {
266 // this should not be reached, since we don't represent TypeName::Error with TypeCtor 286 // this should not be reached, since we don't represent TypeName::Error with TypeCtor
267 unreachable!() 287 unreachable!()
268 } 288 }
@@ -347,6 +367,18 @@ impl ToChalk for Impl {
347 } 367 }
348} 368}
349 369
370impl ToChalk for CallableDef {
371 type Chalk = FnDefId;
372
373 fn to_chalk(self, db: &dyn HirDatabase) -> FnDefId {
374 db.intern_callable_def(self).into()
375 }
376
377 fn from_chalk(db: &dyn HirDatabase, fn_def_id: FnDefId) -> CallableDef {
378 db.lookup_intern_callable_def(fn_def_id.into())
379 }
380}
381
350impl ToChalk for TypeAliasId { 382impl ToChalk for TypeAliasId {
351 type Chalk = AssocTypeId; 383 type Chalk = AssocTypeId;
352 384
@@ -479,7 +511,7 @@ where
479 511
480 fn to_chalk(self, db: &dyn HirDatabase) -> chalk_ir::Canonical<T::Chalk> { 512 fn to_chalk(self, db: &dyn HirDatabase) -> chalk_ir::Canonical<T::Chalk> {
481 let parameter = chalk_ir::CanonicalVarKind::new( 513 let parameter = chalk_ir::CanonicalVarKind::new(
482 chalk_ir::VariableKind::Ty, 514 chalk_ir::VariableKind::Ty(chalk_ir::TyKind::General),
483 chalk_ir::UniverseIndex::ROOT, 515 chalk_ir::UniverseIndex::ROOT,
484 ); 516 );
485 let value = self.value.to_chalk(db); 517 let value = self.value.to_chalk(db);
@@ -550,17 +582,17 @@ impl ToChalk for builtin::BuiltinImplData {
550 type Chalk = ImplDatum; 582 type Chalk = ImplDatum;
551 583
552 fn to_chalk(self, db: &dyn HirDatabase) -> ImplDatum { 584 fn to_chalk(self, db: &dyn HirDatabase) -> ImplDatum {
553 let impl_type = chalk_rust_ir::ImplType::External; 585 let impl_type = rust_ir::ImplType::External;
554 let where_clauses = self.where_clauses.into_iter().map(|w| w.to_chalk(db)).collect(); 586 let where_clauses = self.where_clauses.into_iter().map(|w| w.to_chalk(db)).collect();
555 587
556 let impl_datum_bound = 588 let impl_datum_bound =
557 chalk_rust_ir::ImplDatumBound { trait_ref: self.trait_ref.to_chalk(db), where_clauses }; 589 rust_ir::ImplDatumBound { trait_ref: self.trait_ref.to_chalk(db), where_clauses };
558 let associated_ty_value_ids = 590 let associated_ty_value_ids =
559 self.assoc_ty_values.into_iter().map(|v| v.to_chalk(db)).collect(); 591 self.assoc_ty_values.into_iter().map(|v| v.to_chalk(db)).collect();
560 chalk_rust_ir::ImplDatum { 592 rust_ir::ImplDatum {
561 binders: make_binders(impl_datum_bound, self.num_vars), 593 binders: make_binders(impl_datum_bound, self.num_vars),
562 impl_type, 594 impl_type,
563 polarity: chalk_rust_ir::Polarity::Positive, 595 polarity: rust_ir::Polarity::Positive,
564 associated_ty_value_ids, 596 associated_ty_value_ids,
565 } 597 }
566 } 598 }
@@ -575,9 +607,9 @@ impl ToChalk for builtin::BuiltinImplAssocTyValueData {
575 607
576 fn to_chalk(self, db: &dyn HirDatabase) -> AssociatedTyValue { 608 fn to_chalk(self, db: &dyn HirDatabase) -> AssociatedTyValue {
577 let ty = self.value.to_chalk(db); 609 let ty = self.value.to_chalk(db);
578 let value_bound = chalk_rust_ir::AssociatedTyValueBound { ty }; 610 let value_bound = rust_ir::AssociatedTyValueBound { ty };
579 611
580 chalk_rust_ir::AssociatedTyValue { 612 rust_ir::AssociatedTyValue {
581 associated_ty_id: self.assoc_ty_id.to_chalk(db), 613 associated_ty_id: self.assoc_ty_id.to_chalk(db),
582 impl_id: self.impl_.to_chalk(db), 614 impl_id: self.impl_.to_chalk(db),
583 value: make_binders(value_bound, self.num_vars), 615 value: make_binders(value_bound, self.num_vars),
@@ -599,7 +631,7 @@ where
599 chalk_ir::Binders::new( 631 chalk_ir::Binders::new(
600 chalk_ir::VariableKinds::from( 632 chalk_ir::VariableKinds::from(
601 &Interner, 633 &Interner,
602 std::iter::repeat(chalk_ir::VariableKind::Ty).take(num_vars), 634 std::iter::repeat(chalk_ir::VariableKind::Ty(chalk_ir::TyKind::General)).take(num_vars),
603 ), 635 ),
604 value, 636 value,
605 ) 637 )
@@ -626,7 +658,7 @@ pub(super) fn generic_predicate_to_inline_bound(
626 db: &dyn HirDatabase, 658 db: &dyn HirDatabase,
627 pred: &GenericPredicate, 659 pred: &GenericPredicate,
628 self_ty: &Ty, 660 self_ty: &Ty,
629) -> Option<chalk_rust_ir::InlineBound<Interner>> { 661) -> Option<rust_ir::InlineBound<Interner>> {
630 // An InlineBound is like a GenericPredicate, except the self type is left out. 662 // An InlineBound is like a GenericPredicate, except the self type is left out.
631 // We don't have a special type for this, but Chalk does. 663 // We don't have a special type for this, but Chalk does.
632 match pred { 664 match pred {
@@ -641,8 +673,8 @@ pub(super) fn generic_predicate_to_inline_bound(
641 .map(|ty| ty.clone().to_chalk(db).cast(&Interner)) 673 .map(|ty| ty.clone().to_chalk(db).cast(&Interner))
642 .collect(); 674 .collect();
643 let trait_bound = 675 let trait_bound =
644 chalk_rust_ir::TraitBound { trait_id: trait_ref.trait_.to_chalk(db), args_no_self }; 676 rust_ir::TraitBound { trait_id: trait_ref.trait_.to_chalk(db), args_no_self };
645 Some(chalk_rust_ir::InlineBound::TraitBound(trait_bound)) 677 Some(rust_ir::InlineBound::TraitBound(trait_bound))
646 } 678 }
647 GenericPredicate::Projection(proj) => { 679 GenericPredicate::Projection(proj) => {
648 if &proj.projection_ty.parameters[0] != self_ty { 680 if &proj.projection_ty.parameters[0] != self_ty {
@@ -656,16 +688,13 @@ pub(super) fn generic_predicate_to_inline_bound(
656 .iter() 688 .iter()
657 .map(|ty| ty.clone().to_chalk(db).cast(&Interner)) 689 .map(|ty| ty.clone().to_chalk(db).cast(&Interner))
658 .collect(); 690 .collect();
659 let alias_eq_bound = chalk_rust_ir::AliasEqBound { 691 let alias_eq_bound = rust_ir::AliasEqBound {
660 value: proj.ty.clone().to_chalk(db), 692 value: proj.ty.clone().to_chalk(db),
661 trait_bound: chalk_rust_ir::TraitBound { 693 trait_bound: rust_ir::TraitBound { trait_id: trait_.to_chalk(db), args_no_self },
662 trait_id: trait_.to_chalk(db),
663 args_no_self,
664 },
665 associated_ty_id: proj.projection_ty.associated_ty.to_chalk(db), 694 associated_ty_id: proj.projection_ty.associated_ty.to_chalk(db),
666 parameters: Vec::new(), // FIXME we don't support generic associated types yet 695 parameters: Vec::new(), // FIXME we don't support generic associated types yet
667 }; 696 };
668 Some(chalk_rust_ir::InlineBound::AliasEqBound(alias_eq_bound)) 697 Some(rust_ir::InlineBound::AliasEqBound(alias_eq_bound))
669 } 698 }
670 GenericPredicate::Error => None, 699 GenericPredicate::Error => None,
671 } 700 }
diff --git a/crates/ra_hir_ty/src/traits/chalk/tls.rs b/crates/ra_hir_ty/src/traits/chalk/tls.rs
index ebf402a07..d88828c7c 100644
--- a/crates/ra_hir_ty/src/traits/chalk/tls.rs
+++ b/crates/ra_hir_ty/src/traits/chalk/tls.rs
@@ -247,10 +247,24 @@ impl DebugContext<'_> {
247 247
248 pub fn debug_fn_def_id( 248 pub fn debug_fn_def_id(
249 &self, 249 &self,
250 _fn_def_id: chalk_ir::FnDefId<Interner>, 250 fn_def_id: chalk_ir::FnDefId<Interner>,
251 fmt: &mut fmt::Formatter<'_>, 251 fmt: &mut fmt::Formatter<'_>,
252 ) -> Result<(), fmt::Error> { 252 ) -> Result<(), fmt::Error> {
253 write!(fmt, "fn") 253 let def: CallableDef = from_chalk(self.0, fn_def_id);
254 let name = match def {
255 CallableDef::FunctionId(ff) => self.0.function_data(ff).name.clone(),
256 CallableDef::StructId(s) => self.0.struct_data(s).name.clone(),
257 CallableDef::EnumVariantId(e) => {
258 let enum_data = self.0.enum_data(e.parent);
259 enum_data.variants[e.local_id].name.clone()
260 }
261 };
262 match def {
263 CallableDef::FunctionId(_) => write!(fmt, "{{fn {}}}", name),
264 CallableDef::StructId(_) | CallableDef::EnumVariantId(_) => {
265 write!(fmt, "{{ctor {}}}", name)
266 }
267 }
254 } 268 }
255 269
256 pub fn debug_const( 270 pub fn debug_const(