diff options
Diffstat (limited to 'crates/ra_hir')
-rw-r--r-- | crates/ra_hir/Cargo.toml | 2 | ||||
-rw-r--r-- | crates/ra_hir/src/code_model.rs | 26 | ||||
-rw-r--r-- | crates/ra_hir/src/db.rs | 7 | ||||
-rw-r--r-- | crates/ra_hir/src/from_id.rs | 21 | ||||
-rw-r--r-- | crates/ra_hir/src/lib.rs | 2 | ||||
-rw-r--r-- | crates/ra_hir/src/semantics.rs | 6 | ||||
-rw-r--r-- | crates/ra_hir/src/source_analyzer.rs | 44 |
7 files changed, 66 insertions, 42 deletions
diff --git a/crates/ra_hir/Cargo.toml b/crates/ra_hir/Cargo.toml index 42193b492..ba7b39a19 100644 --- a/crates/ra_hir/Cargo.toml +++ b/crates/ra_hir/Cargo.toml | |||
@@ -13,7 +13,7 @@ rustc-hash = "1.1.0" | |||
13 | either = "1.5.3" | 13 | either = "1.5.3" |
14 | arrayvec = "0.5.1" | 14 | arrayvec = "0.5.1" |
15 | 15 | ||
16 | itertools = "0.8.2" | 16 | itertools = "0.9.0" |
17 | 17 | ||
18 | ra_syntax = { path = "../ra_syntax" } | 18 | ra_syntax = { path = "../ra_syntax" } |
19 | ra_db = { path = "../ra_db" } | 19 | ra_db = { path = "../ra_db" } |
diff --git a/crates/ra_hir/src/code_model.rs b/crates/ra_hir/src/code_model.rs index e91abf6f5..cd2a8fc62 100644 --- a/crates/ra_hir/src/code_model.rs +++ b/crates/ra_hir/src/code_model.rs | |||
@@ -33,7 +33,11 @@ use ra_syntax::{ | |||
33 | }; | 33 | }; |
34 | use rustc_hash::FxHashSet; | 34 | use rustc_hash::FxHashSet; |
35 | 35 | ||
36 | use crate::{db::HirDatabase, has_source::HasSource, CallableDef, HirDisplay, InFile, Name}; | 36 | use crate::{ |
37 | db::{DefDatabase, HirDatabase}, | ||
38 | has_source::HasSource, | ||
39 | CallableDef, HirDisplay, InFile, Name, | ||
40 | }; | ||
37 | 41 | ||
38 | /// hir::Crate describes a single crate. It's the main interface with which | 42 | /// hir::Crate describes a single crate. It's the main interface with which |
39 | /// a crate's dependencies interact. Mostly, it should be just a proxy for the | 43 | /// a crate's dependencies interact. Mostly, it should be just a proxy for the |
@@ -230,6 +234,10 @@ impl Module { | |||
230 | .collect() | 234 | .collect() |
231 | } | 235 | } |
232 | 236 | ||
237 | pub fn visibility_of(self, db: &dyn HirDatabase, def: &ModuleDef) -> Option<Visibility> { | ||
238 | db.crate_def_map(self.id.krate)[self.id.local_id].scope.visibility_of(def.clone().into()) | ||
239 | } | ||
240 | |||
233 | pub fn diagnostics(self, db: &dyn HirDatabase, sink: &mut DiagnosticSink) { | 241 | pub fn diagnostics(self, db: &dyn HirDatabase, sink: &mut DiagnosticSink) { |
234 | let _p = profile("Module::diagnostics"); | 242 | let _p = profile("Module::diagnostics"); |
235 | let crate_def_map = db.crate_def_map(self.id.krate); | 243 | let crate_def_map = db.crate_def_map(self.id.krate); |
@@ -274,20 +282,10 @@ impl Module { | |||
274 | /// this module, if possible. | 282 | /// this module, if possible. |
275 | pub fn find_use_path( | 283 | pub fn find_use_path( |
276 | self, | 284 | self, |
277 | db: &dyn HirDatabase, | 285 | db: &dyn DefDatabase, |
278 | item: ModuleDef, | 286 | item: impl Into<ItemInNs>, |
279 | ) -> Option<hir_def::path::ModPath> { | 287 | ) -> Option<hir_def::path::ModPath> { |
280 | // FIXME expose namespace choice | 288 | hir_def::find_path::find_path(db, item.into(), self.into()) |
281 | hir_def::find_path::find_path(db.upcast(), determine_item_namespace(item), self.into()) | ||
282 | } | ||
283 | } | ||
284 | |||
285 | fn determine_item_namespace(module_def: ModuleDef) -> ItemInNs { | ||
286 | match module_def { | ||
287 | ModuleDef::Static(_) | ModuleDef::Const(_) | ModuleDef::Function(_) => { | ||
288 | ItemInNs::Values(module_def.into()) | ||
289 | } | ||
290 | _ => ItemInNs::Types(module_def.into()), | ||
291 | } | 289 | } |
292 | } | 290 | } |
293 | 291 | ||
diff --git a/crates/ra_hir/src/db.rs b/crates/ra_hir/src/db.rs index fcba95091..ec931b34f 100644 --- a/crates/ra_hir/src/db.rs +++ b/crates/ra_hir/src/db.rs | |||
@@ -10,15 +10,16 @@ pub use hir_def::db::{ | |||
10 | TraitDataQuery, TypeAliasDataQuery, UnionDataQuery, | 10 | TraitDataQuery, TypeAliasDataQuery, UnionDataQuery, |
11 | }; | 11 | }; |
12 | pub use hir_expand::db::{ | 12 | pub use hir_expand::db::{ |
13 | AstDatabase, AstDatabaseStorage, AstIdMapQuery, InternMacroQuery, MacroArgQuery, MacroDefQuery, | 13 | AstDatabase, AstDatabaseStorage, AstIdMapQuery, InternEagerExpansionQuery, InternMacroQuery, |
14 | MacroExpandQuery, ParseMacroQuery, | 14 | MacroArgQuery, MacroDefQuery, MacroExpandQuery, ParseMacroQuery, |
15 | }; | 15 | }; |
16 | pub use hir_ty::db::{ | 16 | pub use hir_ty::db::{ |
17 | AssociatedTyDataQuery, AssociatedTyValueQuery, CallableItemSignatureQuery, FieldTypesQuery, | 17 | AssociatedTyDataQuery, AssociatedTyValueQuery, CallableItemSignatureQuery, FieldTypesQuery, |
18 | GenericDefaultsQuery, GenericPredicatesForParamQuery, GenericPredicatesQuery, HirDatabase, | 18 | GenericDefaultsQuery, GenericPredicatesForParamQuery, GenericPredicatesQuery, HirDatabase, |
19 | HirDatabaseStorage, ImplDatumQuery, ImplSelfTyQuery, ImplTraitQuery, ImplsForTraitQuery, | 19 | HirDatabaseStorage, ImplDatumQuery, ImplSelfTyQuery, ImplTraitQuery, ImplsForTraitQuery, |
20 | ImplsInCrateQuery, InferQueryQuery, InternAssocTyValueQuery, InternChalkImplQuery, | 20 | ImplsInCrateQuery, InferQueryQuery, InternAssocTyValueQuery, InternChalkImplQuery, |
21 | InternTypeCtorQuery, StructDatumQuery, TraitDatumQuery, TraitSolveQuery, TyQuery, ValueTyQuery, | 21 | InternTypeCtorQuery, InternTypeParamIdQuery, StructDatumQuery, TraitDatumQuery, |
22 | TraitSolveQuery, TyQuery, ValueTyQuery, | ||
22 | }; | 23 | }; |
23 | 24 | ||
24 | #[test] | 25 | #[test] |
diff --git a/crates/ra_hir/src/from_id.rs b/crates/ra_hir/src/from_id.rs index c179b13c6..62fb52e72 100644 --- a/crates/ra_hir/src/from_id.rs +++ b/crates/ra_hir/src/from_id.rs | |||
@@ -9,8 +9,8 @@ use hir_def::{ | |||
9 | }; | 9 | }; |
10 | 10 | ||
11 | use crate::{ | 11 | use crate::{ |
12 | Adt, AssocItem, AttrDef, DefWithBody, EnumVariant, GenericDef, Local, ModuleDef, StructField, | 12 | code_model::ItemInNs, Adt, AssocItem, AttrDef, DefWithBody, EnumVariant, GenericDef, Local, |
13 | VariantDef, | 13 | MacroDef, ModuleDef, StructField, VariantDef, |
14 | }; | 14 | }; |
15 | 15 | ||
16 | macro_rules! from_id { | 16 | macro_rules! from_id { |
@@ -228,3 +228,20 @@ impl From<(DefWithBodyId, PatId)> for Local { | |||
228 | Local { parent, pat_id } | 228 | Local { parent, pat_id } |
229 | } | 229 | } |
230 | } | 230 | } |
231 | |||
232 | impl From<MacroDef> for ItemInNs { | ||
233 | fn from(macro_def: MacroDef) -> Self { | ||
234 | ItemInNs::Macros(macro_def.into()) | ||
235 | } | ||
236 | } | ||
237 | |||
238 | impl From<ModuleDef> for ItemInNs { | ||
239 | fn from(module_def: ModuleDef) -> Self { | ||
240 | match module_def { | ||
241 | ModuleDef::Static(_) | ModuleDef::Const(_) | ModuleDef::Function(_) => { | ||
242 | ItemInNs::Values(module_def.into()) | ||
243 | } | ||
244 | _ => ItemInNs::Types(module_def.into()), | ||
245 | } | ||
246 | } | ||
247 | } | ||
diff --git a/crates/ra_hir/src/lib.rs b/crates/ra_hir/src/lib.rs index 713d45f48..5af7e5d6d 100644 --- a/crates/ra_hir/src/lib.rs +++ b/crates/ra_hir/src/lib.rs | |||
@@ -54,7 +54,7 @@ pub use crate::{ | |||
54 | Adt, AsAssocItem, AssocItem, AssocItemContainer, AttrDef, Const, Crate, CrateDependency, | 54 | Adt, AsAssocItem, AssocItem, AssocItemContainer, AttrDef, Const, Crate, CrateDependency, |
55 | DefWithBody, Docs, Enum, EnumVariant, FieldSource, Function, GenericDef, HasAttrs, | 55 | DefWithBody, Docs, Enum, EnumVariant, FieldSource, Function, GenericDef, HasAttrs, |
56 | HasVisibility, ImplDef, Local, MacroDef, Module, ModuleDef, ScopeDef, Static, Struct, | 56 | HasVisibility, ImplDef, Local, MacroDef, Module, ModuleDef, ScopeDef, Static, Struct, |
57 | StructField, Trait, Type, TypeAlias, TypeParam, Union, VariantDef, | 57 | StructField, Trait, Type, TypeAlias, TypeParam, Union, VariantDef, Visibility, |
58 | }, | 58 | }, |
59 | has_source::HasSource, | 59 | has_source::HasSource, |
60 | semantics::{original_range, PathResolution, Semantics, SemanticsScope}, | 60 | semantics::{original_range, PathResolution, Semantics, SemanticsScope}, |
diff --git a/crates/ra_hir/src/semantics.rs b/crates/ra_hir/src/semantics.rs index d982f6ffa..16a5fe968 100644 --- a/crates/ra_hir/src/semantics.rs +++ b/crates/ra_hir/src/semantics.rs | |||
@@ -173,11 +173,11 @@ impl<'db, DB: HirDatabase> Semantics<'db, DB> { | |||
173 | } | 173 | } |
174 | 174 | ||
175 | pub fn resolve_method_call(&self, call: &ast::MethodCallExpr) -> Option<Function> { | 175 | pub fn resolve_method_call(&self, call: &ast::MethodCallExpr) -> Option<Function> { |
176 | self.analyze(call.syntax()).resolve_method_call(call) | 176 | self.analyze(call.syntax()).resolve_method_call(self.db, call) |
177 | } | 177 | } |
178 | 178 | ||
179 | pub fn resolve_field(&self, field: &ast::FieldExpr) -> Option<StructField> { | 179 | pub fn resolve_field(&self, field: &ast::FieldExpr) -> Option<StructField> { |
180 | self.analyze(field.syntax()).resolve_field(field) | 180 | self.analyze(field.syntax()).resolve_field(self.db, field) |
181 | } | 181 | } |
182 | 182 | ||
183 | pub fn resolve_record_field( | 183 | pub fn resolve_record_field( |
@@ -188,7 +188,7 @@ impl<'db, DB: HirDatabase> Semantics<'db, DB> { | |||
188 | } | 188 | } |
189 | 189 | ||
190 | pub fn resolve_record_literal(&self, record_lit: &ast::RecordLit) -> Option<VariantDef> { | 190 | pub fn resolve_record_literal(&self, record_lit: &ast::RecordLit) -> Option<VariantDef> { |
191 | self.analyze(record_lit.syntax()).resolve_record_literal(record_lit) | 191 | self.analyze(record_lit.syntax()).resolve_record_literal(self.db, record_lit) |
192 | } | 192 | } |
193 | 193 | ||
194 | pub fn resolve_record_pattern(&self, record_pat: &ast::RecordPat) -> Option<VariantDef> { | 194 | pub fn resolve_record_pattern(&self, record_pat: &ast::RecordPat) -> Option<VariantDef> { |
diff --git a/crates/ra_hir/src/source_analyzer.rs b/crates/ra_hir/src/source_analyzer.rs index 10c12c910..815ca158c 100644 --- a/crates/ra_hir/src/source_analyzer.rs +++ b/crates/ra_hir/src/source_analyzer.rs | |||
@@ -78,9 +78,15 @@ impl SourceAnalyzer { | |||
78 | } | 78 | } |
79 | } | 79 | } |
80 | 80 | ||
81 | fn expr_id(&self, expr: &ast::Expr) -> Option<ExprId> { | 81 | fn expr_id(&self, db: &dyn HirDatabase, expr: &ast::Expr) -> Option<ExprId> { |
82 | let src = InFile { file_id: self.file_id, value: expr }; | 82 | let src = match expr { |
83 | self.body_source_map.as_ref()?.node_expr(src) | 83 | ast::Expr::MacroCall(call) => { |
84 | self.expand_expr(db, InFile::new(self.file_id, call.clone()))? | ||
85 | } | ||
86 | _ => InFile::new(self.file_id, expr.clone()), | ||
87 | }; | ||
88 | let sm = self.body_source_map.as_ref()?; | ||
89 | sm.node_expr(src.as_ref()) | ||
84 | } | 90 | } |
85 | 91 | ||
86 | fn pat_id(&self, pat: &ast::Pat) -> Option<PatId> { | 92 | fn pat_id(&self, pat: &ast::Pat) -> Option<PatId> { |
@@ -104,14 +110,7 @@ impl SourceAnalyzer { | |||
104 | } | 110 | } |
105 | 111 | ||
106 | pub(crate) fn type_of(&self, db: &dyn HirDatabase, expr: &ast::Expr) -> Option<Type> { | 112 | pub(crate) fn type_of(&self, db: &dyn HirDatabase, expr: &ast::Expr) -> Option<Type> { |
107 | let expr_id = match expr { | 113 | let expr_id = self.expr_id(db, expr)?; |
108 | ast::Expr::MacroCall(call) => { | ||
109 | let expr = self.expand_expr(db, InFile::new(self.file_id, call.clone()))?; | ||
110 | self.body_source_map.as_ref()?.node_expr(expr.as_ref()) | ||
111 | } | ||
112 | _ => self.expr_id(expr), | ||
113 | }?; | ||
114 | |||
115 | let ty = self.infer.as_ref()?[expr_id].clone(); | 114 | let ty = self.infer.as_ref()?[expr_id].clone(); |
116 | Type::new_with_resolver(db, &self.resolver, ty) | 115 | Type::new_with_resolver(db, &self.resolver, ty) |
117 | } | 116 | } |
@@ -122,13 +121,21 @@ impl SourceAnalyzer { | |||
122 | Type::new_with_resolver(db, &self.resolver, ty) | 121 | Type::new_with_resolver(db, &self.resolver, ty) |
123 | } | 122 | } |
124 | 123 | ||
125 | pub(crate) fn resolve_method_call(&self, call: &ast::MethodCallExpr) -> Option<Function> { | 124 | pub(crate) fn resolve_method_call( |
126 | let expr_id = self.expr_id(&call.clone().into())?; | 125 | &self, |
126 | db: &dyn HirDatabase, | ||
127 | call: &ast::MethodCallExpr, | ||
128 | ) -> Option<Function> { | ||
129 | let expr_id = self.expr_id(db, &call.clone().into())?; | ||
127 | self.infer.as_ref()?.method_resolution(expr_id).map(Function::from) | 130 | self.infer.as_ref()?.method_resolution(expr_id).map(Function::from) |
128 | } | 131 | } |
129 | 132 | ||
130 | pub(crate) fn resolve_field(&self, field: &ast::FieldExpr) -> Option<crate::StructField> { | 133 | pub(crate) fn resolve_field( |
131 | let expr_id = self.expr_id(&field.clone().into())?; | 134 | &self, |
135 | db: &dyn HirDatabase, | ||
136 | field: &ast::FieldExpr, | ||
137 | ) -> Option<crate::StructField> { | ||
138 | let expr_id = self.expr_id(db, &field.clone().into())?; | ||
132 | self.infer.as_ref()?.field_resolution(expr_id).map(|it| it.into()) | 139 | self.infer.as_ref()?.field_resolution(expr_id).map(|it| it.into()) |
133 | } | 140 | } |
134 | 141 | ||
@@ -138,7 +145,7 @@ impl SourceAnalyzer { | |||
138 | field: &ast::RecordField, | 145 | field: &ast::RecordField, |
139 | ) -> Option<(crate::StructField, Option<Local>)> { | 146 | ) -> Option<(crate::StructField, Option<Local>)> { |
140 | let (expr_id, local) = match field.expr() { | 147 | let (expr_id, local) = match field.expr() { |
141 | Some(it) => (self.expr_id(&it)?, None), | 148 | Some(it) => (self.expr_id(db, &it)?, None), |
142 | None => { | 149 | None => { |
143 | let src = InFile { file_id: self.file_id, value: field }; | 150 | let src = InFile { file_id: self.file_id, value: field }; |
144 | let expr_id = self.body_source_map.as_ref()?.field_init_shorthand_expr(src)?; | 151 | let expr_id = self.body_source_map.as_ref()?.field_init_shorthand_expr(src)?; |
@@ -159,9 +166,10 @@ impl SourceAnalyzer { | |||
159 | 166 | ||
160 | pub(crate) fn resolve_record_literal( | 167 | pub(crate) fn resolve_record_literal( |
161 | &self, | 168 | &self, |
169 | db: &dyn HirDatabase, | ||
162 | record_lit: &ast::RecordLit, | 170 | record_lit: &ast::RecordLit, |
163 | ) -> Option<crate::VariantDef> { | 171 | ) -> Option<crate::VariantDef> { |
164 | let expr_id = self.expr_id(&record_lit.clone().into())?; | 172 | let expr_id = self.expr_id(db, &record_lit.clone().into())?; |
165 | self.infer.as_ref()?.variant_resolution_for_expr(expr_id).map(|it| it.into()) | 173 | self.infer.as_ref()?.variant_resolution_for_expr(expr_id).map(|it| it.into()) |
166 | } | 174 | } |
167 | 175 | ||
@@ -207,7 +215,7 @@ impl SourceAnalyzer { | |||
207 | path: &ast::Path, | 215 | path: &ast::Path, |
208 | ) -> Option<PathResolution> { | 216 | ) -> Option<PathResolution> { |
209 | if let Some(path_expr) = path.syntax().parent().and_then(ast::PathExpr::cast) { | 217 | if let Some(path_expr) = path.syntax().parent().and_then(ast::PathExpr::cast) { |
210 | let expr_id = self.expr_id(&path_expr.into())?; | 218 | let expr_id = self.expr_id(db, &path_expr.into())?; |
211 | if let Some(assoc) = self.infer.as_ref()?.assoc_resolutions_for_expr(expr_id) { | 219 | if let Some(assoc) = self.infer.as_ref()?.assoc_resolutions_for_expr(expr_id) { |
212 | return Some(PathResolution::AssocItem(assoc.into())); | 220 | return Some(PathResolution::AssocItem(assoc.into())); |
213 | } | 221 | } |