aboutsummaryrefslogtreecommitdiff
path: root/crates/ra_hir/src
diff options
context:
space:
mode:
authorAleksey Kladov <[email protected]>2020-03-13 15:05:46 +0000
committerAleksey Kladov <[email protected]>2020-03-16 16:42:30 +0000
commit9faea2364dee4fbc9391ad233c570b70256ef002 (patch)
tree160af959553ce57fdfcbc0a6c79bafcc3611aeea /crates/ra_hir/src
parent648df02953a6ebf87a5876668eceba208687e8a7 (diff)
Use `dyn Trait` for working with databse
It improves compile time in `--release` mode quite a bit, it doesn't really slow things down and, conceptually, it seems closer to what we want the physical architecture to look like (we don't want to monomorphise EVERYTHING in a single leaf crate).
Diffstat (limited to 'crates/ra_hir/src')
-rw-r--r--crates/ra_hir/src/code_model.rs329
-rw-r--r--crates/ra_hir/src/has_source.rs64
-rw-r--r--crates/ra_hir/src/semantics.rs22
-rw-r--r--crates/ra_hir/src/semantics/source_to_def.rs23
-rw-r--r--crates/ra_hir/src/source_analyzer.rs89
5 files changed, 265 insertions, 262 deletions
diff --git a/crates/ra_hir/src/code_model.rs b/crates/ra_hir/src/code_model.rs
index ff041150b..45e31095c 100644
--- a/crates/ra_hir/src/code_model.rs
+++ b/crates/ra_hir/src/code_model.rs
@@ -33,11 +33,7 @@ use ra_syntax::{
33}; 33};
34use rustc_hash::FxHashSet; 34use rustc_hash::FxHashSet;
35 35
36use crate::{ 36use crate::{db::HirDatabase, has_source::HasSource, CallableDef, HirDisplay, InFile, Name};
37 db::{DefDatabase, HirDatabase},
38 has_source::HasSource,
39 CallableDef, HirDisplay, InFile, Name,
40};
41 37
42/// hir::Crate describes a single crate. It's the main interface with which 38/// hir::Crate describes a single crate. It's the main interface with which
43/// a crate's dependencies interact. Mostly, it should be just a proxy for the 39/// a crate's dependencies interact. Mostly, it should be just a proxy for the
@@ -54,7 +50,7 @@ pub struct CrateDependency {
54} 50}
55 51
56impl Crate { 52impl Crate {
57 pub fn dependencies(self, db: &impl DefDatabase) -> Vec<CrateDependency> { 53 pub fn dependencies(self, db: &dyn HirDatabase) -> Vec<CrateDependency> {
58 db.crate_graph()[self.id] 54 db.crate_graph()[self.id]
59 .dependencies 55 .dependencies
60 .iter() 56 .iter()
@@ -67,7 +63,7 @@ impl Crate {
67 } 63 }
68 64
69 // FIXME: add `transitive_reverse_dependencies`. 65 // FIXME: add `transitive_reverse_dependencies`.
70 pub fn reverse_dependencies(self, db: &impl DefDatabase) -> Vec<Crate> { 66 pub fn reverse_dependencies(self, db: &dyn HirDatabase) -> Vec<Crate> {
71 let crate_graph = db.crate_graph(); 67 let crate_graph = db.crate_graph();
72 crate_graph 68 crate_graph
73 .iter() 69 .iter()
@@ -78,20 +74,20 @@ impl Crate {
78 .collect() 74 .collect()
79 } 75 }
80 76
81 pub fn root_module(self, db: &impl DefDatabase) -> Option<Module> { 77 pub fn root_module(self, db: &dyn HirDatabase) -> Option<Module> {
82 let module_id = db.crate_def_map(self.id).root; 78 let module_id = db.crate_def_map(self.id).root;
83 Some(Module::new(self, module_id)) 79 Some(Module::new(self, module_id))
84 } 80 }
85 81
86 pub fn root_file(self, db: &impl DefDatabase) -> FileId { 82 pub fn root_file(self, db: &dyn HirDatabase) -> FileId {
87 db.crate_graph()[self.id].root_file_id 83 db.crate_graph()[self.id].root_file_id
88 } 84 }
89 85
90 pub fn edition(self, db: &impl DefDatabase) -> Edition { 86 pub fn edition(self, db: &dyn HirDatabase) -> Edition {
91 db.crate_graph()[self.id].edition 87 db.crate_graph()[self.id].edition
92 } 88 }
93 89
94 pub fn all(db: &impl DefDatabase) -> Vec<Crate> { 90 pub fn all(db: &dyn HirDatabase) -> Vec<Crate> {
95 db.crate_graph().iter().map(|id| Crate { id }).collect() 91 db.crate_graph().iter().map(|id| Crate { id }).collect()
96 } 92 }
97} 93}
@@ -128,7 +124,7 @@ impl_froms!(
128); 124);
129 125
130impl ModuleDef { 126impl ModuleDef {
131 pub fn module(self, db: &impl HirDatabase) -> Option<Module> { 127 pub fn module(self, db: &dyn HirDatabase) -> Option<Module> {
132 match self { 128 match self {
133 ModuleDef::Module(it) => it.parent(db), 129 ModuleDef::Module(it) => it.parent(db),
134 ModuleDef::Function(it) => Some(it.module(db)), 130 ModuleDef::Function(it) => Some(it.module(db)),
@@ -153,7 +149,7 @@ impl Module {
153 } 149 }
154 150
155 /// Name of this module. 151 /// Name of this module.
156 pub fn name(self, db: &impl DefDatabase) -> Option<Name> { 152 pub fn name(self, db: &dyn HirDatabase) -> Option<Name> {
157 let def_map = db.crate_def_map(self.id.krate); 153 let def_map = db.crate_def_map(self.id.krate);
158 let parent = def_map[self.id.local_id].parent?; 154 let parent = def_map[self.id.local_id].parent?;
159 def_map[parent].children.iter().find_map(|(name, module_id)| { 155 def_map[parent].children.iter().find_map(|(name, module_id)| {
@@ -173,13 +169,13 @@ impl Module {
173 /// Topmost parent of this module. Every module has a `crate_root`, but some 169 /// Topmost parent of this module. Every module has a `crate_root`, but some
174 /// might be missing `krate`. This can happen if a module's file is not included 170 /// might be missing `krate`. This can happen if a module's file is not included
175 /// in the module tree of any target in `Cargo.toml`. 171 /// in the module tree of any target in `Cargo.toml`.
176 pub fn crate_root(self, db: &impl DefDatabase) -> Module { 172 pub fn crate_root(self, db: &dyn HirDatabase) -> Module {
177 let def_map = db.crate_def_map(self.id.krate); 173 let def_map = db.crate_def_map(self.id.krate);
178 self.with_module_id(def_map.root) 174 self.with_module_id(def_map.root)
179 } 175 }
180 176
181 /// Iterates over all child modules. 177 /// Iterates over all child modules.
182 pub fn children(self, db: &impl DefDatabase) -> impl Iterator<Item = Module> { 178 pub fn children(self, db: &dyn HirDatabase) -> impl Iterator<Item = Module> {
183 let def_map = db.crate_def_map(self.id.krate); 179 let def_map = db.crate_def_map(self.id.krate);
184 let children = def_map[self.id.local_id] 180 let children = def_map[self.id.local_id]
185 .children 181 .children
@@ -190,13 +186,13 @@ impl Module {
190 } 186 }
191 187
192 /// Finds a parent module. 188 /// Finds a parent module.
193 pub fn parent(self, db: &impl DefDatabase) -> Option<Module> { 189 pub fn parent(self, db: &dyn HirDatabase) -> Option<Module> {
194 let def_map = db.crate_def_map(self.id.krate); 190 let def_map = db.crate_def_map(self.id.krate);
195 let parent_id = def_map[self.id.local_id].parent?; 191 let parent_id = def_map[self.id.local_id].parent?;
196 Some(self.with_module_id(parent_id)) 192 Some(self.with_module_id(parent_id))
197 } 193 }
198 194
199 pub fn path_to_root(self, db: &impl HirDatabase) -> Vec<Module> { 195 pub fn path_to_root(self, db: &dyn HirDatabase) -> Vec<Module> {
200 let mut res = vec![self]; 196 let mut res = vec![self];
201 let mut curr = self; 197 let mut curr = self;
202 while let Some(next) = curr.parent(db) { 198 while let Some(next) = curr.parent(db) {
@@ -209,7 +205,7 @@ impl Module {
209 /// Returns a `ModuleScope`: a set of items, visible in this module. 205 /// Returns a `ModuleScope`: a set of items, visible in this module.
210 pub fn scope( 206 pub fn scope(
211 self, 207 self,
212 db: &impl HirDatabase, 208 db: &dyn HirDatabase,
213 visible_from: Option<Module>, 209 visible_from: Option<Module>,
214 ) -> Vec<(Name, ScopeDef)> { 210 ) -> Vec<(Name, ScopeDef)> {
215 db.crate_def_map(self.id.krate)[self.id.local_id] 211 db.crate_def_map(self.id.krate)[self.id.local_id]
@@ -217,7 +213,8 @@ impl Module {
217 .entries() 213 .entries()
218 .filter_map(|(name, def)| { 214 .filter_map(|(name, def)| {
219 if let Some(m) = visible_from { 215 if let Some(m) = visible_from {
220 let filtered = def.filter_visibility(|vis| vis.is_visible_from(db, m.id)); 216 let filtered =
217 def.filter_visibility(|vis| vis.is_visible_from(db.upcast(), m.id));
221 if filtered.is_none() && !def.is_none() { 218 if filtered.is_none() && !def.is_none() {
222 None 219 None
223 } else { 220 } else {
@@ -233,10 +230,10 @@ impl Module {
233 .collect() 230 .collect()
234 } 231 }
235 232
236 pub fn diagnostics(self, db: &impl HirDatabase, sink: &mut DiagnosticSink) { 233 pub fn diagnostics(self, db: &dyn HirDatabase, sink: &mut DiagnosticSink) {
237 let _p = profile("Module::diagnostics"); 234 let _p = profile("Module::diagnostics");
238 let crate_def_map = db.crate_def_map(self.id.krate); 235 let crate_def_map = db.crate_def_map(self.id.krate);
239 crate_def_map.add_diagnostics(db, self.id.local_id, sink); 236 crate_def_map.add_diagnostics(db.upcast(), self.id.local_id, sink);
240 for decl in self.declarations(db) { 237 for decl in self.declarations(db) {
241 match decl { 238 match decl {
242 crate::ModuleDef::Function(f) => f.diagnostics(db, sink), 239 crate::ModuleDef::Function(f) => f.diagnostics(db, sink),
@@ -259,12 +256,12 @@ impl Module {
259 } 256 }
260 } 257 }
261 258
262 pub fn declarations(self, db: &impl DefDatabase) -> Vec<ModuleDef> { 259 pub fn declarations(self, db: &dyn HirDatabase) -> Vec<ModuleDef> {
263 let def_map = db.crate_def_map(self.id.krate); 260 let def_map = db.crate_def_map(self.id.krate);
264 def_map[self.id.local_id].scope.declarations().map(ModuleDef::from).collect() 261 def_map[self.id.local_id].scope.declarations().map(ModuleDef::from).collect()
265 } 262 }
266 263
267 pub fn impl_defs(self, db: &impl DefDatabase) -> Vec<ImplDef> { 264 pub fn impl_defs(self, db: &dyn HirDatabase) -> Vec<ImplDef> {
268 let def_map = db.crate_def_map(self.id.krate); 265 let def_map = db.crate_def_map(self.id.krate);
269 def_map[self.id.local_id].scope.impls().map(ImplDef::from).collect() 266 def_map[self.id.local_id].scope.impls().map(ImplDef::from).collect()
270 } 267 }
@@ -277,11 +274,11 @@ impl Module {
277 /// this module, if possible. 274 /// this module, if possible.
278 pub fn find_use_path( 275 pub fn find_use_path(
279 self, 276 self,
280 db: &impl DefDatabase, 277 db: &dyn HirDatabase,
281 item: ModuleDef, 278 item: ModuleDef,
282 ) -> Option<hir_def::path::ModPath> { 279 ) -> Option<hir_def::path::ModPath> {
283 // FIXME expose namespace choice 280 // FIXME expose namespace choice
284 hir_def::find_path::find_path(db, determine_item_namespace(item), self.into()) 281 hir_def::find_path::find_path(db.upcast(), determine_item_namespace(item), self.into())
285 } 282 }
286} 283}
287 284
@@ -307,7 +304,7 @@ pub enum FieldSource {
307} 304}
308 305
309impl StructField { 306impl StructField {
310 pub fn name(&self, db: &impl HirDatabase) -> Name { 307 pub fn name(&self, db: &dyn HirDatabase) -> Name {
311 self.parent.variant_data(db).fields()[self.id].name.clone() 308 self.parent.variant_data(db).fields()[self.id].name.clone()
312 } 309 }
313 310
@@ -315,7 +312,7 @@ impl StructField {
315 /// placeholder types for type parameters). This is good for showing 312 /// placeholder types for type parameters). This is good for showing
316 /// signature help, but not so good to actually get the type of the field 313 /// signature help, but not so good to actually get the type of the field
317 /// when you actually have a variable of the struct. 314 /// when you actually have a variable of the struct.
318 pub fn signature_ty(&self, db: &impl HirDatabase) -> Type { 315 pub fn signature_ty(&self, db: &dyn HirDatabase) -> Type {
319 let var_id = self.parent.into(); 316 let var_id = self.parent.into();
320 let generic_def_id: GenericDefId = match self.parent { 317 let generic_def_id: GenericDefId = match self.parent {
321 VariantDef::Struct(it) => it.id.into(), 318 VariantDef::Struct(it) => it.id.into(),
@@ -327,17 +324,17 @@ impl StructField {
327 Type::new(db, self.parent.module(db).id.krate, var_id, ty) 324 Type::new(db, self.parent.module(db).id.krate, var_id, ty)
328 } 325 }
329 326
330 pub fn parent_def(&self, _db: &impl HirDatabase) -> VariantDef { 327 pub fn parent_def(&self, _db: &dyn HirDatabase) -> VariantDef {
331 self.parent 328 self.parent
332 } 329 }
333} 330}
334 331
335impl HasVisibility for StructField { 332impl HasVisibility for StructField {
336 fn visibility(&self, db: &impl HirDatabase) -> Visibility { 333 fn visibility(&self, db: &dyn HirDatabase) -> Visibility {
337 let variant_data = self.parent.variant_data(db); 334 let variant_data = self.parent.variant_data(db);
338 let visibility = &variant_data.fields()[self.id].visibility; 335 let visibility = &variant_data.fields()[self.id].visibility;
339 let parent_id: hir_def::VariantId = self.parent.into(); 336 let parent_id: hir_def::VariantId = self.parent.into();
340 visibility.resolve(db, &parent_id.resolver(db)) 337 visibility.resolve(db.upcast(), &parent_id.resolver(db.upcast()))
341 } 338 }
342} 339}
343 340
@@ -347,19 +344,19 @@ pub struct Struct {
347} 344}
348 345
349impl Struct { 346impl Struct {
350 pub fn module(self, db: &impl DefDatabase) -> Module { 347 pub fn module(self, db: &dyn HirDatabase) -> Module {
351 Module { id: self.id.lookup(db).container.module(db) } 348 Module { id: self.id.lookup(db.upcast()).container.module(db.upcast()) }
352 } 349 }
353 350
354 pub fn krate(self, db: &impl DefDatabase) -> Option<Crate> { 351 pub fn krate(self, db: &dyn HirDatabase) -> Option<Crate> {
355 Some(self.module(db).krate()) 352 Some(self.module(db).krate())
356 } 353 }
357 354
358 pub fn name(self, db: &impl DefDatabase) -> Name { 355 pub fn name(self, db: &dyn HirDatabase) -> Name {
359 db.struct_data(self.id).name.clone() 356 db.struct_data(self.id).name.clone()
360 } 357 }
361 358
362 pub fn fields(self, db: &impl HirDatabase) -> Vec<StructField> { 359 pub fn fields(self, db: &dyn HirDatabase) -> Vec<StructField> {
363 db.struct_data(self.id) 360 db.struct_data(self.id)
364 .variant_data 361 .variant_data
365 .fields() 362 .fields()
@@ -368,11 +365,11 @@ impl Struct {
368 .collect() 365 .collect()
369 } 366 }
370 367
371 pub fn ty(self, db: &impl HirDatabase) -> Type { 368 pub fn ty(self, db: &dyn HirDatabase) -> Type {
372 Type::from_def(db, self.id.lookup(db).container.module(db).krate, self.id) 369 Type::from_def(db, self.id.lookup(db.upcast()).container.module(db.upcast()).krate, self.id)
373 } 370 }
374 371
375 fn variant_data(self, db: &impl DefDatabase) -> Arc<VariantData> { 372 fn variant_data(self, db: &dyn HirDatabase) -> Arc<VariantData> {
376 db.struct_data(self.id).variant_data.clone() 373 db.struct_data(self.id).variant_data.clone()
377 } 374 }
378} 375}
@@ -383,19 +380,19 @@ pub struct Union {
383} 380}
384 381
385impl Union { 382impl Union {
386 pub fn name(self, db: &impl DefDatabase) -> Name { 383 pub fn name(self, db: &dyn HirDatabase) -> Name {
387 db.union_data(self.id).name.clone() 384 db.union_data(self.id).name.clone()
388 } 385 }
389 386
390 pub fn module(self, db: &impl DefDatabase) -> Module { 387 pub fn module(self, db: &dyn HirDatabase) -> Module {
391 Module { id: self.id.lookup(db).container.module(db) } 388 Module { id: self.id.lookup(db.upcast()).container.module(db.upcast()) }
392 } 389 }
393 390
394 pub fn ty(self, db: &impl HirDatabase) -> Type { 391 pub fn ty(self, db: &dyn HirDatabase) -> Type {
395 Type::from_def(db, self.id.lookup(db).container.module(db).krate, self.id) 392 Type::from_def(db, self.id.lookup(db.upcast()).container.module(db.upcast()).krate, self.id)
396 } 393 }
397 394
398 pub fn fields(self, db: &impl HirDatabase) -> Vec<StructField> { 395 pub fn fields(self, db: &dyn HirDatabase) -> Vec<StructField> {
399 db.union_data(self.id) 396 db.union_data(self.id)
400 .variant_data 397 .variant_data
401 .fields() 398 .fields()
@@ -404,7 +401,7 @@ impl Union {
404 .collect() 401 .collect()
405 } 402 }
406 403
407 fn variant_data(self, db: &impl DefDatabase) -> Arc<VariantData> { 404 fn variant_data(self, db: &dyn HirDatabase) -> Arc<VariantData> {
408 db.union_data(self.id).variant_data.clone() 405 db.union_data(self.id).variant_data.clone()
409 } 406 }
410} 407}
@@ -415,19 +412,19 @@ pub struct Enum {
415} 412}
416 413
417impl Enum { 414impl Enum {
418 pub fn module(self, db: &impl DefDatabase) -> Module { 415 pub fn module(self, db: &dyn HirDatabase) -> Module {
419 Module { id: self.id.lookup(db).container.module(db) } 416 Module { id: self.id.lookup(db.upcast()).container.module(db.upcast()) }
420 } 417 }
421 418
422 pub fn krate(self, db: &impl DefDatabase) -> Option<Crate> { 419 pub fn krate(self, db: &dyn HirDatabase) -> Option<Crate> {
423 Some(self.module(db).krate()) 420 Some(self.module(db).krate())
424 } 421 }
425 422
426 pub fn name(self, db: &impl DefDatabase) -> Name { 423 pub fn name(self, db: &dyn HirDatabase) -> Name {
427 db.enum_data(self.id).name.clone() 424 db.enum_data(self.id).name.clone()
428 } 425 }
429 426
430 pub fn variants(self, db: &impl DefDatabase) -> Vec<EnumVariant> { 427 pub fn variants(self, db: &dyn HirDatabase) -> Vec<EnumVariant> {
431 db.enum_data(self.id) 428 db.enum_data(self.id)
432 .variants 429 .variants
433 .iter() 430 .iter()
@@ -435,8 +432,8 @@ impl Enum {
435 .collect() 432 .collect()
436 } 433 }
437 434
438 pub fn ty(self, db: &impl HirDatabase) -> Type { 435 pub fn ty(self, db: &dyn HirDatabase) -> Type {
439 Type::from_def(db, self.id.lookup(db).container.module(db).krate, self.id) 436 Type::from_def(db, self.id.lookup(db.upcast()).container.module(db.upcast()).krate, self.id)
440 } 437 }
441} 438}
442 439
@@ -447,18 +444,18 @@ pub struct EnumVariant {
447} 444}
448 445
449impl EnumVariant { 446impl EnumVariant {
450 pub fn module(self, db: &impl HirDatabase) -> Module { 447 pub fn module(self, db: &dyn HirDatabase) -> Module {
451 self.parent.module(db) 448 self.parent.module(db)
452 } 449 }
453 pub fn parent_enum(self, _db: &impl DefDatabase) -> Enum { 450 pub fn parent_enum(self, _db: &dyn HirDatabase) -> Enum {
454 self.parent 451 self.parent
455 } 452 }
456 453
457 pub fn name(self, db: &impl DefDatabase) -> Name { 454 pub fn name(self, db: &dyn HirDatabase) -> Name {
458 db.enum_data(self.parent.id).variants[self.id].name.clone() 455 db.enum_data(self.parent.id).variants[self.id].name.clone()
459 } 456 }
460 457
461 pub fn fields(self, db: &impl HirDatabase) -> Vec<StructField> { 458 pub fn fields(self, db: &dyn HirDatabase) -> Vec<StructField> {
462 self.variant_data(db) 459 self.variant_data(db)
463 .fields() 460 .fields()
464 .iter() 461 .iter()
@@ -466,11 +463,11 @@ impl EnumVariant {
466 .collect() 463 .collect()
467 } 464 }
468 465
469 pub fn kind(self, db: &impl HirDatabase) -> StructKind { 466 pub fn kind(self, db: &dyn HirDatabase) -> StructKind {
470 self.variant_data(db).kind() 467 self.variant_data(db).kind()
471 } 468 }
472 469
473 pub(crate) fn variant_data(self, db: &impl DefDatabase) -> Arc<VariantData> { 470 pub(crate) fn variant_data(self, db: &dyn HirDatabase) -> Arc<VariantData> {
474 db.enum_data(self.parent.id).variants[self.id].variant_data.clone() 471 db.enum_data(self.parent.id).variants[self.id].variant_data.clone()
475 } 472 }
476} 473}
@@ -485,7 +482,7 @@ pub enum Adt {
485impl_froms!(Adt: Struct, Union, Enum); 482impl_froms!(Adt: Struct, Union, Enum);
486 483
487impl Adt { 484impl Adt {
488 pub fn has_non_default_type_params(self, db: &impl HirDatabase) -> bool { 485 pub fn has_non_default_type_params(self, db: &dyn HirDatabase) -> bool {
489 let subst = db.generic_defaults(self.into()); 486 let subst = db.generic_defaults(self.into());
490 subst.iter().any(|ty| ty == &Ty::Unknown) 487 subst.iter().any(|ty| ty == &Ty::Unknown)
491 } 488 }
@@ -493,12 +490,12 @@ impl Adt {
493 /// Turns this ADT into a type. Any type parameters of the ADT will be 490 /// Turns this ADT into a type. Any type parameters of the ADT will be
494 /// turned into unknown types, which is good for e.g. finding the most 491 /// turned into unknown types, which is good for e.g. finding the most
495 /// general set of completions, but will not look very nice when printed. 492 /// general set of completions, but will not look very nice when printed.
496 pub fn ty(self, db: &impl HirDatabase) -> Type { 493 pub fn ty(self, db: &dyn HirDatabase) -> Type {
497 let id = AdtId::from(self); 494 let id = AdtId::from(self);
498 Type::from_def(db, id.module(db).krate, id) 495 Type::from_def(db, id.module(db.upcast()).krate, id)
499 } 496 }
500 497
501 pub fn module(self, db: &impl DefDatabase) -> Module { 498 pub fn module(self, db: &dyn HirDatabase) -> Module {
502 match self { 499 match self {
503 Adt::Struct(s) => s.module(db), 500 Adt::Struct(s) => s.module(db),
504 Adt::Union(s) => s.module(db), 501 Adt::Union(s) => s.module(db),
@@ -506,11 +503,11 @@ impl Adt {
506 } 503 }
507 } 504 }
508 505
509 pub fn krate(self, db: &impl HirDatabase) -> Option<Crate> { 506 pub fn krate(self, db: &dyn HirDatabase) -> Option<Crate> {
510 Some(self.module(db).krate()) 507 Some(self.module(db).krate())
511 } 508 }
512 509
513 pub fn name(&self, db: &impl HirDatabase) -> Name { 510 pub fn name(&self, db: &dyn HirDatabase) -> Name {
514 match self { 511 match self {
515 Adt::Struct(s) => s.name(db), 512 Adt::Struct(s) => s.name(db),
516 Adt::Union(u) => u.name(db), 513 Adt::Union(u) => u.name(db),
@@ -528,7 +525,7 @@ pub enum VariantDef {
528impl_froms!(VariantDef: Struct, Union, EnumVariant); 525impl_froms!(VariantDef: Struct, Union, EnumVariant);
529 526
530impl VariantDef { 527impl VariantDef {
531 pub fn fields(self, db: &impl HirDatabase) -> Vec<StructField> { 528 pub fn fields(self, db: &dyn HirDatabase) -> Vec<StructField> {
532 match self { 529 match self {
533 VariantDef::Struct(it) => it.fields(db), 530 VariantDef::Struct(it) => it.fields(db),
534 VariantDef::Union(it) => it.fields(db), 531 VariantDef::Union(it) => it.fields(db),
@@ -536,7 +533,7 @@ impl VariantDef {
536 } 533 }
537 } 534 }
538 535
539 pub fn module(self, db: &impl HirDatabase) -> Module { 536 pub fn module(self, db: &dyn HirDatabase) -> Module {
540 match self { 537 match self {
541 VariantDef::Struct(it) => it.module(db), 538 VariantDef::Struct(it) => it.module(db),
542 VariantDef::Union(it) => it.module(db), 539 VariantDef::Union(it) => it.module(db),
@@ -544,7 +541,7 @@ impl VariantDef {
544 } 541 }
545 } 542 }
546 543
547 pub fn name(&self, db: &impl HirDatabase) -> Name { 544 pub fn name(&self, db: &dyn HirDatabase) -> Name {
548 match self { 545 match self {
549 VariantDef::Struct(s) => s.name(db), 546 VariantDef::Struct(s) => s.name(db),
550 VariantDef::Union(u) => u.name(db), 547 VariantDef::Union(u) => u.name(db),
@@ -552,7 +549,7 @@ impl VariantDef {
552 } 549 }
553 } 550 }
554 551
555 pub(crate) fn variant_data(self, db: &impl DefDatabase) -> Arc<VariantData> { 552 pub(crate) fn variant_data(self, db: &dyn HirDatabase) -> Arc<VariantData> {
556 match self { 553 match self {
557 VariantDef::Struct(it) => it.variant_data(db), 554 VariantDef::Struct(it) => it.variant_data(db),
558 VariantDef::Union(it) => it.variant_data(db), 555 VariantDef::Union(it) => it.variant_data(db),
@@ -572,7 +569,7 @@ pub enum DefWithBody {
572impl_froms!(DefWithBody: Function, Const, Static); 569impl_froms!(DefWithBody: Function, Const, Static);
573 570
574impl DefWithBody { 571impl DefWithBody {
575 pub fn module(self, db: &impl HirDatabase) -> Module { 572 pub fn module(self, db: &dyn HirDatabase) -> Module {
576 match self { 573 match self {
577 DefWithBody::Const(c) => c.module(db), 574 DefWithBody::Const(c) => c.module(db),
578 DefWithBody::Function(f) => f.module(db), 575 DefWithBody::Function(f) => f.module(db),
@@ -580,7 +577,7 @@ impl DefWithBody {
580 } 577 }
581 } 578 }
582 579
583 pub fn name(self, db: &impl HirDatabase) -> Option<Name> { 580 pub fn name(self, db: &dyn HirDatabase) -> Option<Name> {
584 match self { 581 match self {
585 DefWithBody::Function(f) => Some(f.name(db)), 582 DefWithBody::Function(f) => Some(f.name(db)),
586 DefWithBody::Static(s) => s.name(db), 583 DefWithBody::Static(s) => s.name(db),
@@ -595,27 +592,27 @@ pub struct Function {
595} 592}
596 593
597impl Function { 594impl Function {
598 pub fn module(self, db: &impl DefDatabase) -> Module { 595 pub fn module(self, db: &dyn HirDatabase) -> Module {
599 self.id.lookup(db).module(db).into() 596 self.id.lookup(db.upcast()).module(db.upcast()).into()
600 } 597 }
601 598
602 pub fn krate(self, db: &impl DefDatabase) -> Option<Crate> { 599 pub fn krate(self, db: &dyn HirDatabase) -> Option<Crate> {
603 Some(self.module(db).krate()) 600 Some(self.module(db).krate())
604 } 601 }
605 602
606 pub fn name(self, db: &impl HirDatabase) -> Name { 603 pub fn name(self, db: &dyn HirDatabase) -> Name {
607 db.function_data(self.id).name.clone() 604 db.function_data(self.id).name.clone()
608 } 605 }
609 606
610 pub fn has_self_param(self, db: &impl HirDatabase) -> bool { 607 pub fn has_self_param(self, db: &dyn HirDatabase) -> bool {
611 db.function_data(self.id).has_self_param 608 db.function_data(self.id).has_self_param
612 } 609 }
613 610
614 pub fn params(self, db: &impl HirDatabase) -> Vec<TypeRef> { 611 pub fn params(self, db: &dyn HirDatabase) -> Vec<TypeRef> {
615 db.function_data(self.id).params.clone() 612 db.function_data(self.id).params.clone()
616 } 613 }
617 614
618 pub fn diagnostics(self, db: &impl HirDatabase, sink: &mut DiagnosticSink) { 615 pub fn diagnostics(self, db: &dyn HirDatabase, sink: &mut DiagnosticSink) {
619 let _p = profile("Function::diagnostics"); 616 let _p = profile("Function::diagnostics");
620 let infer = db.infer(self.id.into()); 617 let infer = db.infer(self.id.into());
621 infer.add_diagnostics(db, self.id, sink); 618 infer.add_diagnostics(db, self.id, sink);
@@ -625,10 +622,10 @@ impl Function {
625} 622}
626 623
627impl HasVisibility for Function { 624impl HasVisibility for Function {
628 fn visibility(&self, db: &impl HirDatabase) -> Visibility { 625 fn visibility(&self, db: &dyn HirDatabase) -> Visibility {
629 let function_data = db.function_data(self.id); 626 let function_data = db.function_data(self.id);
630 let visibility = &function_data.visibility; 627 let visibility = &function_data.visibility;
631 visibility.resolve(db, &self.id.resolver(db)) 628 visibility.resolve(db.upcast(), &self.id.resolver(db.upcast()))
632 } 629 }
633} 630}
634 631
@@ -638,24 +635,24 @@ pub struct Const {
638} 635}
639 636
640impl Const { 637impl Const {
641 pub fn module(self, db: &impl DefDatabase) -> Module { 638 pub fn module(self, db: &dyn HirDatabase) -> Module {
642 Module { id: self.id.lookup(db).module(db) } 639 Module { id: self.id.lookup(db.upcast()).module(db.upcast()) }
643 } 640 }
644 641
645 pub fn krate(self, db: &impl DefDatabase) -> Option<Crate> { 642 pub fn krate(self, db: &dyn HirDatabase) -> Option<Crate> {
646 Some(self.module(db).krate()) 643 Some(self.module(db).krate())
647 } 644 }
648 645
649 pub fn name(self, db: &impl HirDatabase) -> Option<Name> { 646 pub fn name(self, db: &dyn HirDatabase) -> Option<Name> {
650 db.const_data(self.id).name.clone() 647 db.const_data(self.id).name.clone()
651 } 648 }
652} 649}
653 650
654impl HasVisibility for Const { 651impl HasVisibility for Const {
655 fn visibility(&self, db: &impl HirDatabase) -> Visibility { 652 fn visibility(&self, db: &dyn HirDatabase) -> Visibility {
656 let function_data = db.const_data(self.id); 653 let function_data = db.const_data(self.id);
657 let visibility = &function_data.visibility; 654 let visibility = &function_data.visibility;
658 visibility.resolve(db, &self.id.resolver(db)) 655 visibility.resolve(db.upcast(), &self.id.resolver(db.upcast()))
659 } 656 }
660} 657}
661 658
@@ -665,15 +662,15 @@ pub struct Static {
665} 662}
666 663
667impl Static { 664impl Static {
668 pub fn module(self, db: &impl DefDatabase) -> Module { 665 pub fn module(self, db: &dyn HirDatabase) -> Module {
669 Module { id: self.id.lookup(db).module(db) } 666 Module { id: self.id.lookup(db.upcast()).module(db.upcast()) }
670 } 667 }
671 668
672 pub fn krate(self, db: &impl DefDatabase) -> Option<Crate> { 669 pub fn krate(self, db: &dyn HirDatabase) -> Option<Crate> {
673 Some(self.module(db).krate()) 670 Some(self.module(db).krate())
674 } 671 }
675 672
676 pub fn name(self, db: &impl HirDatabase) -> Option<Name> { 673 pub fn name(self, db: &dyn HirDatabase) -> Option<Name> {
677 db.static_data(self.id).name.clone() 674 db.static_data(self.id).name.clone()
678 } 675 }
679} 676}
@@ -684,19 +681,19 @@ pub struct Trait {
684} 681}
685 682
686impl Trait { 683impl Trait {
687 pub fn module(self, db: &impl DefDatabase) -> Module { 684 pub fn module(self, db: &dyn HirDatabase) -> Module {
688 Module { id: self.id.lookup(db).container.module(db) } 685 Module { id: self.id.lookup(db.upcast()).container.module(db.upcast()) }
689 } 686 }
690 687
691 pub fn name(self, db: &impl DefDatabase) -> Name { 688 pub fn name(self, db: &dyn HirDatabase) -> Name {
692 db.trait_data(self.id).name.clone() 689 db.trait_data(self.id).name.clone()
693 } 690 }
694 691
695 pub fn items(self, db: &impl DefDatabase) -> Vec<AssocItem> { 692 pub fn items(self, db: &dyn HirDatabase) -> Vec<AssocItem> {
696 db.trait_data(self.id).items.iter().map(|(_name, it)| (*it).into()).collect() 693 db.trait_data(self.id).items.iter().map(|(_name, it)| (*it).into()).collect()
697 } 694 }
698 695
699 pub fn is_auto(self, db: &impl DefDatabase) -> bool { 696 pub fn is_auto(self, db: &dyn HirDatabase) -> bool {
700 db.trait_data(self.id).auto 697 db.trait_data(self.id).auto
701 } 698 }
702} 699}
@@ -707,37 +704,37 @@ pub struct TypeAlias {
707} 704}
708 705
709impl TypeAlias { 706impl TypeAlias {
710 pub fn has_non_default_type_params(self, db: &impl HirDatabase) -> bool { 707 pub fn has_non_default_type_params(self, db: &dyn HirDatabase) -> bool {
711 let subst = db.generic_defaults(self.id.into()); 708 let subst = db.generic_defaults(self.id.into());
712 subst.iter().any(|ty| ty == &Ty::Unknown) 709 subst.iter().any(|ty| ty == &Ty::Unknown)
713 } 710 }
714 711
715 pub fn module(self, db: &impl DefDatabase) -> Module { 712 pub fn module(self, db: &dyn HirDatabase) -> Module {
716 Module { id: self.id.lookup(db).module(db) } 713 Module { id: self.id.lookup(db.upcast()).module(db.upcast()) }
717 } 714 }
718 715
719 pub fn krate(self, db: &impl DefDatabase) -> Option<Crate> { 716 pub fn krate(self, db: &dyn HirDatabase) -> Option<Crate> {
720 Some(self.module(db).krate()) 717 Some(self.module(db).krate())
721 } 718 }
722 719
723 pub fn type_ref(self, db: &impl DefDatabase) -> Option<TypeRef> { 720 pub fn type_ref(self, db: &dyn HirDatabase) -> Option<TypeRef> {
724 db.type_alias_data(self.id).type_ref.clone() 721 db.type_alias_data(self.id).type_ref.clone()
725 } 722 }
726 723
727 pub fn ty(self, db: &impl HirDatabase) -> Type { 724 pub fn ty(self, db: &dyn HirDatabase) -> Type {
728 Type::from_def(db, self.id.lookup(db).module(db).krate, self.id) 725 Type::from_def(db, self.id.lookup(db.upcast()).module(db.upcast()).krate, self.id)
729 } 726 }
730 727
731 pub fn name(self, db: &impl DefDatabase) -> Name { 728 pub fn name(self, db: &dyn HirDatabase) -> Name {
732 db.type_alias_data(self.id).name.clone() 729 db.type_alias_data(self.id).name.clone()
733 } 730 }
734} 731}
735 732
736impl HasVisibility for TypeAlias { 733impl HasVisibility for TypeAlias {
737 fn visibility(&self, db: &impl HirDatabase) -> Visibility { 734 fn visibility(&self, db: &dyn HirDatabase) -> Visibility {
738 let function_data = db.type_alias_data(self.id); 735 let function_data = db.type_alias_data(self.id);
739 let visibility = &function_data.visibility; 736 let visibility = &function_data.visibility;
740 visibility.resolve(db, &self.id.resolver(db)) 737 visibility.resolve(db.upcast(), &self.id.resolver(db.upcast()))
741 } 738 }
742} 739}
743 740
@@ -750,14 +747,14 @@ impl MacroDef {
750 /// FIXME: right now, this just returns the root module of the crate that 747 /// FIXME: right now, this just returns the root module of the crate that
751 /// defines this macro. The reasons for this is that macros are expanded 748 /// defines this macro. The reasons for this is that macros are expanded
752 /// early, in `ra_hir_expand`, where modules simply do not exist yet. 749 /// early, in `ra_hir_expand`, where modules simply do not exist yet.
753 pub fn module(self, db: &impl HirDatabase) -> Option<Module> { 750 pub fn module(self, db: &dyn HirDatabase) -> Option<Module> {
754 let krate = self.id.krate?; 751 let krate = self.id.krate?;
755 let module_id = db.crate_def_map(krate).root; 752 let module_id = db.crate_def_map(krate).root;
756 Some(Module::new(Crate { id: krate }, module_id)) 753 Some(Module::new(Crate { id: krate }, module_id))
757 } 754 }
758 755
759 /// XXX: this parses the file 756 /// XXX: this parses the file
760 pub fn name(self, db: &impl HirDatabase) -> Option<Name> { 757 pub fn name(self, db: &dyn HirDatabase) -> Option<Name> {
761 self.source(db).value.name().map(|it| it.as_name()) 758 self.source(db).value.name().map(|it| it.as_name())
762 } 759 }
763} 760}
@@ -775,50 +772,50 @@ pub enum AssocItemContainer {
775 ImplDef(ImplDef), 772 ImplDef(ImplDef),
776} 773}
777pub trait AsAssocItem { 774pub trait AsAssocItem {
778 fn as_assoc_item(self, db: &impl DefDatabase) -> Option<AssocItem>; 775 fn as_assoc_item(self, db: &dyn HirDatabase) -> Option<AssocItem>;
779} 776}
780 777
781impl AsAssocItem for Function { 778impl AsAssocItem for Function {
782 fn as_assoc_item(self, db: &impl DefDatabase) -> Option<AssocItem> { 779 fn as_assoc_item(self, db: &dyn HirDatabase) -> Option<AssocItem> {
783 as_assoc_item(db, AssocItem::Function, self.id) 780 as_assoc_item(db, AssocItem::Function, self.id)
784 } 781 }
785} 782}
786impl AsAssocItem for Const { 783impl AsAssocItem for Const {
787 fn as_assoc_item(self, db: &impl DefDatabase) -> Option<AssocItem> { 784 fn as_assoc_item(self, db: &dyn HirDatabase) -> Option<AssocItem> {
788 as_assoc_item(db, AssocItem::Const, self.id) 785 as_assoc_item(db, AssocItem::Const, self.id)
789 } 786 }
790} 787}
791impl AsAssocItem for TypeAlias { 788impl AsAssocItem for TypeAlias {
792 fn as_assoc_item(self, db: &impl DefDatabase) -> Option<AssocItem> { 789 fn as_assoc_item(self, db: &dyn HirDatabase) -> Option<AssocItem> {
793 as_assoc_item(db, AssocItem::TypeAlias, self.id) 790 as_assoc_item(db, AssocItem::TypeAlias, self.id)
794 } 791 }
795} 792}
796fn as_assoc_item<ID, DEF, CTOR, AST>(db: &impl DefDatabase, ctor: CTOR, id: ID) -> Option<AssocItem> 793fn as_assoc_item<ID, DEF, CTOR, AST>(db: &dyn HirDatabase, ctor: CTOR, id: ID) -> Option<AssocItem>
797where 794where
798 ID: Lookup<Data = AssocItemLoc<AST>>, 795 ID: Lookup<Data = AssocItemLoc<AST>>,
799 DEF: From<ID>, 796 DEF: From<ID>,
800 CTOR: FnOnce(DEF) -> AssocItem, 797 CTOR: FnOnce(DEF) -> AssocItem,
801 AST: AstNode, 798 AST: AstNode,
802{ 799{
803 match id.lookup(db).container { 800 match id.lookup(db.upcast()).container {
804 AssocContainerId::TraitId(_) | AssocContainerId::ImplId(_) => Some(ctor(DEF::from(id))), 801 AssocContainerId::TraitId(_) | AssocContainerId::ImplId(_) => Some(ctor(DEF::from(id))),
805 AssocContainerId::ContainerId(_) => None, 802 AssocContainerId::ContainerId(_) => None,
806 } 803 }
807} 804}
808 805
809impl AssocItem { 806impl AssocItem {
810 pub fn module(self, db: &impl DefDatabase) -> Module { 807 pub fn module(self, db: &dyn HirDatabase) -> Module {
811 match self { 808 match self {
812 AssocItem::Function(f) => f.module(db), 809 AssocItem::Function(f) => f.module(db),
813 AssocItem::Const(c) => c.module(db), 810 AssocItem::Const(c) => c.module(db),
814 AssocItem::TypeAlias(t) => t.module(db), 811 AssocItem::TypeAlias(t) => t.module(db),
815 } 812 }
816 } 813 }
817 pub fn container(self, db: &impl DefDatabase) -> AssocItemContainer { 814 pub fn container(self, db: &dyn HirDatabase) -> AssocItemContainer {
818 let container = match self { 815 let container = match self {
819 AssocItem::Function(it) => it.id.lookup(db).container, 816 AssocItem::Function(it) => it.id.lookup(db.upcast()).container,
820 AssocItem::Const(it) => it.id.lookup(db).container, 817 AssocItem::Const(it) => it.id.lookup(db.upcast()).container,
821 AssocItem::TypeAlias(it) => it.id.lookup(db).container, 818 AssocItem::TypeAlias(it) => it.id.lookup(db.upcast()).container,
822 }; 819 };
823 match container { 820 match container {
824 AssocContainerId::TraitId(id) => AssocItemContainer::Trait(id.into()), 821 AssocContainerId::TraitId(id) => AssocItemContainer::Trait(id.into()),
@@ -829,7 +826,7 @@ impl AssocItem {
829} 826}
830 827
831impl HasVisibility for AssocItem { 828impl HasVisibility for AssocItem {
832 fn visibility(&self, db: &impl HirDatabase) -> Visibility { 829 fn visibility(&self, db: &dyn HirDatabase) -> Visibility {
833 match self { 830 match self {
834 AssocItem::Function(f) => f.visibility(db), 831 AssocItem::Function(f) => f.visibility(db),
835 AssocItem::Const(c) => c.visibility(db), 832 AssocItem::Const(c) => c.visibility(db),
@@ -862,7 +859,7 @@ impl_froms!(
862); 859);
863 860
864impl GenericDef { 861impl GenericDef {
865 pub fn params(self, db: &impl HirDatabase) -> Vec<TypeParam> { 862 pub fn params(self, db: &dyn HirDatabase) -> Vec<TypeParam> {
866 let generics: Arc<hir_def::generics::GenericParams> = db.generic_params(self.into()); 863 let generics: Arc<hir_def::generics::GenericParams> = db.generic_params(self.into());
867 generics 864 generics
868 .types 865 .types
@@ -880,7 +877,7 @@ pub struct Local {
880 877
881impl Local { 878impl Local {
882 // FIXME: why is this an option? It shouldn't be? 879 // FIXME: why is this an option? It shouldn't be?
883 pub fn name(self, db: &impl HirDatabase) -> Option<Name> { 880 pub fn name(self, db: &dyn HirDatabase) -> Option<Name> {
884 let body = db.body(self.parent.into()); 881 let body = db.body(self.parent.into());
885 match &body[self.pat_id] { 882 match &body[self.pat_id] {
886 Pat::Bind { name, .. } => Some(name.clone()), 883 Pat::Bind { name, .. } => Some(name.clone()),
@@ -888,11 +885,11 @@ impl Local {
888 } 885 }
889 } 886 }
890 887
891 pub fn is_self(self, db: &impl HirDatabase) -> bool { 888 pub fn is_self(self, db: &dyn HirDatabase) -> bool {
892 self.name(db) == Some(name![self]) 889 self.name(db) == Some(name![self])
893 } 890 }
894 891
895 pub fn is_mut(self, db: &impl HirDatabase) -> bool { 892 pub fn is_mut(self, db: &dyn HirDatabase) -> bool {
896 let body = db.body(self.parent.into()); 893 let body = db.body(self.parent.into());
897 match &body[self.pat_id] { 894 match &body[self.pat_id] {
898 Pat::Bind { mode, .. } => match mode { 895 Pat::Bind { mode, .. } => match mode {
@@ -903,28 +900,28 @@ impl Local {
903 } 900 }
904 } 901 }
905 902
906 pub fn parent(self, _db: &impl HirDatabase) -> DefWithBody { 903 pub fn parent(self, _db: &dyn HirDatabase) -> DefWithBody {
907 self.parent.into() 904 self.parent.into()
908 } 905 }
909 906
910 pub fn module(self, db: &impl HirDatabase) -> Module { 907 pub fn module(self, db: &dyn HirDatabase) -> Module {
911 self.parent(db).module(db) 908 self.parent(db).module(db)
912 } 909 }
913 910
914 pub fn ty(self, db: &impl HirDatabase) -> Type { 911 pub fn ty(self, db: &dyn HirDatabase) -> Type {
915 let def = DefWithBodyId::from(self.parent); 912 let def = DefWithBodyId::from(self.parent);
916 let infer = db.infer(def); 913 let infer = db.infer(def);
917 let ty = infer[self.pat_id].clone(); 914 let ty = infer[self.pat_id].clone();
918 let resolver = def.resolver(db); 915 let resolver = def.resolver(db.upcast());
919 let krate = def.module(db).krate; 916 let krate = def.module(db.upcast()).krate;
920 let environment = TraitEnvironment::lower(db, &resolver); 917 let environment = TraitEnvironment::lower(db, &resolver);
921 Type { krate, ty: InEnvironment { value: ty, environment } } 918 Type { krate, ty: InEnvironment { value: ty, environment } }
922 } 919 }
923 920
924 pub fn source(self, db: &impl HirDatabase) -> InFile<Either<ast::BindPat, ast::SelfParam>> { 921 pub fn source(self, db: &dyn HirDatabase) -> InFile<Either<ast::BindPat, ast::SelfParam>> {
925 let (_body, source_map) = db.body_with_source_map(self.parent.into()); 922 let (_body, source_map) = db.body_with_source_map(self.parent.into());
926 let src = source_map.pat_syntax(self.pat_id).unwrap(); // Hmm... 923 let src = source_map.pat_syntax(self.pat_id).unwrap(); // Hmm...
927 let root = src.file_syntax(db); 924 let root = src.file_syntax(db.upcast());
928 src.map(|ast| { 925 src.map(|ast| {
929 ast.map_left(|it| it.cast().unwrap().to_node(&root)).map_right(|it| it.to_node(&root)) 926 ast.map_left(|it| it.cast().unwrap().to_node(&root)).map_right(|it| it.to_node(&root))
930 }) 927 })
@@ -937,13 +934,13 @@ pub struct TypeParam {
937} 934}
938 935
939impl TypeParam { 936impl TypeParam {
940 pub fn name(self, db: &impl HirDatabase) -> Name { 937 pub fn name(self, db: &dyn HirDatabase) -> Name {
941 let params = db.generic_params(self.id.parent); 938 let params = db.generic_params(self.id.parent);
942 params.types[self.id.local_id].name.clone().unwrap_or_else(Name::missing) 939 params.types[self.id.local_id].name.clone().unwrap_or_else(Name::missing)
943 } 940 }
944 941
945 pub fn module(self, db: &impl HirDatabase) -> Module { 942 pub fn module(self, db: &dyn HirDatabase) -> Module {
946 self.id.parent.module(db).into() 943 self.id.parent.module(db.upcast()).into()
947 } 944 }
948} 945}
949 946
@@ -954,55 +951,55 @@ pub struct ImplDef {
954} 951}
955 952
956impl ImplDef { 953impl ImplDef {
957 pub fn all_in_crate(db: &impl HirDatabase, krate: Crate) -> Vec<ImplDef> { 954 pub fn all_in_crate(db: &dyn HirDatabase, krate: Crate) -> Vec<ImplDef> {
958 let impls = db.impls_in_crate(krate.id); 955 let impls = db.impls_in_crate(krate.id);
959 impls.all_impls().map(Self::from).collect() 956 impls.all_impls().map(Self::from).collect()
960 } 957 }
961 pub fn for_trait(db: &impl HirDatabase, krate: Crate, trait_: Trait) -> Vec<ImplDef> { 958 pub fn for_trait(db: &dyn HirDatabase, krate: Crate, trait_: Trait) -> Vec<ImplDef> {
962 let impls = db.impls_in_crate(krate.id); 959 let impls = db.impls_in_crate(krate.id);
963 impls.lookup_impl_defs_for_trait(trait_.id).map(Self::from).collect() 960 impls.lookup_impl_defs_for_trait(trait_.id).map(Self::from).collect()
964 } 961 }
965 962
966 pub fn target_trait(&self, db: &impl DefDatabase) -> Option<TypeRef> { 963 pub fn target_trait(&self, db: &dyn HirDatabase) -> Option<TypeRef> {
967 db.impl_data(self.id).target_trait.clone() 964 db.impl_data(self.id).target_trait.clone()
968 } 965 }
969 966
970 pub fn target_type(&self, db: &impl DefDatabase) -> TypeRef { 967 pub fn target_type(&self, db: &dyn HirDatabase) -> TypeRef {
971 db.impl_data(self.id).target_type.clone() 968 db.impl_data(self.id).target_type.clone()
972 } 969 }
973 970
974 pub fn target_ty(&self, db: &impl HirDatabase) -> Type { 971 pub fn target_ty(&self, db: &dyn HirDatabase) -> Type {
975 let impl_data = db.impl_data(self.id); 972 let impl_data = db.impl_data(self.id);
976 let resolver = self.id.resolver(db); 973 let resolver = self.id.resolver(db.upcast());
977 let ctx = hir_ty::TyLoweringContext::new(db, &resolver); 974 let ctx = hir_ty::TyLoweringContext::new(db, &resolver);
978 let environment = TraitEnvironment::lower(db, &resolver); 975 let environment = TraitEnvironment::lower(db, &resolver);
979 let ty = Ty::from_hir(&ctx, &impl_data.target_type); 976 let ty = Ty::from_hir(&ctx, &impl_data.target_type);
980 Type { 977 Type {
981 krate: self.id.lookup(db).container.module(db).krate, 978 krate: self.id.lookup(db.upcast()).container.module(db.upcast()).krate,
982 ty: InEnvironment { value: ty, environment }, 979 ty: InEnvironment { value: ty, environment },
983 } 980 }
984 } 981 }
985 982
986 pub fn items(&self, db: &impl DefDatabase) -> Vec<AssocItem> { 983 pub fn items(&self, db: &dyn HirDatabase) -> Vec<AssocItem> {
987 db.impl_data(self.id).items.iter().map(|it| (*it).into()).collect() 984 db.impl_data(self.id).items.iter().map(|it| (*it).into()).collect()
988 } 985 }
989 986
990 pub fn is_negative(&self, db: &impl DefDatabase) -> bool { 987 pub fn is_negative(&self, db: &dyn HirDatabase) -> bool {
991 db.impl_data(self.id).is_negative 988 db.impl_data(self.id).is_negative
992 } 989 }
993 990
994 pub fn module(&self, db: &impl DefDatabase) -> Module { 991 pub fn module(&self, db: &dyn HirDatabase) -> Module {
995 self.id.lookup(db).container.module(db).into() 992 self.id.lookup(db.upcast()).container.module(db.upcast()).into()
996 } 993 }
997 994
998 pub fn krate(&self, db: &impl DefDatabase) -> Crate { 995 pub fn krate(&self, db: &dyn HirDatabase) -> Crate {
999 Crate { id: self.module(db).id.krate } 996 Crate { id: self.module(db).id.krate }
1000 } 997 }
1001 998
1002 pub fn is_builtin_derive(&self, db: &impl DefDatabase) -> Option<InFile<ast::Attr>> { 999 pub fn is_builtin_derive(&self, db: &dyn HirDatabase) -> Option<InFile<ast::Attr>> {
1003 let src = self.source(db); 1000 let src = self.source(db);
1004 let item = src.file_id.is_builtin_derive(db)?; 1001 let item = src.file_id.is_builtin_derive(db.upcast())?;
1005 let hygenic = hir_expand::hygiene::Hygiene::new(db, item.file_id); 1002 let hygenic = hir_expand::hygiene::Hygiene::new(db.upcast(), item.file_id);
1006 1003
1007 let attr = item 1004 let attr = item
1008 .value 1005 .value
@@ -1028,14 +1025,14 @@ pub struct Type {
1028} 1025}
1029 1026
1030impl Type { 1027impl Type {
1031 fn new(db: &impl HirDatabase, krate: CrateId, lexical_env: impl HasResolver, ty: Ty) -> Type { 1028 fn new(db: &dyn HirDatabase, krate: CrateId, lexical_env: impl HasResolver, ty: Ty) -> Type {
1032 let resolver = lexical_env.resolver(db); 1029 let resolver = lexical_env.resolver(db.upcast());
1033 let environment = TraitEnvironment::lower(db, &resolver); 1030 let environment = TraitEnvironment::lower(db, &resolver);
1034 Type { krate, ty: InEnvironment { value: ty, environment } } 1031 Type { krate, ty: InEnvironment { value: ty, environment } }
1035 } 1032 }
1036 1033
1037 fn from_def( 1034 fn from_def(
1038 db: &impl HirDatabase, 1035 db: &dyn HirDatabase,
1039 krate: CrateId, 1036 krate: CrateId,
1040 def: impl HasResolver + Into<TyDefId> + Into<GenericDefId>, 1037 def: impl HasResolver + Into<TyDefId> + Into<GenericDefId>,
1041 ) -> Type { 1038 ) -> Type {
@@ -1073,7 +1070,7 @@ impl Type {
1073 1070
1074 /// Checks that particular type `ty` implements `std::future::Future`. 1071 /// Checks that particular type `ty` implements `std::future::Future`.
1075 /// This function is used in `.await` syntax completion. 1072 /// This function is used in `.await` syntax completion.
1076 pub fn impls_future(&self, db: &impl HirDatabase) -> bool { 1073 pub fn impls_future(&self, db: &dyn HirDatabase) -> bool {
1077 let krate = self.krate; 1074 let krate = self.krate;
1078 1075
1079 let std_future_trait = 1076 let std_future_trait =
@@ -1110,7 +1107,7 @@ impl Type {
1110 } 1107 }
1111 } 1108 }
1112 1109
1113 pub fn fields(&self, db: &impl HirDatabase) -> Vec<(StructField, Type)> { 1110 pub fn fields(&self, db: &dyn HirDatabase) -> Vec<(StructField, Type)> {
1114 if let Ty::Apply(a_ty) = &self.ty.value { 1111 if let Ty::Apply(a_ty) = &self.ty.value {
1115 if let TypeCtor::Adt(AdtId::StructId(s)) = a_ty.ctor { 1112 if let TypeCtor::Adt(AdtId::StructId(s)) = a_ty.ctor {
1116 let var_def = s.into(); 1113 let var_def = s.into();
@@ -1128,7 +1125,7 @@ impl Type {
1128 Vec::new() 1125 Vec::new()
1129 } 1126 }
1130 1127
1131 pub fn tuple_fields(&self, _db: &impl HirDatabase) -> Vec<Type> { 1128 pub fn tuple_fields(&self, _db: &dyn HirDatabase) -> Vec<Type> {
1132 let mut res = Vec::new(); 1129 let mut res = Vec::new();
1133 if let Ty::Apply(a_ty) = &self.ty.value { 1130 if let Ty::Apply(a_ty) = &self.ty.value {
1134 if let TypeCtor::Tuple { .. } = a_ty.ctor { 1131 if let TypeCtor::Tuple { .. } = a_ty.ctor {
@@ -1143,7 +1140,7 @@ impl Type {
1143 1140
1144 pub fn variant_fields( 1141 pub fn variant_fields(
1145 &self, 1142 &self,
1146 db: &impl HirDatabase, 1143 db: &dyn HirDatabase,
1147 def: VariantDef, 1144 def: VariantDef,
1148 ) -> Vec<(StructField, Type)> { 1145 ) -> Vec<(StructField, Type)> {
1149 // FIXME: check that ty and def match 1146 // FIXME: check that ty and def match
@@ -1162,7 +1159,7 @@ impl Type {
1162 } 1159 }
1163 } 1160 }
1164 1161
1165 pub fn autoderef<'a>(&'a self, db: &'a impl HirDatabase) -> impl Iterator<Item = Type> + 'a { 1162 pub fn autoderef<'a>(&'a self, db: &'a dyn HirDatabase) -> impl Iterator<Item = Type> + 'a {
1166 // There should be no inference vars in types passed here 1163 // There should be no inference vars in types passed here
1167 // FIXME check that? 1164 // FIXME check that?
1168 let canonical = Canonical { value: self.ty.value.clone(), num_vars: 0 }; 1165 let canonical = Canonical { value: self.ty.value.clone(), num_vars: 0 };
@@ -1177,7 +1174,7 @@ impl Type {
1177 // lifetime problems, because we need to borrow temp `CrateImplDefs`. 1174 // lifetime problems, because we need to borrow temp `CrateImplDefs`.
1178 pub fn iterate_impl_items<T>( 1175 pub fn iterate_impl_items<T>(
1179 self, 1176 self,
1180 db: &impl HirDatabase, 1177 db: &dyn HirDatabase,
1181 krate: Crate, 1178 krate: Crate,
1182 mut callback: impl FnMut(AssocItem) -> Option<T>, 1179 mut callback: impl FnMut(AssocItem) -> Option<T>,
1183 ) -> Option<T> { 1180 ) -> Option<T> {
@@ -1197,7 +1194,7 @@ impl Type {
1197 1194
1198 pub fn iterate_method_candidates<T>( 1195 pub fn iterate_method_candidates<T>(
1199 &self, 1196 &self,
1200 db: &impl HirDatabase, 1197 db: &dyn HirDatabase,
1201 krate: Crate, 1198 krate: Crate,
1202 traits_in_scope: &FxHashSet<TraitId>, 1199 traits_in_scope: &FxHashSet<TraitId>,
1203 name: Option<&Name>, 1200 name: Option<&Name>,
@@ -1228,7 +1225,7 @@ impl Type {
1228 1225
1229 pub fn iterate_path_candidates<T>( 1226 pub fn iterate_path_candidates<T>(
1230 &self, 1227 &self,
1231 db: &impl HirDatabase, 1228 db: &dyn HirDatabase,
1232 krate: Crate, 1229 krate: Crate,
1233 traits_in_scope: &FxHashSet<TraitId>, 1230 traits_in_scope: &FxHashSet<TraitId>,
1234 name: Option<&Name>, 1231 name: Option<&Name>,
@@ -1283,7 +1280,7 @@ impl Type {
1283} 1280}
1284 1281
1285impl HirDisplay for Type { 1282impl HirDisplay for Type {
1286 fn hir_fmt(&self, f: &mut HirFormatter<impl HirDatabase>) -> std::fmt::Result { 1283 fn hir_fmt(&self, f: &mut HirFormatter) -> std::fmt::Result {
1287 self.ty.value.hir_fmt(f) 1284 self.ty.value.hir_fmt(f)
1288 } 1285 }
1289} 1286}
@@ -1360,30 +1357,30 @@ impl_froms!(
1360); 1357);
1361 1358
1362pub trait HasAttrs { 1359pub trait HasAttrs {
1363 fn attrs(self, db: &impl DefDatabase) -> Attrs; 1360 fn attrs(self, db: &dyn HirDatabase) -> Attrs;
1364} 1361}
1365 1362
1366impl<T: Into<AttrDef>> HasAttrs for T { 1363impl<T: Into<AttrDef>> HasAttrs for T {
1367 fn attrs(self, db: &impl DefDatabase) -> Attrs { 1364 fn attrs(self, db: &dyn HirDatabase) -> Attrs {
1368 let def: AttrDef = self.into(); 1365 let def: AttrDef = self.into();
1369 db.attrs(def.into()) 1366 db.attrs(def.into())
1370 } 1367 }
1371} 1368}
1372 1369
1373pub trait Docs { 1370pub trait Docs {
1374 fn docs(&self, db: &impl HirDatabase) -> Option<Documentation>; 1371 fn docs(&self, db: &dyn HirDatabase) -> Option<Documentation>;
1375} 1372}
1376impl<T: Into<AttrDef> + Copy> Docs for T { 1373impl<T: Into<AttrDef> + Copy> Docs for T {
1377 fn docs(&self, db: &impl HirDatabase) -> Option<Documentation> { 1374 fn docs(&self, db: &dyn HirDatabase) -> Option<Documentation> {
1378 let def: AttrDef = (*self).into(); 1375 let def: AttrDef = (*self).into();
1379 db.documentation(def.into()) 1376 db.documentation(def.into())
1380 } 1377 }
1381} 1378}
1382 1379
1383pub trait HasVisibility { 1380pub trait HasVisibility {
1384 fn visibility(&self, db: &impl HirDatabase) -> Visibility; 1381 fn visibility(&self, db: &dyn HirDatabase) -> Visibility;
1385 fn is_visible_from(&self, db: &impl HirDatabase, module: Module) -> bool { 1382 fn is_visible_from(&self, db: &dyn HirDatabase, module: Module) -> bool {
1386 let vis = self.visibility(db); 1383 let vis = self.visibility(db);
1387 vis.is_visible_from(db, module.id) 1384 vis.is_visible_from(db.upcast(), module.id)
1388 } 1385 }
1389} 1386}
diff --git a/crates/ra_hir/src/has_source.rs b/crates/ra_hir/src/has_source.rs
index f121e1eff..129764e0a 100644
--- a/crates/ra_hir/src/has_source.rs
+++ b/crates/ra_hir/src/has_source.rs
@@ -9,7 +9,7 @@ use hir_def::{
9use ra_syntax::ast; 9use ra_syntax::ast;
10 10
11use crate::{ 11use crate::{
12 db::DefDatabase, Const, Enum, EnumVariant, FieldSource, Function, ImplDef, MacroDef, Module, 12 db::HirDatabase, Const, Enum, EnumVariant, FieldSource, Function, ImplDef, MacroDef, Module,
13 Static, Struct, StructField, Trait, TypeAlias, TypeParam, Union, 13 Static, Struct, StructField, Trait, TypeAlias, TypeParam, Union,
14}; 14};
15 15
@@ -17,31 +17,31 @@ pub use hir_expand::InFile;
17 17
18pub trait HasSource { 18pub trait HasSource {
19 type Ast; 19 type Ast;
20 fn source(self, db: &impl DefDatabase) -> InFile<Self::Ast>; 20 fn source(self, db: &dyn HirDatabase) -> InFile<Self::Ast>;
21} 21}
22 22
23/// NB: Module is !HasSource, because it has two source nodes at the same time: 23/// NB: Module is !HasSource, because it has two source nodes at the same time:
24/// definition and declaration. 24/// definition and declaration.
25impl Module { 25impl Module {
26 /// Returns a node which defines this module. That is, a file or a `mod foo {}` with items. 26 /// Returns a node which defines this module. That is, a file or a `mod foo {}` with items.
27 pub fn definition_source(self, db: &impl DefDatabase) -> InFile<ModuleSource> { 27 pub fn definition_source(self, db: &dyn HirDatabase) -> InFile<ModuleSource> {
28 let def_map = db.crate_def_map(self.id.krate); 28 let def_map = db.crate_def_map(self.id.krate);
29 def_map[self.id.local_id].definition_source(db) 29 def_map[self.id.local_id].definition_source(db.upcast())
30 } 30 }
31 31
32 /// Returns a node which declares this module, either a `mod foo;` or a `mod foo {}`. 32 /// Returns a node which declares this module, either a `mod foo;` or a `mod foo {}`.
33 /// `None` for the crate root. 33 /// `None` for the crate root.
34 pub fn declaration_source(self, db: &impl DefDatabase) -> Option<InFile<ast::Module>> { 34 pub fn declaration_source(self, db: &dyn HirDatabase) -> Option<InFile<ast::Module>> {
35 let def_map = db.crate_def_map(self.id.krate); 35 let def_map = db.crate_def_map(self.id.krate);
36 def_map[self.id.local_id].declaration_source(db) 36 def_map[self.id.local_id].declaration_source(db.upcast())
37 } 37 }
38} 38}
39 39
40impl HasSource for StructField { 40impl HasSource for StructField {
41 type Ast = FieldSource; 41 type Ast = FieldSource;
42 fn source(self, db: &impl DefDatabase) -> InFile<FieldSource> { 42 fn source(self, db: &dyn HirDatabase) -> InFile<FieldSource> {
43 let var = VariantId::from(self.parent); 43 let var = VariantId::from(self.parent);
44 let src = var.child_source(db); 44 let src = var.child_source(db.upcast());
45 src.map(|it| match it[self.id].clone() { 45 src.map(|it| match it[self.id].clone() {
46 Either::Left(it) => FieldSource::Pos(it), 46 Either::Left(it) => FieldSource::Pos(it),
47 Either::Right(it) => FieldSource::Named(it), 47 Either::Right(it) => FieldSource::Named(it),
@@ -50,78 +50,78 @@ impl HasSource for StructField {
50} 50}
51impl HasSource for Struct { 51impl HasSource for Struct {
52 type Ast = ast::StructDef; 52 type Ast = ast::StructDef;
53 fn source(self, db: &impl DefDatabase) -> InFile<ast::StructDef> { 53 fn source(self, db: &dyn HirDatabase) -> InFile<ast::StructDef> {
54 self.id.lookup(db).source(db) 54 self.id.lookup(db.upcast()).source(db.upcast())
55 } 55 }
56} 56}
57impl HasSource for Union { 57impl HasSource for Union {
58 type Ast = ast::UnionDef; 58 type Ast = ast::UnionDef;
59 fn source(self, db: &impl DefDatabase) -> InFile<ast::UnionDef> { 59 fn source(self, db: &dyn HirDatabase) -> InFile<ast::UnionDef> {
60 self.id.lookup(db).source(db) 60 self.id.lookup(db.upcast()).source(db.upcast())
61 } 61 }
62} 62}
63impl HasSource for Enum { 63impl HasSource for Enum {
64 type Ast = ast::EnumDef; 64 type Ast = ast::EnumDef;
65 fn source(self, db: &impl DefDatabase) -> InFile<ast::EnumDef> { 65 fn source(self, db: &dyn HirDatabase) -> InFile<ast::EnumDef> {
66 self.id.lookup(db).source(db) 66 self.id.lookup(db.upcast()).source(db.upcast())
67 } 67 }
68} 68}
69impl HasSource for EnumVariant { 69impl HasSource for EnumVariant {
70 type Ast = ast::EnumVariant; 70 type Ast = ast::EnumVariant;
71 fn source(self, db: &impl DefDatabase) -> InFile<ast::EnumVariant> { 71 fn source(self, db: &dyn HirDatabase) -> InFile<ast::EnumVariant> {
72 self.parent.id.child_source(db).map(|map| map[self.id].clone()) 72 self.parent.id.child_source(db.upcast()).map(|map| map[self.id].clone())
73 } 73 }
74} 74}
75impl HasSource for Function { 75impl HasSource for Function {
76 type Ast = ast::FnDef; 76 type Ast = ast::FnDef;
77 fn source(self, db: &impl DefDatabase) -> InFile<ast::FnDef> { 77 fn source(self, db: &dyn HirDatabase) -> InFile<ast::FnDef> {
78 self.id.lookup(db).source(db) 78 self.id.lookup(db.upcast()).source(db.upcast())
79 } 79 }
80} 80}
81impl HasSource for Const { 81impl HasSource for Const {
82 type Ast = ast::ConstDef; 82 type Ast = ast::ConstDef;
83 fn source(self, db: &impl DefDatabase) -> InFile<ast::ConstDef> { 83 fn source(self, db: &dyn HirDatabase) -> InFile<ast::ConstDef> {
84 self.id.lookup(db).source(db) 84 self.id.lookup(db.upcast()).source(db.upcast())
85 } 85 }
86} 86}
87impl HasSource for Static { 87impl HasSource for Static {
88 type Ast = ast::StaticDef; 88 type Ast = ast::StaticDef;
89 fn source(self, db: &impl DefDatabase) -> InFile<ast::StaticDef> { 89 fn source(self, db: &dyn HirDatabase) -> InFile<ast::StaticDef> {
90 self.id.lookup(db).source(db) 90 self.id.lookup(db.upcast()).source(db.upcast())
91 } 91 }
92} 92}
93impl HasSource for Trait { 93impl HasSource for Trait {
94 type Ast = ast::TraitDef; 94 type Ast = ast::TraitDef;
95 fn source(self, db: &impl DefDatabase) -> InFile<ast::TraitDef> { 95 fn source(self, db: &dyn HirDatabase) -> InFile<ast::TraitDef> {
96 self.id.lookup(db).source(db) 96 self.id.lookup(db.upcast()).source(db.upcast())
97 } 97 }
98} 98}
99impl HasSource for TypeAlias { 99impl HasSource for TypeAlias {
100 type Ast = ast::TypeAliasDef; 100 type Ast = ast::TypeAliasDef;
101 fn source(self, db: &impl DefDatabase) -> InFile<ast::TypeAliasDef> { 101 fn source(self, db: &dyn HirDatabase) -> InFile<ast::TypeAliasDef> {
102 self.id.lookup(db).source(db) 102 self.id.lookup(db.upcast()).source(db.upcast())
103 } 103 }
104} 104}
105impl HasSource for MacroDef { 105impl HasSource for MacroDef {
106 type Ast = ast::MacroCall; 106 type Ast = ast::MacroCall;
107 fn source(self, db: &impl DefDatabase) -> InFile<ast::MacroCall> { 107 fn source(self, db: &dyn HirDatabase) -> InFile<ast::MacroCall> {
108 InFile { 108 InFile {
109 file_id: self.id.ast_id.expect("MacroDef without ast_id").file_id, 109 file_id: self.id.ast_id.expect("MacroDef without ast_id").file_id,
110 value: self.id.ast_id.expect("MacroDef without ast_id").to_node(db), 110 value: self.id.ast_id.expect("MacroDef without ast_id").to_node(db.upcast()),
111 } 111 }
112 } 112 }
113} 113}
114impl HasSource for ImplDef { 114impl HasSource for ImplDef {
115 type Ast = ast::ImplDef; 115 type Ast = ast::ImplDef;
116 fn source(self, db: &impl DefDatabase) -> InFile<ast::ImplDef> { 116 fn source(self, db: &dyn HirDatabase) -> InFile<ast::ImplDef> {
117 self.id.lookup(db).source(db) 117 self.id.lookup(db.upcast()).source(db.upcast())
118 } 118 }
119} 119}
120 120
121impl HasSource for TypeParam { 121impl HasSource for TypeParam {
122 type Ast = Either<ast::TraitDef, ast::TypeParam>; 122 type Ast = Either<ast::TraitDef, ast::TypeParam>;
123 fn source(self, db: &impl DefDatabase) -> InFile<Self::Ast> { 123 fn source(self, db: &dyn HirDatabase) -> InFile<Self::Ast> {
124 let child_source = self.id.parent.child_source(db); 124 let child_source = self.id.parent.child_source(db.upcast());
125 child_source.map(|it| it[self.id.local_id].clone()) 125 child_source.map(|it| it[self.id.local_id].clone())
126 } 126 }
127} 127}
diff --git a/crates/ra_hir/src/semantics.rs b/crates/ra_hir/src/semantics.rs
index 788bb3eb7..55e634528 100644
--- a/crates/ra_hir/src/semantics.rs
+++ b/crates/ra_hir/src/semantics.rs
@@ -190,7 +190,7 @@ impl<'db, DB: HirDatabase> Semantics<'db, DB> {
190 T::to_def(self, src) 190 T::to_def(self, src)
191 } 191 }
192 192
193 fn with_ctx<F: FnOnce(&mut SourceToDefCtx<&DB>) -> T, T>(&self, f: F) -> T { 193 fn with_ctx<F: FnOnce(&mut SourceToDefCtx) -> T, T>(&self, f: F) -> T {
194 let mut cache = self.s2d_cache.borrow_mut(); 194 let mut cache = self.s2d_cache.borrow_mut();
195 let mut ctx = SourceToDefCtx { db: self.db, cache: &mut *cache }; 195 let mut ctx = SourceToDefCtx { db: self.db, cache: &mut *cache };
196 f(&mut ctx) 196 f(&mut ctx)
@@ -369,35 +369,35 @@ impl<'a, DB: HirDatabase> SemanticsScope<'a, DB> {
369} 369}
370 370
371// FIXME: Change `HasSource` trait to work with `Semantics` and remove this? 371// FIXME: Change `HasSource` trait to work with `Semantics` and remove this?
372pub fn original_range(db: &impl HirDatabase, node: InFile<&SyntaxNode>) -> FileRange { 372pub fn original_range(db: &dyn HirDatabase, node: InFile<&SyntaxNode>) -> FileRange {
373 if let Some(range) = original_range_opt(db, node) { 373 if let Some(range) = original_range_opt(db, node) {
374 let original_file = range.file_id.original_file(db); 374 let original_file = range.file_id.original_file(db.upcast());
375 if range.file_id == original_file.into() { 375 if range.file_id == original_file.into() {
376 return FileRange { file_id: original_file, range: range.value }; 376 return FileRange { file_id: original_file, range: range.value };
377 } 377 }
378 378
379 log::error!("Fail to mapping up more for {:?}", range); 379 log::error!("Fail to mapping up more for {:?}", range);
380 return FileRange { file_id: range.file_id.original_file(db), range: range.value }; 380 return FileRange { file_id: range.file_id.original_file(db.upcast()), range: range.value };
381 } 381 }
382 382
383 // Fall back to whole macro call 383 // Fall back to whole macro call
384 if let Some(expansion) = node.file_id.expansion_info(db) { 384 if let Some(expansion) = node.file_id.expansion_info(db.upcast()) {
385 if let Some(call_node) = expansion.call_node() { 385 if let Some(call_node) = expansion.call_node() {
386 return FileRange { 386 return FileRange {
387 file_id: call_node.file_id.original_file(db), 387 file_id: call_node.file_id.original_file(db.upcast()),
388 range: call_node.value.text_range(), 388 range: call_node.value.text_range(),
389 }; 389 };
390 } 390 }
391 } 391 }
392 392
393 FileRange { file_id: node.file_id.original_file(db), range: node.value.text_range() } 393 FileRange { file_id: node.file_id.original_file(db.upcast()), range: node.value.text_range() }
394} 394}
395 395
396fn original_range_opt( 396fn original_range_opt(
397 db: &impl HirDatabase, 397 db: &dyn HirDatabase,
398 node: InFile<&SyntaxNode>, 398 node: InFile<&SyntaxNode>,
399) -> Option<InFile<TextRange>> { 399) -> Option<InFile<TextRange>> {
400 let expansion = node.file_id.expansion_info(db)?; 400 let expansion = node.file_id.expansion_info(db.upcast())?;
401 401
402 // the input node has only one token ? 402 // the input node has only one token ?
403 let single = skip_trivia_token(node.value.first_token()?, Direction::Next)? 403 let single = skip_trivia_token(node.value.first_token()?, Direction::Next)?
@@ -419,7 +419,7 @@ fn original_range_opt(
419} 419}
420 420
421fn ascend_call_token( 421fn ascend_call_token(
422 db: &impl HirDatabase, 422 db: &dyn HirDatabase,
423 expansion: &ExpansionInfo, 423 expansion: &ExpansionInfo,
424 token: InFile<SyntaxToken>, 424 token: InFile<SyntaxToken>,
425) -> Option<InFile<SyntaxToken>> { 425) -> Option<InFile<SyntaxToken>> {
@@ -427,7 +427,7 @@ fn ascend_call_token(
427 if origin != Origin::Call { 427 if origin != Origin::Call {
428 return None; 428 return None;
429 } 429 }
430 if let Some(info) = mapped.file_id.expansion_info(db) { 430 if let Some(info) = mapped.file_id.expansion_info(db.upcast()) {
431 return ascend_call_token(db, &info, mapped); 431 return ascend_call_token(db, &info, mapped);
432 } 432 }
433 Some(mapped) 433 Some(mapped)
diff --git a/crates/ra_hir/src/semantics/source_to_def.rs b/crates/ra_hir/src/semantics/source_to_def.rs
index 67b243222..8843f2835 100644
--- a/crates/ra_hir/src/semantics/source_to_def.rs
+++ b/crates/ra_hir/src/semantics/source_to_def.rs
@@ -21,12 +21,12 @@ use crate::{db::HirDatabase, InFile, MacroDefId};
21 21
22pub(super) type SourceToDefCache = FxHashMap<ChildContainer, DynMap>; 22pub(super) type SourceToDefCache = FxHashMap<ChildContainer, DynMap>;
23 23
24pub(super) struct SourceToDefCtx<'a, DB> { 24pub(super) struct SourceToDefCtx<'a, 'b> {
25 pub(super) db: DB, 25 pub(super) db: &'b dyn HirDatabase,
26 pub(super) cache: &'a mut SourceToDefCache, 26 pub(super) cache: &'a mut SourceToDefCache,
27} 27}
28 28
29impl<DB: HirDatabase> SourceToDefCtx<'_, &'_ DB> { 29impl SourceToDefCtx<'_, '_> {
30 pub(super) fn file_to_def(&mut self, file: FileId) -> Option<ModuleId> { 30 pub(super) fn file_to_def(&mut self, file: FileId) -> Option<ModuleId> {
31 let _p = profile("SourceBinder::to_module_def"); 31 let _p = profile("SourceBinder::to_module_def");
32 let (krate, local_id) = self.db.relevant_crates(file).iter().find_map(|&crate_id| { 32 let (krate, local_id) = self.db.relevant_crates(file).iter().find_map(|&crate_id| {
@@ -43,7 +43,7 @@ impl<DB: HirDatabase> SourceToDefCtx<'_, &'_ DB> {
43 .as_ref() 43 .as_ref()
44 .map(|it| it.syntax()) 44 .map(|it| it.syntax())
45 .cloned() 45 .cloned()
46 .ancestors_with_macros(self.db) 46 .ancestors_with_macros(self.db.upcast())
47 .skip(1) 47 .skip(1)
48 .find_map(|it| { 48 .find_map(|it| {
49 let m = ast::Module::cast(it.value.clone())?; 49 let m = ast::Module::cast(it.value.clone())?;
@@ -53,7 +53,7 @@ impl<DB: HirDatabase> SourceToDefCtx<'_, &'_ DB> {
53 let parent_module = match parent_declaration { 53 let parent_module = match parent_declaration {
54 Some(parent_declaration) => self.module_to_def(parent_declaration), 54 Some(parent_declaration) => self.module_to_def(parent_declaration),
55 None => { 55 None => {
56 let file_id = src.file_id.original_file(self.db); 56 let file_id = src.file_id.original_file(self.db.upcast());
57 self.file_to_def(file_id) 57 self.file_to_def(file_id)
58 } 58 }
59 }?; 59 }?;
@@ -147,7 +147,7 @@ impl<DB: HirDatabase> SourceToDefCtx<'_, &'_ DB> {
147 // FIXME: use DynMap as well? 147 // FIXME: use DynMap as well?
148 pub(super) fn macro_call_to_def(&mut self, src: InFile<ast::MacroCall>) -> Option<MacroDefId> { 148 pub(super) fn macro_call_to_def(&mut self, src: InFile<ast::MacroCall>) -> Option<MacroDefId> {
149 let kind = MacroDefKind::Declarative; 149 let kind = MacroDefKind::Declarative;
150 let file_id = src.file_id.original_file(self.db); 150 let file_id = src.file_id.original_file(self.db.upcast());
151 let krate = self.file_to_def(file_id)?.krate; 151 let krate = self.file_to_def(file_id)?.krate;
152 let file_ast_id = self.db.ast_id_map(src.file_id).ast_id(&src.value); 152 let file_ast_id = self.db.ast_id_map(src.file_id).ast_id(&src.value);
153 let ast_id = Some(AstId::new(src.file_id, file_ast_id)); 153 let ast_id = Some(AstId::new(src.file_id, file_ast_id));
@@ -155,7 +155,7 @@ impl<DB: HirDatabase> SourceToDefCtx<'_, &'_ DB> {
155 } 155 }
156 156
157 pub(super) fn find_container(&mut self, src: InFile<&SyntaxNode>) -> Option<ChildContainer> { 157 pub(super) fn find_container(&mut self, src: InFile<&SyntaxNode>) -> Option<ChildContainer> {
158 for container in src.cloned().ancestors_with_macros(self.db).skip(1) { 158 for container in src.cloned().ancestors_with_macros(self.db.upcast()).skip(1) {
159 let res: ChildContainer = match_ast! { 159 let res: ChildContainer = match_ast! {
160 match (container.value) { 160 match (container.value) {
161 ast::Module(it) => { 161 ast::Module(it) => {
@@ -200,12 +200,12 @@ impl<DB: HirDatabase> SourceToDefCtx<'_, &'_ DB> {
200 return Some(res); 200 return Some(res);
201 } 201 }
202 202
203 let def = self.file_to_def(src.file_id.original_file(self.db))?; 203 let def = self.file_to_def(src.file_id.original_file(self.db.upcast()))?;
204 Some(def.into()) 204 Some(def.into())
205 } 205 }
206 206
207 fn find_type_param_container(&mut self, src: InFile<&SyntaxNode>) -> Option<GenericDefId> { 207 fn find_type_param_container(&mut self, src: InFile<&SyntaxNode>) -> Option<GenericDefId> {
208 for container in src.cloned().ancestors_with_macros(self.db).skip(1) { 208 for container in src.cloned().ancestors_with_macros(self.db.upcast()).skip(1) {
209 let res: GenericDefId = match_ast! { 209 let res: GenericDefId = match_ast! {
210 match (container.value) { 210 match (container.value) {
211 ast::FnDef(it) => { self.fn_to_def(container.with_value(it))?.into() }, 211 ast::FnDef(it) => { self.fn_to_def(container.with_value(it))?.into() },
@@ -223,7 +223,7 @@ impl<DB: HirDatabase> SourceToDefCtx<'_, &'_ DB> {
223 } 223 }
224 224
225 fn find_pat_container(&mut self, src: InFile<&SyntaxNode>) -> Option<DefWithBodyId> { 225 fn find_pat_container(&mut self, src: InFile<&SyntaxNode>) -> Option<DefWithBodyId> {
226 for container in src.cloned().ancestors_with_macros(self.db).skip(1) { 226 for container in src.cloned().ancestors_with_macros(self.db.upcast()).skip(1) {
227 let res: DefWithBodyId = match_ast! { 227 let res: DefWithBodyId = match_ast! {
228 match (container.value) { 228 match (container.value) {
229 ast::ConstDef(it) => { self.const_to_def(container.with_value(it))?.into() }, 229 ast::ConstDef(it) => { self.const_to_def(container.with_value(it))?.into() },
@@ -262,7 +262,8 @@ impl_froms! {
262} 262}
263 263
264impl ChildContainer { 264impl ChildContainer {
265 fn child_by_source(self, db: &impl HirDatabase) -> DynMap { 265 fn child_by_source(self, db: &dyn HirDatabase) -> DynMap {
266 let db = db.upcast();
266 match self { 267 match self {
267 ChildContainer::DefWithBodyId(it) => it.child_by_source(db), 268 ChildContainer::DefWithBodyId(it) => it.child_by_source(db),
268 ChildContainer::ModuleId(it) => it.child_by_source(db), 269 ChildContainer::ModuleId(it) => it.child_by_source(db),
diff --git a/crates/ra_hir/src/source_analyzer.rs b/crates/ra_hir/src/source_analyzer.rs
index 331ecdd9c..e8afef328 100644
--- a/crates/ra_hir/src/source_analyzer.rs
+++ b/crates/ra_hir/src/source_analyzer.rs
@@ -42,7 +42,7 @@ pub(crate) struct SourceAnalyzer {
42 42
43impl SourceAnalyzer { 43impl SourceAnalyzer {
44 pub(crate) fn new_for_body( 44 pub(crate) fn new_for_body(
45 db: &impl HirDatabase, 45 db: &dyn HirDatabase,
46 def: DefWithBodyId, 46 def: DefWithBodyId,
47 node: InFile<&SyntaxNode>, 47 node: InFile<&SyntaxNode>,
48 offset: Option<TextUnit>, 48 offset: Option<TextUnit>,
@@ -53,7 +53,7 @@ impl SourceAnalyzer {
53 None => scope_for(&scopes, &source_map, node), 53 None => scope_for(&scopes, &source_map, node),
54 Some(offset) => scope_for_offset(&scopes, &source_map, node.with_value(offset)), 54 Some(offset) => scope_for_offset(&scopes, &source_map, node.with_value(offset)),
55 }; 55 };
56 let resolver = resolver_for_scope(db, def, scope); 56 let resolver = resolver_for_scope(db.upcast(), def, scope);
57 SourceAnalyzer { 57 SourceAnalyzer {
58 resolver, 58 resolver,
59 body: Some(body), 59 body: Some(body),
@@ -90,7 +90,7 @@ impl SourceAnalyzer {
90 90
91 fn expand_expr( 91 fn expand_expr(
92 &self, 92 &self,
93 db: &impl HirDatabase, 93 db: &dyn HirDatabase,
94 expr: InFile<ast::MacroCall>, 94 expr: InFile<ast::MacroCall>,
95 ) -> Option<InFile<ast::Expr>> { 95 ) -> Option<InFile<ast::Expr>> {
96 let macro_file = self.body_source_map.as_ref()?.node_macro_file(expr.as_ref())?; 96 let macro_file = self.body_source_map.as_ref()?.node_macro_file(expr.as_ref())?;
@@ -103,11 +103,11 @@ impl SourceAnalyzer {
103 Some(res) 103 Some(res)
104 } 104 }
105 105
106 fn trait_env(&self, db: &impl HirDatabase) -> Arc<TraitEnvironment> { 106 fn trait_env(&self, db: &dyn HirDatabase) -> Arc<TraitEnvironment> {
107 TraitEnvironment::lower(db, &self.resolver) 107 TraitEnvironment::lower(db, &self.resolver)
108 } 108 }
109 109
110 pub(crate) fn type_of(&self, db: &impl HirDatabase, expr: &ast::Expr) -> Option<Type> { 110 pub(crate) fn type_of(&self, db: &dyn HirDatabase, expr: &ast::Expr) -> Option<Type> {
111 let expr_id = match expr { 111 let expr_id = match expr {
112 ast::Expr::MacroCall(call) => { 112 ast::Expr::MacroCall(call) => {
113 let expr = self.expand_expr(db, InFile::new(self.file_id, call.clone()))?; 113 let expr = self.expand_expr(db, InFile::new(self.file_id, call.clone()))?;
@@ -121,7 +121,7 @@ impl SourceAnalyzer {
121 Some(Type { krate: self.resolver.krate()?, ty: InEnvironment { value: ty, environment } }) 121 Some(Type { krate: self.resolver.krate()?, ty: InEnvironment { value: ty, environment } })
122 } 122 }
123 123
124 pub(crate) fn type_of_pat(&self, db: &impl HirDatabase, pat: &ast::Pat) -> Option<Type> { 124 pub(crate) fn type_of_pat(&self, db: &dyn HirDatabase, pat: &ast::Pat) -> Option<Type> {
125 let pat_id = self.pat_id(pat)?; 125 let pat_id = self.pat_id(pat)?;
126 let ty = self.infer.as_ref()?[pat_id].clone(); 126 let ty = self.infer.as_ref()?[pat_id].clone();
127 let environment = self.trait_env(db); 127 let environment = self.trait_env(db);
@@ -140,7 +140,7 @@ impl SourceAnalyzer {
140 140
141 pub(crate) fn resolve_record_field( 141 pub(crate) fn resolve_record_field(
142 &self, 142 &self,
143 db: &impl HirDatabase, 143 db: &dyn HirDatabase,
144 field: &ast::RecordField, 144 field: &ast::RecordField,
145 ) -> Option<(crate::StructField, Option<Local>)> { 145 ) -> Option<(crate::StructField, Option<Local>)> {
146 let (expr_id, local) = match field.expr() { 146 let (expr_id, local) = match field.expr() {
@@ -150,7 +150,7 @@ impl SourceAnalyzer {
150 let expr_id = self.body_source_map.as_ref()?.field_init_shorthand_expr(src)?; 150 let expr_id = self.body_source_map.as_ref()?.field_init_shorthand_expr(src)?;
151 let local_name = field.name_ref()?.as_name(); 151 let local_name = field.name_ref()?.as_name();
152 let path = ModPath::from_segments(PathKind::Plain, once(local_name)); 152 let path = ModPath::from_segments(PathKind::Plain, once(local_name));
153 let local = match self.resolver.resolve_path_in_value_ns_fully(db, &path) { 153 let local = match self.resolver.resolve_path_in_value_ns_fully(db.upcast(), &path) {
154 Some(ValueNs::LocalBinding(pat_id)) => { 154 Some(ValueNs::LocalBinding(pat_id)) => {
155 Some(Local { pat_id, parent: self.resolver.body_owner()? }) 155 Some(Local { pat_id, parent: self.resolver.body_owner()? })
156 } 156 }
@@ -181,17 +181,17 @@ impl SourceAnalyzer {
181 181
182 pub(crate) fn resolve_macro_call( 182 pub(crate) fn resolve_macro_call(
183 &self, 183 &self,
184 db: &impl HirDatabase, 184 db: &dyn HirDatabase,
185 macro_call: InFile<&ast::MacroCall>, 185 macro_call: InFile<&ast::MacroCall>,
186 ) -> Option<MacroDef> { 186 ) -> Option<MacroDef> {
187 let hygiene = Hygiene::new(db, macro_call.file_id); 187 let hygiene = Hygiene::new(db.upcast(), macro_call.file_id);
188 let path = macro_call.value.path().and_then(|ast| Path::from_src(ast, &hygiene))?; 188 let path = macro_call.value.path().and_then(|ast| Path::from_src(ast, &hygiene))?;
189 self.resolver.resolve_path_as_macro(db, path.mod_path()).map(|it| it.into()) 189 self.resolver.resolve_path_as_macro(db.upcast(), path.mod_path()).map(|it| it.into())
190 } 190 }
191 191
192 pub(crate) fn resolve_bind_pat_to_const( 192 pub(crate) fn resolve_bind_pat_to_const(
193 &self, 193 &self,
194 db: &impl HirDatabase, 194 db: &dyn HirDatabase,
195 pat: &ast::BindPat, 195 pat: &ast::BindPat,
196 ) -> Option<ModuleDef> { 196 ) -> Option<ModuleDef> {
197 let pat_id = self.pat_id(&pat.clone().into())?; 197 let pat_id = self.pat_id(&pat.clone().into())?;
@@ -209,7 +209,7 @@ impl SourceAnalyzer {
209 209
210 pub(crate) fn resolve_path( 210 pub(crate) fn resolve_path(
211 &self, 211 &self,
212 db: &impl HirDatabase, 212 db: &dyn HirDatabase,
213 path: &ast::Path, 213 path: &ast::Path,
214 ) -> Option<PathResolution> { 214 ) -> Option<PathResolution> {
215 if let Some(path_expr) = path.syntax().parent().and_then(ast::PathExpr::cast) { 215 if let Some(path_expr) = path.syntax().parent().and_then(ast::PathExpr::cast) {
@@ -231,11 +231,12 @@ impl SourceAnalyzer {
231 231
232 pub(crate) fn expand( 232 pub(crate) fn expand(
233 &self, 233 &self,
234 db: &impl HirDatabase, 234 db: &dyn HirDatabase,
235 macro_call: InFile<&ast::MacroCall>, 235 macro_call: InFile<&ast::MacroCall>,
236 ) -> Option<HirFileId> { 236 ) -> Option<HirFileId> {
237 let macro_call_id = 237 let macro_call_id = macro_call.as_call_id(db.upcast(), |path| {
238 macro_call.as_call_id(db, |path| self.resolver.resolve_path_as_macro(db, &path))?; 238 self.resolver.resolve_path_as_macro(db.upcast(), &path)
239 })?;
239 Some(macro_call_id.as_file()) 240 Some(macro_call_id.as_file())
240 } 241 }
241} 242}
@@ -283,42 +284,46 @@ fn scope_for_offset(
283} 284}
284 285
285pub(crate) fn resolve_hir_path( 286pub(crate) fn resolve_hir_path(
286 db: &impl HirDatabase, 287 db: &dyn HirDatabase,
287 resolver: &Resolver, 288 resolver: &Resolver,
288 path: &crate::Path, 289 path: &crate::Path,
289) -> Option<PathResolution> { 290) -> Option<PathResolution> {
290 let types = resolver.resolve_path_in_type_ns_fully(db, path.mod_path()).map(|ty| match ty { 291 let types =
291 TypeNs::SelfType(it) => PathResolution::SelfType(it.into()), 292 resolver.resolve_path_in_type_ns_fully(db.upcast(), path.mod_path()).map(|ty| match ty {
292 TypeNs::GenericParam(id) => PathResolution::TypeParam(TypeParam { id }), 293 TypeNs::SelfType(it) => PathResolution::SelfType(it.into()),
293 TypeNs::AdtSelfType(it) | TypeNs::AdtId(it) => PathResolution::Def(Adt::from(it).into()), 294 TypeNs::GenericParam(id) => PathResolution::TypeParam(TypeParam { id }),
294 TypeNs::EnumVariantId(it) => PathResolution::Def(EnumVariant::from(it).into()), 295 TypeNs::AdtSelfType(it) | TypeNs::AdtId(it) => {
295 TypeNs::TypeAliasId(it) => PathResolution::Def(TypeAlias::from(it).into()), 296 PathResolution::Def(Adt::from(it).into())
296 TypeNs::BuiltinType(it) => PathResolution::Def(it.into()),
297 TypeNs::TraitId(it) => PathResolution::Def(Trait::from(it).into()),
298 });
299 let body_owner = resolver.body_owner();
300 let values = resolver.resolve_path_in_value_ns_fully(db, path.mod_path()).and_then(|val| {
301 let res = match val {
302 ValueNs::LocalBinding(pat_id) => {
303 let var = Local { parent: body_owner?.into(), pat_id };
304 PathResolution::Local(var)
305 } 297 }
306 ValueNs::FunctionId(it) => PathResolution::Def(Function::from(it).into()), 298 TypeNs::EnumVariantId(it) => PathResolution::Def(EnumVariant::from(it).into()),
307 ValueNs::ConstId(it) => PathResolution::Def(Const::from(it).into()), 299 TypeNs::TypeAliasId(it) => PathResolution::Def(TypeAlias::from(it).into()),
308 ValueNs::StaticId(it) => PathResolution::Def(Static::from(it).into()), 300 TypeNs::BuiltinType(it) => PathResolution::Def(it.into()),
309 ValueNs::StructId(it) => PathResolution::Def(Struct::from(it).into()), 301 TypeNs::TraitId(it) => PathResolution::Def(Trait::from(it).into()),
310 ValueNs::EnumVariantId(it) => PathResolution::Def(EnumVariant::from(it).into()), 302 });
311 }; 303 let body_owner = resolver.body_owner();
312 Some(res) 304 let values =
313 }); 305 resolver.resolve_path_in_value_ns_fully(db.upcast(), path.mod_path()).and_then(|val| {
306 let res = match val {
307 ValueNs::LocalBinding(pat_id) => {
308 let var = Local { parent: body_owner?.into(), pat_id };
309 PathResolution::Local(var)
310 }
311 ValueNs::FunctionId(it) => PathResolution::Def(Function::from(it).into()),
312 ValueNs::ConstId(it) => PathResolution::Def(Const::from(it).into()),
313 ValueNs::StaticId(it) => PathResolution::Def(Static::from(it).into()),
314 ValueNs::StructId(it) => PathResolution::Def(Struct::from(it).into()),
315 ValueNs::EnumVariantId(it) => PathResolution::Def(EnumVariant::from(it).into()),
316 };
317 Some(res)
318 });
314 319
315 let items = resolver 320 let items = resolver
316 .resolve_module_path_in_items(db, path.mod_path()) 321 .resolve_module_path_in_items(db.upcast(), path.mod_path())
317 .take_types() 322 .take_types()
318 .map(|it| PathResolution::Def(it.into())); 323 .map(|it| PathResolution::Def(it.into()));
319 types.or(values).or(items).or_else(|| { 324 types.or(values).or(items).or_else(|| {
320 resolver 325 resolver
321 .resolve_path_as_macro(db, path.mod_path()) 326 .resolve_path_as_macro(db.upcast(), path.mod_path())
322 .map(|def| PathResolution::Macro(def.into())) 327 .map(|def| PathResolution::Macro(def.into()))
323 }) 328 })
324} 329}