diff options
Diffstat (limited to 'crates/hir_ty/src/infer/expr.rs')
-rw-r--r-- | crates/hir_ty/src/infer/expr.rs | 229 |
1 files changed, 110 insertions, 119 deletions
diff --git a/crates/hir_ty/src/infer/expr.rs b/crates/hir_ty/src/infer/expr.rs index cb59a6937..7852b3d23 100644 --- a/crates/hir_ty/src/infer/expr.rs +++ b/crates/hir_ty/src/infer/expr.rs | |||
@@ -3,8 +3,8 @@ | |||
3 | use std::iter::{repeat, repeat_with}; | 3 | use std::iter::{repeat, repeat_with}; |
4 | use std::{mem, sync::Arc}; | 4 | use std::{mem, sync::Arc}; |
5 | 5 | ||
6 | use chalk_ir::TyVariableKind; | ||
6 | use hir_def::{ | 7 | use hir_def::{ |
7 | builtin_type::Signedness, | ||
8 | expr::{Array, BinaryOp, Expr, ExprId, Literal, Statement, UnaryOp}, | 8 | expr::{Array, BinaryOp, Expr, ExprId, Literal, Statement, UnaryOp}, |
9 | path::{GenericArg, GenericArgs}, | 9 | path::{GenericArg, GenericArgs}, |
10 | resolver::resolver_for_expr, | 10 | resolver::resolver_for_expr, |
@@ -16,10 +16,11 @@ use test_utils::mark; | |||
16 | 16 | ||
17 | use crate::{ | 17 | use crate::{ |
18 | autoderef, method_resolution, op, | 18 | autoderef, method_resolution, op, |
19 | primitive::{self, UintTy}, | ||
19 | traits::{FnTrait, InEnvironment}, | 20 | traits::{FnTrait, InEnvironment}, |
20 | utils::{generics, variant_data, Generics}, | 21 | utils::{generics, variant_data, Generics}, |
21 | ApplicationTy, Binders, CallableDefId, InferTy, IntTy, Mutability, Obligation, OpaqueTyId, | 22 | Binders, CallableDefId, FnPointer, FnSig, Mutability, Obligation, OpaqueTyId, Rawness, Scalar, |
22 | Rawness, Substs, TraitRef, Ty, TypeCtor, | 23 | Substs, TraitRef, Ty, |
23 | }; | 24 | }; |
24 | 25 | ||
25 | use super::{ | 26 | use super::{ |
@@ -82,10 +83,7 @@ impl<'a> InferenceContext<'a> { | |||
82 | arg_tys.push(arg); | 83 | arg_tys.push(arg); |
83 | } | 84 | } |
84 | let parameters = param_builder.build(); | 85 | let parameters = param_builder.build(); |
85 | let arg_ty = Ty::Apply(ApplicationTy { | 86 | let arg_ty = Ty::Tuple(num_args, parameters); |
86 | ctor: TypeCtor::Tuple { cardinality: num_args as u16 }, | ||
87 | parameters, | ||
88 | }); | ||
89 | let substs = | 87 | let substs = |
90 | Substs::build_for_generics(&generic_params).push(ty.clone()).push(arg_ty).build(); | 88 | Substs::build_for_generics(&generic_params).push(ty.clone()).push(arg_ty).build(); |
91 | 89 | ||
@@ -120,7 +118,7 @@ impl<'a> InferenceContext<'a> { | |||
120 | Expr::Missing => Ty::Unknown, | 118 | Expr::Missing => Ty::Unknown, |
121 | Expr::If { condition, then_branch, else_branch } => { | 119 | Expr::If { condition, then_branch, else_branch } => { |
122 | // if let is desugared to match, so this is always simple if | 120 | // if let is desugared to match, so this is always simple if |
123 | self.infer_expr(*condition, &Expectation::has_type(Ty::simple(TypeCtor::Bool))); | 121 | self.infer_expr(*condition, &Expectation::has_type(Ty::Scalar(Scalar::Bool))); |
124 | 122 | ||
125 | let condition_diverges = mem::replace(&mut self.diverges, Diverges::Maybe); | 123 | let condition_diverges = mem::replace(&mut self.diverges, Diverges::Maybe); |
126 | let mut both_arms_diverge = Diverges::Always; | 124 | let mut both_arms_diverge = Diverges::Always; |
@@ -175,7 +173,7 @@ impl<'a> InferenceContext<'a> { | |||
175 | // existenail type AsyncBlockImplTrait<InnerType>: Future<Output = InnerType> | 173 | // existenail type AsyncBlockImplTrait<InnerType>: Future<Output = InnerType> |
176 | let inner_ty = self.infer_expr(*body, &Expectation::none()); | 174 | let inner_ty = self.infer_expr(*body, &Expectation::none()); |
177 | let opaque_ty_id = OpaqueTyId::AsyncBlockTypeImplTrait(self.owner, *body); | 175 | let opaque_ty_id = OpaqueTyId::AsyncBlockTypeImplTrait(self.owner, *body); |
178 | Ty::apply_one(TypeCtor::OpaqueType(opaque_ty_id), inner_ty) | 176 | Ty::OpaqueType(opaque_ty_id, Substs::single(inner_ty)) |
179 | } | 177 | } |
180 | Expr::Loop { body, label } => { | 178 | Expr::Loop { body, label } => { |
181 | self.breakables.push(BreakableContext { | 179 | self.breakables.push(BreakableContext { |
@@ -193,7 +191,7 @@ impl<'a> InferenceContext<'a> { | |||
193 | if ctxt.may_break { | 191 | if ctxt.may_break { |
194 | ctxt.break_ty | 192 | ctxt.break_ty |
195 | } else { | 193 | } else { |
196 | Ty::simple(TypeCtor::Never) | 194 | Ty::Never |
197 | } | 195 | } |
198 | } | 196 | } |
199 | Expr::While { condition, body, label } => { | 197 | Expr::While { condition, body, label } => { |
@@ -203,7 +201,7 @@ impl<'a> InferenceContext<'a> { | |||
203 | label: label.map(|label| self.body[label].name.clone()), | 201 | label: label.map(|label| self.body[label].name.clone()), |
204 | }); | 202 | }); |
205 | // while let is desugared to a match loop, so this is always simple while | 203 | // while let is desugared to a match loop, so this is always simple while |
206 | self.infer_expr(*condition, &Expectation::has_type(Ty::simple(TypeCtor::Bool))); | 204 | self.infer_expr(*condition, &Expectation::has_type(Ty::Scalar(Scalar::Bool))); |
207 | self.infer_expr(*body, &Expectation::has_type(Ty::unit())); | 205 | self.infer_expr(*body, &Expectation::has_type(Ty::unit())); |
208 | let _ctxt = self.breakables.pop().expect("breakable stack broken"); | 206 | let _ctxt = self.breakables.pop().expect("breakable stack broken"); |
209 | // the body may not run, so it diverging doesn't mean we diverge | 207 | // the body may not run, so it diverging doesn't mean we diverge |
@@ -250,12 +248,12 @@ impl<'a> InferenceContext<'a> { | |||
250 | None => self.table.new_type_var(), | 248 | None => self.table.new_type_var(), |
251 | }; | 249 | }; |
252 | sig_tys.push(ret_ty.clone()); | 250 | sig_tys.push(ret_ty.clone()); |
253 | let sig_ty = Ty::apply( | 251 | let sig_ty = Ty::Function(FnPointer { |
254 | TypeCtor::FnPtr { num_args: sig_tys.len() as u16 - 1, is_varargs: false }, | 252 | num_args: sig_tys.len() - 1, |
255 | Substs(sig_tys.clone().into()), | 253 | sig: FnSig { variadic: false }, |
256 | ); | 254 | substs: Substs(sig_tys.clone().into()), |
257 | let closure_ty = | 255 | }); |
258 | Ty::apply_one(TypeCtor::Closure { def: self.owner, expr: tgt_expr }, sig_ty); | 256 | let closure_ty = Ty::Closure(self.owner, tgt_expr, Substs::single(sig_ty)); |
259 | 257 | ||
260 | // Eagerly try to relate the closure type with the expected | 258 | // Eagerly try to relate the closure type with the expected |
261 | // type, otherwise we often won't have enough information to | 259 | // type, otherwise we often won't have enough information to |
@@ -306,11 +304,8 @@ impl<'a> InferenceContext<'a> { | |||
306 | Expr::Match { expr, arms } => { | 304 | Expr::Match { expr, arms } => { |
307 | let input_ty = self.infer_expr(*expr, &Expectation::none()); | 305 | let input_ty = self.infer_expr(*expr, &Expectation::none()); |
308 | 306 | ||
309 | let mut result_ty = if arms.is_empty() { | 307 | let mut result_ty = |
310 | Ty::simple(TypeCtor::Never) | 308 | if arms.is_empty() { Ty::Never } else { self.table.new_type_var() }; |
311 | } else { | ||
312 | self.table.new_type_var() | ||
313 | }; | ||
314 | 309 | ||
315 | let matchee_diverges = self.diverges; | 310 | let matchee_diverges = self.diverges; |
316 | let mut all_arms_diverge = Diverges::Always; | 311 | let mut all_arms_diverge = Diverges::Always; |
@@ -321,7 +316,7 @@ impl<'a> InferenceContext<'a> { | |||
321 | if let Some(guard_expr) = arm.guard { | 316 | if let Some(guard_expr) = arm.guard { |
322 | self.infer_expr( | 317 | self.infer_expr( |
323 | guard_expr, | 318 | guard_expr, |
324 | &Expectation::has_type(Ty::simple(TypeCtor::Bool)), | 319 | &Expectation::has_type(Ty::Scalar(Scalar::Bool)), |
325 | ); | 320 | ); |
326 | } | 321 | } |
327 | 322 | ||
@@ -339,7 +334,7 @@ impl<'a> InferenceContext<'a> { | |||
339 | let resolver = resolver_for_expr(self.db.upcast(), self.owner, tgt_expr); | 334 | let resolver = resolver_for_expr(self.db.upcast(), self.owner, tgt_expr); |
340 | self.infer_path(&resolver, p, tgt_expr.into()).unwrap_or(Ty::Unknown) | 335 | self.infer_path(&resolver, p, tgt_expr.into()).unwrap_or(Ty::Unknown) |
341 | } | 336 | } |
342 | Expr::Continue { .. } => Ty::simple(TypeCtor::Never), | 337 | Expr::Continue { .. } => Ty::Never, |
343 | Expr::Break { expr, label } => { | 338 | Expr::Break { expr, label } => { |
344 | let val_ty = if let Some(expr) = expr { | 339 | let val_ty = if let Some(expr) = expr { |
345 | self.infer_expr(*expr, &Expectation::none()) | 340 | self.infer_expr(*expr, &Expectation::none()) |
@@ -364,8 +359,7 @@ impl<'a> InferenceContext<'a> { | |||
364 | expr: tgt_expr, | 359 | expr: tgt_expr, |
365 | }); | 360 | }); |
366 | } | 361 | } |
367 | 362 | Ty::Never | |
368 | Ty::simple(TypeCtor::Never) | ||
369 | } | 363 | } |
370 | Expr::Return { expr } => { | 364 | Expr::Return { expr } => { |
371 | if let Some(expr) = expr { | 365 | if let Some(expr) = expr { |
@@ -374,14 +368,14 @@ impl<'a> InferenceContext<'a> { | |||
374 | let unit = Ty::unit(); | 368 | let unit = Ty::unit(); |
375 | self.coerce(&unit, &self.return_ty.clone()); | 369 | self.coerce(&unit, &self.return_ty.clone()); |
376 | } | 370 | } |
377 | Ty::simple(TypeCtor::Never) | 371 | Ty::Never |
378 | } | 372 | } |
379 | Expr::Yield { expr } => { | 373 | Expr::Yield { expr } => { |
380 | // FIXME: track yield type for coercion | 374 | // FIXME: track yield type for coercion |
381 | if let Some(expr) = expr { | 375 | if let Some(expr) = expr { |
382 | self.infer_expr(*expr, &Expectation::none()); | 376 | self.infer_expr(*expr, &Expectation::none()); |
383 | } | 377 | } |
384 | Ty::simple(TypeCtor::Never) | 378 | Ty::Never |
385 | } | 379 | } |
386 | Expr::RecordLit { path, fields, spread } => { | 380 | Expr::RecordLit { path, fields, spread } => { |
387 | let (ty, def_id) = self.resolve_variant(path.as_ref()); | 381 | let (ty, def_id) = self.resolve_variant(path.as_ref()); |
@@ -391,7 +385,7 @@ impl<'a> InferenceContext<'a> { | |||
391 | 385 | ||
392 | self.unify(&ty, &expected.ty); | 386 | self.unify(&ty, &expected.ty); |
393 | 387 | ||
394 | let substs = ty.substs().unwrap_or_else(Substs::empty); | 388 | let substs = ty.substs().cloned().unwrap_or_else(Substs::empty); |
395 | let field_types = def_id.map(|it| self.db.field_types(it)).unwrap_or_default(); | 389 | let field_types = def_id.map(|it| self.db.field_types(it)).unwrap_or_default(); |
396 | let variant_data = def_id.map(|it| variant_data(self.db.upcast(), it)); | 390 | let variant_data = def_id.map(|it| variant_data(self.db.upcast(), it)); |
397 | for (field_idx, field) in fields.iter().enumerate() { | 391 | for (field_idx, field) in fields.iter().enumerate() { |
@@ -430,30 +424,23 @@ impl<'a> InferenceContext<'a> { | |||
430 | }, | 424 | }, |
431 | ) | 425 | ) |
432 | .find_map(|derefed_ty| match canonicalized.decanonicalize_ty(derefed_ty.value) { | 426 | .find_map(|derefed_ty| match canonicalized.decanonicalize_ty(derefed_ty.value) { |
433 | Ty::Apply(a_ty) => match a_ty.ctor { | 427 | Ty::Tuple(_, substs) => { |
434 | TypeCtor::Tuple { .. } => name | 428 | name.as_tuple_index().and_then(|idx| substs.0.get(idx).cloned()) |
435 | .as_tuple_index() | 429 | } |
436 | .and_then(|idx| a_ty.parameters.0.get(idx).cloned()), | 430 | Ty::Adt(AdtId::StructId(s), parameters) => { |
437 | TypeCtor::Adt(AdtId::StructId(s)) => { | 431 | self.db.struct_data(s).variant_data.field(name).map(|local_id| { |
438 | self.db.struct_data(s).variant_data.field(name).map(|local_id| { | 432 | let field = FieldId { parent: s.into(), local_id }; |
439 | let field = FieldId { parent: s.into(), local_id }; | 433 | self.write_field_resolution(tgt_expr, field); |
440 | self.write_field_resolution(tgt_expr, field); | 434 | self.db.field_types(s.into())[field.local_id].clone().subst(¶meters) |
441 | self.db.field_types(s.into())[field.local_id] | 435 | }) |
442 | .clone() | 436 | } |
443 | .subst(&a_ty.parameters) | 437 | Ty::Adt(AdtId::UnionId(u), parameters) => { |
444 | }) | 438 | self.db.union_data(u).variant_data.field(name).map(|local_id| { |
445 | } | 439 | let field = FieldId { parent: u.into(), local_id }; |
446 | TypeCtor::Adt(AdtId::UnionId(u)) => { | 440 | self.write_field_resolution(tgt_expr, field); |
447 | self.db.union_data(u).variant_data.field(name).map(|local_id| { | 441 | self.db.field_types(u.into())[field.local_id].clone().subst(¶meters) |
448 | let field = FieldId { parent: u.into(), local_id }; | 442 | }) |
449 | self.write_field_resolution(tgt_expr, field); | 443 | } |
450 | self.db.field_types(u.into())[field.local_id] | ||
451 | .clone() | ||
452 | .subst(&a_ty.parameters) | ||
453 | }) | ||
454 | } | ||
455 | _ => None, | ||
456 | }, | ||
457 | _ => None, | 444 | _ => None, |
458 | }) | 445 | }) |
459 | .unwrap_or(Ty::Unknown); | 446 | .unwrap_or(Ty::Unknown); |
@@ -491,19 +478,24 @@ impl<'a> InferenceContext<'a> { | |||
491 | Expectation::none() | 478 | Expectation::none() |
492 | }; | 479 | }; |
493 | let inner_ty = self.infer_expr_inner(*expr, &expectation); | 480 | let inner_ty = self.infer_expr_inner(*expr, &expectation); |
494 | let ty = match rawness { | 481 | match rawness { |
495 | Rawness::RawPtr => TypeCtor::RawPtr(*mutability), | 482 | Rawness::RawPtr => Ty::Raw(*mutability, Substs::single(inner_ty)), |
496 | Rawness::Ref => TypeCtor::Ref(*mutability), | 483 | Rawness::Ref => Ty::Ref(*mutability, Substs::single(inner_ty)), |
497 | }; | 484 | } |
498 | Ty::apply_one(ty, inner_ty) | ||
499 | } | 485 | } |
500 | Expr::Box { expr } => { | 486 | Expr::Box { expr } => { |
501 | let inner_ty = self.infer_expr_inner(*expr, &Expectation::none()); | 487 | let inner_ty = self.infer_expr_inner(*expr, &Expectation::none()); |
502 | if let Some(box_) = self.resolve_boxed_box() { | 488 | if let Some(box_) = self.resolve_boxed_box() { |
503 | let mut sb = Substs::build_for_type_ctor(self.db, TypeCtor::Adt(box_)); | 489 | let mut sb = Substs::builder(generics(self.db.upcast(), box_.into()).len()); |
504 | sb = sb.push(inner_ty); | 490 | sb = sb.push(inner_ty); |
491 | match self.db.generic_defaults(box_.into()).as_ref() { | ||
492 | [_, alloc_ty, ..] if !alloc_ty.value.is_unknown() => { | ||
493 | sb = sb.push(alloc_ty.value.clone()); | ||
494 | } | ||
495 | _ => (), | ||
496 | } | ||
505 | sb = sb.fill(repeat_with(|| self.table.new_type_var())); | 497 | sb = sb.fill(repeat_with(|| self.table.new_type_var())); |
506 | Ty::apply(TypeCtor::Adt(box_), sb.build()) | 498 | Ty::Adt(box_, sb.build()) |
507 | } else { | 499 | } else { |
508 | Ty::Unknown | 500 | Ty::Unknown |
509 | } | 501 | } |
@@ -533,13 +525,11 @@ impl<'a> InferenceContext<'a> { | |||
533 | UnaryOp::Neg => { | 525 | UnaryOp::Neg => { |
534 | match &inner_ty { | 526 | match &inner_ty { |
535 | // Fast path for builtins | 527 | // Fast path for builtins |
536 | Ty::Apply(ApplicationTy { | 528 | Ty::Scalar(Scalar::Int(_)) |
537 | ctor: TypeCtor::Int(IntTy { signedness: Signedness::Signed, .. }), | 529 | | Ty::Scalar(Scalar::Uint(_)) |
538 | .. | 530 | | Ty::Scalar(Scalar::Float(_)) |
539 | }) | 531 | | Ty::InferenceVar(_, TyVariableKind::Integer) |
540 | | Ty::Apply(ApplicationTy { ctor: TypeCtor::Float(_), .. }) | 532 | | Ty::InferenceVar(_, TyVariableKind::Float) => inner_ty, |
541 | | Ty::Infer(InferTy::IntVar(..)) | ||
542 | | Ty::Infer(InferTy::FloatVar(..)) => inner_ty, | ||
543 | // Otherwise we resolve via the std::ops::Neg trait | 533 | // Otherwise we resolve via the std::ops::Neg trait |
544 | _ => self | 534 | _ => self |
545 | .resolve_associated_type(inner_ty, self.resolve_ops_neg_output()), | 535 | .resolve_associated_type(inner_ty, self.resolve_ops_neg_output()), |
@@ -548,9 +538,10 @@ impl<'a> InferenceContext<'a> { | |||
548 | UnaryOp::Not => { | 538 | UnaryOp::Not => { |
549 | match &inner_ty { | 539 | match &inner_ty { |
550 | // Fast path for builtins | 540 | // Fast path for builtins |
551 | Ty::Apply(ApplicationTy { ctor: TypeCtor::Bool, .. }) | 541 | Ty::Scalar(Scalar::Bool) |
552 | | Ty::Apply(ApplicationTy { ctor: TypeCtor::Int(_), .. }) | 542 | | Ty::Scalar(Scalar::Int(_)) |
553 | | Ty::Infer(InferTy::IntVar(..)) => inner_ty, | 543 | | Ty::Scalar(Scalar::Uint(_)) |
544 | | Ty::InferenceVar(_, TyVariableKind::Integer) => inner_ty, | ||
554 | // Otherwise we resolve via the std::ops::Not trait | 545 | // Otherwise we resolve via the std::ops::Not trait |
555 | _ => self | 546 | _ => self |
556 | .resolve_associated_type(inner_ty, self.resolve_ops_not_output()), | 547 | .resolve_associated_type(inner_ty, self.resolve_ops_not_output()), |
@@ -561,7 +552,7 @@ impl<'a> InferenceContext<'a> { | |||
561 | Expr::BinaryOp { lhs, rhs, op } => match op { | 552 | Expr::BinaryOp { lhs, rhs, op } => match op { |
562 | Some(op) => { | 553 | Some(op) => { |
563 | let lhs_expectation = match op { | 554 | let lhs_expectation = match op { |
564 | BinaryOp::LogicOp(..) => Expectation::has_type(Ty::simple(TypeCtor::Bool)), | 555 | BinaryOp::LogicOp(..) => Expectation::has_type(Ty::Scalar(Scalar::Bool)), |
565 | _ => Expectation::none(), | 556 | _ => Expectation::none(), |
566 | }; | 557 | }; |
567 | let lhs_ty = self.infer_expr(*lhs, &lhs_expectation); | 558 | let lhs_ty = self.infer_expr(*lhs, &lhs_expectation); |
@@ -592,31 +583,31 @@ impl<'a> InferenceContext<'a> { | |||
592 | let rhs_ty = rhs.map(|e| self.infer_expr(e, &rhs_expect)); | 583 | let rhs_ty = rhs.map(|e| self.infer_expr(e, &rhs_expect)); |
593 | match (range_type, lhs_ty, rhs_ty) { | 584 | match (range_type, lhs_ty, rhs_ty) { |
594 | (RangeOp::Exclusive, None, None) => match self.resolve_range_full() { | 585 | (RangeOp::Exclusive, None, None) => match self.resolve_range_full() { |
595 | Some(adt) => Ty::simple(TypeCtor::Adt(adt)), | 586 | Some(adt) => Ty::Adt(adt, Substs::empty()), |
596 | None => Ty::Unknown, | 587 | None => Ty::Unknown, |
597 | }, | 588 | }, |
598 | (RangeOp::Exclusive, None, Some(ty)) => match self.resolve_range_to() { | 589 | (RangeOp::Exclusive, None, Some(ty)) => match self.resolve_range_to() { |
599 | Some(adt) => Ty::apply_one(TypeCtor::Adt(adt), ty), | 590 | Some(adt) => Ty::Adt(adt, Substs::single(ty)), |
600 | None => Ty::Unknown, | 591 | None => Ty::Unknown, |
601 | }, | 592 | }, |
602 | (RangeOp::Inclusive, None, Some(ty)) => { | 593 | (RangeOp::Inclusive, None, Some(ty)) => { |
603 | match self.resolve_range_to_inclusive() { | 594 | match self.resolve_range_to_inclusive() { |
604 | Some(adt) => Ty::apply_one(TypeCtor::Adt(adt), ty), | 595 | Some(adt) => Ty::Adt(adt, Substs::single(ty)), |
605 | None => Ty::Unknown, | 596 | None => Ty::Unknown, |
606 | } | 597 | } |
607 | } | 598 | } |
608 | (RangeOp::Exclusive, Some(_), Some(ty)) => match self.resolve_range() { | 599 | (RangeOp::Exclusive, Some(_), Some(ty)) => match self.resolve_range() { |
609 | Some(adt) => Ty::apply_one(TypeCtor::Adt(adt), ty), | 600 | Some(adt) => Ty::Adt(adt, Substs::single(ty)), |
610 | None => Ty::Unknown, | 601 | None => Ty::Unknown, |
611 | }, | 602 | }, |
612 | (RangeOp::Inclusive, Some(_), Some(ty)) => { | 603 | (RangeOp::Inclusive, Some(_), Some(ty)) => { |
613 | match self.resolve_range_inclusive() { | 604 | match self.resolve_range_inclusive() { |
614 | Some(adt) => Ty::apply_one(TypeCtor::Adt(adt), ty), | 605 | Some(adt) => Ty::Adt(adt, Substs::single(ty)), |
615 | None => Ty::Unknown, | 606 | None => Ty::Unknown, |
616 | } | 607 | } |
617 | } | 608 | } |
618 | (RangeOp::Exclusive, Some(ty), None) => match self.resolve_range_from() { | 609 | (RangeOp::Exclusive, Some(ty), None) => match self.resolve_range_from() { |
619 | Some(adt) => Ty::apply_one(TypeCtor::Adt(adt), ty), | 610 | Some(adt) => Ty::Adt(adt, Substs::single(ty)), |
620 | None => Ty::Unknown, | 611 | None => Ty::Unknown, |
621 | }, | 612 | }, |
622 | (RangeOp::Inclusive, _, None) => Ty::Unknown, | 613 | (RangeOp::Inclusive, _, None) => Ty::Unknown, |
@@ -650,7 +641,7 @@ impl<'a> InferenceContext<'a> { | |||
650 | } | 641 | } |
651 | Expr::Tuple { exprs } => { | 642 | Expr::Tuple { exprs } => { |
652 | let mut tys = match &expected.ty { | 643 | let mut tys = match &expected.ty { |
653 | ty_app!(TypeCtor::Tuple { .. }, st) => st | 644 | Ty::Tuple(_, substs) => substs |
654 | .iter() | 645 | .iter() |
655 | .cloned() | 646 | .cloned() |
656 | .chain(repeat_with(|| self.table.new_type_var())) | 647 | .chain(repeat_with(|| self.table.new_type_var())) |
@@ -663,15 +654,11 @@ impl<'a> InferenceContext<'a> { | |||
663 | self.infer_expr_coerce(*expr, &Expectation::has_type(ty.clone())); | 654 | self.infer_expr_coerce(*expr, &Expectation::has_type(ty.clone())); |
664 | } | 655 | } |
665 | 656 | ||
666 | Ty::apply(TypeCtor::Tuple { cardinality: tys.len() as u16 }, Substs(tys.into())) | 657 | Ty::Tuple(tys.len(), Substs(tys.into())) |
667 | } | 658 | } |
668 | Expr::Array(array) => { | 659 | Expr::Array(array) => { |
669 | let elem_ty = match &expected.ty { | 660 | let elem_ty = match &expected.ty { |
670 | // FIXME: remove when https://github.com/rust-lang/rust/issues/80501 is fixed | 661 | Ty::Array(st) | Ty::Slice(st) => st.as_single().clone(), |
671 | #[allow(unreachable_patterns)] | ||
672 | ty_app!(TypeCtor::Array, st) | ty_app!(TypeCtor::Slice, st) => { | ||
673 | st.as_single().clone() | ||
674 | } | ||
675 | _ => self.table.new_type_var(), | 662 | _ => self.table.new_type_var(), |
676 | }; | 663 | }; |
677 | 664 | ||
@@ -688,30 +675,38 @@ impl<'a> InferenceContext<'a> { | |||
688 | ); | 675 | ); |
689 | self.infer_expr( | 676 | self.infer_expr( |
690 | *repeat, | 677 | *repeat, |
691 | &Expectation::has_type(Ty::simple(TypeCtor::Int(IntTy::usize()))), | 678 | &Expectation::has_type(Ty::Scalar(Scalar::Uint(UintTy::Usize))), |
692 | ); | 679 | ); |
693 | } | 680 | } |
694 | } | 681 | } |
695 | 682 | ||
696 | Ty::apply_one(TypeCtor::Array, elem_ty) | 683 | Ty::Array(Substs::single(elem_ty)) |
697 | } | 684 | } |
698 | Expr::Literal(lit) => match lit { | 685 | Expr::Literal(lit) => match lit { |
699 | Literal::Bool(..) => Ty::simple(TypeCtor::Bool), | 686 | Literal::Bool(..) => Ty::Scalar(Scalar::Bool), |
700 | Literal::String(..) => { | 687 | Literal::String(..) => Ty::Ref(Mutability::Shared, Substs::single(Ty::Str)), |
701 | Ty::apply_one(TypeCtor::Ref(Mutability::Shared), Ty::simple(TypeCtor::Str)) | ||
702 | } | ||
703 | Literal::ByteString(..) => { | 688 | Literal::ByteString(..) => { |
704 | let byte_type = Ty::simple(TypeCtor::Int(IntTy::u8())); | 689 | let byte_type = Ty::Scalar(Scalar::Uint(UintTy::U8)); |
705 | let array_type = Ty::apply_one(TypeCtor::Array, byte_type); | 690 | let array_type = Ty::Array(Substs::single(byte_type)); |
706 | Ty::apply_one(TypeCtor::Ref(Mutability::Shared), array_type) | 691 | Ty::Ref(Mutability::Shared, Substs::single(array_type)) |
707 | } | 692 | } |
708 | Literal::Char(..) => Ty::simple(TypeCtor::Char), | 693 | Literal::Char(..) => Ty::Scalar(Scalar::Char), |
709 | Literal::Int(_v, ty) => match ty { | 694 | Literal::Int(_v, ty) => match ty { |
710 | Some(int_ty) => Ty::simple(TypeCtor::Int((*int_ty).into())), | 695 | Some(int_ty) => { |
696 | Ty::Scalar(Scalar::Int(primitive::int_ty_from_builtin(*int_ty))) | ||
697 | } | ||
698 | None => self.table.new_integer_var(), | ||
699 | }, | ||
700 | Literal::Uint(_v, ty) => match ty { | ||
701 | Some(int_ty) => { | ||
702 | Ty::Scalar(Scalar::Uint(primitive::uint_ty_from_builtin(*int_ty))) | ||
703 | } | ||
711 | None => self.table.new_integer_var(), | 704 | None => self.table.new_integer_var(), |
712 | }, | 705 | }, |
713 | Literal::Float(_v, ty) => match ty { | 706 | Literal::Float(_v, ty) => match ty { |
714 | Some(float_ty) => Ty::simple(TypeCtor::Float((*float_ty).into())), | 707 | Some(float_ty) => { |
708 | Ty::Scalar(Scalar::Float(primitive::float_ty_from_builtin(*float_ty))) | ||
709 | } | ||
715 | None => self.table.new_float_var(), | 710 | None => self.table.new_float_var(), |
716 | }, | 711 | }, |
717 | }, | 712 | }, |
@@ -767,7 +762,7 @@ impl<'a> InferenceContext<'a> { | |||
767 | // `!`). | 762 | // `!`). |
768 | if self.diverges.is_always() { | 763 | if self.diverges.is_always() { |
769 | // we don't even make an attempt at coercion | 764 | // we don't even make an attempt at coercion |
770 | self.table.new_maybe_never_type_var() | 765 | self.table.new_maybe_never_var() |
771 | } else { | 766 | } else { |
772 | self.coerce(&Ty::unit(), expected.coercion_target()); | 767 | self.coerce(&Ty::unit(), expected.coercion_target()); |
773 | Ty::unit() | 768 | Ty::unit() |
@@ -824,7 +819,7 @@ impl<'a> InferenceContext<'a> { | |||
824 | // Apply autoref so the below unification works correctly | 819 | // Apply autoref so the below unification works correctly |
825 | // FIXME: return correct autorefs from lookup_method | 820 | // FIXME: return correct autorefs from lookup_method |
826 | let actual_receiver_ty = match expected_receiver_ty.as_reference() { | 821 | let actual_receiver_ty = match expected_receiver_ty.as_reference() { |
827 | Some((_, mutability)) => Ty::apply_one(TypeCtor::Ref(mutability), derefed_receiver_ty), | 822 | Some((_, mutability)) => Ty::Ref(mutability, Substs::single(derefed_receiver_ty)), |
828 | _ => derefed_receiver_ty, | 823 | _ => derefed_receiver_ty, |
829 | }; | 824 | }; |
830 | self.unify(&expected_receiver_ty, &actual_receiver_ty); | 825 | self.unify(&expected_receiver_ty, &actual_receiver_ty); |
@@ -901,30 +896,26 @@ impl<'a> InferenceContext<'a> { | |||
901 | } | 896 | } |
902 | 897 | ||
903 | fn register_obligations_for_call(&mut self, callable_ty: &Ty) { | 898 | fn register_obligations_for_call(&mut self, callable_ty: &Ty) { |
904 | if let Ty::Apply(a_ty) = callable_ty { | 899 | if let &Ty::FnDef(def, ref parameters) = callable_ty { |
905 | if let TypeCtor::FnDef(def) = a_ty.ctor { | 900 | let generic_predicates = self.db.generic_predicates(def.into()); |
906 | let generic_predicates = self.db.generic_predicates(def.into()); | 901 | for predicate in generic_predicates.iter() { |
907 | for predicate in generic_predicates.iter() { | 902 | let predicate = predicate.clone().subst(parameters); |
908 | let predicate = predicate.clone().subst(&a_ty.parameters); | 903 | if let Some(obligation) = Obligation::from_predicate(predicate) { |
909 | if let Some(obligation) = Obligation::from_predicate(predicate) { | 904 | self.obligations.push(obligation); |
910 | self.obligations.push(obligation); | ||
911 | } | ||
912 | } | 905 | } |
913 | // add obligation for trait implementation, if this is a trait method | 906 | } |
914 | match def { | 907 | // add obligation for trait implementation, if this is a trait method |
915 | CallableDefId::FunctionId(f) => { | 908 | match def { |
916 | if let AssocContainerId::TraitId(trait_) = | 909 | CallableDefId::FunctionId(f) => { |
917 | f.lookup(self.db.upcast()).container | 910 | if let AssocContainerId::TraitId(trait_) = f.lookup(self.db.upcast()).container |
918 | { | 911 | { |
919 | // construct a TraitDef | 912 | // construct a TraitDef |
920 | let substs = a_ty | 913 | let substs = |
921 | .parameters | 914 | parameters.prefix(generics(self.db.upcast(), trait_.into()).len()); |
922 | .prefix(generics(self.db.upcast(), trait_.into()).len()); | 915 | self.obligations.push(Obligation::Trait(TraitRef { trait_, substs })); |
923 | self.obligations.push(Obligation::Trait(TraitRef { trait_, substs })); | ||
924 | } | ||
925 | } | 916 | } |
926 | CallableDefId::StructId(_) | CallableDefId::EnumVariantId(_) => {} | ||
927 | } | 917 | } |
918 | CallableDefId::StructId(_) | CallableDefId::EnumVariantId(_) => {} | ||
928 | } | 919 | } |
929 | } | 920 | } |
930 | } | 921 | } |