aboutsummaryrefslogtreecommitdiff
path: root/crates/ra_hir_ty/src/infer/expr.rs
diff options
context:
space:
mode:
Diffstat (limited to 'crates/ra_hir_ty/src/infer/expr.rs')
-rw-r--r--crates/ra_hir_ty/src/infer/expr.rs873
1 files changed, 0 insertions, 873 deletions
diff --git a/crates/ra_hir_ty/src/infer/expr.rs b/crates/ra_hir_ty/src/infer/expr.rs
deleted file mode 100644
index a2f849d02..000000000
--- a/crates/ra_hir_ty/src/infer/expr.rs
+++ /dev/null
@@ -1,873 +0,0 @@
1//! Type inference for expressions.
2
3use std::iter::{repeat, repeat_with};
4use std::{mem, sync::Arc};
5
6use hir_def::{
7 builtin_type::Signedness,
8 expr::{Array, BinaryOp, Expr, ExprId, Literal, Statement, UnaryOp},
9 path::{GenericArg, GenericArgs},
10 resolver::resolver_for_expr,
11 AdtId, AssocContainerId, FieldId, Lookup,
12};
13use hir_expand::name::{name, Name};
14use syntax::ast::RangeOp;
15
16use crate::{
17 autoderef, method_resolution, op,
18 traits::{FnTrait, InEnvironment},
19 utils::{generics, variant_data, Generics},
20 ApplicationTy, Binders, CallableDefId, InferTy, IntTy, Mutability, Obligation, Rawness, Substs,
21 TraitRef, Ty, TypeCtor,
22};
23
24use super::{
25 find_breakable, BindingMode, BreakableContext, Diverges, Expectation, InferenceContext,
26 InferenceDiagnostic, TypeMismatch,
27};
28
29impl<'a> InferenceContext<'a> {
30 pub(super) fn infer_expr(&mut self, tgt_expr: ExprId, expected: &Expectation) -> Ty {
31 let ty = self.infer_expr_inner(tgt_expr, expected);
32 if ty.is_never() {
33 // Any expression that produces a value of type `!` must have diverged
34 self.diverges = Diverges::Always;
35 }
36 let could_unify = self.unify(&ty, &expected.ty);
37 if !could_unify {
38 self.result.type_mismatches.insert(
39 tgt_expr,
40 TypeMismatch { expected: expected.ty.clone(), actual: ty.clone() },
41 );
42 }
43 self.resolve_ty_as_possible(ty)
44 }
45
46 /// Infer type of expression with possibly implicit coerce to the expected type.
47 /// Return the type after possible coercion.
48 pub(super) fn infer_expr_coerce(&mut self, expr: ExprId, expected: &Expectation) -> Ty {
49 let ty = self.infer_expr_inner(expr, &expected);
50 let ty = if !self.coerce(&ty, &expected.coercion_target()) {
51 self.result
52 .type_mismatches
53 .insert(expr, TypeMismatch { expected: expected.ty.clone(), actual: ty.clone() });
54 // Return actual type when type mismatch.
55 // This is needed for diagnostic when return type mismatch.
56 ty
57 } else if expected.coercion_target() == &Ty::Unknown {
58 ty
59 } else {
60 expected.ty.clone()
61 };
62
63 self.resolve_ty_as_possible(ty)
64 }
65
66 fn callable_sig_from_fn_trait(&mut self, ty: &Ty, num_args: usize) -> Option<(Vec<Ty>, Ty)> {
67 let krate = self.resolver.krate()?;
68 let fn_once_trait = FnTrait::FnOnce.get_id(self.db, krate)?;
69 let output_assoc_type =
70 self.db.trait_data(fn_once_trait).associated_type_by_name(&name![Output])?;
71 let generic_params = generics(self.db.upcast(), fn_once_trait.into());
72 if generic_params.len() != 2 {
73 return None;
74 }
75
76 let mut param_builder = Substs::builder(num_args);
77 let mut arg_tys = vec![];
78 for _ in 0..num_args {
79 let arg = self.table.new_type_var();
80 param_builder = param_builder.push(arg.clone());
81 arg_tys.push(arg);
82 }
83 let parameters = param_builder.build();
84 let arg_ty = Ty::Apply(ApplicationTy {
85 ctor: TypeCtor::Tuple { cardinality: num_args as u16 },
86 parameters,
87 });
88 let substs =
89 Substs::build_for_generics(&generic_params).push(ty.clone()).push(arg_ty).build();
90
91 let trait_env = Arc::clone(&self.trait_env);
92 let implements_fn_trait =
93 Obligation::Trait(TraitRef { trait_: fn_once_trait, substs: substs.clone() });
94 let goal = self.canonicalizer().canonicalize_obligation(InEnvironment {
95 value: implements_fn_trait.clone(),
96 environment: trait_env,
97 });
98 if self.db.trait_solve(krate, goal.value).is_some() {
99 self.obligations.push(implements_fn_trait);
100 let output_proj_ty =
101 crate::ProjectionTy { associated_ty: output_assoc_type, parameters: substs };
102 let return_ty = self.normalize_projection_ty(output_proj_ty);
103 Some((arg_tys, return_ty))
104 } else {
105 None
106 }
107 }
108
109 pub fn callable_sig(&mut self, ty: &Ty, num_args: usize) -> Option<(Vec<Ty>, Ty)> {
110 match ty.callable_sig(self.db) {
111 Some(sig) => Some((sig.params().to_vec(), sig.ret().clone())),
112 None => self.callable_sig_from_fn_trait(ty, num_args),
113 }
114 }
115
116 fn infer_expr_inner(&mut self, tgt_expr: ExprId, expected: &Expectation) -> Ty {
117 let body = Arc::clone(&self.body); // avoid borrow checker problem
118 let ty = match &body[tgt_expr] {
119 Expr::Missing => Ty::Unknown,
120 Expr::If { condition, then_branch, else_branch } => {
121 // if let is desugared to match, so this is always simple if
122 self.infer_expr(*condition, &Expectation::has_type(Ty::simple(TypeCtor::Bool)));
123
124 let condition_diverges = mem::replace(&mut self.diverges, Diverges::Maybe);
125 let mut both_arms_diverge = Diverges::Always;
126
127 let then_ty = self.infer_expr_inner(*then_branch, &expected);
128 both_arms_diverge &= mem::replace(&mut self.diverges, Diverges::Maybe);
129 let else_ty = match else_branch {
130 Some(else_branch) => self.infer_expr_inner(*else_branch, &expected),
131 None => Ty::unit(),
132 };
133 both_arms_diverge &= self.diverges;
134
135 self.diverges = condition_diverges | both_arms_diverge;
136
137 self.coerce_merge_branch(&then_ty, &else_ty)
138 }
139 Expr::Block { statements, tail, .. } => {
140 // FIXME: Breakable block inference
141 self.infer_block(statements, *tail, expected)
142 }
143 Expr::Unsafe { body } => self.infer_expr(*body, expected),
144 Expr::TryBlock { body } => {
145 let _inner = self.infer_expr(*body, expected);
146 // FIXME should be std::result::Result<{inner}, _>
147 Ty::Unknown
148 }
149 Expr::Loop { body, label } => {
150 self.breakables.push(BreakableContext {
151 may_break: false,
152 break_ty: self.table.new_type_var(),
153 label: label.clone(),
154 });
155 self.infer_expr(*body, &Expectation::has_type(Ty::unit()));
156
157 let ctxt = self.breakables.pop().expect("breakable stack broken");
158 if ctxt.may_break {
159 self.diverges = Diverges::Maybe;
160 }
161
162 if ctxt.may_break {
163 ctxt.break_ty
164 } else {
165 Ty::simple(TypeCtor::Never)
166 }
167 }
168 Expr::While { condition, body, label } => {
169 self.breakables.push(BreakableContext {
170 may_break: false,
171 break_ty: Ty::Unknown,
172 label: label.clone(),
173 });
174 // while let is desugared to a match loop, so this is always simple while
175 self.infer_expr(*condition, &Expectation::has_type(Ty::simple(TypeCtor::Bool)));
176 self.infer_expr(*body, &Expectation::has_type(Ty::unit()));
177 let _ctxt = self.breakables.pop().expect("breakable stack broken");
178 // the body may not run, so it diverging doesn't mean we diverge
179 self.diverges = Diverges::Maybe;
180 Ty::unit()
181 }
182 Expr::For { iterable, body, pat, label } => {
183 let iterable_ty = self.infer_expr(*iterable, &Expectation::none());
184
185 self.breakables.push(BreakableContext {
186 may_break: false,
187 break_ty: Ty::Unknown,
188 label: label.clone(),
189 });
190 let pat_ty =
191 self.resolve_associated_type(iterable_ty, self.resolve_into_iter_item());
192
193 self.infer_pat(*pat, &pat_ty, BindingMode::default());
194
195 self.infer_expr(*body, &Expectation::has_type(Ty::unit()));
196 let _ctxt = self.breakables.pop().expect("breakable stack broken");
197 // the body may not run, so it diverging doesn't mean we diverge
198 self.diverges = Diverges::Maybe;
199 Ty::unit()
200 }
201 Expr::Lambda { body, args, ret_type, arg_types } => {
202 assert_eq!(args.len(), arg_types.len());
203
204 let mut sig_tys = Vec::new();
205
206 // collect explicitly written argument types
207 for arg_type in arg_types.iter() {
208 let arg_ty = if let Some(type_ref) = arg_type {
209 self.make_ty(type_ref)
210 } else {
211 self.table.new_type_var()
212 };
213 sig_tys.push(arg_ty);
214 }
215
216 // add return type
217 let ret_ty = match ret_type {
218 Some(type_ref) => self.make_ty(type_ref),
219 None => self.table.new_type_var(),
220 };
221 sig_tys.push(ret_ty.clone());
222 let sig_ty = Ty::apply(
223 TypeCtor::FnPtr { num_args: sig_tys.len() as u16 - 1, is_varargs: false },
224 Substs(sig_tys.clone().into()),
225 );
226 let closure_ty =
227 Ty::apply_one(TypeCtor::Closure { def: self.owner, expr: tgt_expr }, sig_ty);
228
229 // Eagerly try to relate the closure type with the expected
230 // type, otherwise we often won't have enough information to
231 // infer the body.
232 self.coerce(&closure_ty, &expected.ty);
233
234 // Now go through the argument patterns
235 for (arg_pat, arg_ty) in args.iter().zip(sig_tys) {
236 let resolved = self.resolve_ty_as_possible(arg_ty);
237 self.infer_pat(*arg_pat, &resolved, BindingMode::default());
238 }
239
240 let prev_diverges = mem::replace(&mut self.diverges, Diverges::Maybe);
241 let prev_ret_ty = mem::replace(&mut self.return_ty, ret_ty.clone());
242
243 self.infer_expr_coerce(*body, &Expectation::has_type(ret_ty));
244
245 self.diverges = prev_diverges;
246 self.return_ty = prev_ret_ty;
247
248 closure_ty
249 }
250 Expr::Call { callee, args } => {
251 let callee_ty = self.infer_expr(*callee, &Expectation::none());
252 let canonicalized = self.canonicalizer().canonicalize_ty(callee_ty.clone());
253 let mut derefs = autoderef(
254 self.db,
255 self.resolver.krate(),
256 InEnvironment {
257 value: canonicalized.value.clone(),
258 environment: self.trait_env.clone(),
259 },
260 );
261 let (param_tys, ret_ty): (Vec<Ty>, Ty) = derefs
262 .find_map(|callee_deref_ty| {
263 self.callable_sig(
264 &canonicalized.decanonicalize_ty(callee_deref_ty.value),
265 args.len(),
266 )
267 })
268 .unwrap_or((Vec::new(), Ty::Unknown));
269 self.register_obligations_for_call(&callee_ty);
270 self.check_call_arguments(args, &param_tys);
271 self.normalize_associated_types_in(ret_ty)
272 }
273 Expr::MethodCall { receiver, args, method_name, generic_args } => self
274 .infer_method_call(tgt_expr, *receiver, &args, &method_name, generic_args.as_ref()),
275 Expr::Match { expr, arms } => {
276 let input_ty = self.infer_expr(*expr, &Expectation::none());
277
278 let mut result_ty = if arms.is_empty() {
279 Ty::simple(TypeCtor::Never)
280 } else {
281 self.table.new_type_var()
282 };
283
284 let matchee_diverges = self.diverges;
285 let mut all_arms_diverge = Diverges::Always;
286
287 for arm in arms {
288 self.diverges = Diverges::Maybe;
289 let _pat_ty = self.infer_pat(arm.pat, &input_ty, BindingMode::default());
290 if let Some(guard_expr) = arm.guard {
291 self.infer_expr(
292 guard_expr,
293 &Expectation::has_type(Ty::simple(TypeCtor::Bool)),
294 );
295 }
296
297 let arm_ty = self.infer_expr_inner(arm.expr, &expected);
298 all_arms_diverge &= self.diverges;
299 result_ty = self.coerce_merge_branch(&result_ty, &arm_ty);
300 }
301
302 self.diverges = matchee_diverges | all_arms_diverge;
303
304 result_ty
305 }
306 Expr::Path(p) => {
307 // FIXME this could be more efficient...
308 let resolver = resolver_for_expr(self.db.upcast(), self.owner, tgt_expr);
309 self.infer_path(&resolver, p, tgt_expr.into()).unwrap_or(Ty::Unknown)
310 }
311 Expr::Continue { .. } => Ty::simple(TypeCtor::Never),
312 Expr::Break { expr, label } => {
313 let val_ty = if let Some(expr) = expr {
314 self.infer_expr(*expr, &Expectation::none())
315 } else {
316 Ty::unit()
317 };
318
319 let last_ty =
320 if let Some(ctxt) = find_breakable(&mut self.breakables, label.as_ref()) {
321 ctxt.break_ty.clone()
322 } else {
323 Ty::Unknown
324 };
325
326 let merged_type = self.coerce_merge_branch(&last_ty, &val_ty);
327
328 if let Some(ctxt) = find_breakable(&mut self.breakables, label.as_ref()) {
329 ctxt.break_ty = merged_type;
330 ctxt.may_break = true;
331 } else {
332 self.push_diagnostic(InferenceDiagnostic::BreakOutsideOfLoop {
333 expr: tgt_expr,
334 });
335 }
336
337 Ty::simple(TypeCtor::Never)
338 }
339 Expr::Return { expr } => {
340 if let Some(expr) = expr {
341 self.infer_expr_coerce(*expr, &Expectation::has_type(self.return_ty.clone()));
342 } else {
343 let unit = Ty::unit();
344 self.coerce(&unit, &self.return_ty.clone());
345 }
346 Ty::simple(TypeCtor::Never)
347 }
348 Expr::RecordLit { path, fields, spread } => {
349 let (ty, def_id) = self.resolve_variant(path.as_ref());
350 if let Some(variant) = def_id {
351 self.write_variant_resolution(tgt_expr.into(), variant);
352 }
353
354 self.unify(&ty, &expected.ty);
355
356 let substs = ty.substs().unwrap_or_else(Substs::empty);
357 let field_types = def_id.map(|it| self.db.field_types(it)).unwrap_or_default();
358 let variant_data = def_id.map(|it| variant_data(self.db.upcast(), it));
359 for (field_idx, field) in fields.iter().enumerate() {
360 let field_def =
361 variant_data.as_ref().and_then(|it| match it.field(&field.name) {
362 Some(local_id) => Some(FieldId { parent: def_id.unwrap(), local_id }),
363 None => {
364 self.push_diagnostic(InferenceDiagnostic::NoSuchField {
365 expr: tgt_expr,
366 field: field_idx,
367 });
368 None
369 }
370 });
371 if let Some(field_def) = field_def {
372 self.result.record_field_resolutions.insert(field.expr, field_def);
373 }
374 let field_ty = field_def
375 .map_or(Ty::Unknown, |it| field_types[it.local_id].clone().subst(&substs));
376 self.infer_expr_coerce(field.expr, &Expectation::has_type(field_ty));
377 }
378 if let Some(expr) = spread {
379 self.infer_expr(*expr, &Expectation::has_type(ty.clone()));
380 }
381 ty
382 }
383 Expr::Field { expr, name } => {
384 let receiver_ty = self.infer_expr_inner(*expr, &Expectation::none());
385 let canonicalized = self.canonicalizer().canonicalize_ty(receiver_ty);
386 let ty = autoderef::autoderef(
387 self.db,
388 self.resolver.krate(),
389 InEnvironment {
390 value: canonicalized.value.clone(),
391 environment: self.trait_env.clone(),
392 },
393 )
394 .find_map(|derefed_ty| match canonicalized.decanonicalize_ty(derefed_ty.value) {
395 Ty::Apply(a_ty) => match a_ty.ctor {
396 TypeCtor::Tuple { .. } => name
397 .as_tuple_index()
398 .and_then(|idx| a_ty.parameters.0.get(idx).cloned()),
399 TypeCtor::Adt(AdtId::StructId(s)) => {
400 self.db.struct_data(s).variant_data.field(name).map(|local_id| {
401 let field = FieldId { parent: s.into(), local_id };
402 self.write_field_resolution(tgt_expr, field);
403 self.db.field_types(s.into())[field.local_id]
404 .clone()
405 .subst(&a_ty.parameters)
406 })
407 }
408 TypeCtor::Adt(AdtId::UnionId(u)) => {
409 self.db.union_data(u).variant_data.field(name).map(|local_id| {
410 let field = FieldId { parent: u.into(), local_id };
411 self.write_field_resolution(tgt_expr, field);
412 self.db.field_types(u.into())[field.local_id]
413 .clone()
414 .subst(&a_ty.parameters)
415 })
416 }
417 _ => None,
418 },
419 _ => None,
420 })
421 .unwrap_or(Ty::Unknown);
422 let ty = self.insert_type_vars(ty);
423 self.normalize_associated_types_in(ty)
424 }
425 Expr::Await { expr } => {
426 let inner_ty = self.infer_expr_inner(*expr, &Expectation::none());
427 self.resolve_associated_type(inner_ty, self.resolve_future_future_output())
428 }
429 Expr::Try { expr } => {
430 let inner_ty = self.infer_expr_inner(*expr, &Expectation::none());
431 self.resolve_associated_type(inner_ty, self.resolve_ops_try_ok())
432 }
433 Expr::Cast { expr, type_ref } => {
434 let _inner_ty = self.infer_expr_inner(*expr, &Expectation::none());
435 let cast_ty = self.make_ty(type_ref);
436 // FIXME check the cast...
437 cast_ty
438 }
439 Expr::Ref { expr, rawness, mutability } => {
440 let expectation = if let Some((exp_inner, exp_rawness, exp_mutability)) =
441 &expected.ty.as_reference_or_ptr()
442 {
443 if *exp_mutability == Mutability::Mut && *mutability == Mutability::Shared {
444 // FIXME: throw type error - expected mut reference but found shared ref,
445 // which cannot be coerced
446 }
447 if *exp_rawness == Rawness::Ref && *rawness == Rawness::RawPtr {
448 // FIXME: throw type error - expected reference but found ptr,
449 // which cannot be coerced
450 }
451 Expectation::rvalue_hint(Ty::clone(exp_inner))
452 } else {
453 Expectation::none()
454 };
455 let inner_ty = self.infer_expr_inner(*expr, &expectation);
456 let ty = match rawness {
457 Rawness::RawPtr => TypeCtor::RawPtr(*mutability),
458 Rawness::Ref => TypeCtor::Ref(*mutability),
459 };
460 Ty::apply_one(ty, inner_ty)
461 }
462 Expr::Box { expr } => {
463 let inner_ty = self.infer_expr_inner(*expr, &Expectation::none());
464 if let Some(box_) = self.resolve_boxed_box() {
465 Ty::apply_one(TypeCtor::Adt(box_), inner_ty)
466 } else {
467 Ty::Unknown
468 }
469 }
470 Expr::UnaryOp { expr, op } => {
471 let inner_ty = self.infer_expr_inner(*expr, &Expectation::none());
472 match op {
473 UnaryOp::Deref => match self.resolver.krate() {
474 Some(krate) => {
475 let canonicalized = self.canonicalizer().canonicalize_ty(inner_ty);
476 match autoderef::deref(
477 self.db,
478 krate,
479 InEnvironment {
480 value: &canonicalized.value,
481 environment: self.trait_env.clone(),
482 },
483 ) {
484 Some(derefed_ty) => {
485 canonicalized.decanonicalize_ty(derefed_ty.value)
486 }
487 None => Ty::Unknown,
488 }
489 }
490 None => Ty::Unknown,
491 },
492 UnaryOp::Neg => {
493 match &inner_ty {
494 // Fast path for builtins
495 Ty::Apply(ApplicationTy {
496 ctor: TypeCtor::Int(IntTy { signedness: Signedness::Signed, .. }),
497 ..
498 })
499 | Ty::Apply(ApplicationTy { ctor: TypeCtor::Float(_), .. })
500 | Ty::Infer(InferTy::IntVar(..))
501 | Ty::Infer(InferTy::FloatVar(..)) => inner_ty,
502 // Otherwise we resolve via the std::ops::Neg trait
503 _ => self
504 .resolve_associated_type(inner_ty, self.resolve_ops_neg_output()),
505 }
506 }
507 UnaryOp::Not => {
508 match &inner_ty {
509 // Fast path for builtins
510 Ty::Apply(ApplicationTy { ctor: TypeCtor::Bool, .. })
511 | Ty::Apply(ApplicationTy { ctor: TypeCtor::Int(_), .. })
512 | Ty::Infer(InferTy::IntVar(..)) => inner_ty,
513 // Otherwise we resolve via the std::ops::Not trait
514 _ => self
515 .resolve_associated_type(inner_ty, self.resolve_ops_not_output()),
516 }
517 }
518 }
519 }
520 Expr::BinaryOp { lhs, rhs, op } => match op {
521 Some(op) => {
522 let lhs_expectation = match op {
523 BinaryOp::LogicOp(..) => Expectation::has_type(Ty::simple(TypeCtor::Bool)),
524 _ => Expectation::none(),
525 };
526 let lhs_ty = self.infer_expr(*lhs, &lhs_expectation);
527 // FIXME: find implementation of trait corresponding to operation
528 // symbol and resolve associated `Output` type
529 let rhs_expectation = op::binary_op_rhs_expectation(*op, lhs_ty.clone());
530 let rhs_ty = self.infer_expr(*rhs, &Expectation::has_type(rhs_expectation));
531
532 // FIXME: similar as above, return ty is often associated trait type
533 op::binary_op_return_ty(*op, lhs_ty, rhs_ty)
534 }
535 _ => Ty::Unknown,
536 },
537 Expr::Range { lhs, rhs, range_type } => {
538 let lhs_ty = lhs.map(|e| self.infer_expr_inner(e, &Expectation::none()));
539 let rhs_expect = lhs_ty
540 .as_ref()
541 .map_or_else(Expectation::none, |ty| Expectation::has_type(ty.clone()));
542 let rhs_ty = rhs.map(|e| self.infer_expr(e, &rhs_expect));
543 match (range_type, lhs_ty, rhs_ty) {
544 (RangeOp::Exclusive, None, None) => match self.resolve_range_full() {
545 Some(adt) => Ty::simple(TypeCtor::Adt(adt)),
546 None => Ty::Unknown,
547 },
548 (RangeOp::Exclusive, None, Some(ty)) => match self.resolve_range_to() {
549 Some(adt) => Ty::apply_one(TypeCtor::Adt(adt), ty),
550 None => Ty::Unknown,
551 },
552 (RangeOp::Inclusive, None, Some(ty)) => {
553 match self.resolve_range_to_inclusive() {
554 Some(adt) => Ty::apply_one(TypeCtor::Adt(adt), ty),
555 None => Ty::Unknown,
556 }
557 }
558 (RangeOp::Exclusive, Some(_), Some(ty)) => match self.resolve_range() {
559 Some(adt) => Ty::apply_one(TypeCtor::Adt(adt), ty),
560 None => Ty::Unknown,
561 },
562 (RangeOp::Inclusive, Some(_), Some(ty)) => {
563 match self.resolve_range_inclusive() {
564 Some(adt) => Ty::apply_one(TypeCtor::Adt(adt), ty),
565 None => Ty::Unknown,
566 }
567 }
568 (RangeOp::Exclusive, Some(ty), None) => match self.resolve_range_from() {
569 Some(adt) => Ty::apply_one(TypeCtor::Adt(adt), ty),
570 None => Ty::Unknown,
571 },
572 (RangeOp::Inclusive, _, None) => Ty::Unknown,
573 }
574 }
575 Expr::Index { base, index } => {
576 let base_ty = self.infer_expr_inner(*base, &Expectation::none());
577 let index_ty = self.infer_expr(*index, &Expectation::none());
578
579 if let (Some(index_trait), Some(krate)) =
580 (self.resolve_ops_index(), self.resolver.krate())
581 {
582 let canonicalized = self.canonicalizer().canonicalize_ty(base_ty);
583 let self_ty = method_resolution::resolve_indexing_op(
584 self.db,
585 &canonicalized.value,
586 self.trait_env.clone(),
587 krate,
588 index_trait,
589 );
590 let self_ty =
591 self_ty.map_or(Ty::Unknown, |t| canonicalized.decanonicalize_ty(t.value));
592 self.resolve_associated_type_with_params(
593 self_ty,
594 self.resolve_ops_index_output(),
595 &[index_ty],
596 )
597 } else {
598 Ty::Unknown
599 }
600 }
601 Expr::Tuple { exprs } => {
602 let mut tys = match &expected.ty {
603 ty_app!(TypeCtor::Tuple { .. }, st) => st
604 .iter()
605 .cloned()
606 .chain(repeat_with(|| self.table.new_type_var()))
607 .take(exprs.len())
608 .collect::<Vec<_>>(),
609 _ => (0..exprs.len()).map(|_| self.table.new_type_var()).collect(),
610 };
611
612 for (expr, ty) in exprs.iter().zip(tys.iter_mut()) {
613 self.infer_expr_coerce(*expr, &Expectation::has_type(ty.clone()));
614 }
615
616 Ty::apply(TypeCtor::Tuple { cardinality: tys.len() as u16 }, Substs(tys.into()))
617 }
618 Expr::Array(array) => {
619 let elem_ty = match &expected.ty {
620 ty_app!(TypeCtor::Array, st) | ty_app!(TypeCtor::Slice, st) => {
621 st.as_single().clone()
622 }
623 _ => self.table.new_type_var(),
624 };
625
626 match array {
627 Array::ElementList(items) => {
628 for expr in items.iter() {
629 self.infer_expr_coerce(*expr, &Expectation::has_type(elem_ty.clone()));
630 }
631 }
632 Array::Repeat { initializer, repeat } => {
633 self.infer_expr_coerce(
634 *initializer,
635 &Expectation::has_type(elem_ty.clone()),
636 );
637 self.infer_expr(
638 *repeat,
639 &Expectation::has_type(Ty::simple(TypeCtor::Int(IntTy::usize()))),
640 );
641 }
642 }
643
644 Ty::apply_one(TypeCtor::Array, elem_ty)
645 }
646 Expr::Literal(lit) => match lit {
647 Literal::Bool(..) => Ty::simple(TypeCtor::Bool),
648 Literal::String(..) => {
649 Ty::apply_one(TypeCtor::Ref(Mutability::Shared), Ty::simple(TypeCtor::Str))
650 }
651 Literal::ByteString(..) => {
652 let byte_type = Ty::simple(TypeCtor::Int(IntTy::u8()));
653 let array_type = Ty::apply_one(TypeCtor::Array, byte_type);
654 Ty::apply_one(TypeCtor::Ref(Mutability::Shared), array_type)
655 }
656 Literal::Char(..) => Ty::simple(TypeCtor::Char),
657 Literal::Int(_v, ty) => match ty {
658 Some(int_ty) => Ty::simple(TypeCtor::Int((*int_ty).into())),
659 None => self.table.new_integer_var(),
660 },
661 Literal::Float(_v, ty) => match ty {
662 Some(float_ty) => Ty::simple(TypeCtor::Float((*float_ty).into())),
663 None => self.table.new_float_var(),
664 },
665 },
666 };
667 // use a new type variable if we got Ty::Unknown here
668 let ty = self.insert_type_vars_shallow(ty);
669 let ty = self.resolve_ty_as_possible(ty);
670 self.write_expr_ty(tgt_expr, ty.clone());
671 ty
672 }
673
674 fn infer_block(
675 &mut self,
676 statements: &[Statement],
677 tail: Option<ExprId>,
678 expected: &Expectation,
679 ) -> Ty {
680 for stmt in statements {
681 match stmt {
682 Statement::Let { pat, type_ref, initializer } => {
683 let decl_ty =
684 type_ref.as_ref().map(|tr| self.make_ty(tr)).unwrap_or(Ty::Unknown);
685
686 // Always use the declared type when specified
687 let mut ty = decl_ty.clone();
688
689 if let Some(expr) = initializer {
690 let actual_ty =
691 self.infer_expr_coerce(*expr, &Expectation::has_type(decl_ty.clone()));
692 if decl_ty == Ty::Unknown {
693 ty = actual_ty;
694 }
695 }
696
697 let ty = self.resolve_ty_as_possible(ty);
698 self.infer_pat(*pat, &ty, BindingMode::default());
699 }
700 Statement::Expr(expr) => {
701 self.infer_expr(*expr, &Expectation::none());
702 }
703 }
704 }
705
706 let ty = if let Some(expr) = tail {
707 self.infer_expr_coerce(expr, expected)
708 } else {
709 // Citing rustc: if there is no explicit tail expression,
710 // that is typically equivalent to a tail expression
711 // of `()` -- except if the block diverges. In that
712 // case, there is no value supplied from the tail
713 // expression (assuming there are no other breaks,
714 // this implies that the type of the block will be
715 // `!`).
716 if self.diverges.is_always() {
717 // we don't even make an attempt at coercion
718 self.table.new_maybe_never_type_var()
719 } else {
720 self.coerce(&Ty::unit(), expected.coercion_target());
721 Ty::unit()
722 }
723 };
724 ty
725 }
726
727 fn infer_method_call(
728 &mut self,
729 tgt_expr: ExprId,
730 receiver: ExprId,
731 args: &[ExprId],
732 method_name: &Name,
733 generic_args: Option<&GenericArgs>,
734 ) -> Ty {
735 let receiver_ty = self.infer_expr(receiver, &Expectation::none());
736 let canonicalized_receiver = self.canonicalizer().canonicalize_ty(receiver_ty.clone());
737
738 let traits_in_scope = self.resolver.traits_in_scope(self.db.upcast());
739
740 let resolved = self.resolver.krate().and_then(|krate| {
741 method_resolution::lookup_method(
742 &canonicalized_receiver.value,
743 self.db,
744 self.trait_env.clone(),
745 krate,
746 &traits_in_scope,
747 method_name,
748 )
749 });
750 let (derefed_receiver_ty, method_ty, def_generics) = match resolved {
751 Some((ty, func)) => {
752 let ty = canonicalized_receiver.decanonicalize_ty(ty);
753 self.write_method_resolution(tgt_expr, func);
754 (ty, self.db.value_ty(func.into()), Some(generics(self.db.upcast(), func.into())))
755 }
756 None => (receiver_ty, Binders::new(0, Ty::Unknown), None),
757 };
758 let substs = self.substs_for_method_call(def_generics, generic_args, &derefed_receiver_ty);
759 let method_ty = method_ty.subst(&substs);
760 let method_ty = self.insert_type_vars(method_ty);
761 self.register_obligations_for_call(&method_ty);
762 let (expected_receiver_ty, param_tys, ret_ty) = match method_ty.callable_sig(self.db) {
763 Some(sig) => {
764 if !sig.params().is_empty() {
765 (sig.params()[0].clone(), sig.params()[1..].to_vec(), sig.ret().clone())
766 } else {
767 (Ty::Unknown, Vec::new(), sig.ret().clone())
768 }
769 }
770 None => (Ty::Unknown, Vec::new(), Ty::Unknown),
771 };
772 // Apply autoref so the below unification works correctly
773 // FIXME: return correct autorefs from lookup_method
774 let actual_receiver_ty = match expected_receiver_ty.as_reference() {
775 Some((_, mutability)) => Ty::apply_one(TypeCtor::Ref(mutability), derefed_receiver_ty),
776 _ => derefed_receiver_ty,
777 };
778 self.unify(&expected_receiver_ty, &actual_receiver_ty);
779
780 self.check_call_arguments(args, &param_tys);
781 self.normalize_associated_types_in(ret_ty)
782 }
783
784 fn check_call_arguments(&mut self, args: &[ExprId], param_tys: &[Ty]) {
785 // Quoting https://github.com/rust-lang/rust/blob/6ef275e6c3cb1384ec78128eceeb4963ff788dca/src/librustc_typeck/check/mod.rs#L3325 --
786 // We do this in a pretty awful way: first we type-check any arguments
787 // that are not closures, then we type-check the closures. This is so
788 // that we have more information about the types of arguments when we
789 // type-check the functions. This isn't really the right way to do this.
790 for &check_closures in &[false, true] {
791 let param_iter = param_tys.iter().cloned().chain(repeat(Ty::Unknown));
792 for (&arg, param_ty) in args.iter().zip(param_iter) {
793 let is_closure = matches!(&self.body[arg], Expr::Lambda { .. });
794 if is_closure != check_closures {
795 continue;
796 }
797
798 let param_ty = self.normalize_associated_types_in(param_ty);
799 self.infer_expr_coerce(arg, &Expectation::has_type(param_ty.clone()));
800 }
801 }
802 }
803
804 fn substs_for_method_call(
805 &mut self,
806 def_generics: Option<Generics>,
807 generic_args: Option<&GenericArgs>,
808 receiver_ty: &Ty,
809 ) -> Substs {
810 let (parent_params, self_params, type_params, impl_trait_params) =
811 def_generics.as_ref().map_or((0, 0, 0, 0), |g| g.provenance_split());
812 assert_eq!(self_params, 0); // method shouldn't have another Self param
813 let total_len = parent_params + type_params + impl_trait_params;
814 let mut substs = Vec::with_capacity(total_len);
815 // Parent arguments are unknown, except for the receiver type
816 if let Some(parent_generics) = def_generics.as_ref().map(|p| p.iter_parent()) {
817 for (_id, param) in parent_generics {
818 if param.provenance == hir_def::generics::TypeParamProvenance::TraitSelf {
819 substs.push(receiver_ty.clone());
820 } else {
821 substs.push(Ty::Unknown);
822 }
823 }
824 }
825 // handle provided type arguments
826 if let Some(generic_args) = generic_args {
827 // if args are provided, it should be all of them, but we can't rely on that
828 for arg in generic_args.args.iter().take(type_params) {
829 match arg {
830 GenericArg::Type(type_ref) => {
831 let ty = self.make_ty(type_ref);
832 substs.push(ty);
833 }
834 }
835 }
836 };
837 let supplied_params = substs.len();
838 for _ in supplied_params..total_len {
839 substs.push(Ty::Unknown);
840 }
841 assert_eq!(substs.len(), total_len);
842 Substs(substs.into())
843 }
844
845 fn register_obligations_for_call(&mut self, callable_ty: &Ty) {
846 if let Ty::Apply(a_ty) = callable_ty {
847 if let TypeCtor::FnDef(def) = a_ty.ctor {
848 let generic_predicates = self.db.generic_predicates(def.into());
849 for predicate in generic_predicates.iter() {
850 let predicate = predicate.clone().subst(&a_ty.parameters);
851 if let Some(obligation) = Obligation::from_predicate(predicate) {
852 self.obligations.push(obligation);
853 }
854 }
855 // add obligation for trait implementation, if this is a trait method
856 match def {
857 CallableDefId::FunctionId(f) => {
858 if let AssocContainerId::TraitId(trait_) =
859 f.lookup(self.db.upcast()).container
860 {
861 // construct a TraitDef
862 let substs = a_ty
863 .parameters
864 .prefix(generics(self.db.upcast(), trait_.into()).len());
865 self.obligations.push(Obligation::Trait(TraitRef { trait_, substs }));
866 }
867 }
868 CallableDefId::StructId(_) | CallableDefId::EnumVariantId(_) => {}
869 }
870 }
871 }
872 }
873}