From 816971ebc9207c5fb5779d448613dd171c27f398 Mon Sep 17 00:00:00 2001 From: Ville Penttinen Date: Thu, 21 Feb 2019 12:04:14 +0200 Subject: Implement basic support for Associated Methods and Constants This is done in `infer_path_expr`. When `Resolver::resolve_path` returns `PartiallyResolved`, we use the returned `Resolution` together with the given `segment_index` to check if we can find something matching the segment at segment_index in the impls for that particular type. --- crates/ra_hir/src/impl_block.rs | 2 +- crates/ra_hir/src/nameres.rs | 78 +++++++++--- crates/ra_hir/src/resolve.rs | 46 ++++++- crates/ra_hir/src/ty.rs | 108 +++++++++++++---- .../snapshots/tests__infer_associated_const.snap | 14 +++ .../tests__infer_associated_method_enum.snap | 20 ++++ .../tests__infer_associated_method_generics.snap | 16 +++ .../tests__infer_associated_method_struct.snap | 16 +++ ...ests__infer_associated_method_with_modules.snap | 23 ++++ crates/ra_hir/src/ty/tests.rs | 133 +++++++++++++++++++++ 10 files changed, 408 insertions(+), 48 deletions(-) create mode 100644 crates/ra_hir/src/ty/snapshots/tests__infer_associated_const.snap create mode 100644 crates/ra_hir/src/ty/snapshots/tests__infer_associated_method_enum.snap create mode 100644 crates/ra_hir/src/ty/snapshots/tests__infer_associated_method_generics.snap create mode 100644 crates/ra_hir/src/ty/snapshots/tests__infer_associated_method_struct.snap create mode 100644 crates/ra_hir/src/ty/snapshots/tests__infer_associated_method_with_modules.snap (limited to 'crates/ra_hir/src') diff --git a/crates/ra_hir/src/impl_block.rs b/crates/ra_hir/src/impl_block.rs index 7ecf8c368..f4dccf165 100644 --- a/crates/ra_hir/src/impl_block.rs +++ b/crates/ra_hir/src/impl_block.rs @@ -88,7 +88,7 @@ impl ImplBlock { if let Some(TypeRef::Path(path)) = self.target_trait_ref(db) { let resolver = self.resolver(db); if let Some(Resolution::Def(ModuleDef::Trait(tr))) = - resolver.resolve_path(db, &path).take_types() + resolver.resolve_path(db, &path).into_per_ns().take_types() { return Some(tr); } diff --git a/crates/ra_hir/src/nameres.rs b/crates/ra_hir/src/nameres.rs index bd920bfea..b78a178c1 100644 --- a/crates/ra_hir/src/nameres.rs +++ b/crates/ra_hir/src/nameres.rs @@ -119,6 +119,10 @@ impl PerNs { self.types.is_some() && self.values.is_some() } + pub fn is_values(&self) -> bool { + self.values.is_some() && self.types.is_none() + } + pub fn take(self, namespace: Namespace) -> Option { match namespace { Namespace::Types => self.types, @@ -297,7 +301,14 @@ where ); (res, if res.is_none() { ReachedFixedPoint::No } else { ReachedFixedPoint::Yes }) } else { - self.result.resolve_path_fp(self.db, ResolveMode::Import, original_module, &import.path) + let res = self.result.resolve_path_fp( + self.db, + ResolveMode::Import, + original_module, + &import.path, + ); + + (res.module, res.reached_fixedpoint) }; if reached_fixedpoint != ReachedFixedPoint::Yes { @@ -435,6 +446,27 @@ where } } +#[derive(Debug, Clone)] +pub struct ResolvePathResult { + pub(crate) module: PerNs, + pub(crate) segment_index: Option, + reached_fixedpoint: ReachedFixedPoint, +} + +impl ResolvePathResult { + fn empty(reached_fixedpoint: ReachedFixedPoint) -> ResolvePathResult { + ResolvePathResult::with(PerNs::none(), reached_fixedpoint, None) + } + + fn with( + module: PerNs, + reached_fixedpoint: ReachedFixedPoint, + segment_index: Option, + ) -> ResolvePathResult { + ResolvePathResult { module, reached_fixedpoint, segment_index } + } +} + #[derive(Debug, Clone, Copy, PartialEq, Eq)] enum ResolveMode { Import, @@ -468,8 +500,9 @@ impl ItemMap { db: &impl PersistentHirDatabase, original_module: Module, path: &Path, - ) -> PerNs { - self.resolve_path_fp(db, ResolveMode::Other, original_module, path).0 + ) -> (PerNs, Option) { + let res = self.resolve_path_fp(db, ResolveMode::Other, original_module, path); + (res.module, res.segment_index) } fn resolve_in_prelude( @@ -534,7 +567,7 @@ impl ItemMap { mode: ResolveMode, original_module: Module, path: &Path, - ) -> (PerNs, ReachedFixedPoint) { + ) -> ResolvePathResult { let mut segments = path.segments.iter().enumerate(); let mut curr_per_ns: PerNs = match path.kind { PathKind::Crate => PerNs::types(original_module.crate_root(db).into()), @@ -549,7 +582,7 @@ impl ItemMap { { let segment = match segments.next() { Some((_, segment)) => segment, - None => return (PerNs::none(), ReachedFixedPoint::Yes), + None => return ResolvePathResult::empty(ReachedFixedPoint::Yes), }; log::debug!("resolving {:?} in crate root (+ extern prelude)", segment); self.resolve_name_in_crate_root_or_extern_prelude( @@ -561,7 +594,7 @@ impl ItemMap { PathKind::Plain => { let segment = match segments.next() { Some((_, segment)) => segment, - None => return (PerNs::none(), ReachedFixedPoint::Yes), + None => return ResolvePathResult::empty(ReachedFixedPoint::Yes), }; log::debug!("resolving {:?} in module", segment); self.resolve_name_in_module(db, original_module, &segment.name) @@ -571,20 +604,20 @@ impl ItemMap { PerNs::types(p.into()) } else { log::debug!("super path in root module"); - return (PerNs::none(), ReachedFixedPoint::Yes); + return ResolvePathResult::empty(ReachedFixedPoint::Yes); } } PathKind::Abs => { // 2018-style absolute path -- only extern prelude let segment = match segments.next() { Some((_, segment)) => segment, - None => return (PerNs::none(), ReachedFixedPoint::Yes), + None => return ResolvePathResult::empty(ReachedFixedPoint::Yes), }; if let Some(def) = self.extern_prelude.get(&segment.name) { log::debug!("absolute path {:?} resolved to crate {:?}", path, def); PerNs::types(*def) } else { - return (PerNs::none(), ReachedFixedPoint::No); // extern crate declarations can add to the extern prelude + return ResolvePathResult::empty(ReachedFixedPoint::No); // extern crate declarations can add to the extern prelude } } }; @@ -598,7 +631,7 @@ impl ItemMap { // (don't break here because `curr_per_ns` might contain // something in the value namespace, and it would be wrong // to return that) - return (PerNs::none(), ReachedFixedPoint::No); + return ResolvePathResult::empty(ReachedFixedPoint::No); } }; // resolve segment in curr @@ -612,15 +645,15 @@ impl ItemMap { }; log::debug!("resolving {:?} in other crate", path); let item_map = db.item_map(module.krate); - let def = item_map.resolve_path(db, *module, &path); - return (def, ReachedFixedPoint::Yes); + let (def, s) = item_map.resolve_path(db, *module, &path); + return ResolvePathResult::with(def, ReachedFixedPoint::Yes, s); } match self[module.module_id].items.get(&segment.name) { Some(res) if !res.def.is_none() => res.def, _ => { log::debug!("path segment {:?} not found", segment.name); - return (PerNs::none(), ReachedFixedPoint::No); + return ResolvePathResult::empty(ReachedFixedPoint::No); } } } @@ -629,9 +662,22 @@ impl ItemMap { tested_by!(item_map_enum_importing); match e.variant(db, &segment.name) { Some(variant) => PerNs::both(variant.into(), variant.into()), - None => PerNs::none(), + None => { + return ResolvePathResult::with( + PerNs::types((*e).into()), + ReachedFixedPoint::Yes, + Some(i), + ); + } } } + ModuleDef::Struct(s) => { + return ResolvePathResult::with( + PerNs::types((*s).into()), + ReachedFixedPoint::Yes, + Some(i), + ); + } _ => { // could be an inherent method call in UFCS form // (`Struct::method`), or some other kind of associated @@ -641,11 +687,11 @@ impl ItemMap { segment.name, curr, ); - return (PerNs::none(), ReachedFixedPoint::Yes); + return ResolvePathResult::empty(ReachedFixedPoint::Yes); } }; } - (curr_per_ns, ReachedFixedPoint::Yes) + ResolvePathResult::with(curr_per_ns, ReachedFixedPoint::Yes, None) } } diff --git a/crates/ra_hir/src/resolve.rs b/crates/ra_hir/src/resolve.rs index 91a531801..00b55eae9 100644 --- a/crates/ra_hir/src/resolve.rs +++ b/crates/ra_hir/src/resolve.rs @@ -32,6 +32,32 @@ pub(crate) struct ExprScope { scope_id: ScopeId, } +#[derive(Debug, Clone)] +pub enum PathResult { + /// Path was fully resolved + FullyResolved(PerNs), + /// Path was partially resolved, first element contains the resolution + /// second contains the index in the Path.segments which we were unable to resolve + PartiallyResolved(PerNs, usize), +} + +impl PathResult { + pub fn segment_index(&self) -> Option { + match self { + PathResult::FullyResolved(_) => None, + PathResult::PartiallyResolved(_, ref i) => Some(*i), + } + } + + /// Consumes `PathResult` and returns the contained `PerNs` + pub fn into_per_ns(self) -> PerNs { + match self { + PathResult::FullyResolved(def) => def, + PathResult::PartiallyResolved(def, _) => def, + } + } +} + #[derive(Debug, Clone)] pub(crate) enum Scope { /// All the items and imported names of a module @@ -67,18 +93,26 @@ impl Resolver { resolution } - pub fn resolve_path(&self, db: &impl HirDatabase, path: &Path) -> PerNs { + pub fn resolve_path(&self, db: &impl HirDatabase, path: &Path) -> PathResult { + use self::PathResult::*; if let Some(name) = path.as_ident() { - self.resolve_name(db, name) + FullyResolved(self.resolve_name(db, name)) } else if path.is_self() { - self.resolve_name(db, &Name::self_param()) + FullyResolved(self.resolve_name(db, &Name::self_param())) } else { let (item_map, module) = match self.module() { Some(m) => m, - _ => return PerNs::none(), + _ => return FullyResolved(PerNs::none()), }; - let module_res = item_map.resolve_path(db, module, path); - module_res.map(Resolution::Def) + let (module_res, segment_index) = item_map.resolve_path(db, module, path); + + let def = module_res.map(Resolution::Def); + + if let Some(index) = segment_index { + PartiallyResolved(def, index) + } else { + FullyResolved(def) + } } } diff --git a/crates/ra_hir/src/ty.rs b/crates/ra_hir/src/ty.rs index d4d896673..2751ab3ab 100644 --- a/crates/ra_hir/src/ty.rs +++ b/crates/ra_hir/src/ty.rs @@ -32,17 +32,20 @@ use rustc_hash::FxHashMap; use test_utils::tested_by; +use ra_syntax::ast::NameOwner; + use crate::{ Function, Struct, StructField, Enum, EnumVariant, Path, Name, + Const, FnSignature, ModuleDef, AdtDef, HirDatabase, type_ref::{TypeRef, Mutability}, - name::KnownName, + name::{KnownName, AsName}, expr::{Body, Expr, BindingAnnotation, Literal, ExprId, Pat, PatId, UnaryOp, BinaryOp, Statement, FieldPat, self}, generics::GenericParams, path::GenericArg, adt::VariantDef, - resolve::{Resolver, Resolution}, nameres::Namespace + resolve::{Resolver, Resolution, PathResult}, nameres::Namespace }; /// The ID of a type variable. @@ -370,7 +373,7 @@ impl Ty { } // Resolve the path (in type namespace) - let resolution = resolver.resolve_path(db, path).take_types(); + let resolution = resolver.resolve_path(db, path).into_per_ns().take_types(); let def = match resolution { Some(Resolution::Def(def)) => def, @@ -678,6 +681,19 @@ fn type_for_fn(db: &impl HirDatabase, def: Function) -> Ty { Ty::FnDef { def: def.into(), sig, name, substs } } +fn type_for_const(db: &impl HirDatabase, resolver: &Resolver, def: Const) -> Ty { + let node = def.source(db).1; + + let tr = node + .type_ref() + .map(TypeRef::from_ast) + .as_ref() + .map(|tr| Ty::from_hir(db, resolver, tr)) + .unwrap_or_else(|| Ty::Unknown); + + tr +} + /// Compute the type of a tuple struct constructor. fn type_for_struct_constructor(db: &impl HirDatabase, def: Struct) -> Ty { let var_data = def.variant_data(db); @@ -1172,15 +1188,56 @@ impl<'a, D: HirDatabase> InferenceContext<'a, D> { } fn infer_path_expr(&mut self, resolver: &Resolver, path: &Path) -> Option { - let resolved = resolver.resolve_path(self.db, &path).take_values()?; + let resolved = resolver.resolve_path(self.db, &path); + + let (resolved, segment_index) = match resolved { + PathResult::FullyResolved(def) => (def.take_values()?, None), + PathResult::PartiallyResolved(def, index) => (def.take_types()?, Some(index)), + }; + match resolved { Resolution::Def(def) => { let typable: Option = def.into(); let typable = typable?; - let substs = Ty::substs_from_path(self.db, &self.resolver, path, typable); - let ty = self.db.type_for_def(typable, Namespace::Values).apply_substs(substs); - let ty = self.insert_type_vars(ty); - Some(ty) + + if let Some(segment_index) = segment_index { + let ty = self.db.type_for_def(typable, Namespace::Types); + // TODO: What to do if segment_index is not the last segment + // in the path + let segment = &path.segments[segment_index]; + + // Attempt to find an impl_item for the type which has a name matching + // the current segment + let ty = ty.iterate_impl_items(self.db, |item| match item { + crate::ImplItem::Method(func) => { + let sig = func.signature(self.db); + if segment.name == *sig.name() { + return Some(type_for_fn(self.db, func)); + } + None + } + crate::ImplItem::Const(c) => { + let node = c.source(self.db).1; + + if let Some(name) = node.name().map(|n| n.as_name()) { + if segment.name == name { + return Some(type_for_const(self.db, resolver, c)); + } + } + + None + } + + // TODO: Resolve associated types + crate::ImplItem::Type(_) => None, + }); + ty + } else { + let substs = Ty::substs_from_path(self.db, &self.resolver, path, typable); + let ty = self.db.type_for_def(typable, Namespace::Values).apply_substs(substs); + let ty = self.insert_type_vars(ty); + Some(ty) + } } Resolution::LocalBinding(pat) => { let ty = self.type_of_pat.get(pat)?; @@ -1204,23 +1261,24 @@ impl<'a, D: HirDatabase> InferenceContext<'a, D> { None => return (Ty::Unknown, None), }; let resolver = &self.resolver; - let typable: Option = match resolver.resolve_path(self.db, &path).take_types() { - Some(Resolution::Def(def)) => def.into(), - Some(Resolution::LocalBinding(..)) => { - // this cannot happen - log::error!("path resolved to local binding in type ns"); - return (Ty::Unknown, None); - } - Some(Resolution::GenericParam(..)) => { - // generic params can't be used in struct literals - return (Ty::Unknown, None); - } - Some(Resolution::SelfType(..)) => { - // TODO this is allowed in an impl for a struct, handle this - return (Ty::Unknown, None); - } - None => return (Ty::Unknown, None), - }; + let typable: Option = + match resolver.resolve_path(self.db, &path).into_per_ns().take_types() { + Some(Resolution::Def(def)) => def.into(), + Some(Resolution::LocalBinding(..)) => { + // this cannot happen + log::error!("path resolved to local binding in type ns"); + return (Ty::Unknown, None); + } + Some(Resolution::GenericParam(..)) => { + // generic params can't be used in struct literals + return (Ty::Unknown, None); + } + Some(Resolution::SelfType(..)) => { + // TODO this is allowed in an impl for a struct, handle this + return (Ty::Unknown, None); + } + None => return (Ty::Unknown, None), + }; let def = match typable { None => return (Ty::Unknown, None), Some(it) => it, diff --git a/crates/ra_hir/src/ty/snapshots/tests__infer_associated_const.snap b/crates/ra_hir/src/ty/snapshots/tests__infer_associated_const.snap new file mode 100644 index 000000000..14ab8ba96 --- /dev/null +++ b/crates/ra_hir/src/ty/snapshots/tests__infer_associated_const.snap @@ -0,0 +1,14 @@ +--- +created: "2019-02-20T11:04:56.553382800Z" +creator: insta@0.6.3 +source: crates/ra_hir/src/ty/tests.rs +expression: "&result" +--- +[227; 305) '{ ...:ID; }': () +[237; 238) 'x': u32 +[241; 252) 'Struct::FOO': u32 +[262; 263) 'y': u32 +[266; 275) 'Enum::BAR': u32 +[285; 286) 'z': u32 +[289; 302) 'TraitTest::ID': u32 + diff --git a/crates/ra_hir/src/ty/snapshots/tests__infer_associated_method_enum.snap b/crates/ra_hir/src/ty/snapshots/tests__infer_associated_method_enum.snap new file mode 100644 index 000000000..8f27af88c --- /dev/null +++ b/crates/ra_hir/src/ty/snapshots/tests__infer_associated_method_enum.snap @@ -0,0 +1,20 @@ +--- +created: "2019-02-20T11:04:56.553382800Z" +creator: insta@0.6.3 +source: crates/ra_hir/src/ty/tests.rs +expression: "&result" +--- +[48; 68) '{ ... }': A +[58; 62) 'A::B': A +[89; 109) '{ ... }': A +[99; 103) 'A::C': A +[122; 179) '{ ... c; }': () +[132; 133) 'a': A +[136; 140) 'A::b': fn b() -> A +[136; 142) 'A::b()': A +[148; 149) 'a': A +[159; 160) 'c': A +[163; 167) 'A::c': fn c() -> A +[163; 169) 'A::c()': A +[175; 176) 'c': A + diff --git a/crates/ra_hir/src/ty/snapshots/tests__infer_associated_method_generics.snap b/crates/ra_hir/src/ty/snapshots/tests__infer_associated_method_generics.snap new file mode 100644 index 000000000..fe5d6590e --- /dev/null +++ b/crates/ra_hir/src/ty/snapshots/tests__infer_associated_method_generics.snap @@ -0,0 +1,16 @@ +--- +created: "2019-02-21T10:25:18.568887300Z" +creator: insta@0.6.3 +source: crates/ra_hir/src/ty/tests.rs +expression: "&result" +--- +[64; 67) 'val': T +[82; 109) '{ ... }': Gen +[92; 103) 'Gen { val }': Gen +[98; 101) 'val': T +[123; 155) '{ ...32); }': () +[133; 134) 'a': Gen<[unknown]> +[137; 146) 'Gen::make': fn make<[unknown]>(T) -> Gen +[137; 152) 'Gen::make(0u32)': Gen<[unknown]> +[147; 151) '0u32': u32 + diff --git a/crates/ra_hir/src/ty/snapshots/tests__infer_associated_method_struct.snap b/crates/ra_hir/src/ty/snapshots/tests__infer_associated_method_struct.snap new file mode 100644 index 000000000..29ca9b0ea --- /dev/null +++ b/crates/ra_hir/src/ty/snapshots/tests__infer_associated_method_struct.snap @@ -0,0 +1,16 @@ +--- +created: "2019-02-20T11:04:56.553382800Z" +creator: insta@0.6.3 +source: crates/ra_hir/src/ty/tests.rs +expression: "&result" +--- +[50; 76) '{ ... }': A +[60; 70) 'A { x: 0 }': A +[67; 68) '0': u32 +[89; 123) '{ ...a.x; }': () +[99; 100) 'a': A +[103; 109) 'A::new': fn new() -> A +[103; 111) 'A::new()': A +[117; 118) 'a': A +[117; 120) 'a.x': u32 + diff --git a/crates/ra_hir/src/ty/snapshots/tests__infer_associated_method_with_modules.snap b/crates/ra_hir/src/ty/snapshots/tests__infer_associated_method_with_modules.snap new file mode 100644 index 000000000..6584bef39 --- /dev/null +++ b/crates/ra_hir/src/ty/snapshots/tests__infer_associated_method_with_modules.snap @@ -0,0 +1,23 @@ +--- +created: "2019-02-21T08:55:53.926725400Z" +creator: insta@0.6.3 +source: crates/ra_hir/src/ty/tests.rs +expression: "&result" +--- +[56; 64) '{ A {} }': A +[58; 62) 'A {}': A +[126; 132) '{ 99 }': u32 +[128; 130) '99': u32 +[202; 210) '{ C {} }': C +[204; 208) 'C {}': C +[241; 325) '{ ...g(); }': () +[251; 252) 'x': A +[255; 266) 'a::A::thing': fn thing() -> A +[255; 268) 'a::A::thing()': A +[278; 279) 'y': u32 +[282; 293) 'b::B::thing': fn thing() -> u32 +[282; 295) 'b::B::thing()': u32 +[305; 306) 'z': C +[309; 320) 'c::C::thing': fn thing() -> C +[309; 322) 'c::C::thing()': C + diff --git a/crates/ra_hir/src/ty/tests.rs b/crates/ra_hir/src/ty/tests.rs index 4ab442b8a..c4b452ba4 100644 --- a/crates/ra_hir/src/ty/tests.rs +++ b/crates/ra_hir/src/ty/tests.rs @@ -586,6 +586,139 @@ fn test() -> i128 { ); } +#[test] +fn infer_associated_const() { + check_inference( + "infer_associated_const", + r#" +struct Struct; + +impl Struct { + const FOO: u32 = 1; +} + +enum Enum; + +impl Enum { + const BAR: u32 = 2; +} + +trait Trait { + const ID: u32; +} + +struct TraitTest; + +impl Trait for TraitTest { + const ID: u32 = 5; +} + +fn test() { + let x = Struct::FOO; + let y = Enum::BAR; + let z = TraitTest::ID; +} +"#, + ); +} + +#[test] +fn infer_associated_method_struct() { + check_inference( + "infer_associated_method_struct", + r#" +struct A { x: u32 }; + +impl A { + fn new() -> A { + A { x: 0 } + } +} +fn test() { + let a = A::new(); + a.x; +} +"#, + ); +} + +#[test] +fn infer_associated_method_enum() { + check_inference( + "infer_associated_method_enum", + r#" +enum A { B, C }; + +impl A { + pub fn b() -> A { + A::B + } + pub fn c() -> A { + A::C + } +} +fn test() { + let a = A::b(); + a; + let c = A::c(); + c; +} +"#, + ); +} + +#[test] +fn infer_associated_method_with_modules() { + check_inference( + "infer_associated_method_with_modules", + r#" +mod a { + struct A; + impl A { pub fn thing() -> A { A {} }} +} + +mod b { + struct B; + impl B { pub fn thing() -> u32 { 99 }} + + mod c { + struct C; + impl C { pub fn thing() -> C { C {} }} + } +} +use b::c; + +fn test() { + let x = a::A::thing(); + let y = b::B::thing(); + let z = c::C::thing(); +} +"#, + ); +} + +#[test] +fn infer_associated_method_generics() { + check_inference( + "infer_associated_method_generics", + r#" +struct Gen { + val: T +} + +impl Gen { + pub fn make(val: T) -> Gen { + Gen { val } + } +} + +fn test() { + let a = Gen::make(0u32); +} +"#, + ); +} + #[test] fn no_panic_on_field_of_enum() { check_inference( -- cgit v1.2.3 From 2e7bc905be156a007a4ee8f1a1bd1d73a1d19ade Mon Sep 17 00:00:00 2001 From: Ville Penttinen Date: Thu, 21 Feb 2019 23:57:07 +0200 Subject: Remove Const inference for now, refactor PathResult --- crates/ra_hir/src/nameres.rs | 23 +++---- crates/ra_hir/src/resolve.rs | 78 ++++++++++++++++------ crates/ra_hir/src/ty.rs | 55 ++++++--------- .../snapshots/tests__infer_associated_const.snap | 14 ++-- 4 files changed, 93 insertions(+), 77 deletions(-) (limited to 'crates/ra_hir/src') diff --git a/crates/ra_hir/src/nameres.rs b/crates/ra_hir/src/nameres.rs index b78a178c1..b18721104 100644 --- a/crates/ra_hir/src/nameres.rs +++ b/crates/ra_hir/src/nameres.rs @@ -119,10 +119,6 @@ impl PerNs { self.types.is_some() && self.values.is_some() } - pub fn is_values(&self) -> bool { - self.values.is_some() && self.types.is_none() - } - pub fn take(self, namespace: Namespace) -> Option { match namespace { Namespace::Types => self.types, @@ -671,23 +667,20 @@ impl ItemMap { } } } - ModuleDef::Struct(s) => { - return ResolvePathResult::with( - PerNs::types((*s).into()), - ReachedFixedPoint::Yes, - Some(i), - ); - } - _ => { + s => { // could be an inherent method call in UFCS form - // (`Struct::method`), or some other kind of associated - // item... Which we currently don't handle (TODO) + // (`Struct::method`), or some other kind of associated item log::debug!( "path segment {:?} resolved to non-module {:?}, but is not last", segment.name, curr, ); - return ResolvePathResult::empty(ReachedFixedPoint::Yes); + + return ResolvePathResult::with( + PerNs::types((*s).into()), + ReachedFixedPoint::Yes, + Some(i), + ); } }; } diff --git a/crates/ra_hir/src/resolve.rs b/crates/ra_hir/src/resolve.rs index 00b55eae9..9f4d4ab42 100644 --- a/crates/ra_hir/src/resolve.rs +++ b/crates/ra_hir/src/resolve.rs @@ -33,27 +33,68 @@ pub(crate) struct ExprScope { } #[derive(Debug, Clone)] -pub enum PathResult { - /// Path was fully resolved - FullyResolved(PerNs), - /// Path was partially resolved, first element contains the resolution - /// second contains the index in the Path.segments which we were unable to resolve - PartiallyResolved(PerNs, usize), +pub struct PathResult { + /// The actual path resolution + resolution: PerNs, + /// The first index in the path that we + /// were unable to resolve. + /// When path is fully resolved, this is 0. + remaining_index: usize, } impl PathResult { - pub fn segment_index(&self) -> Option { - match self { - PathResult::FullyResolved(_) => None, - PathResult::PartiallyResolved(_, ref i) => Some(*i), + /// Returns the remaining index in the result + /// returns None if the path was fully resolved + pub fn remaining_index(&self) -> Option { + if self.remaining_index > 0 { + Some(self.remaining_index) + } else { + None } } /// Consumes `PathResult` and returns the contained `PerNs` + /// if the path was fully resolved, meaning we have no remaining items pub fn into_per_ns(self) -> PerNs { - match self { - PathResult::FullyResolved(def) => def, - PathResult::PartiallyResolved(def, _) => def, + if self.remaining_index().is_none() { + self.resolution + } else { + PerNs::none() + } + } + + /// Consumes `PathResult` and returns the resolution and the + /// remaining_index as a tuple. + pub fn into_inner(self) -> (PerNs, Option) { + let index = self.remaining_index(); + (self.resolution, index) + } + + /// Path is fully resolved when `remaining_index` is none + /// and the resolution contains anything + pub fn is_fully_resolved(&self) -> bool { + !self.resolution.is_none() && self.remaining_index().is_none() + } + + /// Empty path result is where the resolution is `none` + /// and the remaining index is 0 + pub fn is_empty(&self) -> bool { + self.resolution.is_none() && self.remaining_index().is_none() + } + + fn empty() -> PathResult { + PathResult { resolution: PerNs::none(), remaining_index: 0 } + } + + fn from_resolution(res: PerNs) -> PathResult { + PathResult::from_resolution_with_index(res, 0) + } + + fn from_resolution_with_index(res: PerNs, remaining_index: usize) -> PathResult { + if res.is_none() { + PathResult::empty() + } else { + PathResult { resolution: res, remaining_index } } } } @@ -94,24 +135,23 @@ impl Resolver { } pub fn resolve_path(&self, db: &impl HirDatabase, path: &Path) -> PathResult { - use self::PathResult::*; if let Some(name) = path.as_ident() { - FullyResolved(self.resolve_name(db, name)) + PathResult::from_resolution(self.resolve_name(db, name)) } else if path.is_self() { - FullyResolved(self.resolve_name(db, &Name::self_param())) + PathResult::from_resolution(self.resolve_name(db, &Name::self_param())) } else { let (item_map, module) = match self.module() { Some(m) => m, - _ => return FullyResolved(PerNs::none()), + _ => return PathResult::empty(), }; let (module_res, segment_index) = item_map.resolve_path(db, module, path); let def = module_res.map(Resolution::Def); if let Some(index) = segment_index { - PartiallyResolved(def, index) + PathResult::from_resolution_with_index(def, index) } else { - FullyResolved(def) + PathResult::from_resolution(def) } } } diff --git a/crates/ra_hir/src/ty.rs b/crates/ra_hir/src/ty.rs index 2751ab3ab..b1f35ab1f 100644 --- a/crates/ra_hir/src/ty.rs +++ b/crates/ra_hir/src/ty.rs @@ -32,20 +32,17 @@ use rustc_hash::FxHashMap; use test_utils::tested_by; -use ra_syntax::ast::NameOwner; - use crate::{ Function, Struct, StructField, Enum, EnumVariant, Path, Name, - Const, FnSignature, ModuleDef, AdtDef, HirDatabase, type_ref::{TypeRef, Mutability}, - name::{KnownName, AsName}, + name::{KnownName}, expr::{Body, Expr, BindingAnnotation, Literal, ExprId, Pat, PatId, UnaryOp, BinaryOp, Statement, FieldPat, self}, generics::GenericParams, path::GenericArg, adt::VariantDef, - resolve::{Resolver, Resolution, PathResult}, nameres::Namespace + resolve::{Resolver, Resolution}, nameres::Namespace }; /// The ID of a type variable. @@ -681,19 +678,6 @@ fn type_for_fn(db: &impl HirDatabase, def: Function) -> Ty { Ty::FnDef { def: def.into(), sig, name, substs } } -fn type_for_const(db: &impl HirDatabase, resolver: &Resolver, def: Const) -> Ty { - let node = def.source(db).1; - - let tr = node - .type_ref() - .map(TypeRef::from_ast) - .as_ref() - .map(|tr| Ty::from_hir(db, resolver, tr)) - .unwrap_or_else(|| Ty::Unknown); - - tr -} - /// Compute the type of a tuple struct constructor. fn type_for_struct_constructor(db: &impl HirDatabase, def: Struct) -> Ty { let var_data = def.variant_data(db); @@ -1190,21 +1174,28 @@ impl<'a, D: HirDatabase> InferenceContext<'a, D> { fn infer_path_expr(&mut self, resolver: &Resolver, path: &Path) -> Option { let resolved = resolver.resolve_path(self.db, &path); - let (resolved, segment_index) = match resolved { - PathResult::FullyResolved(def) => (def.take_values()?, None), - PathResult::PartiallyResolved(def, index) => (def.take_types()?, Some(index)), - }; + let (def, remaining_index) = resolved.into_inner(); + + // if the remaining_index is None, we expect the path + // to be fully resolved, in this case we continue with + // the default by attempting to `take_values´ from the resolution. + // Otherwise the path was partially resolved, which means + // we might have resolved into a type for which + // we may find some associated item starting at the + // path.segment pointed to by `remaining_index´ + let resolved = + if remaining_index.is_none() { def.take_values()? } else { def.take_types()? }; match resolved { Resolution::Def(def) => { let typable: Option = def.into(); let typable = typable?; - if let Some(segment_index) = segment_index { + if let Some(remaining_index) = remaining_index { let ty = self.db.type_for_def(typable, Namespace::Types); - // TODO: What to do if segment_index is not the last segment - // in the path - let segment = &path.segments[segment_index]; + // TODO: Keep resolving the segments + // if we have more segments to process + let segment = &path.segments[remaining_index]; // Attempt to find an impl_item for the type which has a name matching // the current segment @@ -1216,17 +1207,9 @@ impl<'a, D: HirDatabase> InferenceContext<'a, D> { } None } - crate::ImplItem::Const(c) => { - let node = c.source(self.db).1; - - if let Some(name) = node.name().map(|n| n.as_name()) { - if segment.name == name { - return Some(type_for_const(self.db, resolver, c)); - } - } - None - } + // TODO: Resolve associated const + crate::ImplItem::Const(_) => None, // TODO: Resolve associated types crate::ImplItem::Type(_) => None, diff --git a/crates/ra_hir/src/ty/snapshots/tests__infer_associated_const.snap b/crates/ra_hir/src/ty/snapshots/tests__infer_associated_const.snap index 14ab8ba96..131d1fa16 100644 --- a/crates/ra_hir/src/ty/snapshots/tests__infer_associated_const.snap +++ b/crates/ra_hir/src/ty/snapshots/tests__infer_associated_const.snap @@ -1,14 +1,14 @@ --- -created: "2019-02-20T11:04:56.553382800Z" +created: "2019-02-21T21:51:46.497925200Z" creator: insta@0.6.3 source: crates/ra_hir/src/ty/tests.rs expression: "&result" --- [227; 305) '{ ...:ID; }': () -[237; 238) 'x': u32 -[241; 252) 'Struct::FOO': u32 -[262; 263) 'y': u32 -[266; 275) 'Enum::BAR': u32 -[285; 286) 'z': u32 -[289; 302) 'TraitTest::ID': u32 +[237; 238) 'x': [unknown] +[241; 252) 'Struct::FOO': [unknown] +[262; 263) 'y': [unknown] +[266; 275) 'Enum::BAR': [unknown] +[285; 286) 'z': [unknown] +[289; 302) 'TraitTest::ID': [unknown] -- cgit v1.2.3 From a34eb98a781453a66ce0442e1f7722c0774ae49f Mon Sep 17 00:00:00 2001 From: Ville Penttinen Date: Fri, 22 Feb 2019 00:11:21 +0200 Subject: Make nameres::ResolvePathResult private and refactor --- crates/ra_hir/src/nameres.rs | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) (limited to 'crates/ra_hir/src') diff --git a/crates/ra_hir/src/nameres.rs b/crates/ra_hir/src/nameres.rs index b18721104..8067b8415 100644 --- a/crates/ra_hir/src/nameres.rs +++ b/crates/ra_hir/src/nameres.rs @@ -304,7 +304,7 @@ where &import.path, ); - (res.module, res.reached_fixedpoint) + (res.resolved_def, res.reached_fixedpoint) }; if reached_fixedpoint != ReachedFixedPoint::Yes { @@ -443,9 +443,9 @@ where } #[derive(Debug, Clone)] -pub struct ResolvePathResult { - pub(crate) module: PerNs, - pub(crate) segment_index: Option, +struct ResolvePathResult { + resolved_def: PerNs, + segment_index: Option, reached_fixedpoint: ReachedFixedPoint, } @@ -455,11 +455,11 @@ impl ResolvePathResult { } fn with( - module: PerNs, + resolved_def: PerNs, reached_fixedpoint: ReachedFixedPoint, segment_index: Option, ) -> ResolvePathResult { - ResolvePathResult { module, reached_fixedpoint, segment_index } + ResolvePathResult { resolved_def, reached_fixedpoint, segment_index } } } @@ -498,7 +498,7 @@ impl ItemMap { path: &Path, ) -> (PerNs, Option) { let res = self.resolve_path_fp(db, ResolveMode::Other, original_module, path); - (res.module, res.segment_index) + (res.resolved_def, res.segment_index) } fn resolve_in_prelude( -- cgit v1.2.3 From 39679d499f5e0b5d26ae0f0b707f6db8901cdb66 Mon Sep 17 00:00:00 2001 From: Ville Penttinen Date: Fri, 22 Feb 2019 00:27:22 +0200 Subject: Ignore failing test for now --- crates/ra_hir/src/ty/tests.rs | 1 + 1 file changed, 1 insertion(+) (limited to 'crates/ra_hir/src') diff --git a/crates/ra_hir/src/ty/tests.rs b/crates/ra_hir/src/ty/tests.rs index c4b452ba4..fee6ed0b3 100644 --- a/crates/ra_hir/src/ty/tests.rs +++ b/crates/ra_hir/src/ty/tests.rs @@ -698,6 +698,7 @@ fn test() { } #[test] +#[ignore] // FIXME: After https://github.com/rust-analyzer/rust-analyzer/pull/866 is merged fn infer_associated_method_generics() { check_inference( "infer_associated_method_generics", -- cgit v1.2.3 From 247d1c17b385ff8a8c1dda2e899495146b643b98 Mon Sep 17 00:00:00 2001 From: Ville Penttinen Date: Fri, 22 Feb 2019 10:15:23 +0200 Subject: Change resolve_path to return the fully resolved path or PerNs::none This also adds new pub(crate) resolve_path_segments which returns the PathResult, which may or may not be fully resolved. PathResult is also now pub(crate) since it is an implementation detail. --- crates/ra_hir/src/impl_block.rs | 2 +- crates/ra_hir/src/resolve.rs | 29 ++++++++++++++++------------- crates/ra_hir/src/ty.rs | 39 +++++++++++++++++++-------------------- 3 files changed, 36 insertions(+), 34 deletions(-) (limited to 'crates/ra_hir/src') diff --git a/crates/ra_hir/src/impl_block.rs b/crates/ra_hir/src/impl_block.rs index f4dccf165..7ecf8c368 100644 --- a/crates/ra_hir/src/impl_block.rs +++ b/crates/ra_hir/src/impl_block.rs @@ -88,7 +88,7 @@ impl ImplBlock { if let Some(TypeRef::Path(path)) = self.target_trait_ref(db) { let resolver = self.resolver(db); if let Some(Resolution::Def(ModuleDef::Trait(tr))) = - resolver.resolve_path(db, &path).into_per_ns().take_types() + resolver.resolve_path(db, &path).take_types() { return Some(tr); } diff --git a/crates/ra_hir/src/resolve.rs b/crates/ra_hir/src/resolve.rs index 9f4d4ab42..57e7d0b9a 100644 --- a/crates/ra_hir/src/resolve.rs +++ b/crates/ra_hir/src/resolve.rs @@ -33,7 +33,7 @@ pub(crate) struct ExprScope { } #[derive(Debug, Clone)] -pub struct PathResult { +pub(crate) struct PathResult { /// The actual path resolution resolution: PerNs, /// The first index in the path that we @@ -45,7 +45,7 @@ pub struct PathResult { impl PathResult { /// Returns the remaining index in the result /// returns None if the path was fully resolved - pub fn remaining_index(&self) -> Option { + pub(crate) fn remaining_index(&self) -> Option { if self.remaining_index > 0 { Some(self.remaining_index) } else { @@ -55,8 +55,8 @@ impl PathResult { /// Consumes `PathResult` and returns the contained `PerNs` /// if the path was fully resolved, meaning we have no remaining items - pub fn into_per_ns(self) -> PerNs { - if self.remaining_index().is_none() { + pub(crate) fn into_fully_resolved(self) -> PerNs { + if self.is_fully_resolved() { self.resolution } else { PerNs::none() @@ -65,23 +65,17 @@ impl PathResult { /// Consumes `PathResult` and returns the resolution and the /// remaining_index as a tuple. - pub fn into_inner(self) -> (PerNs, Option) { + pub(crate) fn into_inner(self) -> (PerNs, Option) { let index = self.remaining_index(); (self.resolution, index) } /// Path is fully resolved when `remaining_index` is none /// and the resolution contains anything - pub fn is_fully_resolved(&self) -> bool { + pub(crate) fn is_fully_resolved(&self) -> bool { !self.resolution.is_none() && self.remaining_index().is_none() } - /// Empty path result is where the resolution is `none` - /// and the remaining index is 0 - pub fn is_empty(&self) -> bool { - self.resolution.is_none() && self.remaining_index().is_none() - } - fn empty() -> PathResult { PathResult { resolution: PerNs::none(), remaining_index: 0 } } @@ -134,7 +128,9 @@ impl Resolver { resolution } - pub fn resolve_path(&self, db: &impl HirDatabase, path: &Path) -> PathResult { + /// Returns the resolved path segments + /// Which may be fully resolved, empty or partially resolved. + pub(crate) fn resolve_path_segments(&self, db: &impl HirDatabase, path: &Path) -> PathResult { if let Some(name) = path.as_ident() { PathResult::from_resolution(self.resolve_name(db, name)) } else if path.is_self() { @@ -156,6 +152,13 @@ impl Resolver { } } + /// Returns the fully resolved path if we were able to resolve it. + /// otherwise returns `PerNs::none` + pub fn resolve_path(&self, db: &impl HirDatabase, path: &Path) -> PerNs { + // into_fully_resolved() returns the fully resolved path or PerNs::none() otherwise + self.resolve_path_segments(db, path).into_fully_resolved() + } + pub fn all_names(&self, db: &impl HirDatabase) -> FxHashMap> { let mut names = FxHashMap::default(); for scope in self.scopes.iter().rev() { diff --git a/crates/ra_hir/src/ty.rs b/crates/ra_hir/src/ty.rs index b1f35ab1f..89e854dd7 100644 --- a/crates/ra_hir/src/ty.rs +++ b/crates/ra_hir/src/ty.rs @@ -370,7 +370,7 @@ impl Ty { } // Resolve the path (in type namespace) - let resolution = resolver.resolve_path(db, path).into_per_ns().take_types(); + let resolution = resolver.resolve_path(db, path).take_types(); let def = match resolution { Some(Resolution::Def(def)) => def, @@ -1172,7 +1172,7 @@ impl<'a, D: HirDatabase> InferenceContext<'a, D> { } fn infer_path_expr(&mut self, resolver: &Resolver, path: &Path) -> Option { - let resolved = resolver.resolve_path(self.db, &path); + let resolved = resolver.resolve_path_segments(self.db, &path); let (def, remaining_index) = resolved.into_inner(); @@ -1244,24 +1244,23 @@ impl<'a, D: HirDatabase> InferenceContext<'a, D> { None => return (Ty::Unknown, None), }; let resolver = &self.resolver; - let typable: Option = - match resolver.resolve_path(self.db, &path).into_per_ns().take_types() { - Some(Resolution::Def(def)) => def.into(), - Some(Resolution::LocalBinding(..)) => { - // this cannot happen - log::error!("path resolved to local binding in type ns"); - return (Ty::Unknown, None); - } - Some(Resolution::GenericParam(..)) => { - // generic params can't be used in struct literals - return (Ty::Unknown, None); - } - Some(Resolution::SelfType(..)) => { - // TODO this is allowed in an impl for a struct, handle this - return (Ty::Unknown, None); - } - None => return (Ty::Unknown, None), - }; + let typable: Option = match resolver.resolve_path(self.db, &path).take_types() { + Some(Resolution::Def(def)) => def.into(), + Some(Resolution::LocalBinding(..)) => { + // this cannot happen + log::error!("path resolved to local binding in type ns"); + return (Ty::Unknown, None); + } + Some(Resolution::GenericParam(..)) => { + // generic params can't be used in struct literals + return (Ty::Unknown, None); + } + Some(Resolution::SelfType(..)) => { + // TODO this is allowed in an impl for a struct, handle this + return (Ty::Unknown, None); + } + None => return (Ty::Unknown, None), + }; let def = match typable { None => return (Ty::Unknown, None), Some(it) => it, -- cgit v1.2.3