aboutsummaryrefslogtreecommitdiff
path: root/crates/ra_hir/src/code_model.rs
diff options
context:
space:
mode:
Diffstat (limited to 'crates/ra_hir/src/code_model.rs')
-rw-r--r--crates/ra_hir/src/code_model.rs329
1 files changed, 163 insertions, 166 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}