From 6cfdfdecbaed38534397f16e1ea1cda38b0b9395 Mon Sep 17 00:00:00 2001 From: Florian Diebold Date: Mon, 5 Aug 2019 21:13:34 +0200 Subject: Add representations of associated types This adds three different representations, copied from the Chalk model: - `Ty::Projection` is an associated type projection written somewhere in the code, like `::Bar`. - `Ty::UnselectedProjection` is similar, but we don't know the trait yet (`Foo::Bar`). - The above representations are normalized to their actual types during type inference. When that isn't possible, for example for `T::Item` inside an `fn foo`, the type is normalized to an application type with `TypeCtor::AssociatedType`. --- crates/ra_hir/src/ty/traits/chalk.rs | 23 +++++++++++++++++++++++ 1 file changed, 23 insertions(+) (limited to 'crates/ra_hir/src/ty') diff --git a/crates/ra_hir/src/ty/traits/chalk.rs b/crates/ra_hir/src/ty/traits/chalk.rs index 9e7ae0724..21055dcfd 100644 --- a/crates/ra_hir/src/ty/traits/chalk.rs +++ b/crates/ra_hir/src/ty/traits/chalk.rs @@ -50,6 +50,19 @@ impl ToChalk for Ty { let parameters = apply_ty.parameters.to_chalk(db); chalk_ir::ApplicationTy { name, parameters }.cast() } + Ty::Projection(proj_ty) => { + let associated_ty_id = proj_ty.associated_ty.to_chalk(db); + let parameters = proj_ty.parameters.to_chalk(db); + chalk_ir::ProjectionTy { associated_ty_id, parameters }.cast() + } + Ty::UnselectedProjection(proj_ty) => { + let type_name = lalrpop_intern::intern(&proj_ty.type_name.to_string()); + let parameters = proj_ty.parameters.to_chalk(db); + chalk_ir::Ty::UnselectedProjection(chalk_ir::UnselectedProjectionTy { + type_name, + parameters, + }) + } Ty::Param { idx, .. } => { PlaceholderIndex { ui: UniverseIndex::ROOT, idx: idx as usize }.to_ty() } @@ -529,6 +542,16 @@ pub(crate) fn struct_datum_query( adt.krate(db) != Some(krate), ) } + TypeCtor::AssociatedType(type_alias) => { + let generic_params = type_alias.generic_params(db); + let bound_vars = Substs::bound_vars(&generic_params); + let where_clauses = convert_where_clauses(db, type_alias.into(), &bound_vars); + ( + generic_params.count_params_including_parent(), + where_clauses, + type_alias.krate(db) != Some(krate), + ) + } }; let flags = chalk_rust_ir::StructFlags { upstream, -- cgit v1.2.3