From b634ba41e0439cbbb89b12a3d340c8463b35b93e Mon Sep 17 00:00:00 2001 From: Florian Diebold Date: Tue, 29 Oct 2019 11:04:42 +0100 Subject: Get trait assoc item resolution mostly working --- crates/ra_hir/src/ty/infer/path.rs | 106 ++++++++++++++++++++++++++++++++++--- 1 file changed, 98 insertions(+), 8 deletions(-) (limited to 'crates/ra_hir/src/ty/infer/path.rs') diff --git a/crates/ra_hir/src/ty/infer/path.rs b/crates/ra_hir/src/ty/infer/path.rs index 77aa35ce1..885588174 100644 --- a/crates/ra_hir/src/ty/infer/path.rs +++ b/crates/ra_hir/src/ty/infer/path.rs @@ -6,9 +6,11 @@ use super::{ExprOrPatId, InferenceContext, TraitRef}; use crate::{ db::HirDatabase, resolve::{ResolveValueResult, Resolver, TypeNs, ValueNs}, + ty::{lower, traits::TraitEnvironment, Canonical}, ty::{Substs, Ty, TypableDef, TypeWalk}, - AssocItem, HasGenericParams, Namespace, Path, + AssocItem, HasGenericParams, Name, Namespace, Path, Trait, }; +use std::sync::Arc; impl<'a, D: HirDatabase> InferenceContext<'a, D> { pub(super) fn infer_path( @@ -39,7 +41,7 @@ impl<'a, D: HirDatabase> InferenceContext<'a, D> { let ty = Ty::from_type_relative_path(self.db, resolver, ty, remaining_segments_for_ty); self.resolve_ty_assoc_item( ty, - path.segments.last().expect("path had at least one segment"), + &path.segments.last().expect("path had at least one segment").name, id, )? } else { @@ -125,7 +127,7 @@ impl<'a, D: HirDatabase> InferenceContext<'a, D> { let segment = remaining_segments.last().expect("there should be at least one segment here"); - self.resolve_ty_assoc_item(ty, segment, id) + self.resolve_ty_assoc_item(ty, &segment.name, id) } } } @@ -162,7 +164,7 @@ impl<'a, D: HirDatabase> InferenceContext<'a, D> { }; let substs = Substs::build_for_def(self.db, item) .use_parent_substs(&trait_ref.substs) - .fill_with_unknown() + .fill_with_params() .build(); self.write_assoc_resolution(id, item); @@ -172,20 +174,29 @@ impl<'a, D: HirDatabase> InferenceContext<'a, D> { fn resolve_ty_assoc_item( &mut self, ty: Ty, - segment: &PathSegment, + name: &Name, id: ExprOrPatId, ) -> Option<(ValueNs, Option)> { if let Ty::Unknown = ty { return None; } + self.find_inherent_assoc_candidate(ty.clone(), name, id) + .or_else(|| self.find_trait_assoc_candidate(ty.clone(), name, id)) + } + + fn find_inherent_assoc_candidate( + &mut self, + ty: Ty, + name: &Name, + id: ExprOrPatId, + ) -> Option<(ValueNs, Option)> { let krate = self.resolver.krate()?; // Find impl - // FIXME: consider trait candidates let item = ty.clone().iterate_impl_items(self.db, krate, |item| match item { AssocItem::Function(func) => { - if segment.name == func.name(self.db) { + if *name == func.name(self.db) { Some(AssocItem::Function(func)) } else { None @@ -193,7 +204,7 @@ impl<'a, D: HirDatabase> InferenceContext<'a, D> { } AssocItem::Const(konst) => { - if konst.name(self.db).map_or(false, |n| n == segment.name) { + if konst.name(self.db).map_or(false, |n| n == *name) { Some(AssocItem::Const(konst)) } else { None @@ -212,6 +223,65 @@ impl<'a, D: HirDatabase> InferenceContext<'a, D> { Some((def, substs)) } + fn find_trait_assoc_candidate( + &mut self, + ty: Ty, + name: &Name, + _id: ExprOrPatId, + ) -> Option<(ValueNs, Option)> { + let krate = self.resolver.krate()?; + + let canonical_ty = self.canonicalizer().canonicalize_ty(ty.clone()); + + let env = lower::trait_env(self.db, &self.resolver); + // if we have `T: Trait` in the param env, the trait doesn't need to be in scope + let traits_from_env = env + .trait_predicates_for_self_ty(&ty) + .map(|tr| tr.trait_) + .flat_map(|t| t.all_super_traits(self.db)); + let traits = traits_from_env.chain(self.resolver.traits_in_scope(self.db)); + + 'traits: for t in traits { + let data = t.trait_data(self.db); + let mut known_implemented = false; + for item in data.items() { + if let AssocItem::Function(f) = *item { + if f.name(self.db) == *name { + if !known_implemented { + let goal = generic_implements_goal( + self.db, + env.clone(), + t, + canonical_ty.value.clone(), + ); + if self.db.trait_solve(krate, goal).is_none() { + continue 'traits; + } + } + known_implemented = true; + + // we're picking this method + let trait_substs = Substs::build_for_def(self.db, t) + .push(ty.clone()) + .fill(std::iter::repeat_with(|| self.new_type_var())) + .build(); + let substs = Substs::build_for_def(self.db, f) + .use_parent_substs(&trait_substs) + .fill_with_params() + .build(); + self.obligations.push(super::Obligation::Trait(TraitRef { + trait_: t, + substs: trait_substs, + })); + return Some((ValueNs::Function(f), Some(substs))); + } + } + } + } + + None + } + fn find_self_types(&self, def: &ValueNs, actual_def_ty: Ty) -> Option { if let ValueNs::Function(func) = def { // We only do the infer if parent has generic params @@ -242,3 +312,23 @@ impl<'a, D: HirDatabase> InferenceContext<'a, D> { } } } + +// TODO remove duplication +/// This creates Substs for a trait with the given Self type and type variables +/// for all other parameters, to query Chalk with it. +fn generic_implements_goal( + db: &impl HirDatabase, + env: Arc, + trait_: Trait, + self_ty: Canonical, +) -> Canonical> { + let num_vars = self_ty.num_vars; + let substs = super::Substs::build_for_def(db, trait_) + .push(self_ty.value) + .fill_with_bound_vars(num_vars as u32) + .build(); + let num_vars = substs.len() - 1 + self_ty.num_vars; + let trait_ref = TraitRef { trait_, substs }; + let obligation = super::Obligation::Trait(trait_ref); + Canonical { num_vars, value: super::InEnvironment::new(env, obligation) } +} -- cgit v1.2.3 From 7b7133ec58818894d3d56df021ae70159e2c3252 Mon Sep 17 00:00:00 2001 From: Florian Diebold Date: Wed, 30 Oct 2019 21:22:46 +0100 Subject: Insert type vars before doing assoc item resolution --- crates/ra_hir/src/ty/infer/path.rs | 3 +++ 1 file changed, 3 insertions(+) (limited to 'crates/ra_hir/src/ty/infer/path.rs') diff --git a/crates/ra_hir/src/ty/infer/path.rs b/crates/ra_hir/src/ty/infer/path.rs index 885588174..0cde77265 100644 --- a/crates/ra_hir/src/ty/infer/path.rs +++ b/crates/ra_hir/src/ty/infer/path.rs @@ -124,6 +124,9 @@ impl<'a, D: HirDatabase> InferenceContext<'a, D> { return None; } + let ty = self.insert_type_vars(ty); + let ty = self.normalize_associated_types_in(ty); + let segment = remaining_segments.last().expect("there should be at least one segment here"); -- cgit v1.2.3 From c7cedea270c492e9a2c8b81c1312fda44fd8217e Mon Sep 17 00:00:00 2001 From: Florian Diebold Date: Thu, 31 Oct 2019 15:13:52 +0100 Subject: Record assoc item resolution --- crates/ra_hir/src/ty/infer/path.rs | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) (limited to 'crates/ra_hir/src/ty/infer/path.rs') diff --git a/crates/ra_hir/src/ty/infer/path.rs b/crates/ra_hir/src/ty/infer/path.rs index 0cde77265..c58564b22 100644 --- a/crates/ra_hir/src/ty/infer/path.rs +++ b/crates/ra_hir/src/ty/infer/path.rs @@ -230,7 +230,7 @@ impl<'a, D: HirDatabase> InferenceContext<'a, D> { &mut self, ty: Ty, name: &Name, - _id: ExprOrPatId, + id: ExprOrPatId, ) -> Option<(ValueNs, Option)> { let krate = self.resolver.krate()?; @@ -276,6 +276,8 @@ impl<'a, D: HirDatabase> InferenceContext<'a, D> { trait_: t, substs: trait_substs, })); + + self.write_assoc_resolution(id, *item); return Some((ValueNs::Function(f), Some(substs))); } } -- cgit v1.2.3 From 1173c3dab5f77a1afd367d547790dd82c558fe0d Mon Sep 17 00:00:00 2001 From: Florian Diebold Date: Thu, 31 Oct 2019 19:28:33 +0100 Subject: Refactor to unify with method resolution --- crates/ra_hir/src/ty/infer/path.rs | 135 ++++++++----------------------------- 1 file changed, 27 insertions(+), 108 deletions(-) (limited to 'crates/ra_hir/src/ty/infer/path.rs') diff --git a/crates/ra_hir/src/ty/infer/path.rs b/crates/ra_hir/src/ty/infer/path.rs index c58564b22..1946bf608 100644 --- a/crates/ra_hir/src/ty/infer/path.rs +++ b/crates/ra_hir/src/ty/infer/path.rs @@ -6,11 +6,9 @@ use super::{ExprOrPatId, InferenceContext, TraitRef}; use crate::{ db::HirDatabase, resolve::{ResolveValueResult, Resolver, TypeNs, ValueNs}, - ty::{lower, traits::TraitEnvironment, Canonical}, - ty::{Substs, Ty, TypableDef, TypeWalk}, - AssocItem, HasGenericParams, Name, Namespace, Path, Trait, + ty::{method_resolution, Substs, Ty, TypableDef, TypeWalk}, + AssocItem, Container, HasGenericParams, Name, Namespace, Path, }; -use std::sync::Arc; impl<'a, D: HirDatabase> InferenceContext<'a, D> { pub(super) fn infer_path( @@ -184,91 +182,34 @@ impl<'a, D: HirDatabase> InferenceContext<'a, D> { return None; } - self.find_inherent_assoc_candidate(ty.clone(), name, id) - .or_else(|| self.find_trait_assoc_candidate(ty.clone(), name, id)) - } - - fn find_inherent_assoc_candidate( - &mut self, - ty: Ty, - name: &Name, - id: ExprOrPatId, - ) -> Option<(ValueNs, Option)> { - let krate = self.resolver.krate()?; - - // Find impl - let item = ty.clone().iterate_impl_items(self.db, krate, |item| match item { - AssocItem::Function(func) => { - if *name == func.name(self.db) { - Some(AssocItem::Function(func)) - } else { - None - } - } - - AssocItem::Const(konst) => { - if konst.name(self.db).map_or(false, |n| n == *name) { - Some(AssocItem::Const(konst)) - } else { - None - } - } - AssocItem::TypeAlias(_) => None, - })?; - let def = match item { - AssocItem::Function(f) => ValueNs::Function(f), - AssocItem::Const(c) => ValueNs::Const(c), - AssocItem::TypeAlias(_) => unreachable!(), - }; - let substs = self.find_self_types(&def, ty); - - self.write_assoc_resolution(id, item); - Some((def, substs)) - } - - fn find_trait_assoc_candidate( - &mut self, - ty: Ty, - name: &Name, - id: ExprOrPatId, - ) -> Option<(ValueNs, Option)> { - let krate = self.resolver.krate()?; - let canonical_ty = self.canonicalizer().canonicalize_ty(ty.clone()); - let env = lower::trait_env(self.db, &self.resolver); - // if we have `T: Trait` in the param env, the trait doesn't need to be in scope - let traits_from_env = env - .trait_predicates_for_self_ty(&ty) - .map(|tr| tr.trait_) - .flat_map(|t| t.all_super_traits(self.db)); - let traits = traits_from_env.chain(self.resolver.traits_in_scope(self.db)); - - 'traits: for t in traits { - let data = t.trait_data(self.db); - let mut known_implemented = false; - for item in data.items() { - if let AssocItem::Function(f) = *item { - if f.name(self.db) == *name { - if !known_implemented { - let goal = generic_implements_goal( - self.db, - env.clone(), - t, - canonical_ty.value.clone(), - ); - if self.db.trait_solve(krate, goal).is_none() { - continue 'traits; - } - } - known_implemented = true; - + method_resolution::iterate_method_candidates( + &canonical_ty.value, + self.db, + &self.resolver.clone(), + Some(name), + method_resolution::LookupMode::Path, + move |_ty, item| { + let def = match item { + AssocItem::Function(f) => ValueNs::Function(f), + AssocItem::Const(c) => ValueNs::Const(c), + AssocItem::TypeAlias(_) => unreachable!(), + }; + match item.container(self.db) { + Container::ImplBlock(_) => { + let substs = self.find_self_types(&def, ty.clone()); + + self.write_assoc_resolution(id, item); + Some((def, substs)) + } + Container::Trait(t) => { // we're picking this method let trait_substs = Substs::build_for_def(self.db, t) .push(ty.clone()) .fill(std::iter::repeat_with(|| self.new_type_var())) .build(); - let substs = Substs::build_for_def(self.db, f) + let substs = Substs::build_for_def(self.db, item) .use_parent_substs(&trait_substs) .fill_with_params() .build(); @@ -277,14 +218,12 @@ impl<'a, D: HirDatabase> InferenceContext<'a, D> { substs: trait_substs, })); - self.write_assoc_resolution(id, *item); - return Some((ValueNs::Function(f), Some(substs))); + self.write_assoc_resolution(id, item); + Some((def, Some(substs))) } } - } - } - - None + }, + ) } fn find_self_types(&self, def: &ValueNs, actual_def_ty: Ty) -> Option { @@ -317,23 +256,3 @@ impl<'a, D: HirDatabase> InferenceContext<'a, D> { } } } - -// TODO remove duplication -/// This creates Substs for a trait with the given Self type and type variables -/// for all other parameters, to query Chalk with it. -fn generic_implements_goal( - db: &impl HirDatabase, - env: Arc, - trait_: Trait, - self_ty: Canonical, -) -> Canonical> { - let num_vars = self_ty.num_vars; - let substs = super::Substs::build_for_def(db, trait_) - .push(self_ty.value) - .fill_with_bound_vars(num_vars as u32) - .build(); - let num_vars = substs.len() - 1 + self_ty.num_vars; - let trait_ref = TraitRef { trait_, substs }; - let obligation = super::Obligation::Trait(trait_ref); - Canonical { num_vars, value: super::InEnvironment::new(env, obligation) } -} -- cgit v1.2.3 From b29092ade31d7ff37532649dfbe1dc811edf3651 Mon Sep 17 00:00:00 2001 From: Florian Diebold Date: Fri, 1 Nov 2019 19:56:56 +0100 Subject: Various review fixes --- crates/ra_hir/src/ty/infer/path.rs | 18 +++++++----------- 1 file changed, 7 insertions(+), 11 deletions(-) (limited to 'crates/ra_hir/src/ty/infer/path.rs') diff --git a/crates/ra_hir/src/ty/infer/path.rs b/crates/ra_hir/src/ty/infer/path.rs index 1946bf608..59b7f7eb6 100644 --- a/crates/ra_hir/src/ty/infer/path.rs +++ b/crates/ra_hir/src/ty/infer/path.rs @@ -196,13 +196,8 @@ impl<'a, D: HirDatabase> InferenceContext<'a, D> { AssocItem::Const(c) => ValueNs::Const(c), AssocItem::TypeAlias(_) => unreachable!(), }; - match item.container(self.db) { - Container::ImplBlock(_) => { - let substs = self.find_self_types(&def, ty.clone()); - - self.write_assoc_resolution(id, item); - Some((def, substs)) - } + let substs = match item.container(self.db) { + Container::ImplBlock(_) => self.find_self_types(&def, ty.clone()), Container::Trait(t) => { // we're picking this method let trait_substs = Substs::build_for_def(self.db, t) @@ -217,11 +212,12 @@ impl<'a, D: HirDatabase> InferenceContext<'a, D> { trait_: t, substs: trait_substs, })); - - self.write_assoc_resolution(id, item); - Some((def, Some(substs))) + Some(substs) } - } + }; + + self.write_assoc_resolution(id, item); + Some((def, substs)) }, ) } -- cgit v1.2.3