From 4f1afe77b98566bd27bb587aff6fe2c744373b68 Mon Sep 17 00:00:00 2001 From: Florian Diebold Date: Mon, 23 Sep 2019 18:53:52 +0200 Subject: Split off path expression inference code into submodule --- crates/ra_hir/src/ty/infer.rs | 176 +-------------------------------- crates/ra_hir/src/ty/infer/path.rs | 195 +++++++++++++++++++++++++++++++++++++ 2 files changed, 199 insertions(+), 172 deletions(-) create mode 100644 crates/ra_hir/src/ty/infer/path.rs (limited to 'crates') diff --git a/crates/ra_hir/src/ty/infer.rs b/crates/ra_hir/src/ty/infer.rs index 70da7f311..81a8623bf 100644 --- a/crates/ra_hir/src/ty/infer.rs +++ b/crates/ra_hir/src/ty/infer.rs @@ -45,13 +45,14 @@ use crate::{ name, nameres::Namespace, path::{known, GenericArg, GenericArgs}, - resolve::{ResolveValueResult, Resolver, TypeNs, ValueNs}, + resolve::{Resolver, TypeNs}, ty::infer::diagnostics::InferenceDiagnostic, type_ref::{Mutability, TypeRef}, Adt, AssocItem, ConstData, DefWithBody, FnData, Function, HasBody, Name, Path, StructField, }; mod unify; +mod path; /// The entry point of type inference. pub fn infer_query(db: &impl HirDatabase, def: DefWithBody) -> Arc { @@ -466,175 +467,6 @@ impl<'a, D: HirDatabase> InferenceContext<'a, D> { }) } - fn infer_path_expr(&mut self, resolver: &Resolver, path: &Path, id: ExprOrPatId) -> Option { - let (value, self_subst) = if let crate::PathKind::Type(type_ref) = &path.kind { - if path.segments.is_empty() { - // This can't actually happen syntax-wise - return None; - } - let ty = self.make_ty(type_ref); - let remaining_segments_for_ty = &path.segments[..path.segments.len() - 1]; - 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"), - id, - )? - } else { - let value_or_partial = resolver.resolve_path_in_value_ns(self.db, &path)?; - - match value_or_partial { - ResolveValueResult::ValueNs(it) => (it, None), - ResolveValueResult::Partial(def, remaining_index) => { - self.resolve_assoc_item(def, path, remaining_index, id)? - } - } - }; - - let typable: TypableDef = match value { - ValueNs::LocalBinding(pat) => { - let ty = self.result.type_of_pat.get(pat)?.clone(); - let ty = self.resolve_ty_as_possible(&mut vec![], ty); - return Some(ty); - } - ValueNs::Function(it) => it.into(), - ValueNs::Const(it) => it.into(), - ValueNs::Static(it) => it.into(), - ValueNs::Struct(it) => it.into(), - ValueNs::EnumVariant(it) => it.into(), - }; - - let mut ty = self.db.type_for_def(typable, Namespace::Values); - if let Some(self_subst) = self_subst { - ty = ty.subst(&self_subst); - } - - let substs = Ty::substs_from_path(self.db, &self.resolver, path, typable); - let ty = ty.subst(&substs); - let ty = self.insert_type_vars(ty); - let ty = self.normalize_associated_types_in(ty); - Some(ty) - } - - fn resolve_assoc_item( - &mut self, - def: TypeNs, - path: &Path, - remaining_index: usize, - id: ExprOrPatId, - ) -> Option<(ValueNs, Option)> { - assert!(remaining_index < path.segments.len()); - // there may be more intermediate segments between the resolved one and - // the end. Only the last segment needs to be resolved to a value; from - // the segments before that, we need to get either a type or a trait ref. - - let resolved_segment = &path.segments[remaining_index - 1]; - let remaining_segments = &path.segments[remaining_index..]; - let is_before_last = remaining_segments.len() == 1; - - match (def, is_before_last) { - (TypeNs::Trait(_trait), true) => { - // FIXME Associated item of trait, e.g. `Default::default` - None - } - (def, _) => { - // Either we already have a type (e.g. `Vec::new`), or we have a - // trait but it's not the last segment, so the next segment - // should resolve to an associated type of that trait (e.g. `::Item::default`) - let remaining_segments_for_ty = &remaining_segments[..remaining_segments.len() - 1]; - let ty = Ty::from_partly_resolved_hir_path( - self.db, - &self.resolver, - def, - resolved_segment, - remaining_segments_for_ty, - ); - if let Ty::Unknown = ty { - return None; - } - - let segment = - remaining_segments.last().expect("there should be at least one segment here"); - - self.resolve_ty_assoc_item(ty, segment, id) - } - } - } - - fn resolve_ty_assoc_item( - &mut self, - ty: Ty, - segment: &crate::path::PathSegment, - id: ExprOrPatId, - ) -> Option<(ValueNs, Option)> { - if let Ty::Unknown = ty { - return None; - } - - 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) { - Some(AssocItem::Function(func)) - } else { - None - } - } - - AssocItem::Const(konst) => { - if konst.name(self.db).map_or(false, |n| n == segment.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_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 - let gen = func.generic_params(self.db); - if gen.count_parent_params() == 0 { - return None; - } - - let impl_block = func.impl_block(self.db)?.target_ty(self.db); - let impl_block_substs = impl_block.substs()?; - let actual_substs = actual_def_ty.substs()?; - - let mut new_substs = vec![Ty::Unknown; gen.count_parent_params()]; - - // The following code *link up* the function actual parma type - // and impl_block type param index - impl_block_substs.iter().zip(actual_substs.iter()).for_each(|(param, pty)| { - if let Ty::Param { idx, .. } = param { - if let Some(s) = new_substs.get_mut(*idx as usize) { - *s = pty.clone(); - } - } - }); - - Some(Substs(new_substs.into())) - } else { - None - } - } - fn resolve_variant(&mut self, path: Option<&Path>) -> (Ty, Option) { let path = match path { Some(path) => path, @@ -807,7 +639,7 @@ impl<'a, D: HirDatabase> InferenceContext<'a, D> { Pat::Path(path) => { // FIXME use correct resolver for the surrounding expression let resolver = self.resolver.clone(); - self.infer_path_expr(&resolver, &path, pat.into()).unwrap_or(Ty::Unknown) + self.infer_path(&resolver, &path, pat.into()).unwrap_or(Ty::Unknown) } Pat::Bind { mode, name: _, subpat } => { let mode = if mode == &BindingAnnotation::Unannotated { @@ -1121,7 +953,7 @@ impl<'a, D: HirDatabase> InferenceContext<'a, D> { Expr::Path(p) => { // FIXME this could be more efficient... let resolver = expr::resolver_for_expr(self.body.clone(), self.db, tgt_expr); - self.infer_path_expr(&resolver, p, tgt_expr.into()).unwrap_or(Ty::Unknown) + self.infer_path(&resolver, p, tgt_expr.into()).unwrap_or(Ty::Unknown) } Expr::Continue => Ty::simple(TypeCtor::Never), Expr::Break { expr } => { diff --git a/crates/ra_hir/src/ty/infer/path.rs b/crates/ra_hir/src/ty/infer/path.rs new file mode 100644 index 000000000..54aae4f0c --- /dev/null +++ b/crates/ra_hir/src/ty/infer/path.rs @@ -0,0 +1,195 @@ +//! Path expression resolution. + +use super::{ExprOrPatId, InferenceContext}; +use crate::{ + db::HirDatabase, + resolve::{ResolveValueResult, Resolver, TypeNs, ValueNs}, + ty::{Substs, Ty, TypableDef, TypeWalk}, + AssocItem, HasGenericParams, Namespace, Path, +}; + +impl<'a, D: HirDatabase> InferenceContext<'a, D> { + pub(super) fn infer_path( + &mut self, + resolver: &Resolver, + path: &Path, + id: ExprOrPatId, + ) -> Option { + let ty = self.resolve_value_path(resolver, path, id)?; + let ty = self.insert_type_vars(ty); + let ty = self.normalize_associated_types_in(ty); + Some(ty) + } + + fn resolve_value_path( + &mut self, + resolver: &Resolver, + path: &Path, + id: ExprOrPatId, + ) -> Option { + let (value, self_subst) = if let crate::PathKind::Type(type_ref) = &path.kind { + if path.segments.is_empty() { + // This can't actually happen syntax-wise + return None; + } + let ty = self.make_ty(type_ref); + let remaining_segments_for_ty = &path.segments[..path.segments.len() - 1]; + 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"), + id, + )? + } else { + let value_or_partial = resolver.resolve_path_in_value_ns(self.db, &path)?; + + match value_or_partial { + ResolveValueResult::ValueNs(it) => (it, None), + ResolveValueResult::Partial(def, remaining_index) => { + self.resolve_assoc_item(def, path, remaining_index, id)? + } + } + }; + + let typable: TypableDef = match value { + ValueNs::LocalBinding(pat) => { + let ty = self.result.type_of_pat.get(pat)?.clone(); + let ty = self.resolve_ty_as_possible(&mut vec![], ty); + return Some(ty); + } + ValueNs::Function(it) => it.into(), + ValueNs::Const(it) => it.into(), + ValueNs::Static(it) => it.into(), + ValueNs::Struct(it) => it.into(), + ValueNs::EnumVariant(it) => it.into(), + }; + + let mut ty = self.db.type_for_def(typable, Namespace::Values); + if let Some(self_subst) = self_subst { + ty = ty.subst(&self_subst); + } + + let substs = Ty::substs_from_path(self.db, &self.resolver, path, typable); + let ty = ty.subst(&substs); + Some(ty) + } + + fn resolve_assoc_item( + &mut self, + def: TypeNs, + path: &Path, + remaining_index: usize, + id: ExprOrPatId, + ) -> Option<(ValueNs, Option)> { + assert!(remaining_index < path.segments.len()); + // there may be more intermediate segments between the resolved one and + // the end. Only the last segment needs to be resolved to a value; from + // the segments before that, we need to get either a type or a trait ref. + + let resolved_segment = &path.segments[remaining_index - 1]; + let remaining_segments = &path.segments[remaining_index..]; + let is_before_last = remaining_segments.len() == 1; + + match (def, is_before_last) { + (TypeNs::Trait(_trait), true) => { + // FIXME Associated item of trait, e.g. `Default::default` + None + } + (def, _) => { + // Either we already have a type (e.g. `Vec::new`), or we have a + // trait but it's not the last segment, so the next segment + // should resolve to an associated type of that trait (e.g. `::Item::default`) + let remaining_segments_for_ty = &remaining_segments[..remaining_segments.len() - 1]; + let ty = Ty::from_partly_resolved_hir_path( + self.db, + &self.resolver, + def, + resolved_segment, + remaining_segments_for_ty, + ); + if let Ty::Unknown = ty { + return None; + } + + let segment = + remaining_segments.last().expect("there should be at least one segment here"); + + self.resolve_ty_assoc_item(ty, segment, id) + } + } + } + + fn resolve_ty_assoc_item( + &mut self, + ty: Ty, + segment: &crate::path::PathSegment, + id: ExprOrPatId, + ) -> Option<(ValueNs, Option)> { + if let Ty::Unknown = ty { + return None; + } + + 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) { + Some(AssocItem::Function(func)) + } else { + None + } + } + + AssocItem::Const(konst) => { + if konst.name(self.db).map_or(false, |n| n == segment.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_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 + let gen = func.generic_params(self.db); + if gen.count_parent_params() == 0 { + return None; + } + + let impl_block = func.impl_block(self.db)?.target_ty(self.db); + let impl_block_substs = impl_block.substs()?; + let actual_substs = actual_def_ty.substs()?; + + let mut new_substs = vec![Ty::Unknown; gen.count_parent_params()]; + + // The following code *link up* the function actual parma type + // and impl_block type param index + impl_block_substs.iter().zip(actual_substs.iter()).for_each(|(param, pty)| { + if let Ty::Param { idx, .. } = param { + if let Some(s) = new_substs.get_mut(*idx as usize) { + *s = pty.clone(); + } + } + }); + + Some(Substs(new_substs.into())) + } else { + None + } + } +} -- cgit v1.2.3