diff options
author | Florian Diebold <[email protected]> | 2019-09-26 20:37:03 +0100 |
---|---|---|
committer | Florian Diebold <[email protected]> | 2019-09-26 22:03:39 +0100 |
commit | daaf46177e5dc63e20e5a1ec5692e53cc8f7bc34 (patch) | |
tree | 2229733948df98f119aee5eca600a50726192c68 /crates/ra_hir/src/ty/traits | |
parent | 1002e470747fe5887a2915689e21d9be3a1ca5d8 (diff) |
Add SubstsBuilder
+ further refactoring.
Diffstat (limited to 'crates/ra_hir/src/ty/traits')
-rw-r--r-- | crates/ra_hir/src/ty/traits/chalk.rs | 80 |
1 files changed, 19 insertions, 61 deletions
diff --git a/crates/ra_hir/src/ty/traits/chalk.rs b/crates/ra_hir/src/ty/traits/chalk.rs index 538b4d3ec..2642a54bf 100644 --- a/crates/ra_hir/src/ty/traits/chalk.rs +++ b/crates/ra_hir/src/ty/traits/chalk.rs | |||
@@ -10,17 +10,13 @@ use chalk_ir::{ | |||
10 | use chalk_rust_ir::{AssociatedTyDatum, ImplDatum, StructDatum, TraitDatum}; | 10 | use chalk_rust_ir::{AssociatedTyDatum, ImplDatum, StructDatum, TraitDatum}; |
11 | 11 | ||
12 | use ra_db::salsa::{InternId, InternKey}; | 12 | use ra_db::salsa::{InternId, InternKey}; |
13 | use test_utils::tested_by; | ||
14 | 13 | ||
15 | use super::{Canonical, ChalkContext, Impl, Obligation}; | 14 | use super::{Canonical, ChalkContext, Impl, Obligation}; |
16 | use crate::{ | 15 | use crate::{ |
17 | db::HirDatabase, | 16 | db::HirDatabase, |
18 | generics::GenericDef, | 17 | generics::GenericDef, |
19 | ty::display::HirDisplay, | 18 | ty::display::HirDisplay, |
20 | ty::{ | 19 | ty::{ApplicationTy, GenericPredicate, ProjectionTy, Substs, TraitRef, Ty, TypeCtor, TypeWalk}, |
21 | ApplicationTy, CallableDef, GenericPredicate, ProjectionTy, Substs, TraitRef, Ty, TypeCtor, | ||
22 | TypeWalk, | ||
23 | }, | ||
24 | AssocItem, Crate, HasGenericParams, ImplBlock, Trait, TypeAlias, | 20 | AssocItem, Crate, HasGenericParams, ImplBlock, Trait, TypeAlias, |
25 | }; | 21 | }; |
26 | 22 | ||
@@ -124,14 +120,15 @@ impl ToChalk for Substs { | |||
124 | } | 120 | } |
125 | 121 | ||
126 | fn from_chalk(db: &impl HirDatabase, parameters: Vec<chalk_ir::Parameter>) -> Substs { | 122 | fn from_chalk(db: &impl HirDatabase, parameters: Vec<chalk_ir::Parameter>) -> Substs { |
127 | parameters | 123 | let tys = parameters |
128 | .into_iter() | 124 | .into_iter() |
129 | .map(|p| match p { | 125 | .map(|p| match p { |
130 | chalk_ir::Parameter(chalk_ir::ParameterKind::Ty(ty)) => from_chalk(db, ty), | 126 | chalk_ir::Parameter(chalk_ir::ParameterKind::Ty(ty)) => from_chalk(db, ty), |
131 | chalk_ir::Parameter(chalk_ir::ParameterKind::Lifetime(_)) => unimplemented!(), | 127 | chalk_ir::Parameter(chalk_ir::ParameterKind::Lifetime(_)) => unimplemented!(), |
132 | }) | 128 | }) |
133 | .collect::<Vec<_>>() | 129 | .collect::<Vec<_>>() |
134 | .into() | 130 | .into(); |
131 | Substs(tys) | ||
135 | } | 132 | } |
136 | } | 133 | } |
137 | 134 | ||
@@ -539,60 +536,18 @@ pub(crate) fn struct_datum_query( | |||
539 | struct_id: chalk_ir::StructId, | 536 | struct_id: chalk_ir::StructId, |
540 | ) -> Arc<StructDatum> { | 537 | ) -> Arc<StructDatum> { |
541 | debug!("struct_datum {:?}", struct_id); | 538 | debug!("struct_datum {:?}", struct_id); |
542 | let type_ctor = from_chalk(db, struct_id); | 539 | let type_ctor: TypeCtor = from_chalk(db, struct_id); |
543 | debug!("struct {:?} = {:?}", struct_id, type_ctor); | 540 | debug!("struct {:?} = {:?}", struct_id, type_ctor); |
544 | // FIXME might be nicer if we can create a fake GenericParams for the TypeCtor | 541 | let num_params = type_ctor.num_ty_params(db); |
545 | // FIXME extract this to a method on Ty | 542 | let upstream = type_ctor.krate(db) != Some(krate); |
546 | let (num_params, where_clauses, upstream) = match type_ctor { | 543 | let where_clauses = type_ctor |
547 | TypeCtor::Bool | 544 | .as_generic_def() |
548 | | TypeCtor::Char | 545 | .map(|generic_def| { |
549 | | TypeCtor::Int(_) | ||
550 | | TypeCtor::Float(_) | ||
551 | | TypeCtor::Never | ||
552 | | TypeCtor::Str => (0, vec![], true), | ||
553 | TypeCtor::Slice | TypeCtor::Array | TypeCtor::RawPtr(_) | TypeCtor::Ref(_) => { | ||
554 | (1, vec![], true) | ||
555 | } | ||
556 | TypeCtor::FnPtr { num_args } => (num_args as usize + 1, vec![], true), | ||
557 | TypeCtor::Tuple { cardinality } => (cardinality as usize, vec![], true), | ||
558 | TypeCtor::FnDef(callable) => { | ||
559 | tested_by!(trait_resolution_on_fn_type); | ||
560 | let upstream = match callable { | ||
561 | CallableDef::Function(f) => f.module(db).krate(db), | ||
562 | CallableDef::Struct(s) => s.module(db).krate(db), | ||
563 | CallableDef::EnumVariant(v) => v.parent_enum(db).module(db).krate(db), | ||
564 | } != Some(krate); | ||
565 | let generic_def: GenericDef = callable.into(); | ||
566 | let generic_params = generic_def.generic_params(db); | 546 | let generic_params = generic_def.generic_params(db); |
567 | let bound_vars = Substs::bound_vars(&generic_params); | 547 | let bound_vars = Substs::bound_vars(&generic_params); |
568 | let where_clauses = convert_where_clauses(db, generic_def, &bound_vars); | 548 | convert_where_clauses(db, generic_def, &bound_vars) |
569 | (generic_params.count_params_including_parent(), where_clauses, upstream) | 549 | }) |
570 | } | 550 | .unwrap_or_else(Vec::new); |
571 | TypeCtor::Adt(adt) => { | ||
572 | let generic_params = adt.generic_params(db); | ||
573 | let bound_vars = Substs::bound_vars(&generic_params); | ||
574 | let where_clauses = convert_where_clauses(db, adt.into(), &bound_vars); | ||
575 | ( | ||
576 | generic_params.count_params_including_parent(), | ||
577 | where_clauses, | ||
578 | adt.krate(db) != Some(krate), | ||
579 | ) | ||
580 | } | ||
581 | TypeCtor::AssociatedType(type_alias) => { | ||
582 | let generic_params = type_alias.generic_params(db); | ||
583 | let bound_vars = Substs::bound_vars(&generic_params); | ||
584 | let where_clauses = convert_where_clauses(db, type_alias.into(), &bound_vars); | ||
585 | ( | ||
586 | generic_params.count_params_including_parent(), | ||
587 | where_clauses, | ||
588 | type_alias.krate(db) != Some(krate), | ||
589 | ) | ||
590 | } | ||
591 | TypeCtor::Closure { def, .. } => { | ||
592 | let upstream = def.krate(db) != Some(krate); | ||
593 | (1, vec![], upstream) | ||
594 | } | ||
595 | }; | ||
596 | let flags = chalk_rust_ir::StructFlags { | 551 | let flags = chalk_rust_ir::StructFlags { |
597 | upstream, | 552 | upstream, |
598 | // FIXME set fundamental flag correctly | 553 | // FIXME set fundamental flag correctly |
@@ -729,17 +684,20 @@ fn closure_fn_trait_impl_datum( | |||
729 | 684 | ||
730 | let arg_ty = Ty::apply( | 685 | let arg_ty = Ty::apply( |
731 | TypeCtor::Tuple { cardinality: num_args }, | 686 | TypeCtor::Tuple { cardinality: num_args }, |
732 | (0..num_args).map(|i| Ty::Bound(i.into())).collect::<Vec<_>>().into(), | 687 | Substs::builder(num_args as usize).fill_with_bound_vars(0).build(), |
733 | ); | 688 | ); |
734 | let output_ty = Ty::Bound(num_args.into()); | 689 | let output_ty = Ty::Bound(num_args.into()); |
735 | let sig_ty = Ty::apply( | 690 | let sig_ty = Ty::apply( |
736 | TypeCtor::FnPtr { num_args }, | 691 | TypeCtor::FnPtr { num_args }, |
737 | (0..num_args + 1).map(|i| Ty::Bound(i.into())).collect::<Vec<_>>().into(), | 692 | Substs::builder(num_args as usize + 1).fill_with_bound_vars(0).build(), |
738 | ); | 693 | ); |
739 | 694 | ||
740 | let self_ty = Ty::apply_one(TypeCtor::Closure { def: data.def, expr: data.expr }, sig_ty); | 695 | let self_ty = Ty::apply_one(TypeCtor::Closure { def: data.def, expr: data.expr }, sig_ty); |
741 | 696 | ||
742 | let trait_ref = TraitRef { trait_, substs: vec![self_ty, arg_ty].into() }; | 697 | let trait_ref = TraitRef { |
698 | trait_, | ||
699 | substs: Substs::build_for_def(db, trait_).push(self_ty).push(arg_ty).build(), | ||
700 | }; | ||
743 | 701 | ||
744 | let output_ty_id = fn_once_trait.associated_type_by_name(db, &crate::name::OUTPUT_TYPE)?; | 702 | let output_ty_id = fn_once_trait.associated_type_by_name(db, &crate::name::OUTPUT_TYPE)?; |
745 | 703 | ||