aboutsummaryrefslogtreecommitdiff
path: root/crates/ra_hir_def
diff options
context:
space:
mode:
Diffstat (limited to 'crates/ra_hir_def')
-rw-r--r--crates/ra_hir_def/src/adt.rs20
-rw-r--r--crates/ra_hir_def/src/body.rs31
-rw-r--r--crates/ra_hir_def/src/body/lower.rs3
-rw-r--r--crates/ra_hir_def/src/body/scope.rs6
-rw-r--r--crates/ra_hir_def/src/lang_item.rs5
-rw-r--r--crates/ra_hir_def/src/lib.rs23
-rw-r--r--crates/ra_hir_def/src/nameres.rs32
-rw-r--r--crates/ra_hir_def/src/nameres/collector.rs5
-rw-r--r--crates/ra_hir_def/src/nameres/raw.rs20
-rw-r--r--crates/ra_hir_def/src/nameres/tests.rs2
-rw-r--r--crates/ra_hir_def/src/path.rs2
-rw-r--r--crates/ra_hir_def/src/per_ns.rs5
-rw-r--r--crates/ra_hir_def/src/resolver.rs26
-rw-r--r--crates/ra_hir_def/src/trace.rs42
14 files changed, 109 insertions, 113 deletions
diff --git a/crates/ra_hir_def/src/adt.rs b/crates/ra_hir_def/src/adt.rs
index 20e9a1eb5..c9f30923e 100644
--- a/crates/ra_hir_def/src/adt.rs
+++ b/crates/ra_hir_def/src/adt.rs
@@ -95,7 +95,7 @@ fn lower_enum(
95 name: var.name().map(|it| it.as_name()), 95 name: var.name().map(|it| it.as_name()),
96 variant_data: Arc::new(VariantData::new(var.kind())), 96 variant_data: Arc::new(VariantData::new(var.kind())),
97 }, 97 },
98 ) 98 );
99 } 99 }
100} 100}
101 101
@@ -109,10 +109,18 @@ impl VariantData {
109 } 109 }
110 } 110 }
111 111
112 pub fn fields(&self) -> Option<&Arena<LocalStructFieldId, StructFieldData>> { 112 pub fn fields(&self) -> &Arena<LocalStructFieldId, StructFieldData> {
113 const EMPTY: &Arena<LocalStructFieldId, StructFieldData> = &Arena::new();
113 match &self { 114 match &self {
114 VariantData::Record(fields) | VariantData::Tuple(fields) => Some(fields), 115 VariantData::Record(fields) | VariantData::Tuple(fields) => fields,
115 _ => None, 116 _ => EMPTY,
117 }
118 }
119
120 pub fn is_unit(&self) -> bool {
121 match self {
122 VariantData::Unit => true,
123 _ => false,
116 } 124 }
117 } 125 }
118} 126}
@@ -160,7 +168,7 @@ fn lower_struct(
160 name: Name::new_tuple_field(i), 168 name: Name::new_tuple_field(i),
161 type_ref: TypeRef::from_ast_opt(fd.type_ref()), 169 type_ref: TypeRef::from_ast_opt(fd.type_ref()),
162 }, 170 },
163 ) 171 );
164 } 172 }
165 StructKind::Tuple 173 StructKind::Tuple
166 } 174 }
@@ -172,7 +180,7 @@ fn lower_struct(
172 name: fd.name().map(|n| n.as_name()).unwrap_or_else(Name::missing), 180 name: fd.name().map(|n| n.as_name()).unwrap_or_else(Name::missing),
173 type_ref: TypeRef::from_ast_opt(fd.ascribed_type()), 181 type_ref: TypeRef::from_ast_opt(fd.ascribed_type()),
174 }, 182 },
175 ) 183 );
176 } 184 }
177 StructKind::Record 185 StructKind::Record
178 } 186 }
diff --git a/crates/ra_hir_def/src/body.rs b/crates/ra_hir_def/src/body.rs
index c06997cf1..d77ccb272 100644
--- a/crates/ra_hir_def/src/body.rs
+++ b/crates/ra_hir_def/src/body.rs
@@ -1,4 +1,5 @@
1//! FIXME: write short doc here 1//! Defines `Body`: a lowered representation of bodies of functions, statics and
2//! consts.
2mod lower; 3mod lower;
3pub mod scope; 4pub mod scope;
4 5
@@ -20,7 +21,7 @@ use crate::{
20 DefWithBodyId, HasModule, HasSource, Lookup, ModuleId, 21 DefWithBodyId, HasModule, HasSource, Lookup, ModuleId,
21}; 22};
22 23
23pub struct Expander { 24struct Expander {
24 crate_def_map: Arc<CrateDefMap>, 25 crate_def_map: Arc<CrateDefMap>,
25 current_file_id: HirFileId, 26 current_file_id: HirFileId,
26 hygiene: Hygiene, 27 hygiene: Hygiene,
@@ -28,7 +29,7 @@ pub struct Expander {
28} 29}
29 30
30impl Expander { 31impl Expander {
31 pub fn new(db: &impl DefDatabase, current_file_id: HirFileId, module: ModuleId) -> Expander { 32 fn new(db: &impl DefDatabase, current_file_id: HirFileId, module: ModuleId) -> Expander {
32 let crate_def_map = db.crate_def_map(module.krate); 33 let crate_def_map = db.crate_def_map(module.krate);
33 let hygiene = Hygiene::new(db, current_file_id); 34 let hygiene = Hygiene::new(db, current_file_id);
34 Expander { crate_def_map, current_file_id, hygiene, module } 35 Expander { crate_def_map, current_file_id, hygiene, module }
@@ -101,17 +102,17 @@ impl Drop for Mark {
101/// The body of an item (function, const etc.). 102/// The body of an item (function, const etc.).
102#[derive(Debug, Eq, PartialEq)] 103#[derive(Debug, Eq, PartialEq)]
103pub struct Body { 104pub struct Body {
104 exprs: Arena<ExprId, Expr>, 105 pub exprs: Arena<ExprId, Expr>,
105 pats: Arena<PatId, Pat>, 106 pub pats: Arena<PatId, Pat>,
106 /// The patterns for the function's parameters. While the parameter types are 107 /// The patterns for the function's parameters. While the parameter types are
107 /// part of the function signature, the patterns are not (they don't change 108 /// part of the function signature, the patterns are not (they don't change
108 /// the external type of the function). 109 /// the external type of the function).
109 /// 110 ///
110 /// If this `Body` is for the body of a constant, this will just be 111 /// If this `Body` is for the body of a constant, this will just be
111 /// empty. 112 /// empty.
112 params: Vec<PatId>, 113 pub params: Vec<PatId>,
113 /// The `ExprId` of the actual body expression. 114 /// The `ExprId` of the actual body expression.
114 body_expr: ExprId, 115 pub body_expr: ExprId,
115} 116}
116 117
117pub type ExprPtr = Either<AstPtr<ast::Expr>, AstPtr<ast::RecordField>>; 118pub type ExprPtr = Either<AstPtr<ast::Expr>, AstPtr<ast::RecordField>>;
@@ -182,22 +183,6 @@ impl Body {
182 ) -> (Body, BodySourceMap) { 183 ) -> (Body, BodySourceMap) {
183 lower::lower(db, expander, params, body) 184 lower::lower(db, expander, params, body)
184 } 185 }
185
186 pub fn params(&self) -> &[PatId] {
187 &self.params
188 }
189
190 pub fn body_expr(&self) -> ExprId {
191 self.body_expr
192 }
193
194 pub fn exprs(&self) -> impl Iterator<Item = (ExprId, &Expr)> {
195 self.exprs.iter()
196 }
197
198 pub fn pats(&self) -> impl Iterator<Item = (PatId, &Pat)> {
199 self.pats.iter()
200 }
201} 186}
202 187
203impl Index<ExprId> for Body { 188impl Index<ExprId> for Body {
diff --git a/crates/ra_hir_def/src/body/lower.rs b/crates/ra_hir_def/src/body/lower.rs
index f4640dfa4..331736cb2 100644
--- a/crates/ra_hir_def/src/body/lower.rs
+++ b/crates/ra_hir_def/src/body/lower.rs
@@ -1,4 +1,5 @@
1//! FIXME: write short doc here 1//! Transforms `ast::Expr` into an equivalent `hir_def::expr::Expr`
2//! representation.
2 3
3use hir_expand::{ 4use hir_expand::{
4 either::Either, 5 either::Either,
diff --git a/crates/ra_hir_def/src/body/scope.rs b/crates/ra_hir_def/src/body/scope.rs
index 20d707bc4..625aa39dd 100644
--- a/crates/ra_hir_def/src/body/scope.rs
+++ b/crates/ra_hir_def/src/body/scope.rs
@@ -1,4 +1,4 @@
1//! FIXME: write short doc here 1//! Name resolution for expressions.
2use std::sync::Arc; 2use std::sync::Arc;
3 3
4use hir_expand::name::Name; 4use hir_expand::name::Name;
@@ -54,8 +54,8 @@ impl ExprScopes {
54 let mut scopes = 54 let mut scopes =
55 ExprScopes { scopes: Arena::default(), scope_by_expr: FxHashMap::default() }; 55 ExprScopes { scopes: Arena::default(), scope_by_expr: FxHashMap::default() };
56 let root = scopes.root_scope(); 56 let root = scopes.root_scope();
57 scopes.add_params_bindings(body, root, body.params()); 57 scopes.add_params_bindings(body, root, &body.params);
58 compute_expr_scopes(body.body_expr(), body, &mut scopes, root); 58 compute_expr_scopes(body.body_expr, body, &mut scopes, root);
59 scopes 59 scopes
60 } 60 }
61 61
diff --git a/crates/ra_hir_def/src/lang_item.rs b/crates/ra_hir_def/src/lang_item.rs
index 3b9fb0328..f15c23db9 100644
--- a/crates/ra_hir_def/src/lang_item.rs
+++ b/crates/ra_hir_def/src/lang_item.rs
@@ -39,8 +39,9 @@ impl LangItems {
39 let crate_def_map = db.crate_def_map(krate); 39 let crate_def_map = db.crate_def_map(krate);
40 40
41 crate_def_map 41 crate_def_map
42 .modules() 42 .modules
43 .filter_map(|module_id| db.module_lang_items(ModuleId { krate, module_id })) 43 .iter()
44 .filter_map(|(module_id, _)| db.module_lang_items(ModuleId { krate, module_id }))
44 .for_each(|it| lang_items.items.extend(it.items.iter().map(|(k, v)| (k.clone(), *v)))); 45 .for_each(|it| lang_items.items.extend(it.items.iter().map(|(k, v)| (k.clone(), *v))));
45 46
46 Arc::new(lang_items) 47 Arc::new(lang_items)
diff --git a/crates/ra_hir_def/src/lib.rs b/crates/ra_hir_def/src/lib.rs
index f63c3dd64..8e8c2d749 100644
--- a/crates/ra_hir_def/src/lib.rs
+++ b/crates/ra_hir_def/src/lib.rs
@@ -192,12 +192,6 @@ pub struct LocalEnumVariantId(RawId);
192impl_arena_id!(LocalEnumVariantId); 192impl_arena_id!(LocalEnumVariantId);
193 193
194#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)] 194#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
195pub enum VariantId {
196 EnumVariantId(EnumVariantId),
197 StructId(StructId),
198}
199
200#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
201pub struct StructFieldId { 195pub struct StructFieldId {
202 pub parent: VariantId, 196 pub parent: VariantId,
203 pub local_id: LocalStructFieldId, 197 pub local_id: LocalStructFieldId,
@@ -437,6 +431,13 @@ impl_froms!(
437 ImplId 431 ImplId
438); 432);
439 433
434#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
435pub enum VariantId {
436 EnumVariantId(EnumVariantId),
437 StructId(StructId),
438}
439impl_froms!(VariantId: EnumVariantId, StructId);
440
440trait Intern { 441trait Intern {
441 type ID; 442 type ID;
442 fn intern(self, db: &impl db::DefDatabase) -> Self::ID; 443 fn intern(self, db: &impl db::DefDatabase) -> Self::ID;
@@ -481,6 +482,16 @@ impl HasModule for ConstLoc {
481 } 482 }
482} 483}
483 484
485impl HasModule for AdtId {
486 fn module(&self, db: &impl db::DefDatabase) -> ModuleId {
487 match self {
488 AdtId::StructId(it) => it.0.module(db),
489 AdtId::UnionId(it) => it.0.module(db),
490 AdtId::EnumId(it) => it.module(db),
491 }
492 }
493}
494
484impl HasModule for StaticLoc { 495impl HasModule for StaticLoc {
485 fn module(&self, _db: &impl db::DefDatabase) -> ModuleId { 496 fn module(&self, _db: &impl db::DefDatabase) -> ModuleId {
486 self.container 497 self.container
diff --git a/crates/ra_hir_def/src/nameres.rs b/crates/ra_hir_def/src/nameres.rs
index 5919771b0..2359386c2 100644
--- a/crates/ra_hir_def/src/nameres.rs
+++ b/crates/ra_hir_def/src/nameres.rs
@@ -80,16 +80,16 @@ use crate::{
80/// Contains all top-level defs from a macro-expanded crate 80/// Contains all top-level defs from a macro-expanded crate
81#[derive(Debug, PartialEq, Eq)] 81#[derive(Debug, PartialEq, Eq)]
82pub struct CrateDefMap { 82pub struct CrateDefMap {
83 krate: CrateId, 83 pub root: LocalModuleId,
84 edition: Edition, 84 pub modules: Arena<LocalModuleId, ModuleData>,
85 pub(crate) krate: CrateId,
85 /// The prelude module for this crate. This either comes from an import 86 /// The prelude module for this crate. This either comes from an import
86 /// marked with the `prelude_import` attribute, or (in the normal case) from 87 /// marked with the `prelude_import` attribute, or (in the normal case) from
87 /// a dependency (`std` or `core`). 88 /// a dependency (`std` or `core`).
88 prelude: Option<ModuleId>, 89 pub(crate) prelude: Option<ModuleId>,
89 extern_prelude: FxHashMap<Name, ModuleDefId>, 90 pub(crate) extern_prelude: FxHashMap<Name, ModuleDefId>,
90 root: LocalModuleId,
91 modules: Arena<LocalModuleId, ModuleData>,
92 91
92 edition: Edition,
93 diagnostics: Vec<DefDiagnostic>, 93 diagnostics: Vec<DefDiagnostic>,
94} 94}
95 95
@@ -229,22 +229,6 @@ impl CrateDefMap {
229 Arc::new(def_map) 229 Arc::new(def_map)
230 } 230 }
231 231
232 pub fn krate(&self) -> CrateId {
233 self.krate
234 }
235
236 pub fn root(&self) -> LocalModuleId {
237 self.root
238 }
239
240 pub fn prelude(&self) -> Option<ModuleId> {
241 self.prelude
242 }
243
244 pub fn extern_prelude(&self) -> &FxHashMap<Name, ModuleDefId> {
245 &self.extern_prelude
246 }
247
248 pub fn add_diagnostics( 232 pub fn add_diagnostics(
249 &self, 233 &self,
250 db: &impl DefDatabase, 234 db: &impl DefDatabase,
@@ -254,10 +238,6 @@ impl CrateDefMap {
254 self.diagnostics.iter().for_each(|it| it.add_to(db, module, sink)) 238 self.diagnostics.iter().for_each(|it| it.add_to(db, module, sink))
255 } 239 }
256 240
257 pub fn modules(&self) -> impl Iterator<Item = LocalModuleId> + '_ {
258 self.modules.iter().map(|(id, _data)| id)
259 }
260
261 pub fn modules_for_file(&self, file_id: FileId) -> impl Iterator<Item = LocalModuleId> + '_ { 241 pub fn modules_for_file(&self, file_id: FileId) -> impl Iterator<Item = LocalModuleId> + '_ {
262 self.modules 242 self.modules
263 .iter() 243 .iter()
diff --git a/crates/ra_hir_def/src/nameres/collector.rs b/crates/ra_hir_def/src/nameres/collector.rs
index df01a20e1..41becf8df 100644
--- a/crates/ra_hir_def/src/nameres/collector.rs
+++ b/crates/ra_hir_def/src/nameres/collector.rs
@@ -1,4 +1,7 @@
1//! FIXME: write short doc here 1//! The core of the module-level name resolution algorithm.
2//!
3//! `DefCollector::collect` contains the fixed-point iteration loop which
4//! resolves imports and expands macros.
2 5
3use hir_expand::{ 6use hir_expand::{
4 builtin_macro::find_builtin_macro, 7 builtin_macro::find_builtin_macro,
diff --git a/crates/ra_hir_def/src/nameres/raw.rs b/crates/ra_hir_def/src/nameres/raw.rs
index 2ec84f2cc..401af031c 100644
--- a/crates/ra_hir_def/src/nameres/raw.rs
+++ b/crates/ra_hir_def/src/nameres/raw.rs
@@ -22,7 +22,8 @@ use ra_syntax::{
22use test_utils::tested_by; 22use test_utils::tested_by;
23 23
24use crate::{ 24use crate::{
25 attr::Attrs, db::DefDatabase, path::Path, FileAstId, HirFileId, LocalImportId, Source, 25 attr::Attrs, db::DefDatabase, path::Path, trace::Trace, FileAstId, HirFileId, LocalImportId,
26 Source,
26}; 27};
27 28
28/// `RawItems` is a set of top-level items in a file (except for impls). 29/// `RawItems` is a set of top-level items in a file (except for impls).
@@ -48,10 +49,6 @@ pub struct ImportSourceMap {
48type ImportSourcePtr = Either<AstPtr<ast::UseTree>, AstPtr<ast::ExternCrateItem>>; 49type ImportSourcePtr = Either<AstPtr<ast::UseTree>, AstPtr<ast::ExternCrateItem>>;
49 50
50impl ImportSourceMap { 51impl ImportSourceMap {
51 fn insert(&mut self, import: LocalImportId, ptr: ImportSourcePtr) {
52 self.map.insert(import, ptr)
53 }
54
55 pub fn get(&self, import: LocalImportId) -> ImportSourcePtr { 52 pub fn get(&self, import: LocalImportId) -> ImportSourcePtr {
56 self.map[import].clone() 53 self.map[import].clone()
57 } 54 }
@@ -72,7 +69,7 @@ impl RawItems {
72 let mut collector = RawItemsCollector { 69 let mut collector = RawItemsCollector {
73 raw_items: RawItems::default(), 70 raw_items: RawItems::default(),
74 source_ast_id_map: db.ast_id_map(file_id), 71 source_ast_id_map: db.ast_id_map(file_id),
75 source_map: ImportSourceMap::default(), 72 imports: Trace::new(),
76 file_id, 73 file_id,
77 hygiene: Hygiene::new(db, file_id), 74 hygiene: Hygiene::new(db, file_id),
78 }; 75 };
@@ -83,7 +80,11 @@ impl RawItems {
83 collector.process_module(None, item_list); 80 collector.process_module(None, item_list);
84 } 81 }
85 } 82 }
86 (Arc::new(collector.raw_items), Arc::new(collector.source_map)) 83 let mut raw_items = collector.raw_items;
84 let (arena, map) = collector.imports.into_arena_and_map();
85 raw_items.imports = arena;
86 let source_map = ImportSourceMap { map };
87 (Arc::new(raw_items), Arc::new(source_map))
87 } 88 }
88 89
89 pub(super) fn items(&self) -> &[RawItem] { 90 pub(super) fn items(&self) -> &[RawItem] {
@@ -207,8 +208,8 @@ pub(super) struct ImplData {
207 208
208struct RawItemsCollector { 209struct RawItemsCollector {
209 raw_items: RawItems, 210 raw_items: RawItems,
211 imports: Trace<LocalImportId, ImportData, ImportSourcePtr>,
210 source_ast_id_map: Arc<AstIdMap>, 212 source_ast_id_map: Arc<AstIdMap>,
211 source_map: ImportSourceMap,
212 file_id: HirFileId, 213 file_id: HirFileId,
213 hygiene: Hygiene, 214 hygiene: Hygiene,
214} 215}
@@ -392,8 +393,7 @@ impl RawItemsCollector {
392 data: ImportData, 393 data: ImportData,
393 source: ImportSourcePtr, 394 source: ImportSourcePtr,
394 ) { 395 ) {
395 let import = self.raw_items.imports.alloc(data); 396 let import = self.imports.alloc(|| source, || data);
396 self.source_map.insert(import, source);
397 self.push_item(current_module, attrs, RawItemKind::Import(import)) 397 self.push_item(current_module, attrs, RawItemKind::Import(import))
398 } 398 }
399 399
diff --git a/crates/ra_hir_def/src/nameres/tests.rs b/crates/ra_hir_def/src/nameres/tests.rs
index f0b86af7c..f502f1cb3 100644
--- a/crates/ra_hir_def/src/nameres/tests.rs
+++ b/crates/ra_hir_def/src/nameres/tests.rs
@@ -25,7 +25,7 @@ fn compute_crate_def_map(fixture: &str) -> Arc<CrateDefMap> {
25 25
26fn render_crate_def_map(map: &CrateDefMap) -> String { 26fn render_crate_def_map(map: &CrateDefMap) -> String {
27 let mut buf = String::new(); 27 let mut buf = String::new();
28 go(&mut buf, map, "\ncrate", map.root()); 28 go(&mut buf, map, "\ncrate", map.root);
29 return buf.trim().to_string(); 29 return buf.trim().to_string();
30 30
31 fn go(buf: &mut String, map: &CrateDefMap, path: &str, module: LocalModuleId) { 31 fn go(buf: &mut String, map: &CrateDefMap, path: &str, module: LocalModuleId) {
diff --git a/crates/ra_hir_def/src/path.rs b/crates/ra_hir_def/src/path.rs
index 7b2723d57..0e606fd0e 100644
--- a/crates/ra_hir_def/src/path.rs
+++ b/crates/ra_hir_def/src/path.rs
@@ -195,7 +195,7 @@ impl Path {
195 } 195 }
196 196
197 /// Converts an `ast::NameRef` into a single-identifier `Path`. 197 /// Converts an `ast::NameRef` into a single-identifier `Path`.
198 pub fn from_name_ref(name_ref: &ast::NameRef) -> Path { 198 pub(crate) fn from_name_ref(name_ref: &ast::NameRef) -> Path {
199 name_ref.as_name().into() 199 name_ref.as_name().into()
200 } 200 }
201 201
diff --git a/crates/ra_hir_def/src/per_ns.rs b/crates/ra_hir_def/src/per_ns.rs
index 06ef6c9fc..00e866bf9 100644
--- a/crates/ra_hir_def/src/per_ns.rs
+++ b/crates/ra_hir_def/src/per_ns.rs
@@ -1,4 +1,7 @@
1//! FIXME: write short doc here 1//! In rust, it is possible to have a value, a type and a macro with the same
2//! name without conflicts.
3//!
4//! `PerNs` (per namespace) captures this.
2 5
3use hir_expand::MacroDefId; 6use hir_expand::MacroDefId;
4 7
diff --git a/crates/ra_hir_def/src/resolver.rs b/crates/ra_hir_def/src/resolver.rs
index 7182b8a4d..95b3c926d 100644
--- a/crates/ra_hir_def/src/resolver.rs
+++ b/crates/ra_hir_def/src/resolver.rs
@@ -18,8 +18,8 @@ use crate::{
18 path::{Path, PathKind}, 18 path::{Path, PathKind},
19 per_ns::PerNs, 19 per_ns::PerNs,
20 AdtId, AstItemDef, ConstId, ContainerId, DefWithBodyId, EnumId, EnumVariantId, FunctionId, 20 AdtId, AstItemDef, ConstId, ContainerId, DefWithBodyId, EnumId, EnumVariantId, FunctionId,
21 GenericDefId, ImplId, LocalModuleId, Lookup, ModuleDefId, ModuleId, StaticId, StructId, 21 GenericDefId, HasModule, ImplId, LocalModuleId, Lookup, ModuleDefId, ModuleId, StaticId,
22 TraitId, TypeAliasId, 22 StructId, TraitId, TypeAliasId,
23}; 23};
24 24
25#[derive(Debug, Clone, Default)] 25#[derive(Debug, Clone, Default)]
@@ -29,20 +29,20 @@ pub struct Resolver {
29 29
30// FIXME how to store these best 30// FIXME how to store these best
31#[derive(Debug, Clone)] 31#[derive(Debug, Clone)]
32pub(crate) struct ModuleItemMap { 32struct ModuleItemMap {
33 crate_def_map: Arc<CrateDefMap>, 33 crate_def_map: Arc<CrateDefMap>,
34 module_id: LocalModuleId, 34 module_id: LocalModuleId,
35} 35}
36 36
37#[derive(Debug, Clone)] 37#[derive(Debug, Clone)]
38pub(crate) struct ExprScope { 38struct ExprScope {
39 owner: DefWithBodyId, 39 owner: DefWithBodyId,
40 expr_scopes: Arc<ExprScopes>, 40 expr_scopes: Arc<ExprScopes>,
41 scope_id: ScopeId, 41 scope_id: ScopeId,
42} 42}
43 43
44#[derive(Debug, Clone)] 44#[derive(Debug, Clone)]
45pub(crate) enum Scope { 45enum Scope {
46 /// All the items and imported names of a module 46 /// All the items and imported names of a module
47 ModuleScope(ModuleItemMap), 47 ModuleScope(ModuleItemMap),
48 /// Brings the generic parameters of an item into scope 48 /// Brings the generic parameters of an item into scope
@@ -321,7 +321,7 @@ impl Resolver {
321 let mut traits = FxHashSet::default(); 321 let mut traits = FxHashSet::default();
322 for scope in &self.scopes { 322 for scope in &self.scopes {
323 if let Scope::ModuleScope(m) = scope { 323 if let Scope::ModuleScope(m) = scope {
324 if let Some(prelude) = m.crate_def_map.prelude() { 324 if let Some(prelude) = m.crate_def_map.prelude {
325 let prelude_def_map = db.crate_def_map(prelude.krate); 325 let prelude_def_map = db.crate_def_map(prelude.krate);
326 traits.extend(prelude_def_map[prelude.module_id].scope.traits()); 326 traits.extend(prelude_def_map[prelude.module_id].scope.traits());
327 } 327 }
@@ -340,7 +340,7 @@ impl Resolver {
340 } 340 }
341 341
342 pub fn krate(&self) -> Option<CrateId> { 342 pub fn krate(&self) -> Option<CrateId> {
343 self.module().map(|t| t.0.krate()) 343 self.module().map(|t| t.0.krate)
344 } 344 }
345 345
346 pub fn where_predicates_in_scope<'a>( 346 pub fn where_predicates_in_scope<'a>(
@@ -395,10 +395,10 @@ impl Scope {
395 m.crate_def_map[m.module_id].scope.legacy_macros().for_each(|(name, macro_)| { 395 m.crate_def_map[m.module_id].scope.legacy_macros().for_each(|(name, macro_)| {
396 f(name.clone(), ScopeDef::PerNs(PerNs::macros(macro_))); 396 f(name.clone(), ScopeDef::PerNs(PerNs::macros(macro_)));
397 }); 397 });
398 m.crate_def_map.extern_prelude().iter().for_each(|(name, &def)| { 398 m.crate_def_map.extern_prelude.iter().for_each(|(name, &def)| {
399 f(name.clone(), ScopeDef::PerNs(PerNs::types(def.into()))); 399 f(name.clone(), ScopeDef::PerNs(PerNs::types(def.into())));
400 }); 400 });
401 if let Some(prelude) = m.crate_def_map.prelude() { 401 if let Some(prelude) = m.crate_def_map.prelude {
402 let prelude_def_map = db.crate_def_map(prelude.krate); 402 let prelude_def_map = db.crate_def_map(prelude.krate);
403 prelude_def_map[prelude.module_id].scope.entries().for_each(|(name, res)| { 403 prelude_def_map[prelude.module_id].scope.entries().for_each(|(name, res)| {
404 f(name.clone(), ScopeDef::PerNs(res.def)); 404 f(name.clone(), ScopeDef::PerNs(res.def));
@@ -503,13 +503,7 @@ impl HasResolver for TraitId {
503impl<T: Into<AdtId>> HasResolver for T { 503impl<T: Into<AdtId>> HasResolver for T {
504 fn resolver(self, db: &impl DefDatabase) -> Resolver { 504 fn resolver(self, db: &impl DefDatabase) -> Resolver {
505 let def = self.into(); 505 let def = self.into();
506 let module = match def { 506 def.module(db)
507 AdtId::StructId(it) => it.0.module(db),
508 AdtId::UnionId(it) => it.0.module(db),
509 AdtId::EnumId(it) => it.module(db),
510 };
511
512 module
513 .resolver(db) 507 .resolver(db)
514 .push_generic_params_scope(db, def.into()) 508 .push_generic_params_scope(db, def.into())
515 .push_scope(Scope::AdtScope(def)) 509 .push_scope(Scope::AdtScope(def))
diff --git a/crates/ra_hir_def/src/trace.rs b/crates/ra_hir_def/src/trace.rs
index fc26f5a48..2bcd707bc 100644
--- a/crates/ra_hir_def/src/trace.rs
+++ b/crates/ra_hir_def/src/trace.rs
@@ -12,38 +12,48 @@
12use ra_arena::{map::ArenaMap, Arena, ArenaId, RawId}; 12use ra_arena::{map::ArenaMap, Arena, ArenaId, RawId};
13 13
14pub(crate) struct Trace<ID: ArenaId, T, V> { 14pub(crate) struct Trace<ID: ArenaId, T, V> {
15 for_arena: bool, 15 arena: Option<Arena<ID, T>>,
16 arena: Arena<ID, T>, 16 map: Option<ArenaMap<ID, V>>,
17 map: ArenaMap<ID, V>,
18 len: u32, 17 len: u32,
19} 18}
20 19
21impl<ID: ra_arena::ArenaId, T, V> Trace<ID, T, V> { 20impl<ID: ra_arena::ArenaId + Copy, T, V> Trace<ID, T, V> {
21 pub(crate) fn new() -> Trace<ID, T, V> {
22 Trace { arena: Some(Arena::default()), map: Some(ArenaMap::default()), len: 0 }
23 }
24
22 pub(crate) fn new_for_arena() -> Trace<ID, T, V> { 25 pub(crate) fn new_for_arena() -> Trace<ID, T, V> {
23 Trace { for_arena: true, arena: Arena::default(), map: ArenaMap::default(), len: 0 } 26 Trace { arena: Some(Arena::default()), map: None, len: 0 }
24 } 27 }
25 28
26 pub(crate) fn new_for_map() -> Trace<ID, T, V> { 29 pub(crate) fn new_for_map() -> Trace<ID, T, V> {
27 Trace { for_arena: false, arena: Arena::default(), map: ArenaMap::default(), len: 0 } 30 Trace { arena: None, map: Some(ArenaMap::default()), len: 0 }
28 } 31 }
29 32
30 pub(crate) fn alloc(&mut self, value: impl Fn() -> V, data: impl Fn() -> T) { 33 pub(crate) fn alloc(&mut self, value: impl FnOnce() -> V, data: impl FnOnce() -> T) -> ID {
31 if self.for_arena { 34 let id = if let Some(arena) = &mut self.arena {
32 self.arena.alloc(data()); 35 arena.alloc(data())
33 } else { 36 } else {
34 let id = ID::from_raw(RawId::from(self.len)); 37 let id = ID::from_raw(RawId::from(self.len));
35 self.len += 1; 38 self.len += 1;
36 self.map.insert(id, value()); 39 id
40 };
41
42 if let Some(map) = &mut self.map {
43 map.insert(id, value());
37 } 44 }
45 id
46 }
47
48 pub(crate) fn into_arena(mut self) -> Arena<ID, T> {
49 self.arena.take().unwrap()
38 } 50 }
39 51
40 pub(crate) fn into_arena(self) -> Arena<ID, T> { 52 pub(crate) fn into_map(mut self) -> ArenaMap<ID, V> {
41 assert!(self.for_arena); 53 self.map.take().unwrap()
42 self.arena
43 } 54 }
44 55
45 pub(crate) fn into_map(self) -> ArenaMap<ID, V> { 56 pub(crate) fn into_arena_and_map(mut self) -> (Arena<ID, T>, ArenaMap<ID, V>) {
46 assert!(!self.for_arena); 57 (self.arena.take().unwrap(), self.map.take().unwrap())
47 self.map
48 } 58 }
49} 59}