aboutsummaryrefslogtreecommitdiff
path: root/crates/hir/src/code_model.rs
diff options
context:
space:
mode:
Diffstat (limited to 'crates/hir/src/code_model.rs')
-rw-r--r--crates/hir/src/code_model.rs1712
1 files changed, 1712 insertions, 0 deletions
diff --git a/crates/hir/src/code_model.rs b/crates/hir/src/code_model.rs
new file mode 100644
index 000000000..8ffb9e99b
--- /dev/null
+++ b/crates/hir/src/code_model.rs
@@ -0,0 +1,1712 @@
1//! FIXME: write short doc here
2use std::{iter, sync::Arc};
3
4use arrayvec::ArrayVec;
5use base_db::{CrateId, Edition, FileId};
6use either::Either;
7use hir_def::{
8 adt::ReprKind,
9 adt::StructKind,
10 adt::VariantData,
11 builtin_type::BuiltinType,
12 docs::Documentation,
13 expr::{BindingAnnotation, Pat, PatId},
14 import_map,
15 per_ns::PerNs,
16 resolver::{HasResolver, Resolver},
17 src::HasSource as _,
18 type_ref::{Mutability, TypeRef},
19 AdtId, AssocContainerId, ConstId, DefWithBodyId, EnumId, FunctionId, GenericDefId, HasModule,
20 ImplId, LocalEnumVariantId, LocalFieldId, LocalModuleId, Lookup, ModuleId, StaticId, StructId,
21 TraitId, TypeAliasId, TypeParamId, UnionId,
22};
23use hir_expand::{
24 diagnostics::DiagnosticSink,
25 name::{name, AsName},
26 MacroDefId, MacroDefKind,
27};
28use hir_ty::{
29 autoderef,
30 display::{HirDisplayError, HirFormatter},
31 method_resolution, ApplicationTy, CallableDefId, Canonical, FnSig, GenericPredicate,
32 InEnvironment, Substs, TraitEnvironment, Ty, TyDefId, TypeCtor,
33};
34use rustc_hash::FxHashSet;
35use stdx::impl_from;
36use syntax::{
37 ast::{self, AttrsOwner, NameOwner},
38 AstNode,
39};
40
41use crate::{
42 db::{DefDatabase, HirDatabase},
43 has_source::HasSource,
44 HirDisplay, InFile, Name,
45};
46
47/// hir::Crate describes a single crate. It's the main interface with which
48/// a crate's dependencies interact. Mostly, it should be just a proxy for the
49/// root module.
50#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
51pub struct Crate {
52 pub(crate) id: CrateId,
53}
54
55#[derive(Debug)]
56pub struct CrateDependency {
57 pub krate: Crate,
58 pub name: Name,
59}
60
61impl Crate {
62 pub fn dependencies(self, db: &dyn HirDatabase) -> Vec<CrateDependency> {
63 db.crate_graph()[self.id]
64 .dependencies
65 .iter()
66 .map(|dep| {
67 let krate = Crate { id: dep.crate_id };
68 let name = dep.as_name();
69 CrateDependency { krate, name }
70 })
71 .collect()
72 }
73
74 // FIXME: add `transitive_reverse_dependencies`.
75 pub fn reverse_dependencies(self, db: &dyn HirDatabase) -> Vec<Crate> {
76 let crate_graph = db.crate_graph();
77 crate_graph
78 .iter()
79 .filter(|&krate| {
80 crate_graph[krate].dependencies.iter().any(|it| it.crate_id == self.id)
81 })
82 .map(|id| Crate { id })
83 .collect()
84 }
85
86 pub fn root_module(self, db: &dyn HirDatabase) -> Module {
87 let module_id = db.crate_def_map(self.id).root;
88 Module::new(self, module_id)
89 }
90
91 pub fn root_file(self, db: &dyn HirDatabase) -> FileId {
92 db.crate_graph()[self.id].root_file_id
93 }
94
95 pub fn edition(self, db: &dyn HirDatabase) -> Edition {
96 db.crate_graph()[self.id].edition
97 }
98
99 pub fn display_name(self, db: &dyn HirDatabase) -> Option<String> {
100 db.crate_graph()[self.id].display_name.clone()
101 }
102
103 pub fn query_external_importables(
104 self,
105 db: &dyn DefDatabase,
106 query: &str,
107 ) -> impl Iterator<Item = Either<ModuleDef, MacroDef>> {
108 import_map::search_dependencies(
109 db,
110 self.into(),
111 import_map::Query::new(query).anchor_end().case_sensitive().limit(40),
112 )
113 .into_iter()
114 .map(|item| match item {
115 ItemInNs::Types(mod_id) | ItemInNs::Values(mod_id) => Either::Left(mod_id.into()),
116 ItemInNs::Macros(mac_id) => Either::Right(mac_id.into()),
117 })
118 }
119
120 pub fn all(db: &dyn HirDatabase) -> Vec<Crate> {
121 db.crate_graph().iter().map(|id| Crate { id }).collect()
122 }
123}
124
125#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
126pub struct Module {
127 pub(crate) id: ModuleId,
128}
129
130/// The defs which can be visible in the module.
131#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
132pub enum ModuleDef {
133 Module(Module),
134 Function(Function),
135 Adt(Adt),
136 // Can't be directly declared, but can be imported.
137 EnumVariant(EnumVariant),
138 Const(Const),
139 Static(Static),
140 Trait(Trait),
141 TypeAlias(TypeAlias),
142 BuiltinType(BuiltinType),
143}
144impl_from!(
145 Module,
146 Function,
147 Adt(Struct, Enum, Union),
148 EnumVariant,
149 Const,
150 Static,
151 Trait,
152 TypeAlias,
153 BuiltinType
154 for ModuleDef
155);
156
157impl ModuleDef {
158 pub fn module(self, db: &dyn HirDatabase) -> Option<Module> {
159 match self {
160 ModuleDef::Module(it) => it.parent(db),
161 ModuleDef::Function(it) => Some(it.module(db)),
162 ModuleDef::Adt(it) => Some(it.module(db)),
163 ModuleDef::EnumVariant(it) => Some(it.module(db)),
164 ModuleDef::Const(it) => Some(it.module(db)),
165 ModuleDef::Static(it) => Some(it.module(db)),
166 ModuleDef::Trait(it) => Some(it.module(db)),
167 ModuleDef::TypeAlias(it) => Some(it.module(db)),
168 ModuleDef::BuiltinType(_) => None,
169 }
170 }
171
172 pub fn definition_visibility(&self, db: &dyn HirDatabase) -> Option<Visibility> {
173 let module = match self {
174 ModuleDef::Module(it) => it.parent(db)?,
175 ModuleDef::Function(it) => return Some(it.visibility(db)),
176 ModuleDef::Adt(it) => it.module(db),
177 ModuleDef::EnumVariant(it) => {
178 let parent = it.parent_enum(db);
179 let module = it.module(db);
180 return module.visibility_of(db, &ModuleDef::Adt(Adt::Enum(parent)));
181 }
182 ModuleDef::Const(it) => return Some(it.visibility(db)),
183 ModuleDef::Static(it) => it.module(db),
184 ModuleDef::Trait(it) => it.module(db),
185 ModuleDef::TypeAlias(it) => return Some(it.visibility(db)),
186 ModuleDef::BuiltinType(_) => return None,
187 };
188
189 module.visibility_of(db, self)
190 }
191
192 pub fn name(self, db: &dyn HirDatabase) -> Option<Name> {
193 match self {
194 ModuleDef::Adt(it) => Some(it.name(db)),
195 ModuleDef::Trait(it) => Some(it.name(db)),
196 ModuleDef::Function(it) => Some(it.name(db)),
197 ModuleDef::EnumVariant(it) => Some(it.name(db)),
198 ModuleDef::TypeAlias(it) => Some(it.name(db)),
199
200 ModuleDef::Module(it) => it.name(db),
201 ModuleDef::Const(it) => it.name(db),
202 ModuleDef::Static(it) => it.name(db),
203
204 ModuleDef::BuiltinType(it) => Some(it.as_name()),
205 }
206 }
207}
208
209pub use hir_def::{
210 attr::Attrs, item_scope::ItemInNs, item_tree::ItemTreeNode, visibility::Visibility,
211 AssocItemId, AssocItemLoc,
212};
213
214impl Module {
215 pub(crate) fn new(krate: Crate, crate_module_id: LocalModuleId) -> Module {
216 Module { id: ModuleId { krate: krate.id, local_id: crate_module_id } }
217 }
218
219 /// Name of this module.
220 pub fn name(self, db: &dyn HirDatabase) -> Option<Name> {
221 let def_map = db.crate_def_map(self.id.krate);
222 let parent = def_map[self.id.local_id].parent?;
223 def_map[parent].children.iter().find_map(|(name, module_id)| {
224 if *module_id == self.id.local_id {
225 Some(name.clone())
226 } else {
227 None
228 }
229 })
230 }
231
232 /// Returns the crate this module is part of.
233 pub fn krate(self) -> Crate {
234 Crate { id: self.id.krate }
235 }
236
237 /// Topmost parent of this module. Every module has a `crate_root`, but some
238 /// might be missing `krate`. This can happen if a module's file is not included
239 /// in the module tree of any target in `Cargo.toml`.
240 pub fn crate_root(self, db: &dyn HirDatabase) -> Module {
241 let def_map = db.crate_def_map(self.id.krate);
242 self.with_module_id(def_map.root)
243 }
244
245 /// Iterates over all child modules.
246 pub fn children(self, db: &dyn HirDatabase) -> impl Iterator<Item = Module> {
247 let def_map = db.crate_def_map(self.id.krate);
248 let children = def_map[self.id.local_id]
249 .children
250 .iter()
251 .map(|(_, module_id)| self.with_module_id(*module_id))
252 .collect::<Vec<_>>();
253 children.into_iter()
254 }
255
256 /// Finds a parent module.
257 pub fn parent(self, db: &dyn HirDatabase) -> Option<Module> {
258 let def_map = db.crate_def_map(self.id.krate);
259 let parent_id = def_map[self.id.local_id].parent?;
260 Some(self.with_module_id(parent_id))
261 }
262
263 pub fn path_to_root(self, db: &dyn HirDatabase) -> Vec<Module> {
264 let mut res = vec![self];
265 let mut curr = self;
266 while let Some(next) = curr.parent(db) {
267 res.push(next);
268 curr = next
269 }
270 res
271 }
272
273 /// Returns a `ModuleScope`: a set of items, visible in this module.
274 pub fn scope(
275 self,
276 db: &dyn HirDatabase,
277 visible_from: Option<Module>,
278 ) -> Vec<(Name, ScopeDef)> {
279 db.crate_def_map(self.id.krate)[self.id.local_id]
280 .scope
281 .entries()
282 .filter_map(|(name, def)| {
283 if let Some(m) = visible_from {
284 let filtered =
285 def.filter_visibility(|vis| vis.is_visible_from(db.upcast(), m.id));
286 if filtered.is_none() && !def.is_none() {
287 None
288 } else {
289 Some((name, filtered))
290 }
291 } else {
292 Some((name, def))
293 }
294 })
295 .flat_map(|(name, def)| {
296 ScopeDef::all_items(def).into_iter().map(move |item| (name.clone(), item))
297 })
298 .collect()
299 }
300
301 pub fn visibility_of(self, db: &dyn HirDatabase, def: &ModuleDef) -> Option<Visibility> {
302 db.crate_def_map(self.id.krate)[self.id.local_id].scope.visibility_of(def.clone().into())
303 }
304
305 pub fn diagnostics(self, db: &dyn HirDatabase, sink: &mut DiagnosticSink) {
306 let _p = profile::span("Module::diagnostics");
307 let crate_def_map = db.crate_def_map(self.id.krate);
308 crate_def_map.add_diagnostics(db.upcast(), self.id.local_id, sink);
309 for decl in self.declarations(db) {
310 match decl {
311 crate::ModuleDef::Function(f) => f.diagnostics(db, sink),
312 crate::ModuleDef::Module(m) => {
313 // Only add diagnostics from inline modules
314 if crate_def_map[m.id.local_id].origin.is_inline() {
315 m.diagnostics(db, sink)
316 }
317 }
318 _ => (),
319 }
320 }
321
322 for impl_def in self.impl_defs(db) {
323 for item in impl_def.items(db) {
324 if let AssocItem::Function(f) = item {
325 f.diagnostics(db, sink);
326 }
327 }
328 }
329 }
330
331 pub fn declarations(self, db: &dyn HirDatabase) -> Vec<ModuleDef> {
332 let def_map = db.crate_def_map(self.id.krate);
333 def_map[self.id.local_id].scope.declarations().map(ModuleDef::from).collect()
334 }
335
336 pub fn impl_defs(self, db: &dyn HirDatabase) -> Vec<ImplDef> {
337 let def_map = db.crate_def_map(self.id.krate);
338 def_map[self.id.local_id].scope.impls().map(ImplDef::from).collect()
339 }
340
341 pub(crate) fn with_module_id(self, module_id: LocalModuleId) -> Module {
342 Module::new(self.krate(), module_id)
343 }
344
345 /// Finds a path that can be used to refer to the given item from within
346 /// this module, if possible.
347 pub fn find_use_path(
348 self,
349 db: &dyn DefDatabase,
350 item: impl Into<ItemInNs>,
351 ) -> Option<hir_def::path::ModPath> {
352 hir_def::find_path::find_path(db, item.into(), self.into())
353 }
354}
355
356#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
357pub struct Field {
358 pub(crate) parent: VariantDef,
359 pub(crate) id: LocalFieldId,
360}
361
362#[derive(Debug, PartialEq, Eq)]
363pub enum FieldSource {
364 Named(ast::RecordField),
365 Pos(ast::TupleField),
366}
367
368impl Field {
369 pub fn name(&self, db: &dyn HirDatabase) -> Name {
370 self.parent.variant_data(db).fields()[self.id].name.clone()
371 }
372
373 /// Returns the type as in the signature of the struct (i.e., with
374 /// placeholder types for type parameters). This is good for showing
375 /// signature help, but not so good to actually get the type of the field
376 /// when you actually have a variable of the struct.
377 pub fn signature_ty(&self, db: &dyn HirDatabase) -> Type {
378 let var_id = self.parent.into();
379 let generic_def_id: GenericDefId = match self.parent {
380 VariantDef::Struct(it) => it.id.into(),
381 VariantDef::Union(it) => it.id.into(),
382 VariantDef::EnumVariant(it) => it.parent.id.into(),
383 };
384 let substs = Substs::type_params(db, generic_def_id);
385 let ty = db.field_types(var_id)[self.id].clone().subst(&substs);
386 Type::new(db, self.parent.module(db).id.krate, var_id, ty)
387 }
388
389 pub fn parent_def(&self, _db: &dyn HirDatabase) -> VariantDef {
390 self.parent
391 }
392}
393
394impl HasVisibility for Field {
395 fn visibility(&self, db: &dyn HirDatabase) -> Visibility {
396 let variant_data = self.parent.variant_data(db);
397 let visibility = &variant_data.fields()[self.id].visibility;
398 let parent_id: hir_def::VariantId = self.parent.into();
399 visibility.resolve(db.upcast(), &parent_id.resolver(db.upcast()))
400 }
401}
402
403#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
404pub struct Struct {
405 pub(crate) id: StructId,
406}
407
408impl Struct {
409 pub fn module(self, db: &dyn HirDatabase) -> Module {
410 Module { id: self.id.lookup(db.upcast()).container.module(db.upcast()) }
411 }
412
413 pub fn krate(self, db: &dyn HirDatabase) -> Option<Crate> {
414 Some(self.module(db).krate())
415 }
416
417 pub fn name(self, db: &dyn HirDatabase) -> Name {
418 db.struct_data(self.id).name.clone()
419 }
420
421 pub fn fields(self, db: &dyn HirDatabase) -> Vec<Field> {
422 db.struct_data(self.id)
423 .variant_data
424 .fields()
425 .iter()
426 .map(|(id, _)| Field { parent: self.into(), id })
427 .collect()
428 }
429
430 pub fn ty(self, db: &dyn HirDatabase) -> Type {
431 Type::from_def(db, self.id.lookup(db.upcast()).container.module(db.upcast()).krate, self.id)
432 }
433
434 pub fn repr(self, db: &dyn HirDatabase) -> Option<ReprKind> {
435 db.struct_data(self.id).repr.clone()
436 }
437
438 fn variant_data(self, db: &dyn HirDatabase) -> Arc<VariantData> {
439 db.struct_data(self.id).variant_data.clone()
440 }
441}
442
443#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
444pub struct Union {
445 pub(crate) id: UnionId,
446}
447
448impl Union {
449 pub fn name(self, db: &dyn HirDatabase) -> Name {
450 db.union_data(self.id).name.clone()
451 }
452
453 pub fn module(self, db: &dyn HirDatabase) -> Module {
454 Module { id: self.id.lookup(db.upcast()).container.module(db.upcast()) }
455 }
456
457 pub fn ty(self, db: &dyn HirDatabase) -> Type {
458 Type::from_def(db, self.id.lookup(db.upcast()).container.module(db.upcast()).krate, self.id)
459 }
460
461 pub fn fields(self, db: &dyn HirDatabase) -> Vec<Field> {
462 db.union_data(self.id)
463 .variant_data
464 .fields()
465 .iter()
466 .map(|(id, _)| Field { parent: self.into(), id })
467 .collect()
468 }
469
470 fn variant_data(self, db: &dyn HirDatabase) -> Arc<VariantData> {
471 db.union_data(self.id).variant_data.clone()
472 }
473}
474
475#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
476pub struct Enum {
477 pub(crate) id: EnumId,
478}
479
480impl Enum {
481 pub fn module(self, db: &dyn HirDatabase) -> Module {
482 Module { id: self.id.lookup(db.upcast()).container.module(db.upcast()) }
483 }
484
485 pub fn krate(self, db: &dyn HirDatabase) -> Option<Crate> {
486 Some(self.module(db).krate())
487 }
488
489 pub fn name(self, db: &dyn HirDatabase) -> Name {
490 db.enum_data(self.id).name.clone()
491 }
492
493 pub fn variants(self, db: &dyn HirDatabase) -> Vec<EnumVariant> {
494 db.enum_data(self.id)
495 .variants
496 .iter()
497 .map(|(id, _)| EnumVariant { parent: self, id })
498 .collect()
499 }
500
501 pub fn ty(self, db: &dyn HirDatabase) -> Type {
502 Type::from_def(db, self.id.lookup(db.upcast()).container.module(db.upcast()).krate, self.id)
503 }
504}
505
506#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
507pub struct EnumVariant {
508 pub(crate) parent: Enum,
509 pub(crate) id: LocalEnumVariantId,
510}
511
512impl EnumVariant {
513 pub fn module(self, db: &dyn HirDatabase) -> Module {
514 self.parent.module(db)
515 }
516 pub fn parent_enum(self, _db: &dyn HirDatabase) -> Enum {
517 self.parent
518 }
519
520 pub fn name(self, db: &dyn HirDatabase) -> Name {
521 db.enum_data(self.parent.id).variants[self.id].name.clone()
522 }
523
524 pub fn fields(self, db: &dyn HirDatabase) -> Vec<Field> {
525 self.variant_data(db)
526 .fields()
527 .iter()
528 .map(|(id, _)| Field { parent: self.into(), id })
529 .collect()
530 }
531
532 pub fn kind(self, db: &dyn HirDatabase) -> StructKind {
533 self.variant_data(db).kind()
534 }
535
536 pub(crate) fn variant_data(self, db: &dyn HirDatabase) -> Arc<VariantData> {
537 db.enum_data(self.parent.id).variants[self.id].variant_data.clone()
538 }
539}
540
541/// A Data Type
542#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)]
543pub enum Adt {
544 Struct(Struct),
545 Union(Union),
546 Enum(Enum),
547}
548impl_from!(Struct, Union, Enum for Adt);
549
550impl Adt {
551 pub fn has_non_default_type_params(self, db: &dyn HirDatabase) -> bool {
552 let subst = db.generic_defaults(self.into());
553 subst.iter().any(|ty| &ty.value == &Ty::Unknown)
554 }
555
556 /// Turns this ADT into a type. Any type parameters of the ADT will be
557 /// turned into unknown types, which is good for e.g. finding the most
558 /// general set of completions, but will not look very nice when printed.
559 pub fn ty(self, db: &dyn HirDatabase) -> Type {
560 let id = AdtId::from(self);
561 Type::from_def(db, id.module(db.upcast()).krate, id)
562 }
563
564 pub fn module(self, db: &dyn HirDatabase) -> Module {
565 match self {
566 Adt::Struct(s) => s.module(db),
567 Adt::Union(s) => s.module(db),
568 Adt::Enum(e) => e.module(db),
569 }
570 }
571
572 pub fn krate(self, db: &dyn HirDatabase) -> Option<Crate> {
573 Some(self.module(db).krate())
574 }
575
576 pub fn name(self, db: &dyn HirDatabase) -> Name {
577 match self {
578 Adt::Struct(s) => s.name(db),
579 Adt::Union(u) => u.name(db),
580 Adt::Enum(e) => e.name(db),
581 }
582 }
583}
584
585#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)]
586pub enum VariantDef {
587 Struct(Struct),
588 Union(Union),
589 EnumVariant(EnumVariant),
590}
591impl_from!(Struct, Union, EnumVariant for VariantDef);
592
593impl VariantDef {
594 pub fn fields(self, db: &dyn HirDatabase) -> Vec<Field> {
595 match self {
596 VariantDef::Struct(it) => it.fields(db),
597 VariantDef::Union(it) => it.fields(db),
598 VariantDef::EnumVariant(it) => it.fields(db),
599 }
600 }
601
602 pub fn module(self, db: &dyn HirDatabase) -> Module {
603 match self {
604 VariantDef::Struct(it) => it.module(db),
605 VariantDef::Union(it) => it.module(db),
606 VariantDef::EnumVariant(it) => it.module(db),
607 }
608 }
609
610 pub fn name(&self, db: &dyn HirDatabase) -> Name {
611 match self {
612 VariantDef::Struct(s) => s.name(db),
613 VariantDef::Union(u) => u.name(db),
614 VariantDef::EnumVariant(e) => e.name(db),
615 }
616 }
617
618 pub(crate) fn variant_data(self, db: &dyn HirDatabase) -> Arc<VariantData> {
619 match self {
620 VariantDef::Struct(it) => it.variant_data(db),
621 VariantDef::Union(it) => it.variant_data(db),
622 VariantDef::EnumVariant(it) => it.variant_data(db),
623 }
624 }
625}
626
627/// The defs which have a body.
628#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
629pub enum DefWithBody {
630 Function(Function),
631 Static(Static),
632 Const(Const),
633}
634impl_from!(Function, Const, Static for DefWithBody);
635
636impl DefWithBody {
637 pub fn module(self, db: &dyn HirDatabase) -> Module {
638 match self {
639 DefWithBody::Const(c) => c.module(db),
640 DefWithBody::Function(f) => f.module(db),
641 DefWithBody::Static(s) => s.module(db),
642 }
643 }
644
645 pub fn name(self, db: &dyn HirDatabase) -> Option<Name> {
646 match self {
647 DefWithBody::Function(f) => Some(f.name(db)),
648 DefWithBody::Static(s) => s.name(db),
649 DefWithBody::Const(c) => c.name(db),
650 }
651 }
652}
653
654#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
655pub struct Function {
656 pub(crate) id: FunctionId,
657}
658
659impl Function {
660 pub fn module(self, db: &dyn HirDatabase) -> Module {
661 self.id.lookup(db.upcast()).module(db.upcast()).into()
662 }
663
664 pub fn krate(self, db: &dyn HirDatabase) -> Option<Crate> {
665 Some(self.module(db).krate())
666 }
667
668 pub fn name(self, db: &dyn HirDatabase) -> Name {
669 db.function_data(self.id).name.clone()
670 }
671
672 pub fn has_self_param(self, db: &dyn HirDatabase) -> bool {
673 db.function_data(self.id).has_self_param
674 }
675
676 pub fn params(self, db: &dyn HirDatabase) -> Vec<TypeRef> {
677 db.function_data(self.id).params.clone()
678 }
679
680 pub fn is_unsafe(self, db: &dyn HirDatabase) -> bool {
681 db.function_data(self.id).is_unsafe
682 }
683
684 pub fn diagnostics(self, db: &dyn HirDatabase, sink: &mut DiagnosticSink) {
685 hir_ty::diagnostics::validate_body(db, self.id.into(), sink)
686 }
687}
688
689impl HasVisibility for Function {
690 fn visibility(&self, db: &dyn HirDatabase) -> Visibility {
691 let function_data = db.function_data(self.id);
692 let visibility = &function_data.visibility;
693 visibility.resolve(db.upcast(), &self.id.resolver(db.upcast()))
694 }
695}
696
697#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
698pub struct Const {
699 pub(crate) id: ConstId,
700}
701
702impl Const {
703 pub fn module(self, db: &dyn HirDatabase) -> Module {
704 Module { id: self.id.lookup(db.upcast()).module(db.upcast()) }
705 }
706
707 pub fn krate(self, db: &dyn HirDatabase) -> Option<Crate> {
708 Some(self.module(db).krate())
709 }
710
711 pub fn name(self, db: &dyn HirDatabase) -> Option<Name> {
712 db.const_data(self.id).name.clone()
713 }
714}
715
716impl HasVisibility for Const {
717 fn visibility(&self, db: &dyn HirDatabase) -> Visibility {
718 let function_data = db.const_data(self.id);
719 let visibility = &function_data.visibility;
720 visibility.resolve(db.upcast(), &self.id.resolver(db.upcast()))
721 }
722}
723
724#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
725pub struct Static {
726 pub(crate) id: StaticId,
727}
728
729impl Static {
730 pub fn module(self, db: &dyn HirDatabase) -> Module {
731 Module { id: self.id.lookup(db.upcast()).module(db.upcast()) }
732 }
733
734 pub fn krate(self, db: &dyn HirDatabase) -> Option<Crate> {
735 Some(self.module(db).krate())
736 }
737
738 pub fn name(self, db: &dyn HirDatabase) -> Option<Name> {
739 db.static_data(self.id).name.clone()
740 }
741
742 pub fn is_mut(self, db: &dyn HirDatabase) -> bool {
743 db.static_data(self.id).mutable
744 }
745}
746
747#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
748pub struct Trait {
749 pub(crate) id: TraitId,
750}
751
752impl Trait {
753 pub fn module(self, db: &dyn HirDatabase) -> Module {
754 Module { id: self.id.lookup(db.upcast()).container.module(db.upcast()) }
755 }
756
757 pub fn name(self, db: &dyn HirDatabase) -> Name {
758 db.trait_data(self.id).name.clone()
759 }
760
761 pub fn items(self, db: &dyn HirDatabase) -> Vec<AssocItem> {
762 db.trait_data(self.id).items.iter().map(|(_name, it)| (*it).into()).collect()
763 }
764
765 pub fn is_auto(self, db: &dyn HirDatabase) -> bool {
766 db.trait_data(self.id).auto
767 }
768}
769
770#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
771pub struct TypeAlias {
772 pub(crate) id: TypeAliasId,
773}
774
775impl TypeAlias {
776 pub fn has_non_default_type_params(self, db: &dyn HirDatabase) -> bool {
777 let subst = db.generic_defaults(self.id.into());
778 subst.iter().any(|ty| &ty.value == &Ty::Unknown)
779 }
780
781 pub fn module(self, db: &dyn HirDatabase) -> Module {
782 Module { id: self.id.lookup(db.upcast()).module(db.upcast()) }
783 }
784
785 pub fn krate(self, db: &dyn HirDatabase) -> Option<Crate> {
786 Some(self.module(db).krate())
787 }
788
789 pub fn type_ref(self, db: &dyn HirDatabase) -> Option<TypeRef> {
790 db.type_alias_data(self.id).type_ref.clone()
791 }
792
793 pub fn ty(self, db: &dyn HirDatabase) -> Type {
794 Type::from_def(db, self.id.lookup(db.upcast()).module(db.upcast()).krate, self.id)
795 }
796
797 pub fn name(self, db: &dyn HirDatabase) -> Name {
798 db.type_alias_data(self.id).name.clone()
799 }
800}
801
802impl HasVisibility for TypeAlias {
803 fn visibility(&self, db: &dyn HirDatabase) -> Visibility {
804 let function_data = db.type_alias_data(self.id);
805 let visibility = &function_data.visibility;
806 visibility.resolve(db.upcast(), &self.id.resolver(db.upcast()))
807 }
808}
809
810#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
811pub struct MacroDef {
812 pub(crate) id: MacroDefId,
813}
814
815impl MacroDef {
816 /// FIXME: right now, this just returns the root module of the crate that
817 /// defines this macro. The reasons for this is that macros are expanded
818 /// early, in `hir_expand`, where modules simply do not exist yet.
819 pub fn module(self, db: &dyn HirDatabase) -> Option<Module> {
820 let krate = self.id.krate?;
821 let module_id = db.crate_def_map(krate).root;
822 Some(Module::new(Crate { id: krate }, module_id))
823 }
824
825 /// XXX: this parses the file
826 pub fn name(self, db: &dyn HirDatabase) -> Option<Name> {
827 self.source(db).value.name().map(|it| it.as_name())
828 }
829
830 /// Indicate it is a proc-macro
831 pub fn is_proc_macro(&self) -> bool {
832 matches!(self.id.kind, MacroDefKind::CustomDerive(_))
833 }
834
835 /// Indicate it is a derive macro
836 pub fn is_derive_macro(&self) -> bool {
837 matches!(self.id.kind, MacroDefKind::CustomDerive(_) | MacroDefKind::BuiltInDerive(_))
838 }
839}
840
841/// Invariant: `inner.as_assoc_item(db).is_some()`
842/// We do not actively enforce this invariant.
843#[derive(Debug, Copy, Clone, PartialEq, Eq, Hash)]
844pub enum AssocItem {
845 Function(Function),
846 Const(Const),
847 TypeAlias(TypeAlias),
848}
849pub enum AssocItemContainer {
850 Trait(Trait),
851 ImplDef(ImplDef),
852}
853pub trait AsAssocItem {
854 fn as_assoc_item(self, db: &dyn HirDatabase) -> Option<AssocItem>;
855}
856
857impl AsAssocItem for Function {
858 fn as_assoc_item(self, db: &dyn HirDatabase) -> Option<AssocItem> {
859 as_assoc_item(db, AssocItem::Function, self.id)
860 }
861}
862impl AsAssocItem for Const {
863 fn as_assoc_item(self, db: &dyn HirDatabase) -> Option<AssocItem> {
864 as_assoc_item(db, AssocItem::Const, self.id)
865 }
866}
867impl AsAssocItem for TypeAlias {
868 fn as_assoc_item(self, db: &dyn HirDatabase) -> Option<AssocItem> {
869 as_assoc_item(db, AssocItem::TypeAlias, self.id)
870 }
871}
872fn as_assoc_item<ID, DEF, CTOR, AST>(db: &dyn HirDatabase, ctor: CTOR, id: ID) -> Option<AssocItem>
873where
874 ID: Lookup<Data = AssocItemLoc<AST>>,
875 DEF: From<ID>,
876 CTOR: FnOnce(DEF) -> AssocItem,
877 AST: ItemTreeNode,
878{
879 match id.lookup(db.upcast()).container {
880 AssocContainerId::TraitId(_) | AssocContainerId::ImplId(_) => Some(ctor(DEF::from(id))),
881 AssocContainerId::ContainerId(_) => None,
882 }
883}
884
885impl AssocItem {
886 pub fn module(self, db: &dyn HirDatabase) -> Module {
887 match self {
888 AssocItem::Function(f) => f.module(db),
889 AssocItem::Const(c) => c.module(db),
890 AssocItem::TypeAlias(t) => t.module(db),
891 }
892 }
893 pub fn container(self, db: &dyn HirDatabase) -> AssocItemContainer {
894 let container = match self {
895 AssocItem::Function(it) => it.id.lookup(db.upcast()).container,
896 AssocItem::Const(it) => it.id.lookup(db.upcast()).container,
897 AssocItem::TypeAlias(it) => it.id.lookup(db.upcast()).container,
898 };
899 match container {
900 AssocContainerId::TraitId(id) => AssocItemContainer::Trait(id.into()),
901 AssocContainerId::ImplId(id) => AssocItemContainer::ImplDef(id.into()),
902 AssocContainerId::ContainerId(_) => panic!("invalid AssocItem"),
903 }
904 }
905}
906
907impl HasVisibility for AssocItem {
908 fn visibility(&self, db: &dyn HirDatabase) -> Visibility {
909 match self {
910 AssocItem::Function(f) => f.visibility(db),
911 AssocItem::Const(c) => c.visibility(db),
912 AssocItem::TypeAlias(t) => t.visibility(db),
913 }
914 }
915}
916
917#[derive(Clone, Copy, PartialEq, Eq, Debug, Hash)]
918pub enum GenericDef {
919 Function(Function),
920 Adt(Adt),
921 Trait(Trait),
922 TypeAlias(TypeAlias),
923 ImplDef(ImplDef),
924 // enum variants cannot have generics themselves, but their parent enums
925 // can, and this makes some code easier to write
926 EnumVariant(EnumVariant),
927 // consts can have type parameters from their parents (i.e. associated consts of traits)
928 Const(Const),
929}
930impl_from!(
931 Function,
932 Adt(Struct, Enum, Union),
933 Trait,
934 TypeAlias,
935 ImplDef,
936 EnumVariant,
937 Const
938 for GenericDef
939);
940
941impl GenericDef {
942 pub fn params(self, db: &dyn HirDatabase) -> Vec<TypeParam> {
943 let generics: Arc<hir_def::generics::GenericParams> = db.generic_params(self.into());
944 generics
945 .types
946 .iter()
947 .map(|(local_id, _)| TypeParam { id: TypeParamId { parent: self.into(), local_id } })
948 .collect()
949 }
950}
951
952#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)]
953pub struct Local {
954 pub(crate) parent: DefWithBodyId,
955 pub(crate) pat_id: PatId,
956}
957
958impl Local {
959 pub fn is_param(self, db: &dyn HirDatabase) -> bool {
960 let src = self.source(db);
961 match src.value {
962 Either::Left(bind_pat) => {
963 bind_pat.syntax().ancestors().any(|it| ast::Param::can_cast(it.kind()))
964 }
965 Either::Right(_self_param) => true,
966 }
967 }
968
969 // FIXME: why is this an option? It shouldn't be?
970 pub fn name(self, db: &dyn HirDatabase) -> Option<Name> {
971 let body = db.body(self.parent.into());
972 match &body[self.pat_id] {
973 Pat::Bind { name, .. } => Some(name.clone()),
974 _ => None,
975 }
976 }
977
978 pub fn is_self(self, db: &dyn HirDatabase) -> bool {
979 self.name(db) == Some(name![self])
980 }
981
982 pub fn is_mut(self, db: &dyn HirDatabase) -> bool {
983 let body = db.body(self.parent.into());
984 match &body[self.pat_id] {
985 Pat::Bind { mode, .. } => match mode {
986 BindingAnnotation::Mutable | BindingAnnotation::RefMut => true,
987 _ => false,
988 },
989 _ => false,
990 }
991 }
992
993 pub fn parent(self, _db: &dyn HirDatabase) -> DefWithBody {
994 self.parent.into()
995 }
996
997 pub fn module(self, db: &dyn HirDatabase) -> Module {
998 self.parent(db).module(db)
999 }
1000
1001 pub fn ty(self, db: &dyn HirDatabase) -> Type {
1002 let def = DefWithBodyId::from(self.parent);
1003 let infer = db.infer(def);
1004 let ty = infer[self.pat_id].clone();
1005 let krate = def.module(db.upcast()).krate;
1006 Type::new(db, krate, def, ty)
1007 }
1008
1009 pub fn source(self, db: &dyn HirDatabase) -> InFile<Either<ast::IdentPat, ast::SelfParam>> {
1010 let (_body, source_map) = db.body_with_source_map(self.parent.into());
1011 let src = source_map.pat_syntax(self.pat_id).unwrap(); // Hmm...
1012 let root = src.file_syntax(db.upcast());
1013 src.map(|ast| {
1014 ast.map_left(|it| it.cast().unwrap().to_node(&root)).map_right(|it| it.to_node(&root))
1015 })
1016 }
1017}
1018
1019#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)]
1020pub struct TypeParam {
1021 pub(crate) id: TypeParamId,
1022}
1023
1024impl TypeParam {
1025 pub fn name(self, db: &dyn HirDatabase) -> Name {
1026 let params = db.generic_params(self.id.parent);
1027 params.types[self.id.local_id].name.clone().unwrap_or_else(Name::missing)
1028 }
1029
1030 pub fn module(self, db: &dyn HirDatabase) -> Module {
1031 self.id.parent.module(db.upcast()).into()
1032 }
1033
1034 pub fn ty(self, db: &dyn HirDatabase) -> Type {
1035 let resolver = self.id.parent.resolver(db.upcast());
1036 let environment = TraitEnvironment::lower(db, &resolver);
1037 let ty = Ty::Placeholder(self.id);
1038 Type {
1039 krate: self.id.parent.module(db.upcast()).krate,
1040 ty: InEnvironment { value: ty, environment },
1041 }
1042 }
1043
1044 pub fn default(self, db: &dyn HirDatabase) -> Option<Type> {
1045 let params = db.generic_defaults(self.id.parent);
1046 let local_idx = hir_ty::param_idx(db, self.id)?;
1047 let resolver = self.id.parent.resolver(db.upcast());
1048 let environment = TraitEnvironment::lower(db, &resolver);
1049 let ty = params.get(local_idx)?.clone();
1050 let subst = Substs::type_params(db, self.id.parent);
1051 let ty = ty.subst(&subst.prefix(local_idx));
1052 Some(Type {
1053 krate: self.id.parent.module(db.upcast()).krate,
1054 ty: InEnvironment { value: ty, environment },
1055 })
1056 }
1057}
1058
1059// FIXME: rename from `ImplDef` to `Impl`
1060#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
1061pub struct ImplDef {
1062 pub(crate) id: ImplId,
1063}
1064
1065impl ImplDef {
1066 pub fn all_in_crate(db: &dyn HirDatabase, krate: Crate) -> Vec<ImplDef> {
1067 let inherent = db.inherent_impls_in_crate(krate.id);
1068 let trait_ = db.trait_impls_in_crate(krate.id);
1069
1070 inherent.all_impls().chain(trait_.all_impls()).map(Self::from).collect()
1071 }
1072 pub fn for_trait(db: &dyn HirDatabase, krate: Crate, trait_: Trait) -> Vec<ImplDef> {
1073 let impls = db.trait_impls_in_crate(krate.id);
1074 impls.for_trait(trait_.id).map(Self::from).collect()
1075 }
1076
1077 pub fn target_trait(self, db: &dyn HirDatabase) -> Option<TypeRef> {
1078 db.impl_data(self.id).target_trait.clone()
1079 }
1080
1081 pub fn target_type(self, db: &dyn HirDatabase) -> TypeRef {
1082 db.impl_data(self.id).target_type.clone()
1083 }
1084
1085 pub fn target_ty(self, db: &dyn HirDatabase) -> Type {
1086 let impl_data = db.impl_data(self.id);
1087 let resolver = self.id.resolver(db.upcast());
1088 let ctx = hir_ty::TyLoweringContext::new(db, &resolver);
1089 let environment = TraitEnvironment::lower(db, &resolver);
1090 let ty = Ty::from_hir(&ctx, &impl_data.target_type);
1091 Type {
1092 krate: self.id.lookup(db.upcast()).container.module(db.upcast()).krate,
1093 ty: InEnvironment { value: ty, environment },
1094 }
1095 }
1096
1097 pub fn items(self, db: &dyn HirDatabase) -> Vec<AssocItem> {
1098 db.impl_data(self.id).items.iter().map(|it| (*it).into()).collect()
1099 }
1100
1101 pub fn is_negative(self, db: &dyn HirDatabase) -> bool {
1102 db.impl_data(self.id).is_negative
1103 }
1104
1105 pub fn module(self, db: &dyn HirDatabase) -> Module {
1106 self.id.lookup(db.upcast()).container.module(db.upcast()).into()
1107 }
1108
1109 pub fn krate(self, db: &dyn HirDatabase) -> Crate {
1110 Crate { id: self.module(db).id.krate }
1111 }
1112
1113 pub fn is_builtin_derive(self, db: &dyn HirDatabase) -> Option<InFile<ast::Attr>> {
1114 let src = self.source(db);
1115 let item = src.file_id.is_builtin_derive(db.upcast())?;
1116 let hygenic = hir_expand::hygiene::Hygiene::new(db.upcast(), item.file_id);
1117
1118 let attr = item
1119 .value
1120 .attrs()
1121 .filter_map(|it| {
1122 let path = hir_def::path::ModPath::from_src(it.path()?, &hygenic)?;
1123 if path.as_ident()?.to_string() == "derive" {
1124 Some(it)
1125 } else {
1126 None
1127 }
1128 })
1129 .last()?;
1130
1131 Some(item.with_value(attr))
1132 }
1133}
1134
1135#[derive(Clone, PartialEq, Eq, Debug)]
1136pub struct Type {
1137 krate: CrateId,
1138 ty: InEnvironment<Ty>,
1139}
1140
1141impl Type {
1142 pub(crate) fn new_with_resolver(
1143 db: &dyn HirDatabase,
1144 resolver: &Resolver,
1145 ty: Ty,
1146 ) -> Option<Type> {
1147 let krate = resolver.krate()?;
1148 Some(Type::new_with_resolver_inner(db, krate, resolver, ty))
1149 }
1150 pub(crate) fn new_with_resolver_inner(
1151 db: &dyn HirDatabase,
1152 krate: CrateId,
1153 resolver: &Resolver,
1154 ty: Ty,
1155 ) -> Type {
1156 let environment = TraitEnvironment::lower(db, &resolver);
1157 Type { krate, ty: InEnvironment { value: ty, environment } }
1158 }
1159
1160 fn new(db: &dyn HirDatabase, krate: CrateId, lexical_env: impl HasResolver, ty: Ty) -> Type {
1161 let resolver = lexical_env.resolver(db.upcast());
1162 let environment = TraitEnvironment::lower(db, &resolver);
1163 Type { krate, ty: InEnvironment { value: ty, environment } }
1164 }
1165
1166 fn from_def(
1167 db: &dyn HirDatabase,
1168 krate: CrateId,
1169 def: impl HasResolver + Into<TyDefId> + Into<GenericDefId>,
1170 ) -> Type {
1171 let substs = Substs::build_for_def(db, def).fill_with_unknown().build();
1172 let ty = db.ty(def.into()).subst(&substs);
1173 Type::new(db, krate, def, ty)
1174 }
1175
1176 pub fn is_unit(&self) -> bool {
1177 matches!(
1178 self.ty.value,
1179 Ty::Apply(ApplicationTy { ctor: TypeCtor::Tuple { cardinality: 0 }, .. })
1180 )
1181 }
1182 pub fn is_bool(&self) -> bool {
1183 matches!(self.ty.value, Ty::Apply(ApplicationTy { ctor: TypeCtor::Bool, .. }))
1184 }
1185
1186 pub fn is_mutable_reference(&self) -> bool {
1187 matches!(
1188 self.ty.value,
1189 Ty::Apply(ApplicationTy { ctor: TypeCtor::Ref(Mutability::Mut), .. })
1190 )
1191 }
1192
1193 pub fn is_unknown(&self) -> bool {
1194 matches!(self.ty.value, Ty::Unknown)
1195 }
1196
1197 /// Checks that particular type `ty` implements `std::future::Future`.
1198 /// This function is used in `.await` syntax completion.
1199 pub fn impls_future(&self, db: &dyn HirDatabase) -> bool {
1200 let krate = self.krate;
1201
1202 let std_future_trait =
1203 db.lang_item(krate, "future_trait".into()).and_then(|it| it.as_trait());
1204 let std_future_trait = match std_future_trait {
1205 Some(it) => it,
1206 None => return false,
1207 };
1208
1209 let canonical_ty = Canonical { value: self.ty.value.clone(), kinds: Arc::new([]) };
1210 method_resolution::implements_trait(
1211 &canonical_ty,
1212 db,
1213 self.ty.environment.clone(),
1214 krate,
1215 std_future_trait,
1216 )
1217 }
1218
1219 pub fn impls_trait(&self, db: &dyn HirDatabase, trait_: Trait, args: &[Type]) -> bool {
1220 let trait_ref = hir_ty::TraitRef {
1221 trait_: trait_.id,
1222 substs: Substs::build_for_def(db, trait_.id)
1223 .push(self.ty.value.clone())
1224 .fill(args.iter().map(|t| t.ty.value.clone()))
1225 .build(),
1226 };
1227
1228 let goal = Canonical {
1229 value: hir_ty::InEnvironment::new(
1230 self.ty.environment.clone(),
1231 hir_ty::Obligation::Trait(trait_ref),
1232 ),
1233 kinds: Arc::new([]),
1234 };
1235
1236 db.trait_solve(self.krate, goal).is_some()
1237 }
1238
1239 pub fn as_callable(&self, db: &dyn HirDatabase) -> Option<Callable> {
1240 let def = match self.ty.value {
1241 Ty::Apply(ApplicationTy { ctor: TypeCtor::FnDef(def), parameters: _ }) => Some(def),
1242 _ => None,
1243 };
1244
1245 let sig = self.ty.value.callable_sig(db)?;
1246 Some(Callable { ty: self.clone(), sig, def, is_bound_method: false })
1247 }
1248
1249 pub fn is_closure(&self) -> bool {
1250 matches!(&self.ty.value, Ty::Apply(ApplicationTy { ctor: TypeCtor::Closure { .. }, .. }))
1251 }
1252
1253 pub fn is_fn(&self) -> bool {
1254 matches!(&self.ty.value,
1255 Ty::Apply(ApplicationTy { ctor: TypeCtor::FnDef(..), .. }) |
1256 Ty::Apply(ApplicationTy { ctor: TypeCtor::FnPtr { .. }, .. })
1257 )
1258 }
1259
1260 pub fn is_packed(&self, db: &dyn HirDatabase) -> bool {
1261 let adt_id = match self.ty.value {
1262 Ty::Apply(ApplicationTy { ctor: TypeCtor::Adt(adt_id), .. }) => adt_id,
1263 _ => return false,
1264 };
1265
1266 let adt = adt_id.into();
1267 match adt {
1268 Adt::Struct(s) => matches!(s.repr(db), Some(ReprKind::Packed)),
1269 _ => false,
1270 }
1271 }
1272
1273 pub fn is_raw_ptr(&self) -> bool {
1274 matches!(&self.ty.value, Ty::Apply(ApplicationTy { ctor: TypeCtor::RawPtr(..), .. }))
1275 }
1276
1277 pub fn contains_unknown(&self) -> bool {
1278 return go(&self.ty.value);
1279
1280 fn go(ty: &Ty) -> bool {
1281 match ty {
1282 Ty::Unknown => true,
1283 Ty::Apply(a_ty) => a_ty.parameters.iter().any(go),
1284 _ => false,
1285 }
1286 }
1287 }
1288
1289 pub fn fields(&self, db: &dyn HirDatabase) -> Vec<(Field, Type)> {
1290 if let Ty::Apply(a_ty) = &self.ty.value {
1291 let variant_id = match a_ty.ctor {
1292 TypeCtor::Adt(AdtId::StructId(s)) => s.into(),
1293 TypeCtor::Adt(AdtId::UnionId(u)) => u.into(),
1294 _ => return Vec::new(),
1295 };
1296
1297 return db
1298 .field_types(variant_id)
1299 .iter()
1300 .map(|(local_id, ty)| {
1301 let def = Field { parent: variant_id.into(), id: local_id };
1302 let ty = ty.clone().subst(&a_ty.parameters);
1303 (def, self.derived(ty))
1304 })
1305 .collect();
1306 };
1307 Vec::new()
1308 }
1309
1310 pub fn tuple_fields(&self, _db: &dyn HirDatabase) -> Vec<Type> {
1311 let mut res = Vec::new();
1312 if let Ty::Apply(a_ty) = &self.ty.value {
1313 if let TypeCtor::Tuple { .. } = a_ty.ctor {
1314 for ty in a_ty.parameters.iter() {
1315 let ty = ty.clone();
1316 res.push(self.derived(ty));
1317 }
1318 }
1319 };
1320 res
1321 }
1322
1323 pub fn autoderef<'a>(&'a self, db: &'a dyn HirDatabase) -> impl Iterator<Item = Type> + 'a {
1324 // There should be no inference vars in types passed here
1325 // FIXME check that?
1326 let canonical = Canonical { value: self.ty.value.clone(), kinds: Arc::new([]) };
1327 let environment = self.ty.environment.clone();
1328 let ty = InEnvironment { value: canonical, environment };
1329 autoderef(db, Some(self.krate), ty)
1330 .map(|canonical| canonical.value)
1331 .map(move |ty| self.derived(ty))
1332 }
1333
1334 // This would be nicer if it just returned an iterator, but that runs into
1335 // lifetime problems, because we need to borrow temp `CrateImplDefs`.
1336 pub fn iterate_assoc_items<T>(
1337 self,
1338 db: &dyn HirDatabase,
1339 krate: Crate,
1340 mut callback: impl FnMut(AssocItem) -> Option<T>,
1341 ) -> Option<T> {
1342 for krate in self.ty.value.def_crates(db, krate.id)? {
1343 let impls = db.inherent_impls_in_crate(krate);
1344
1345 for impl_def in impls.for_self_ty(&self.ty.value) {
1346 for &item in db.impl_data(*impl_def).items.iter() {
1347 if let Some(result) = callback(item.into()) {
1348 return Some(result);
1349 }
1350 }
1351 }
1352 }
1353 None
1354 }
1355
1356 pub fn iterate_method_candidates<T>(
1357 &self,
1358 db: &dyn HirDatabase,
1359 krate: Crate,
1360 traits_in_scope: &FxHashSet<TraitId>,
1361 name: Option<&Name>,
1362 mut callback: impl FnMut(&Ty, Function) -> Option<T>,
1363 ) -> Option<T> {
1364 // There should be no inference vars in types passed here
1365 // FIXME check that?
1366 // FIXME replace Unknown by bound vars here
1367 let canonical = Canonical { value: self.ty.value.clone(), kinds: Arc::new([]) };
1368
1369 let env = self.ty.environment.clone();
1370 let krate = krate.id;
1371
1372 method_resolution::iterate_method_candidates(
1373 &canonical,
1374 db,
1375 env,
1376 krate,
1377 traits_in_scope,
1378 name,
1379 method_resolution::LookupMode::MethodCall,
1380 |ty, it| match it {
1381 AssocItemId::FunctionId(f) => callback(ty, f.into()),
1382 _ => None,
1383 },
1384 )
1385 }
1386
1387 pub fn iterate_path_candidates<T>(
1388 &self,
1389 db: &dyn HirDatabase,
1390 krate: Crate,
1391 traits_in_scope: &FxHashSet<TraitId>,
1392 name: Option<&Name>,
1393 mut callback: impl FnMut(&Ty, AssocItem) -> Option<T>,
1394 ) -> Option<T> {
1395 // There should be no inference vars in types passed here
1396 // FIXME check that?
1397 // FIXME replace Unknown by bound vars here
1398 let canonical = Canonical { value: self.ty.value.clone(), kinds: Arc::new([]) };
1399
1400 let env = self.ty.environment.clone();
1401 let krate = krate.id;
1402
1403 method_resolution::iterate_method_candidates(
1404 &canonical,
1405 db,
1406 env,
1407 krate,
1408 traits_in_scope,
1409 name,
1410 method_resolution::LookupMode::Path,
1411 |ty, it| callback(ty, it.into()),
1412 )
1413 }
1414
1415 pub fn as_adt(&self) -> Option<Adt> {
1416 let (adt, _subst) = self.ty.value.as_adt()?;
1417 Some(adt.into())
1418 }
1419
1420 pub fn as_dyn_trait(&self) -> Option<Trait> {
1421 self.ty.value.dyn_trait().map(Into::into)
1422 }
1423
1424 pub fn as_impl_traits(&self, db: &dyn HirDatabase) -> Option<Vec<Trait>> {
1425 self.ty.value.impl_trait_bounds(db).map(|it| {
1426 it.into_iter()
1427 .filter_map(|pred| match pred {
1428 hir_ty::GenericPredicate::Implemented(trait_ref) => {
1429 Some(Trait::from(trait_ref.trait_))
1430 }
1431 _ => None,
1432 })
1433 .collect()
1434 })
1435 }
1436
1437 pub fn as_associated_type_parent_trait(&self, db: &dyn HirDatabase) -> Option<Trait> {
1438 self.ty.value.associated_type_parent_trait(db).map(Into::into)
1439 }
1440
1441 // FIXME: provide required accessors such that it becomes implementable from outside.
1442 pub fn is_equal_for_find_impls(&self, other: &Type) -> bool {
1443 match (&self.ty.value, &other.ty.value) {
1444 (Ty::Apply(a_original_ty), Ty::Apply(ApplicationTy { ctor, parameters })) => match ctor
1445 {
1446 TypeCtor::Ref(..) => match parameters.as_single() {
1447 Ty::Apply(a_ty) => a_original_ty.ctor == a_ty.ctor,
1448 _ => false,
1449 },
1450 _ => a_original_ty.ctor == *ctor,
1451 },
1452 _ => false,
1453 }
1454 }
1455
1456 fn derived(&self, ty: Ty) -> Type {
1457 Type {
1458 krate: self.krate,
1459 ty: InEnvironment { value: ty, environment: self.ty.environment.clone() },
1460 }
1461 }
1462
1463 pub fn walk(&self, db: &dyn HirDatabase, mut cb: impl FnMut(Type)) {
1464 // TypeWalk::walk for a Ty at first visits parameters and only after that the Ty itself.
1465 // We need a different order here.
1466
1467 fn walk_substs(
1468 db: &dyn HirDatabase,
1469 type_: &Type,
1470 substs: &Substs,
1471 cb: &mut impl FnMut(Type),
1472 ) {
1473 for ty in substs.iter() {
1474 walk_type(db, &type_.derived(ty.clone()), cb);
1475 }
1476 }
1477
1478 fn walk_bounds(
1479 db: &dyn HirDatabase,
1480 type_: &Type,
1481 bounds: &[GenericPredicate],
1482 cb: &mut impl FnMut(Type),
1483 ) {
1484 for pred in bounds {
1485 match pred {
1486 GenericPredicate::Implemented(trait_ref) => {
1487 cb(type_.clone());
1488 walk_substs(db, type_, &trait_ref.substs, cb);
1489 }
1490 _ => (),
1491 }
1492 }
1493 }
1494
1495 fn walk_type(db: &dyn HirDatabase, type_: &Type, cb: &mut impl FnMut(Type)) {
1496 let ty = type_.ty.value.strip_references();
1497 match ty {
1498 Ty::Apply(ApplicationTy { ctor, parameters }) => {
1499 match ctor {
1500 TypeCtor::Adt(_) => {
1501 cb(type_.derived(ty.clone()));
1502 }
1503 TypeCtor::AssociatedType(_) => {
1504 if let Some(_) = ty.associated_type_parent_trait(db) {
1505 cb(type_.derived(ty.clone()));
1506 }
1507 }
1508 _ => (),
1509 }
1510
1511 // adt params, tuples, etc...
1512 walk_substs(db, type_, parameters, cb);
1513 }
1514 Ty::Opaque(opaque_ty) => {
1515 if let Some(bounds) = ty.impl_trait_bounds(db) {
1516 walk_bounds(db, &type_.derived(ty.clone()), &bounds, cb);
1517 }
1518
1519 walk_substs(db, type_, &opaque_ty.parameters, cb);
1520 }
1521 Ty::Placeholder(_) => {
1522 if let Some(bounds) = ty.impl_trait_bounds(db) {
1523 walk_bounds(db, &type_.derived(ty.clone()), &bounds, cb);
1524 }
1525 }
1526 Ty::Dyn(bounds) => {
1527 walk_bounds(db, &type_.derived(ty.clone()), bounds.as_ref(), cb);
1528 }
1529
1530 _ => (),
1531 }
1532 }
1533
1534 walk_type(db, self, &mut cb);
1535 }
1536}
1537
1538impl HirDisplay for Type {
1539 fn hir_fmt(&self, f: &mut HirFormatter) -> Result<(), HirDisplayError> {
1540 self.ty.value.hir_fmt(f)
1541 }
1542}
1543
1544// FIXME: closures
1545#[derive(Debug)]
1546pub struct Callable {
1547 ty: Type,
1548 sig: FnSig,
1549 def: Option<CallableDefId>,
1550 pub(crate) is_bound_method: bool,
1551}
1552
1553pub enum CallableKind {
1554 Function(Function),
1555 TupleStruct(Struct),
1556 TupleEnumVariant(EnumVariant),
1557 Closure,
1558}
1559
1560impl Callable {
1561 pub fn kind(&self) -> CallableKind {
1562 match self.def {
1563 Some(CallableDefId::FunctionId(it)) => CallableKind::Function(it.into()),
1564 Some(CallableDefId::StructId(it)) => CallableKind::TupleStruct(it.into()),
1565 Some(CallableDefId::EnumVariantId(it)) => CallableKind::TupleEnumVariant(it.into()),
1566 None => CallableKind::Closure,
1567 }
1568 }
1569 pub fn receiver_param(&self, db: &dyn HirDatabase) -> Option<ast::SelfParam> {
1570 let func = match self.def {
1571 Some(CallableDefId::FunctionId(it)) if self.is_bound_method => it,
1572 _ => return None,
1573 };
1574 let src = func.lookup(db.upcast()).source(db.upcast());
1575 let param_list = src.value.param_list()?;
1576 param_list.self_param()
1577 }
1578 pub fn n_params(&self) -> usize {
1579 self.sig.params().len() - if self.is_bound_method { 1 } else { 0 }
1580 }
1581 pub fn params(
1582 &self,
1583 db: &dyn HirDatabase,
1584 ) -> Vec<(Option<Either<ast::SelfParam, ast::Pat>>, Type)> {
1585 let types = self
1586 .sig
1587 .params()
1588 .iter()
1589 .skip(if self.is_bound_method { 1 } else { 0 })
1590 .map(|ty| self.ty.derived(ty.clone()));
1591 let patterns = match self.def {
1592 Some(CallableDefId::FunctionId(func)) => {
1593 let src = func.lookup(db.upcast()).source(db.upcast());
1594 src.value.param_list().map(|param_list| {
1595 param_list
1596 .self_param()
1597 .map(|it| Some(Either::Left(it)))
1598 .filter(|_| !self.is_bound_method)
1599 .into_iter()
1600 .chain(param_list.params().map(|it| it.pat().map(Either::Right)))
1601 })
1602 }
1603 _ => None,
1604 };
1605 patterns.into_iter().flatten().chain(iter::repeat(None)).zip(types).collect()
1606 }
1607 pub fn return_type(&self) -> Type {
1608 self.ty.derived(self.sig.ret().clone())
1609 }
1610}
1611
1612/// For IDE only
1613#[derive(Debug)]
1614pub enum ScopeDef {
1615 ModuleDef(ModuleDef),
1616 MacroDef(MacroDef),
1617 GenericParam(TypeParam),
1618 ImplSelfType(ImplDef),
1619 AdtSelfType(Adt),
1620 Local(Local),
1621 Unknown,
1622}
1623
1624impl ScopeDef {
1625 pub fn all_items(def: PerNs) -> ArrayVec<[Self; 3]> {
1626 let mut items = ArrayVec::new();
1627
1628 match (def.take_types(), def.take_values()) {
1629 (Some(m1), None) => items.push(ScopeDef::ModuleDef(m1.into())),
1630 (None, Some(m2)) => items.push(ScopeDef::ModuleDef(m2.into())),
1631 (Some(m1), Some(m2)) => {
1632 // Some items, like unit structs and enum variants, are
1633 // returned as both a type and a value. Here we want
1634 // to de-duplicate them.
1635 if m1 != m2 {
1636 items.push(ScopeDef::ModuleDef(m1.into()));
1637 items.push(ScopeDef::ModuleDef(m2.into()));
1638 } else {
1639 items.push(ScopeDef::ModuleDef(m1.into()));
1640 }
1641 }
1642 (None, None) => {}
1643 };
1644
1645 if let Some(macro_def_id) = def.take_macros() {
1646 items.push(ScopeDef::MacroDef(macro_def_id.into()));
1647 }
1648
1649 if items.is_empty() {
1650 items.push(ScopeDef::Unknown);
1651 }
1652
1653 items
1654 }
1655}
1656
1657#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)]
1658pub enum AttrDef {
1659 Module(Module),
1660 Field(Field),
1661 Adt(Adt),
1662 Function(Function),
1663 EnumVariant(EnumVariant),
1664 Static(Static),
1665 Const(Const),
1666 Trait(Trait),
1667 TypeAlias(TypeAlias),
1668 MacroDef(MacroDef),
1669}
1670
1671impl_from!(
1672 Module,
1673 Field,
1674 Adt(Struct, Enum, Union),
1675 EnumVariant,
1676 Static,
1677 Const,
1678 Function,
1679 Trait,
1680 TypeAlias,
1681 MacroDef
1682 for AttrDef
1683);
1684
1685pub trait HasAttrs {
1686 fn attrs(self, db: &dyn HirDatabase) -> Attrs;
1687}
1688
1689impl<T: Into<AttrDef>> HasAttrs for T {
1690 fn attrs(self, db: &dyn HirDatabase) -> Attrs {
1691 let def: AttrDef = self.into();
1692 db.attrs(def.into())
1693 }
1694}
1695
1696pub trait Docs {
1697 fn docs(&self, db: &dyn HirDatabase) -> Option<Documentation>;
1698}
1699impl<T: Into<AttrDef> + Copy> Docs for T {
1700 fn docs(&self, db: &dyn HirDatabase) -> Option<Documentation> {
1701 let def: AttrDef = (*self).into();
1702 db.documentation(def.into())
1703 }
1704}
1705
1706pub trait HasVisibility {
1707 fn visibility(&self, db: &dyn HirDatabase) -> Visibility;
1708 fn is_visible_from(&self, db: &dyn HirDatabase, module: Module) -> bool {
1709 let vis = self.visibility(db);
1710 vis.is_visible_from(db.upcast(), module.id)
1711 }
1712}