]> git.lizzy.rs Git - rust.git/blob - crates/hir/src/lib.rs
Make replace_derive_with_manual_impl work again
[rust.git] / crates / hir / src / lib.rs
1 //! HIR (previously known as descriptors) provides a high-level object oriented
2 //! access to Rust code.
3 //!
4 //! The principal difference between HIR and syntax trees is that HIR is bound
5 //! to a particular crate instance. That is, it has cfg flags and features
6 //! applied. So, the relation between syntax and HIR is many-to-one.
7 //!
8 //! HIR is the public API of the all of the compiler logic above syntax trees.
9 //! It is written in "OO" style. Each type is self contained (as in, it knows it's
10 //! parents and full context). It should be "clean code".
11 //!
12 //! `hir_*` crates are the implementation of the compiler logic.
13 //! They are written in "ECS" style, with relatively little abstractions.
14 //! Many types are not self-contained, and explicitly use local indexes, arenas, etc.
15 //!
16 //! `hir` is what insulates the "we don't know how to actually write an incremental compiler"
17 //! from the ide with completions, hovers, etc. It is a (soft, internal) boundary:
18 //! <https://www.tedinski.com/2018/02/06/system-boundaries.html>.
19
20 #![recursion_limit = "512"]
21
22 mod semantics;
23 mod source_analyzer;
24
25 mod from_id;
26 mod attrs;
27 mod has_source;
28
29 pub mod diagnostics;
30 pub mod db;
31 pub mod symbols;
32
33 mod display;
34
35 use std::{collections::HashMap, iter, ops::ControlFlow, sync::Arc};
36
37 use arrayvec::ArrayVec;
38 use base_db::{CrateDisplayName, CrateId, CrateOrigin, Edition, FileId};
39 use either::Either;
40 use hir_def::{
41     adt::{ReprKind, VariantData},
42     body::{BodyDiagnostic, SyntheticSyntax},
43     expr::{BindingAnnotation, LabelId, Pat, PatId},
44     item_tree::ItemTreeNode,
45     lang_item::LangItemTarget,
46     nameres::{self, diagnostics::DefDiagnostic},
47     per_ns::PerNs,
48     resolver::{HasResolver, Resolver},
49     src::HasSource as _,
50     AdtId, AssocItemId, AssocItemLoc, AttrDefId, ConstId, ConstParamId, DefWithBodyId, EnumId,
51     FunctionId, GenericDefId, HasModule, ImplId, ItemContainerId, LifetimeParamId,
52     LocalEnumVariantId, LocalFieldId, Lookup, ModuleId, StaticId, StructId, TraitId, TypeAliasId,
53     TypeParamId, UnionId,
54 };
55 use hir_expand::{name::name, MacroCallKind, MacroDefId, MacroDefKind};
56 use hir_ty::{
57     autoderef,
58     consteval::{eval_const, ComputedExpr, ConstEvalCtx, ConstEvalError, ConstExt},
59     could_unify,
60     diagnostics::BodyValidationDiagnostic,
61     method_resolution::{self, TyFingerprint},
62     primitive::UintTy,
63     subst_prefix,
64     traits::FnTrait,
65     AliasEq, AliasTy, BoundVar, CallableDefId, CallableSig, Canonical, CanonicalVarKinds, Cast,
66     DebruijnIndex, InEnvironment, Interner, QuantifiedWhereClause, Scalar, Solution, Substitution,
67     TraitEnvironment, TraitRefExt, Ty, TyBuilder, TyDefId, TyExt, TyKind, TyVariableKind,
68     WhereClause,
69 };
70 use itertools::Itertools;
71 use nameres::diagnostics::DefDiagnosticKind;
72 use once_cell::unsync::Lazy;
73 use rustc_hash::FxHashSet;
74 use stdx::{format_to, impl_from};
75 use syntax::{
76     ast::{self, HasAttrs as _, HasDocComments, HasName},
77     AstNode, AstPtr, SmolStr, SyntaxNodePtr, T,
78 };
79 use tt::{Ident, Leaf, Literal, TokenTree};
80
81 use crate::db::{DefDatabase, HirDatabase};
82
83 pub use crate::{
84     attrs::{HasAttrs, Namespace},
85     diagnostics::{
86         AddReferenceHere, AnyDiagnostic, BreakOutsideOfLoop, InactiveCode, IncorrectCase,
87         InvalidDeriveTarget, MacroError, MalformedDerive, MismatchedArgCount, MissingFields,
88         MissingMatchArms, MissingOkOrSomeInTailExpr, MissingUnsafe, NoSuchField,
89         RemoveThisSemicolon, ReplaceFilterMapNextWithFindMap, UnimplementedBuiltinMacro,
90         UnresolvedExternCrate, UnresolvedImport, UnresolvedMacroCall, UnresolvedModule,
91         UnresolvedProcMacro,
92     },
93     has_source::HasSource,
94     semantics::{PathResolution, Semantics, SemanticsScope, TypeInfo},
95 };
96
97 // Be careful with these re-exports.
98 //
99 // `hir` is the boundary between the compiler and the IDE. It should try hard to
100 // isolate the compiler from the ide, to allow the two to be refactored
101 // independently. Re-exporting something from the compiler is the sure way to
102 // breach the boundary.
103 //
104 // Generally, a refactoring which *removes* a name from this list is a good
105 // idea!
106 pub use {
107     cfg::{CfgAtom, CfgExpr, CfgOptions},
108     hir_def::{
109         adt::StructKind,
110         attr::{Attr, Attrs, AttrsWithOwner, Documentation},
111         builtin_attr::AttributeTemplate,
112         find_path::PrefixKind,
113         import_map,
114         nameres::ModuleSource,
115         path::{ModPath, PathKind},
116         type_ref::{Mutability, TypeRef},
117         visibility::Visibility,
118     },
119     hir_expand::{
120         name::{known, Name},
121         ExpandResult, HirFileId, InFile, MacroFile, Origin,
122     },
123     hir_ty::display::HirDisplay,
124 };
125
126 // These are negative re-exports: pub using these names is forbidden, they
127 // should remain private to hir internals.
128 #[allow(unused)]
129 use {
130     hir_def::path::Path,
131     hir_expand::{hygiene::Hygiene, name::AsName},
132 };
133
134 /// hir::Crate describes a single crate. It's the main interface with which
135 /// a crate's dependencies interact. Mostly, it should be just a proxy for the
136 /// root module.
137 #[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
138 pub struct Crate {
139     pub(crate) id: CrateId,
140 }
141
142 #[derive(Debug)]
143 pub struct CrateDependency {
144     pub krate: Crate,
145     pub name: Name,
146 }
147
148 impl Crate {
149     pub fn origin(self, db: &dyn HirDatabase) -> CrateOrigin {
150         db.crate_graph()[self.id].origin.clone()
151     }
152
153     pub fn dependencies(self, db: &dyn HirDatabase) -> Vec<CrateDependency> {
154         db.crate_graph()[self.id]
155             .dependencies
156             .iter()
157             .map(|dep| {
158                 let krate = Crate { id: dep.crate_id };
159                 let name = dep.as_name();
160                 CrateDependency { krate, name }
161             })
162             .collect()
163     }
164
165     pub fn reverse_dependencies(self, db: &dyn HirDatabase) -> Vec<Crate> {
166         let crate_graph = db.crate_graph();
167         crate_graph
168             .iter()
169             .filter(|&krate| {
170                 crate_graph[krate].dependencies.iter().any(|it| it.crate_id == self.id)
171             })
172             .map(|id| Crate { id })
173             .collect()
174     }
175
176     pub fn transitive_reverse_dependencies(self, db: &dyn HirDatabase) -> Vec<Crate> {
177         db.crate_graph().transitive_rev_deps(self.id).into_iter().map(|id| Crate { id }).collect()
178     }
179
180     pub fn root_module(self, db: &dyn HirDatabase) -> Module {
181         let def_map = db.crate_def_map(self.id);
182         Module { id: def_map.module_id(def_map.root()) }
183     }
184
185     pub fn modules(self, db: &dyn HirDatabase) -> Vec<Module> {
186         let def_map = db.crate_def_map(self.id);
187         def_map.modules().map(|(id, _)| def_map.module_id(id).into()).collect()
188     }
189
190     pub fn root_file(self, db: &dyn HirDatabase) -> FileId {
191         db.crate_graph()[self.id].root_file_id
192     }
193
194     pub fn edition(self, db: &dyn HirDatabase) -> Edition {
195         db.crate_graph()[self.id].edition
196     }
197
198     pub fn version(self, db: &dyn HirDatabase) -> Option<String> {
199         db.crate_graph()[self.id].version.clone()
200     }
201
202     pub fn display_name(self, db: &dyn HirDatabase) -> Option<CrateDisplayName> {
203         db.crate_graph()[self.id].display_name.clone()
204     }
205
206     pub fn query_external_importables(
207         self,
208         db: &dyn DefDatabase,
209         query: import_map::Query,
210     ) -> impl Iterator<Item = Either<ModuleDef, MacroDef>> {
211         let _p = profile::span("query_external_importables");
212         import_map::search_dependencies(db, self.into(), query).into_iter().map(|item| {
213             match ItemInNs::from(item) {
214                 ItemInNs::Types(mod_id) | ItemInNs::Values(mod_id) => Either::Left(mod_id),
215                 ItemInNs::Macros(mac_id) => Either::Right(mac_id),
216             }
217         })
218     }
219
220     pub fn all(db: &dyn HirDatabase) -> Vec<Crate> {
221         db.crate_graph().iter().map(|id| Crate { id }).collect()
222     }
223
224     /// Try to get the root URL of the documentation of a crate.
225     pub fn get_html_root_url(self: &Crate, db: &dyn HirDatabase) -> Option<String> {
226         // Look for #![doc(html_root_url = "...")]
227         let attrs = db.attrs(AttrDefId::ModuleId(self.root_module(db).into()));
228         let doc_attr_q = attrs.by_key("doc");
229
230         if !doc_attr_q.exists() {
231             return None;
232         }
233
234         let doc_url = doc_attr_q.tt_values().map(|tt| {
235             let name = tt.token_trees.iter()
236                 .skip_while(|tt| !matches!(tt, TokenTree::Leaf(Leaf::Ident(Ident { text, ..} )) if text == "html_root_url"))
237                 .nth(2);
238
239             match name {
240                 Some(TokenTree::Leaf(Leaf::Literal(Literal{ref text, ..}))) => Some(text),
241                 _ => None
242             }
243         }).flatten().next();
244
245         doc_url.map(|s| s.trim_matches('"').trim_end_matches('/').to_owned() + "/")
246     }
247
248     pub fn cfg(&self, db: &dyn HirDatabase) -> CfgOptions {
249         db.crate_graph()[self.id].cfg_options.clone()
250     }
251
252     pub fn potential_cfg(&self, db: &dyn HirDatabase) -> CfgOptions {
253         db.crate_graph()[self.id].potential_cfg_options.clone()
254     }
255 }
256
257 #[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
258 pub struct Module {
259     pub(crate) id: ModuleId,
260 }
261
262 /// The defs which can be visible in the module.
263 #[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
264 pub enum ModuleDef {
265     Module(Module),
266     Function(Function),
267     Adt(Adt),
268     // Can't be directly declared, but can be imported.
269     Variant(Variant),
270     Const(Const),
271     Static(Static),
272     Trait(Trait),
273     TypeAlias(TypeAlias),
274     BuiltinType(BuiltinType),
275 }
276 impl_from!(
277     Module,
278     Function,
279     Adt(Struct, Enum, Union),
280     Variant,
281     Const,
282     Static,
283     Trait,
284     TypeAlias,
285     BuiltinType
286     for ModuleDef
287 );
288
289 impl From<VariantDef> for ModuleDef {
290     fn from(var: VariantDef) -> Self {
291         match var {
292             VariantDef::Struct(t) => Adt::from(t).into(),
293             VariantDef::Union(t) => Adt::from(t).into(),
294             VariantDef::Variant(t) => t.into(),
295         }
296     }
297 }
298
299 impl ModuleDef {
300     pub fn module(self, db: &dyn HirDatabase) -> Option<Module> {
301         match self {
302             ModuleDef::Module(it) => it.parent(db),
303             ModuleDef::Function(it) => Some(it.module(db)),
304             ModuleDef::Adt(it) => Some(it.module(db)),
305             ModuleDef::Variant(it) => Some(it.module(db)),
306             ModuleDef::Const(it) => Some(it.module(db)),
307             ModuleDef::Static(it) => Some(it.module(db)),
308             ModuleDef::Trait(it) => Some(it.module(db)),
309             ModuleDef::TypeAlias(it) => Some(it.module(db)),
310             ModuleDef::BuiltinType(_) => None,
311         }
312     }
313
314     pub fn canonical_path(&self, db: &dyn HirDatabase) -> Option<String> {
315         let mut segments = vec![self.name(db)?];
316         for m in self.module(db)?.path_to_root(db) {
317             segments.extend(m.name(db))
318         }
319         segments.reverse();
320         Some(segments.into_iter().join("::"))
321     }
322
323     pub fn canonical_module_path(
324         &self,
325         db: &dyn HirDatabase,
326     ) -> Option<impl Iterator<Item = Module>> {
327         self.module(db).map(|it| it.path_to_root(db).into_iter().rev())
328     }
329
330     pub fn name(self, db: &dyn HirDatabase) -> Option<Name> {
331         let name = match self {
332             ModuleDef::Module(it) => it.name(db)?,
333             ModuleDef::Const(it) => it.name(db)?,
334             ModuleDef::Adt(it) => it.name(db),
335             ModuleDef::Trait(it) => it.name(db),
336             ModuleDef::Function(it) => it.name(db),
337             ModuleDef::Variant(it) => it.name(db),
338             ModuleDef::TypeAlias(it) => it.name(db),
339             ModuleDef::Static(it) => it.name(db),
340             ModuleDef::BuiltinType(it) => it.name(),
341         };
342         Some(name)
343     }
344
345     pub fn diagnostics(self, db: &dyn HirDatabase) -> Vec<AnyDiagnostic> {
346         let id = match self {
347             ModuleDef::Adt(it) => match it {
348                 Adt::Struct(it) => it.id.into(),
349                 Adt::Enum(it) => it.id.into(),
350                 Adt::Union(it) => it.id.into(),
351             },
352             ModuleDef::Trait(it) => it.id.into(),
353             ModuleDef::Function(it) => it.id.into(),
354             ModuleDef::TypeAlias(it) => it.id.into(),
355             ModuleDef::Module(it) => it.id.into(),
356             ModuleDef::Const(it) => it.id.into(),
357             ModuleDef::Static(it) => it.id.into(),
358             _ => return Vec::new(),
359         };
360
361         let module = match self.module(db) {
362             Some(it) => it,
363             None => return Vec::new(),
364         };
365
366         let mut acc = Vec::new();
367
368         match self.as_def_with_body() {
369             Some(def) => {
370                 def.diagnostics(db, &mut acc);
371             }
372             None => {
373                 for diag in hir_ty::diagnostics::incorrect_case(db, module.id.krate(), id) {
374                     acc.push(diag.into())
375                 }
376             }
377         }
378
379         acc
380     }
381
382     pub fn as_def_with_body(self) -> Option<DefWithBody> {
383         match self {
384             ModuleDef::Function(it) => Some(it.into()),
385             ModuleDef::Const(it) => Some(it.into()),
386             ModuleDef::Static(it) => Some(it.into()),
387
388             ModuleDef::Module(_)
389             | ModuleDef::Adt(_)
390             | ModuleDef::Variant(_)
391             | ModuleDef::Trait(_)
392             | ModuleDef::TypeAlias(_)
393             | ModuleDef::BuiltinType(_) => None,
394         }
395     }
396
397     pub fn attrs(&self, db: &dyn HirDatabase) -> Option<AttrsWithOwner> {
398         Some(match self {
399             ModuleDef::Module(it) => it.attrs(db),
400             ModuleDef::Function(it) => it.attrs(db),
401             ModuleDef::Adt(it) => it.attrs(db),
402             ModuleDef::Variant(it) => it.attrs(db),
403             ModuleDef::Const(it) => it.attrs(db),
404             ModuleDef::Static(it) => it.attrs(db),
405             ModuleDef::Trait(it) => it.attrs(db),
406             ModuleDef::TypeAlias(it) => it.attrs(db),
407             ModuleDef::BuiltinType(_) => return None,
408         })
409     }
410 }
411
412 impl HasVisibility for ModuleDef {
413     fn visibility(&self, db: &dyn HirDatabase) -> Visibility {
414         match *self {
415             ModuleDef::Module(it) => it.visibility(db),
416             ModuleDef::Function(it) => it.visibility(db),
417             ModuleDef::Adt(it) => it.visibility(db),
418             ModuleDef::Const(it) => it.visibility(db),
419             ModuleDef::Static(it) => it.visibility(db),
420             ModuleDef::Trait(it) => it.visibility(db),
421             ModuleDef::TypeAlias(it) => it.visibility(db),
422             ModuleDef::Variant(it) => it.visibility(db),
423             ModuleDef::BuiltinType(_) => Visibility::Public,
424         }
425     }
426 }
427
428 impl Module {
429     /// Name of this module.
430     pub fn name(self, db: &dyn HirDatabase) -> Option<Name> {
431         let def_map = self.id.def_map(db.upcast());
432         let parent = def_map[self.id.local_id].parent?;
433         def_map[parent].children.iter().find_map(|(name, module_id)| {
434             if *module_id == self.id.local_id {
435                 Some(name.clone())
436             } else {
437                 None
438             }
439         })
440     }
441
442     /// Returns the crate this module is part of.
443     pub fn krate(self) -> Crate {
444         Crate { id: self.id.krate() }
445     }
446
447     /// Topmost parent of this module. Every module has a `crate_root`, but some
448     /// might be missing `krate`. This can happen if a module's file is not included
449     /// in the module tree of any target in `Cargo.toml`.
450     pub fn crate_root(self, db: &dyn HirDatabase) -> Module {
451         let def_map = db.crate_def_map(self.id.krate());
452         Module { id: def_map.module_id(def_map.root()) }
453     }
454
455     pub fn is_crate_root(self, db: &dyn HirDatabase) -> bool {
456         let def_map = db.crate_def_map(self.id.krate());
457         def_map.root() == self.id.local_id
458     }
459
460     /// Iterates over all child modules.
461     pub fn children(self, db: &dyn HirDatabase) -> impl Iterator<Item = Module> {
462         let def_map = self.id.def_map(db.upcast());
463         let children = def_map[self.id.local_id]
464             .children
465             .iter()
466             .map(|(_, module_id)| Module { id: def_map.module_id(*module_id) })
467             .collect::<Vec<_>>();
468         children.into_iter()
469     }
470
471     /// Finds a parent module.
472     pub fn parent(self, db: &dyn HirDatabase) -> Option<Module> {
473         // FIXME: handle block expressions as modules (their parent is in a different DefMap)
474         let def_map = self.id.def_map(db.upcast());
475         let parent_id = def_map[self.id.local_id].parent?;
476         Some(Module { id: def_map.module_id(parent_id) })
477     }
478
479     pub fn path_to_root(self, db: &dyn HirDatabase) -> Vec<Module> {
480         let mut res = vec![self];
481         let mut curr = self;
482         while let Some(next) = curr.parent(db) {
483             res.push(next);
484             curr = next
485         }
486         res
487     }
488
489     /// Returns a `ModuleScope`: a set of items, visible in this module.
490     pub fn scope(
491         self,
492         db: &dyn HirDatabase,
493         visible_from: Option<Module>,
494     ) -> Vec<(Name, ScopeDef)> {
495         self.id.def_map(db.upcast())[self.id.local_id]
496             .scope
497             .entries()
498             .filter_map(|(name, def)| {
499                 if let Some(m) = visible_from {
500                     let filtered =
501                         def.filter_visibility(|vis| vis.is_visible_from(db.upcast(), m.id));
502                     if filtered.is_none() && !def.is_none() {
503                         None
504                     } else {
505                         Some((name, filtered))
506                     }
507                 } else {
508                     Some((name, def))
509                 }
510             })
511             .flat_map(|(name, def)| {
512                 ScopeDef::all_items(def).into_iter().map(move |item| (name.clone(), item))
513             })
514             .collect()
515     }
516
517     pub fn diagnostics(self, db: &dyn HirDatabase, acc: &mut Vec<AnyDiagnostic>) {
518         let _p = profile::span("Module::diagnostics").detail(|| {
519             format!("{:?}", self.name(db).map_or("<unknown>".into(), |name| name.to_string()))
520         });
521         let def_map = self.id.def_map(db.upcast());
522         for diag in def_map.diagnostics() {
523             if diag.in_module != self.id.local_id {
524                 // FIXME: This is accidentally quadratic.
525                 continue;
526             }
527             emit_def_diagnostic(db, acc, diag);
528         }
529         for decl in self.declarations(db) {
530             match decl {
531                 ModuleDef::Module(m) => {
532                     // Only add diagnostics from inline modules
533                     if def_map[m.id.local_id].origin.is_inline() {
534                         m.diagnostics(db, acc)
535                     }
536                 }
537                 _ => acc.extend(decl.diagnostics(db)),
538             }
539         }
540
541         for impl_def in self.impl_defs(db) {
542             for item in impl_def.items(db) {
543                 let def: DefWithBody = match item {
544                     AssocItem::Function(it) => it.into(),
545                     AssocItem::Const(it) => it.into(),
546                     AssocItem::TypeAlias(_) => continue,
547                 };
548
549                 def.diagnostics(db, acc);
550             }
551         }
552     }
553
554     pub fn declarations(self, db: &dyn HirDatabase) -> Vec<ModuleDef> {
555         let def_map = self.id.def_map(db.upcast());
556         let scope = &def_map[self.id.local_id].scope;
557         scope
558             .declarations()
559             .map(ModuleDef::from)
560             .chain(scope.unnamed_consts().map(|id| ModuleDef::Const(Const::from(id))))
561             .collect()
562     }
563
564     pub fn impl_defs(self, db: &dyn HirDatabase) -> Vec<Impl> {
565         let def_map = self.id.def_map(db.upcast());
566         def_map[self.id.local_id].scope.impls().map(Impl::from).collect()
567     }
568
569     /// Finds a path that can be used to refer to the given item from within
570     /// this module, if possible.
571     pub fn find_use_path(self, db: &dyn DefDatabase, item: impl Into<ItemInNs>) -> Option<ModPath> {
572         hir_def::find_path::find_path(db, item.into().into(), self.into())
573     }
574
575     /// Finds a path that can be used to refer to the given item from within
576     /// this module, if possible. This is used for returning import paths for use-statements.
577     pub fn find_use_path_prefixed(
578         self,
579         db: &dyn DefDatabase,
580         item: impl Into<ItemInNs>,
581         prefix_kind: PrefixKind,
582     ) -> Option<ModPath> {
583         hir_def::find_path::find_path_prefixed(db, item.into().into(), self.into(), prefix_kind)
584     }
585 }
586
587 fn emit_def_diagnostic(db: &dyn HirDatabase, acc: &mut Vec<AnyDiagnostic>, diag: &DefDiagnostic) {
588     match &diag.kind {
589         DefDiagnosticKind::UnresolvedModule { ast: declaration, candidate } => {
590             let decl = declaration.to_node(db.upcast());
591             acc.push(
592                 UnresolvedModule {
593                     decl: InFile::new(declaration.file_id, AstPtr::new(&decl)),
594                     candidate: candidate.clone(),
595                 }
596                 .into(),
597             )
598         }
599         DefDiagnosticKind::UnresolvedExternCrate { ast } => {
600             let item = ast.to_node(db.upcast());
601             acc.push(
602                 UnresolvedExternCrate { decl: InFile::new(ast.file_id, AstPtr::new(&item)) }.into(),
603             );
604         }
605
606         DefDiagnosticKind::UnresolvedImport { id, index } => {
607             let file_id = id.file_id();
608             let item_tree = id.item_tree(db.upcast());
609             let import = &item_tree[id.value];
610
611             let use_tree = import.use_tree_to_ast(db.upcast(), file_id, *index);
612             acc.push(
613                 UnresolvedImport { decl: InFile::new(file_id, AstPtr::new(&use_tree)) }.into(),
614             );
615         }
616
617         DefDiagnosticKind::UnconfiguredCode { ast, cfg, opts } => {
618             let item = ast.to_node(db.upcast());
619             acc.push(
620                 InactiveCode {
621                     node: ast.with_value(AstPtr::new(&item).into()),
622                     cfg: cfg.clone(),
623                     opts: opts.clone(),
624                 }
625                 .into(),
626             );
627         }
628
629         DefDiagnosticKind::UnresolvedProcMacro { ast } => {
630             let mut precise_location = None;
631             let (node, macro_name) = match ast {
632                 MacroCallKind::FnLike { ast_id, .. } => {
633                     let node = ast_id.to_node(db.upcast());
634                     (ast_id.with_value(SyntaxNodePtr::from(AstPtr::new(&node))), None)
635                 }
636                 MacroCallKind::Derive { ast_id, derive_attr_index, derive_index } => {
637                     let node = ast_id.to_node(db.upcast());
638
639                     // Compute the precise location of the macro name's token in the derive
640                     // list.
641                     let token = (|| {
642                         let derive_attr = node.attrs().nth(*derive_attr_index as usize)?;
643                         derive_attr
644                             .syntax()
645                             .children_with_tokens()
646                             .filter_map(|elem| match elem {
647                                 syntax::NodeOrToken::Token(tok) => Some(tok),
648                                 _ => None,
649                             })
650                             .group_by(|t| t.kind() == T![,])
651                             .into_iter()
652                             .filter(|&(comma, _)| !comma)
653                             .nth(*derive_index as usize)
654                             .and_then(|(_, mut g)| g.find(|t| t.kind() == T![ident]))
655                     })();
656                     precise_location = token.as_ref().map(|tok| tok.text_range());
657                     (
658                         ast_id.with_value(SyntaxNodePtr::from(AstPtr::new(&node))),
659                         token.as_ref().map(ToString::to_string),
660                     )
661                 }
662                 MacroCallKind::Attr { ast_id, invoc_attr_index, .. } => {
663                     let node = ast_id.to_node(db.upcast());
664                     let attr = node
665                         .doc_comments_and_attrs()
666                         .nth((*invoc_attr_index) as usize)
667                         .and_then(Either::left)
668                         .unwrap_or_else(|| panic!("cannot find attribute #{}", invoc_attr_index));
669                     (
670                         ast_id.with_value(SyntaxNodePtr::from(AstPtr::new(&attr))),
671                         attr.path()
672                             .and_then(|path| path.segment())
673                             .and_then(|seg| seg.name_ref())
674                             .as_ref()
675                             .map(ToString::to_string),
676                     )
677                 }
678             };
679             acc.push(UnresolvedProcMacro { node, precise_location, macro_name }.into());
680         }
681
682         DefDiagnosticKind::UnresolvedMacroCall { ast, path } => {
683             let node = ast.to_node(db.upcast());
684             acc.push(
685                 UnresolvedMacroCall {
686                     macro_call: InFile::new(ast.file_id, AstPtr::new(&node)),
687                     path: path.clone(),
688                 }
689                 .into(),
690             );
691         }
692
693         DefDiagnosticKind::MacroError { ast, message } => {
694             let node = match ast {
695                 MacroCallKind::FnLike { ast_id, .. } => {
696                     let node = ast_id.to_node(db.upcast());
697                     ast_id.with_value(SyntaxNodePtr::from(AstPtr::new(&node)))
698                 }
699                 MacroCallKind::Derive { ast_id, .. } => {
700                     // FIXME: point to the attribute instead, this creates very large diagnostics
701                     let node = ast_id.to_node(db.upcast());
702                     ast_id.with_value(SyntaxNodePtr::from(AstPtr::new(&node)))
703                 }
704                 MacroCallKind::Attr { ast_id, .. } => {
705                     // FIXME: point to the attribute instead, this creates very large diagnostics
706                     let node = ast_id.to_node(db.upcast());
707                     ast_id.with_value(SyntaxNodePtr::from(AstPtr::new(&node)))
708                 }
709             };
710             acc.push(MacroError { node, message: message.clone() }.into());
711         }
712
713         DefDiagnosticKind::UnimplementedBuiltinMacro { ast } => {
714             let node = ast.to_node(db.upcast());
715             // Must have a name, otherwise we wouldn't emit it.
716             let name = node.name().expect("unimplemented builtin macro with no name");
717             acc.push(
718                 UnimplementedBuiltinMacro {
719                     node: ast.with_value(SyntaxNodePtr::from(AstPtr::new(&name))),
720                 }
721                 .into(),
722             );
723         }
724         DefDiagnosticKind::InvalidDeriveTarget { ast, id } => {
725             let node = ast.to_node(db.upcast());
726             let derive = node.attrs().nth(*id as usize);
727             match derive {
728                 Some(derive) => {
729                     acc.push(
730                         InvalidDeriveTarget {
731                             node: ast.with_value(SyntaxNodePtr::from(AstPtr::new(&derive))),
732                         }
733                         .into(),
734                     );
735                 }
736                 None => stdx::never!("derive diagnostic on item without derive attribute"),
737             }
738         }
739         DefDiagnosticKind::MalformedDerive { ast, id } => {
740             let node = ast.to_node(db.upcast());
741             let derive = node.attrs().nth(*id as usize);
742             match derive {
743                 Some(derive) => {
744                     acc.push(
745                         MalformedDerive {
746                             node: ast.with_value(SyntaxNodePtr::from(AstPtr::new(&derive))),
747                         }
748                         .into(),
749                     );
750                 }
751                 None => stdx::never!("derive diagnostic on item without derive attribute"),
752             }
753         }
754     }
755 }
756
757 impl HasVisibility for Module {
758     fn visibility(&self, db: &dyn HirDatabase) -> Visibility {
759         let def_map = self.id.def_map(db.upcast());
760         let module_data = &def_map[self.id.local_id];
761         module_data.visibility
762     }
763 }
764
765 #[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
766 pub struct Field {
767     pub(crate) parent: VariantDef,
768     pub(crate) id: LocalFieldId,
769 }
770
771 #[derive(Debug, PartialEq, Eq)]
772 pub enum FieldSource {
773     Named(ast::RecordField),
774     Pos(ast::TupleField),
775 }
776
777 impl Field {
778     pub fn name(&self, db: &dyn HirDatabase) -> Name {
779         self.parent.variant_data(db).fields()[self.id].name.clone()
780     }
781
782     /// Returns the type as in the signature of the struct (i.e., with
783     /// placeholder types for type parameters). Only use this in the context of
784     /// the field definition.
785     pub fn ty(&self, db: &dyn HirDatabase) -> Type {
786         let var_id = self.parent.into();
787         let generic_def_id: GenericDefId = match self.parent {
788             VariantDef::Struct(it) => it.id.into(),
789             VariantDef::Union(it) => it.id.into(),
790             VariantDef::Variant(it) => it.parent.id.into(),
791         };
792         let substs = TyBuilder::type_params_subst(db, generic_def_id);
793         let ty = db.field_types(var_id)[self.id].clone().substitute(Interner, &substs);
794         Type::new(db, self.parent.module(db).id.krate(), var_id, ty)
795     }
796
797     pub fn parent_def(&self, _db: &dyn HirDatabase) -> VariantDef {
798         self.parent
799     }
800 }
801
802 impl HasVisibility for Field {
803     fn visibility(&self, db: &dyn HirDatabase) -> Visibility {
804         let variant_data = self.parent.variant_data(db);
805         let visibility = &variant_data.fields()[self.id].visibility;
806         let parent_id: hir_def::VariantId = self.parent.into();
807         visibility.resolve(db.upcast(), &parent_id.resolver(db.upcast()))
808     }
809 }
810
811 #[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
812 pub struct Struct {
813     pub(crate) id: StructId,
814 }
815
816 impl Struct {
817     pub fn module(self, db: &dyn HirDatabase) -> Module {
818         Module { id: self.id.lookup(db.upcast()).container }
819     }
820
821     pub fn name(self, db: &dyn HirDatabase) -> Name {
822         db.struct_data(self.id).name.clone()
823     }
824
825     pub fn fields(self, db: &dyn HirDatabase) -> Vec<Field> {
826         db.struct_data(self.id)
827             .variant_data
828             .fields()
829             .iter()
830             .map(|(id, _)| Field { parent: self.into(), id })
831             .collect()
832     }
833
834     pub fn ty(self, db: &dyn HirDatabase) -> Type {
835         Type::from_def(db, self.id.lookup(db.upcast()).container.krate(), self.id)
836     }
837
838     pub fn repr(self, db: &dyn HirDatabase) -> Option<ReprKind> {
839         db.struct_data(self.id).repr.clone()
840     }
841
842     pub fn kind(self, db: &dyn HirDatabase) -> StructKind {
843         self.variant_data(db).kind()
844     }
845
846     fn variant_data(self, db: &dyn HirDatabase) -> Arc<VariantData> {
847         db.struct_data(self.id).variant_data.clone()
848     }
849 }
850
851 impl HasVisibility for Struct {
852     fn visibility(&self, db: &dyn HirDatabase) -> Visibility {
853         db.struct_data(self.id).visibility.resolve(db.upcast(), &self.id.resolver(db.upcast()))
854     }
855 }
856
857 #[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
858 pub struct Union {
859     pub(crate) id: UnionId,
860 }
861
862 impl Union {
863     pub fn name(self, db: &dyn HirDatabase) -> Name {
864         db.union_data(self.id).name.clone()
865     }
866
867     pub fn module(self, db: &dyn HirDatabase) -> Module {
868         Module { id: self.id.lookup(db.upcast()).container }
869     }
870
871     pub fn ty(self, db: &dyn HirDatabase) -> Type {
872         Type::from_def(db, self.id.lookup(db.upcast()).container.krate(), self.id)
873     }
874
875     pub fn fields(self, db: &dyn HirDatabase) -> Vec<Field> {
876         db.union_data(self.id)
877             .variant_data
878             .fields()
879             .iter()
880             .map(|(id, _)| Field { parent: self.into(), id })
881             .collect()
882     }
883
884     fn variant_data(self, db: &dyn HirDatabase) -> Arc<VariantData> {
885         db.union_data(self.id).variant_data.clone()
886     }
887 }
888
889 impl HasVisibility for Union {
890     fn visibility(&self, db: &dyn HirDatabase) -> Visibility {
891         db.union_data(self.id).visibility.resolve(db.upcast(), &self.id.resolver(db.upcast()))
892     }
893 }
894
895 #[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
896 pub struct Enum {
897     pub(crate) id: EnumId,
898 }
899
900 impl Enum {
901     pub fn module(self, db: &dyn HirDatabase) -> Module {
902         Module { id: self.id.lookup(db.upcast()).container }
903     }
904
905     pub fn name(self, db: &dyn HirDatabase) -> Name {
906         db.enum_data(self.id).name.clone()
907     }
908
909     pub fn variants(self, db: &dyn HirDatabase) -> Vec<Variant> {
910         db.enum_data(self.id).variants.iter().map(|(id, _)| Variant { parent: self, id }).collect()
911     }
912
913     pub fn ty(self, db: &dyn HirDatabase) -> Type {
914         Type::from_def(db, self.id.lookup(db.upcast()).container.krate(), self.id)
915     }
916 }
917
918 impl HasVisibility for Enum {
919     fn visibility(&self, db: &dyn HirDatabase) -> Visibility {
920         db.enum_data(self.id).visibility.resolve(db.upcast(), &self.id.resolver(db.upcast()))
921     }
922 }
923
924 #[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
925 pub struct Variant {
926     pub(crate) parent: Enum,
927     pub(crate) id: LocalEnumVariantId,
928 }
929
930 impl Variant {
931     pub fn module(self, db: &dyn HirDatabase) -> Module {
932         self.parent.module(db)
933     }
934
935     pub fn parent_enum(self, _db: &dyn HirDatabase) -> Enum {
936         self.parent
937     }
938
939     pub fn name(self, db: &dyn HirDatabase) -> Name {
940         db.enum_data(self.parent.id).variants[self.id].name.clone()
941     }
942
943     pub fn fields(self, db: &dyn HirDatabase) -> Vec<Field> {
944         self.variant_data(db)
945             .fields()
946             .iter()
947             .map(|(id, _)| Field { parent: self.into(), id })
948             .collect()
949     }
950
951     pub fn kind(self, db: &dyn HirDatabase) -> StructKind {
952         self.variant_data(db).kind()
953     }
954
955     pub(crate) fn variant_data(self, db: &dyn HirDatabase) -> Arc<VariantData> {
956         db.enum_data(self.parent.id).variants[self.id].variant_data.clone()
957     }
958 }
959
960 /// Variants inherit visibility from the parent enum.
961 impl HasVisibility for Variant {
962     fn visibility(&self, db: &dyn HirDatabase) -> Visibility {
963         self.parent_enum(db).visibility(db)
964     }
965 }
966
967 /// A Data Type
968 #[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)]
969 pub enum Adt {
970     Struct(Struct),
971     Union(Union),
972     Enum(Enum),
973 }
974 impl_from!(Struct, Union, Enum for Adt);
975
976 impl Adt {
977     pub fn has_non_default_type_params(self, db: &dyn HirDatabase) -> bool {
978         let subst = db.generic_defaults(self.into());
979         subst.iter().any(|ty| ty.skip_binders().is_unknown())
980     }
981
982     /// Turns this ADT into a type. Any type parameters of the ADT will be
983     /// turned into unknown types, which is good for e.g. finding the most
984     /// general set of completions, but will not look very nice when printed.
985     pub fn ty(self, db: &dyn HirDatabase) -> Type {
986         let id = AdtId::from(self);
987         Type::from_def(db, id.module(db.upcast()).krate(), id)
988     }
989
990     pub fn module(self, db: &dyn HirDatabase) -> Module {
991         match self {
992             Adt::Struct(s) => s.module(db),
993             Adt::Union(s) => s.module(db),
994             Adt::Enum(e) => e.module(db),
995         }
996     }
997
998     pub fn name(self, db: &dyn HirDatabase) -> Name {
999         match self {
1000             Adt::Struct(s) => s.name(db),
1001             Adt::Union(u) => u.name(db),
1002             Adt::Enum(e) => e.name(db),
1003         }
1004     }
1005 }
1006
1007 impl HasVisibility for Adt {
1008     fn visibility(&self, db: &dyn HirDatabase) -> Visibility {
1009         match self {
1010             Adt::Struct(it) => it.visibility(db),
1011             Adt::Union(it) => it.visibility(db),
1012             Adt::Enum(it) => it.visibility(db),
1013         }
1014     }
1015 }
1016
1017 #[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)]
1018 pub enum VariantDef {
1019     Struct(Struct),
1020     Union(Union),
1021     Variant(Variant),
1022 }
1023 impl_from!(Struct, Union, Variant for VariantDef);
1024
1025 impl VariantDef {
1026     pub fn fields(self, db: &dyn HirDatabase) -> Vec<Field> {
1027         match self {
1028             VariantDef::Struct(it) => it.fields(db),
1029             VariantDef::Union(it) => it.fields(db),
1030             VariantDef::Variant(it) => it.fields(db),
1031         }
1032     }
1033
1034     pub fn module(self, db: &dyn HirDatabase) -> Module {
1035         match self {
1036             VariantDef::Struct(it) => it.module(db),
1037             VariantDef::Union(it) => it.module(db),
1038             VariantDef::Variant(it) => it.module(db),
1039         }
1040     }
1041
1042     pub fn name(&self, db: &dyn HirDatabase) -> Name {
1043         match self {
1044             VariantDef::Struct(s) => s.name(db),
1045             VariantDef::Union(u) => u.name(db),
1046             VariantDef::Variant(e) => e.name(db),
1047         }
1048     }
1049
1050     pub(crate) fn variant_data(self, db: &dyn HirDatabase) -> Arc<VariantData> {
1051         match self {
1052             VariantDef::Struct(it) => it.variant_data(db),
1053             VariantDef::Union(it) => it.variant_data(db),
1054             VariantDef::Variant(it) => it.variant_data(db),
1055         }
1056     }
1057 }
1058
1059 /// The defs which have a body.
1060 #[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
1061 pub enum DefWithBody {
1062     Function(Function),
1063     Static(Static),
1064     Const(Const),
1065 }
1066 impl_from!(Function, Const, Static for DefWithBody);
1067
1068 impl DefWithBody {
1069     pub fn module(self, db: &dyn HirDatabase) -> Module {
1070         match self {
1071             DefWithBody::Const(c) => c.module(db),
1072             DefWithBody::Function(f) => f.module(db),
1073             DefWithBody::Static(s) => s.module(db),
1074         }
1075     }
1076
1077     pub fn name(self, db: &dyn HirDatabase) -> Option<Name> {
1078         match self {
1079             DefWithBody::Function(f) => Some(f.name(db)),
1080             DefWithBody::Static(s) => Some(s.name(db)),
1081             DefWithBody::Const(c) => c.name(db),
1082         }
1083     }
1084
1085     /// Returns the type this def's body has to evaluate to.
1086     pub fn body_type(self, db: &dyn HirDatabase) -> Type {
1087         match self {
1088             DefWithBody::Function(it) => it.ret_type(db),
1089             DefWithBody::Static(it) => it.ty(db),
1090             DefWithBody::Const(it) => it.ty(db),
1091         }
1092     }
1093
1094     pub fn diagnostics(self, db: &dyn HirDatabase, acc: &mut Vec<AnyDiagnostic>) {
1095         let krate = self.module(db).id.krate();
1096
1097         let (body, source_map) = db.body_with_source_map(self.into());
1098
1099         for (_, def_map) in body.blocks(db.upcast()) {
1100             for diag in def_map.diagnostics() {
1101                 emit_def_diagnostic(db, acc, diag);
1102             }
1103         }
1104
1105         for diag in source_map.diagnostics() {
1106             match diag {
1107                 BodyDiagnostic::InactiveCode { node, cfg, opts } => acc.push(
1108                     InactiveCode { node: node.clone(), cfg: cfg.clone(), opts: opts.clone() }
1109                         .into(),
1110                 ),
1111                 BodyDiagnostic::MacroError { node, message } => acc.push(
1112                     MacroError {
1113                         node: node.clone().map(|it| it.into()),
1114                         message: message.to_string(),
1115                     }
1116                     .into(),
1117                 ),
1118                 BodyDiagnostic::UnresolvedProcMacro { node } => acc.push(
1119                     UnresolvedProcMacro {
1120                         node: node.clone().map(|it| it.into()),
1121                         precise_location: None,
1122                         macro_name: None,
1123                     }
1124                     .into(),
1125                 ),
1126                 BodyDiagnostic::UnresolvedMacroCall { node, path } => acc.push(
1127                     UnresolvedMacroCall { macro_call: node.clone(), path: path.clone() }.into(),
1128                 ),
1129             }
1130         }
1131
1132         let infer = db.infer(self.into());
1133         let source_map = Lazy::new(|| db.body_with_source_map(self.into()).1);
1134         for d in &infer.diagnostics {
1135             match d {
1136                 hir_ty::InferenceDiagnostic::NoSuchField { expr } => {
1137                     let field = source_map.field_syntax(*expr);
1138                     acc.push(NoSuchField { field }.into())
1139                 }
1140                 hir_ty::InferenceDiagnostic::BreakOutsideOfLoop { expr } => {
1141                     let expr = source_map
1142                         .expr_syntax(*expr)
1143                         .expect("break outside of loop in synthetic syntax");
1144                     acc.push(BreakOutsideOfLoop { expr }.into())
1145                 }
1146             }
1147         }
1148
1149         for expr in hir_ty::diagnostics::missing_unsafe(db, self.into()) {
1150             match source_map.expr_syntax(expr) {
1151                 Ok(expr) => acc.push(MissingUnsafe { expr }.into()),
1152                 Err(SyntheticSyntax) => {
1153                     // FIXME: Here and eslwhere in this file, the `expr` was
1154                     // desugared, report or assert that this doesn't happen.
1155                 }
1156             }
1157         }
1158
1159         for diagnostic in BodyValidationDiagnostic::collect(db, self.into()) {
1160             match diagnostic {
1161                 BodyValidationDiagnostic::RecordMissingFields {
1162                     record,
1163                     variant,
1164                     missed_fields,
1165                 } => {
1166                     let variant_data = variant.variant_data(db.upcast());
1167                     let missed_fields = missed_fields
1168                         .into_iter()
1169                         .map(|idx| variant_data.fields()[idx].name.clone())
1170                         .collect();
1171
1172                     match record {
1173                         Either::Left(record_expr) => match source_map.expr_syntax(record_expr) {
1174                             Ok(source_ptr) => {
1175                                 let root = source_ptr.file_syntax(db.upcast());
1176                                 if let ast::Expr::RecordExpr(record_expr) =
1177                                     &source_ptr.value.to_node(&root)
1178                                 {
1179                                     if record_expr.record_expr_field_list().is_some() {
1180                                         acc.push(
1181                                             MissingFields {
1182                                                 file: source_ptr.file_id,
1183                                                 field_list_parent: Either::Left(AstPtr::new(
1184                                                     record_expr,
1185                                                 )),
1186                                                 field_list_parent_path: record_expr
1187                                                     .path()
1188                                                     .map(|path| AstPtr::new(&path)),
1189                                                 missed_fields,
1190                                             }
1191                                             .into(),
1192                                         )
1193                                     }
1194                                 }
1195                             }
1196                             Err(SyntheticSyntax) => (),
1197                         },
1198                         Either::Right(record_pat) => match source_map.pat_syntax(record_pat) {
1199                             Ok(source_ptr) => {
1200                                 if let Some(expr) = source_ptr.value.as_ref().left() {
1201                                     let root = source_ptr.file_syntax(db.upcast());
1202                                     if let ast::Pat::RecordPat(record_pat) = expr.to_node(&root) {
1203                                         if record_pat.record_pat_field_list().is_some() {
1204                                             acc.push(
1205                                                 MissingFields {
1206                                                     file: source_ptr.file_id,
1207                                                     field_list_parent: Either::Right(AstPtr::new(
1208                                                         &record_pat,
1209                                                     )),
1210                                                     field_list_parent_path: record_pat
1211                                                         .path()
1212                                                         .map(|path| AstPtr::new(&path)),
1213                                                     missed_fields,
1214                                                 }
1215                                                 .into(),
1216                                             )
1217                                         }
1218                                     }
1219                                 }
1220                             }
1221                             Err(SyntheticSyntax) => (),
1222                         },
1223                     }
1224                 }
1225                 BodyValidationDiagnostic::ReplaceFilterMapNextWithFindMap { method_call_expr } => {
1226                     if let Ok(next_source_ptr) = source_map.expr_syntax(method_call_expr) {
1227                         acc.push(
1228                             ReplaceFilterMapNextWithFindMap {
1229                                 file: next_source_ptr.file_id,
1230                                 next_expr: next_source_ptr.value,
1231                             }
1232                             .into(),
1233                         );
1234                     }
1235                 }
1236                 BodyValidationDiagnostic::MismatchedArgCount { call_expr, expected, found } => {
1237                     match source_map.expr_syntax(call_expr) {
1238                         Ok(source_ptr) => acc.push(
1239                             MismatchedArgCount { call_expr: source_ptr, expected, found }.into(),
1240                         ),
1241                         Err(SyntheticSyntax) => (),
1242                     }
1243                 }
1244                 BodyValidationDiagnostic::RemoveThisSemicolon { expr } => {
1245                     match source_map.expr_syntax(expr) {
1246                         Ok(expr) => acc.push(RemoveThisSemicolon { expr }.into()),
1247                         Err(SyntheticSyntax) => (),
1248                     }
1249                 }
1250                 BodyValidationDiagnostic::MissingOkOrSomeInTailExpr { expr, required } => {
1251                     match source_map.expr_syntax(expr) {
1252                         Ok(expr) => acc.push(
1253                             MissingOkOrSomeInTailExpr {
1254                                 expr,
1255                                 required,
1256                                 expected: self.body_type(db),
1257                             }
1258                             .into(),
1259                         ),
1260                         Err(SyntheticSyntax) => (),
1261                     }
1262                 }
1263                 BodyValidationDiagnostic::MissingMatchArms { match_expr } => {
1264                     match source_map.expr_syntax(match_expr) {
1265                         Ok(source_ptr) => {
1266                             let root = source_ptr.file_syntax(db.upcast());
1267                             if let ast::Expr::MatchExpr(match_expr) =
1268                                 &source_ptr.value.to_node(&root)
1269                             {
1270                                 if let Some(match_expr) = match_expr.expr() {
1271                                     acc.push(
1272                                         MissingMatchArms {
1273                                             file: source_ptr.file_id,
1274                                             match_expr: AstPtr::new(&match_expr),
1275                                         }
1276                                         .into(),
1277                                     );
1278                                 }
1279                             }
1280                         }
1281                         Err(SyntheticSyntax) => (),
1282                     }
1283                 }
1284                 BodyValidationDiagnostic::AddReferenceHere { arg_expr, mutability } => {
1285                     match source_map.expr_syntax(arg_expr) {
1286                         Ok(expr) => acc.push(AddReferenceHere { expr, mutability }.into()),
1287                         Err(SyntheticSyntax) => (),
1288                     }
1289                 }
1290             }
1291         }
1292
1293         let def: ModuleDef = match self {
1294             DefWithBody::Function(it) => it.into(),
1295             DefWithBody::Static(it) => it.into(),
1296             DefWithBody::Const(it) => it.into(),
1297         };
1298         for diag in hir_ty::diagnostics::incorrect_case(db, krate, def.into()) {
1299             acc.push(diag.into())
1300         }
1301     }
1302 }
1303
1304 #[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
1305 pub struct Function {
1306     pub(crate) id: FunctionId,
1307 }
1308
1309 impl Function {
1310     pub fn module(self, db: &dyn HirDatabase) -> Module {
1311         self.id.lookup(db.upcast()).module(db.upcast()).into()
1312     }
1313
1314     pub fn name(self, db: &dyn HirDatabase) -> Name {
1315         db.function_data(self.id).name.clone()
1316     }
1317
1318     /// Get this function's return type
1319     pub fn ret_type(self, db: &dyn HirDatabase) -> Type {
1320         let resolver = self.id.resolver(db.upcast());
1321         let krate = self.id.lookup(db.upcast()).container.module(db.upcast()).krate();
1322         let ret_type = &db.function_data(self.id).ret_type;
1323         let ctx = hir_ty::TyLoweringContext::new(db, &resolver);
1324         let ty = ctx.lower_ty(ret_type);
1325         Type::new_with_resolver_inner(db, krate, &resolver, ty)
1326     }
1327
1328     pub fn self_param(self, db: &dyn HirDatabase) -> Option<SelfParam> {
1329         if !db.function_data(self.id).has_self_param() {
1330             return None;
1331         }
1332         Some(SelfParam { func: self.id })
1333     }
1334
1335     pub fn assoc_fn_params(self, db: &dyn HirDatabase) -> Vec<Param> {
1336         let resolver = self.id.resolver(db.upcast());
1337         let krate = self.id.lookup(db.upcast()).container.module(db.upcast()).krate();
1338         let ctx = hir_ty::TyLoweringContext::new(db, &resolver);
1339         let environment = db.trait_environment(self.id.into());
1340         db.function_data(self.id)
1341             .params
1342             .iter()
1343             .enumerate()
1344             .map(|(idx, (_, type_ref))| {
1345                 let ty = Type { krate, env: environment.clone(), ty: ctx.lower_ty(type_ref) };
1346                 Param { func: self, ty, idx }
1347             })
1348             .collect()
1349     }
1350
1351     pub fn method_params(self, db: &dyn HirDatabase) -> Option<Vec<Param>> {
1352         if self.self_param(db).is_none() {
1353             return None;
1354         }
1355         let mut res = self.assoc_fn_params(db);
1356         res.remove(0);
1357         Some(res)
1358     }
1359
1360     pub fn is_unsafe(self, db: &dyn HirDatabase) -> bool {
1361         db.function_data(self.id).is_unsafe()
1362     }
1363
1364     pub fn is_const(self, db: &dyn HirDatabase) -> bool {
1365         db.function_data(self.id).is_const()
1366     }
1367
1368     pub fn is_async(self, db: &dyn HirDatabase) -> bool {
1369         db.function_data(self.id).is_async()
1370     }
1371
1372     /// Whether this function declaration has a definition.
1373     ///
1374     /// This is false in the case of required (not provided) trait methods.
1375     pub fn has_body(self, db: &dyn HirDatabase) -> bool {
1376         db.function_data(self.id).has_body()
1377     }
1378
1379     pub fn as_proc_macro(self, db: &dyn HirDatabase) -> Option<MacroDef> {
1380         let function_data = db.function_data(self.id);
1381         let attrs = &function_data.attrs;
1382         if !(attrs.is_proc_macro()
1383             || attrs.is_proc_macro_attribute()
1384             || attrs.is_proc_macro_derive())
1385         {
1386             return None;
1387         }
1388         let loc = self.id.lookup(db.upcast());
1389         let krate = loc.krate(db);
1390         let def_map = db.crate_def_map(krate.into());
1391         let name = &function_data.name;
1392         let mut exported_proc_macros = def_map.exported_proc_macros();
1393         exported_proc_macros.find(|(_, mac_name)| mac_name == name).map(|(id, _)| MacroDef { id })
1394     }
1395
1396     /// A textual representation of the HIR of this function for debugging purposes.
1397     pub fn debug_hir(self, db: &dyn HirDatabase) -> String {
1398         let body = db.body(self.id.into());
1399
1400         let mut result = String::new();
1401         format_to!(result, "HIR expressions in the body of `{}`:\n", self.name(db));
1402         for (id, expr) in body.exprs.iter() {
1403             format_to!(result, "{:?}: {:?}\n", id, expr);
1404         }
1405
1406         result
1407     }
1408 }
1409
1410 // Note: logically, this belongs to `hir_ty`, but we are not using it there yet.
1411 pub enum Access {
1412     Shared,
1413     Exclusive,
1414     Owned,
1415 }
1416
1417 impl From<hir_ty::Mutability> for Access {
1418     fn from(mutability: hir_ty::Mutability) -> Access {
1419         match mutability {
1420             hir_ty::Mutability::Not => Access::Shared,
1421             hir_ty::Mutability::Mut => Access::Exclusive,
1422         }
1423     }
1424 }
1425
1426 #[derive(Clone, Debug)]
1427 pub struct Param {
1428     func: Function,
1429     /// The index in parameter list, including self parameter.
1430     idx: usize,
1431     ty: Type,
1432 }
1433
1434 impl Param {
1435     pub fn ty(&self) -> &Type {
1436         &self.ty
1437     }
1438
1439     pub fn name(&self, db: &dyn HirDatabase) -> Option<Name> {
1440         db.function_data(self.func.id).params[self.idx].0.clone()
1441     }
1442
1443     pub fn as_local(&self, db: &dyn HirDatabase) -> Local {
1444         let parent = DefWithBodyId::FunctionId(self.func.into());
1445         let body = db.body(parent);
1446         Local { parent, pat_id: body.params[self.idx] }
1447     }
1448
1449     pub fn pattern_source(&self, db: &dyn HirDatabase) -> Option<ast::Pat> {
1450         self.source(db).and_then(|p| p.value.pat())
1451     }
1452
1453     pub fn source(&self, db: &dyn HirDatabase) -> Option<InFile<ast::Param>> {
1454         let InFile { file_id, value } = self.func.source(db)?;
1455         let params = value.param_list()?;
1456         if params.self_param().is_some() {
1457             params.params().nth(self.idx.checked_sub(1)?)
1458         } else {
1459             params.params().nth(self.idx)
1460         }
1461         .map(|value| InFile { file_id, value })
1462     }
1463 }
1464
1465 #[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
1466 pub struct SelfParam {
1467     func: FunctionId,
1468 }
1469
1470 impl SelfParam {
1471     pub fn access(self, db: &dyn HirDatabase) -> Access {
1472         let func_data = db.function_data(self.func);
1473         func_data
1474             .params
1475             .first()
1476             .map(|(_, param)| match &**param {
1477                 TypeRef::Reference(.., mutability) => match mutability {
1478                     hir_def::type_ref::Mutability::Shared => Access::Shared,
1479                     hir_def::type_ref::Mutability::Mut => Access::Exclusive,
1480                 },
1481                 _ => Access::Owned,
1482             })
1483             .unwrap_or(Access::Owned)
1484     }
1485
1486     pub fn display(self, db: &dyn HirDatabase) -> &'static str {
1487         match self.access(db) {
1488             Access::Shared => "&self",
1489             Access::Exclusive => "&mut self",
1490             Access::Owned => "self",
1491         }
1492     }
1493
1494     pub fn source(&self, db: &dyn HirDatabase) -> Option<InFile<ast::SelfParam>> {
1495         let InFile { file_id, value } = Function::from(self.func).source(db)?;
1496         value
1497             .param_list()
1498             .and_then(|params| params.self_param())
1499             .map(|value| InFile { file_id, value })
1500     }
1501
1502     pub fn ty(&self, db: &dyn HirDatabase) -> Type {
1503         let resolver = self.func.resolver(db.upcast());
1504         let krate = self.func.lookup(db.upcast()).container.module(db.upcast()).krate();
1505         let ctx = hir_ty::TyLoweringContext::new(db, &resolver);
1506         let environment = db.trait_environment(self.func.into());
1507
1508         Type {
1509             krate,
1510             env: environment.clone(),
1511             ty: ctx.lower_ty(&db.function_data(self.func).params[0].1),
1512         }
1513     }
1514 }
1515
1516 impl HasVisibility for Function {
1517     fn visibility(&self, db: &dyn HirDatabase) -> Visibility {
1518         let function_data = db.function_data(self.id);
1519         let visibility = &function_data.visibility;
1520         visibility.resolve(db.upcast(), &self.id.resolver(db.upcast()))
1521     }
1522 }
1523
1524 #[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
1525 pub struct Const {
1526     pub(crate) id: ConstId,
1527 }
1528
1529 impl Const {
1530     pub fn module(self, db: &dyn HirDatabase) -> Module {
1531         Module { id: self.id.lookup(db.upcast()).module(db.upcast()) }
1532     }
1533
1534     pub fn name(self, db: &dyn HirDatabase) -> Option<Name> {
1535         db.const_data(self.id).name.clone()
1536     }
1537
1538     pub fn value(self, db: &dyn HirDatabase) -> Option<ast::Expr> {
1539         self.source(db)?.value.body()
1540     }
1541
1542     pub fn ty(self, db: &dyn HirDatabase) -> Type {
1543         let data = db.const_data(self.id);
1544         let resolver = self.id.resolver(db.upcast());
1545         let krate = self.id.lookup(db.upcast()).container.krate(db);
1546         let ctx = hir_ty::TyLoweringContext::new(db, &resolver);
1547         let ty = ctx.lower_ty(&data.type_ref);
1548         Type::new_with_resolver_inner(db, krate.id, &resolver, ty)
1549     }
1550
1551     pub fn eval(self, db: &dyn HirDatabase) -> Result<ComputedExpr, ConstEvalError> {
1552         let body = db.body(self.id.into());
1553         let root = &body.exprs[body.body_expr];
1554         let infer = db.infer_query(self.id.into());
1555         let infer = infer.as_ref();
1556         let result = eval_const(
1557             root,
1558             &mut ConstEvalCtx {
1559                 exprs: &body.exprs,
1560                 pats: &body.pats,
1561                 local_data: HashMap::default(),
1562                 infer: &mut |x| infer[x].clone(),
1563             },
1564         );
1565         result
1566     }
1567 }
1568
1569 impl HasVisibility for Const {
1570     fn visibility(&self, db: &dyn HirDatabase) -> Visibility {
1571         let function_data = db.const_data(self.id);
1572         let visibility = &function_data.visibility;
1573         visibility.resolve(db.upcast(), &self.id.resolver(db.upcast()))
1574     }
1575 }
1576
1577 #[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
1578 pub struct Static {
1579     pub(crate) id: StaticId,
1580 }
1581
1582 impl Static {
1583     pub fn module(self, db: &dyn HirDatabase) -> Module {
1584         Module { id: self.id.lookup(db.upcast()).module(db.upcast()) }
1585     }
1586
1587     pub fn name(self, db: &dyn HirDatabase) -> Name {
1588         db.static_data(self.id).name.clone()
1589     }
1590
1591     pub fn is_mut(self, db: &dyn HirDatabase) -> bool {
1592         db.static_data(self.id).mutable
1593     }
1594
1595     pub fn value(self, db: &dyn HirDatabase) -> Option<ast::Expr> {
1596         self.source(db)?.value.body()
1597     }
1598
1599     pub fn ty(self, db: &dyn HirDatabase) -> Type {
1600         let data = db.static_data(self.id);
1601         let resolver = self.id.resolver(db.upcast());
1602         let krate = self.id.lookup(db.upcast()).container.module(db.upcast()).krate();
1603         let ctx = hir_ty::TyLoweringContext::new(db, &resolver);
1604         let ty = ctx.lower_ty(&data.type_ref);
1605         Type::new_with_resolver_inner(db, krate, &resolver, ty)
1606     }
1607 }
1608
1609 impl HasVisibility for Static {
1610     fn visibility(&self, db: &dyn HirDatabase) -> Visibility {
1611         db.static_data(self.id).visibility.resolve(db.upcast(), &self.id.resolver(db.upcast()))
1612     }
1613 }
1614
1615 #[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
1616 pub struct Trait {
1617     pub(crate) id: TraitId,
1618 }
1619
1620 impl Trait {
1621     pub fn lang(db: &dyn HirDatabase, krate: Crate, name: &Name) -> Option<Trait> {
1622         db.lang_item(krate.into(), name.to_smol_str())
1623             .and_then(LangItemTarget::as_trait)
1624             .map(Into::into)
1625     }
1626
1627     pub fn module(self, db: &dyn HirDatabase) -> Module {
1628         Module { id: self.id.lookup(db.upcast()).container }
1629     }
1630
1631     pub fn name(self, db: &dyn HirDatabase) -> Name {
1632         db.trait_data(self.id).name.clone()
1633     }
1634
1635     pub fn items(self, db: &dyn HirDatabase) -> Vec<AssocItem> {
1636         db.trait_data(self.id).items.iter().map(|(_name, it)| (*it).into()).collect()
1637     }
1638
1639     pub fn is_auto(self, db: &dyn HirDatabase) -> bool {
1640         db.trait_data(self.id).is_auto
1641     }
1642
1643     pub fn is_unsafe(&self, db: &dyn HirDatabase) -> bool {
1644         db.trait_data(self.id).is_unsafe
1645     }
1646 }
1647
1648 impl HasVisibility for Trait {
1649     fn visibility(&self, db: &dyn HirDatabase) -> Visibility {
1650         db.trait_data(self.id).visibility.resolve(db.upcast(), &self.id.resolver(db.upcast()))
1651     }
1652 }
1653
1654 #[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
1655 pub struct TypeAlias {
1656     pub(crate) id: TypeAliasId,
1657 }
1658
1659 impl TypeAlias {
1660     pub fn has_non_default_type_params(self, db: &dyn HirDatabase) -> bool {
1661         let subst = db.generic_defaults(self.id.into());
1662         subst.iter().any(|ty| ty.skip_binders().is_unknown())
1663     }
1664
1665     pub fn module(self, db: &dyn HirDatabase) -> Module {
1666         Module { id: self.id.lookup(db.upcast()).module(db.upcast()) }
1667     }
1668
1669     pub fn type_ref(self, db: &dyn HirDatabase) -> Option<TypeRef> {
1670         db.type_alias_data(self.id).type_ref.as_deref().cloned()
1671     }
1672
1673     pub fn ty(self, db: &dyn HirDatabase) -> Type {
1674         Type::from_def(db, self.id.lookup(db.upcast()).module(db.upcast()).krate(), self.id)
1675     }
1676
1677     pub fn name(self, db: &dyn HirDatabase) -> Name {
1678         db.type_alias_data(self.id).name.clone()
1679     }
1680 }
1681
1682 impl HasVisibility for TypeAlias {
1683     fn visibility(&self, db: &dyn HirDatabase) -> Visibility {
1684         let function_data = db.type_alias_data(self.id);
1685         let visibility = &function_data.visibility;
1686         visibility.resolve(db.upcast(), &self.id.resolver(db.upcast()))
1687     }
1688 }
1689
1690 #[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
1691 pub struct BuiltinType {
1692     pub(crate) inner: hir_def::builtin_type::BuiltinType,
1693 }
1694
1695 impl BuiltinType {
1696     pub fn str() -> BuiltinType {
1697         BuiltinType { inner: hir_def::builtin_type::BuiltinType::Str }
1698     }
1699
1700     pub fn ty(self, db: &dyn HirDatabase, module: Module) -> Type {
1701         let resolver = module.id.resolver(db.upcast());
1702         Type::new_with_resolver(db, &resolver, TyBuilder::builtin(self.inner))
1703             .expect("crate not present in resolver")
1704     }
1705
1706     pub fn name(self) -> Name {
1707         self.inner.as_name()
1708     }
1709
1710     pub fn is_int(&self) -> bool {
1711         matches!(self.inner, hir_def::builtin_type::BuiltinType::Int(_))
1712     }
1713
1714     pub fn is_uint(&self) -> bool {
1715         matches!(self.inner, hir_def::builtin_type::BuiltinType::Uint(_))
1716     }
1717
1718     pub fn is_float(&self) -> bool {
1719         matches!(self.inner, hir_def::builtin_type::BuiltinType::Float(_))
1720     }
1721
1722     pub fn is_char(&self) -> bool {
1723         matches!(self.inner, hir_def::builtin_type::BuiltinType::Char)
1724     }
1725
1726     pub fn is_str(&self) -> bool {
1727         matches!(self.inner, hir_def::builtin_type::BuiltinType::Str)
1728     }
1729 }
1730
1731 #[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
1732 pub enum MacroKind {
1733     /// `macro_rules!` or Macros 2.0 macro.
1734     Declarative,
1735     /// A built-in or custom derive.
1736     Derive,
1737     /// A built-in function-like macro.
1738     BuiltIn,
1739     /// A procedural attribute macro.
1740     Attr,
1741     /// A function-like procedural macro.
1742     ProcMacro,
1743 }
1744
1745 #[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
1746 pub struct MacroDef {
1747     pub(crate) id: MacroDefId,
1748 }
1749
1750 impl MacroDef {
1751     /// FIXME: right now, this just returns the root module of the crate that
1752     /// defines this macro. The reasons for this is that macros are expanded
1753     /// early, in `hir_expand`, where modules simply do not exist yet.
1754     pub fn module(self, db: &dyn HirDatabase) -> Option<Module> {
1755         let krate = self.id.krate;
1756         let def_map = db.crate_def_map(krate);
1757         let module_id = def_map.root();
1758         Some(Module { id: def_map.module_id(module_id) })
1759     }
1760
1761     /// XXX: this parses the file
1762     pub fn name(self, db: &dyn HirDatabase) -> Option<Name> {
1763         match self.source(db)?.value {
1764             Either::Left(it) => it.name().map(|it| it.as_name()),
1765             Either::Right(_) => {
1766                 let krate = self.id.krate;
1767                 let def_map = db.crate_def_map(krate);
1768                 let (_, name) = def_map.exported_proc_macros().find(|&(id, _)| id == self.id)?;
1769                 Some(name)
1770             }
1771         }
1772     }
1773
1774     pub fn kind(&self) -> MacroKind {
1775         match self.id.kind {
1776             MacroDefKind::Declarative(_) => MacroKind::Declarative,
1777             MacroDefKind::BuiltIn(_, _) | MacroDefKind::BuiltInEager(_, _) => MacroKind::BuiltIn,
1778             MacroDefKind::BuiltInDerive(_, _) => MacroKind::Derive,
1779             MacroDefKind::BuiltInAttr(_, _) => MacroKind::Attr,
1780             MacroDefKind::ProcMacro(_, base_db::ProcMacroKind::CustomDerive, _) => {
1781                 MacroKind::Derive
1782             }
1783             MacroDefKind::ProcMacro(_, base_db::ProcMacroKind::Attr, _) => MacroKind::Attr,
1784             MacroDefKind::ProcMacro(_, base_db::ProcMacroKind::FuncLike, _) => MacroKind::ProcMacro,
1785         }
1786     }
1787
1788     pub fn is_fn_like(&self) -> bool {
1789         match self.kind() {
1790             MacroKind::Declarative | MacroKind::BuiltIn | MacroKind::ProcMacro => true,
1791             MacroKind::Attr | MacroKind::Derive => false,
1792         }
1793     }
1794
1795     pub fn is_builtin_derive(&self) -> bool {
1796         match self.id.kind {
1797             MacroDefKind::BuiltInAttr(exp, _) => exp.is_derive(),
1798             _ => false,
1799         }
1800     }
1801
1802     pub fn is_attr(&self) -> bool {
1803         matches!(self.kind(), MacroKind::Attr)
1804     }
1805 }
1806
1807 #[derive(Clone, Copy, PartialEq, Eq, Debug, Hash)]
1808 pub enum ItemInNs {
1809     Types(ModuleDef),
1810     Values(ModuleDef),
1811     Macros(MacroDef),
1812 }
1813
1814 impl From<MacroDef> for ItemInNs {
1815     fn from(it: MacroDef) -> Self {
1816         Self::Macros(it)
1817     }
1818 }
1819
1820 impl From<ModuleDef> for ItemInNs {
1821     fn from(module_def: ModuleDef) -> Self {
1822         match module_def {
1823             ModuleDef::Static(_) | ModuleDef::Const(_) | ModuleDef::Function(_) => {
1824                 ItemInNs::Values(module_def)
1825             }
1826             _ => ItemInNs::Types(module_def),
1827         }
1828     }
1829 }
1830
1831 impl ItemInNs {
1832     pub fn as_module_def(self) -> Option<ModuleDef> {
1833         match self {
1834             ItemInNs::Types(id) | ItemInNs::Values(id) => Some(id),
1835             ItemInNs::Macros(_) => None,
1836         }
1837     }
1838
1839     /// Returns the crate defining this item (or `None` if `self` is built-in).
1840     pub fn krate(&self, db: &dyn HirDatabase) -> Option<Crate> {
1841         match self {
1842             ItemInNs::Types(did) | ItemInNs::Values(did) => did.module(db).map(|m| m.krate()),
1843             ItemInNs::Macros(id) => id.module(db).map(|m| m.krate()),
1844         }
1845     }
1846
1847     pub fn attrs(&self, db: &dyn HirDatabase) -> Option<AttrsWithOwner> {
1848         match self {
1849             ItemInNs::Types(it) | ItemInNs::Values(it) => it.attrs(db),
1850             ItemInNs::Macros(it) => Some(it.attrs(db)),
1851         }
1852     }
1853 }
1854
1855 /// Invariant: `inner.as_assoc_item(db).is_some()`
1856 /// We do not actively enforce this invariant.
1857 #[derive(Debug, Copy, Clone, PartialEq, Eq, Hash)]
1858 pub enum AssocItem {
1859     Function(Function),
1860     Const(Const),
1861     TypeAlias(TypeAlias),
1862 }
1863 #[derive(Debug)]
1864 pub enum AssocItemContainer {
1865     Trait(Trait),
1866     Impl(Impl),
1867 }
1868 pub trait AsAssocItem {
1869     fn as_assoc_item(self, db: &dyn HirDatabase) -> Option<AssocItem>;
1870 }
1871
1872 impl AsAssocItem for Function {
1873     fn as_assoc_item(self, db: &dyn HirDatabase) -> Option<AssocItem> {
1874         as_assoc_item(db, AssocItem::Function, self.id)
1875     }
1876 }
1877 impl AsAssocItem for Const {
1878     fn as_assoc_item(self, db: &dyn HirDatabase) -> Option<AssocItem> {
1879         as_assoc_item(db, AssocItem::Const, self.id)
1880     }
1881 }
1882 impl AsAssocItem for TypeAlias {
1883     fn as_assoc_item(self, db: &dyn HirDatabase) -> Option<AssocItem> {
1884         as_assoc_item(db, AssocItem::TypeAlias, self.id)
1885     }
1886 }
1887 impl AsAssocItem for ModuleDef {
1888     fn as_assoc_item(self, db: &dyn HirDatabase) -> Option<AssocItem> {
1889         match self {
1890             ModuleDef::Function(it) => it.as_assoc_item(db),
1891             ModuleDef::Const(it) => it.as_assoc_item(db),
1892             ModuleDef::TypeAlias(it) => it.as_assoc_item(db),
1893             _ => None,
1894         }
1895     }
1896 }
1897 fn as_assoc_item<ID, DEF, CTOR, AST>(db: &dyn HirDatabase, ctor: CTOR, id: ID) -> Option<AssocItem>
1898 where
1899     ID: Lookup<Data = AssocItemLoc<AST>>,
1900     DEF: From<ID>,
1901     CTOR: FnOnce(DEF) -> AssocItem,
1902     AST: ItemTreeNode,
1903 {
1904     match id.lookup(db.upcast()).container {
1905         ItemContainerId::TraitId(_) | ItemContainerId::ImplId(_) => Some(ctor(DEF::from(id))),
1906         ItemContainerId::ModuleId(_) | ItemContainerId::ExternBlockId(_) => None,
1907     }
1908 }
1909
1910 impl AssocItem {
1911     pub fn name(self, db: &dyn HirDatabase) -> Option<Name> {
1912         match self {
1913             AssocItem::Function(it) => Some(it.name(db)),
1914             AssocItem::Const(it) => it.name(db),
1915             AssocItem::TypeAlias(it) => Some(it.name(db)),
1916         }
1917     }
1918     pub fn module(self, db: &dyn HirDatabase) -> Module {
1919         match self {
1920             AssocItem::Function(f) => f.module(db),
1921             AssocItem::Const(c) => c.module(db),
1922             AssocItem::TypeAlias(t) => t.module(db),
1923         }
1924     }
1925     pub fn container(self, db: &dyn HirDatabase) -> AssocItemContainer {
1926         let container = match self {
1927             AssocItem::Function(it) => it.id.lookup(db.upcast()).container,
1928             AssocItem::Const(it) => it.id.lookup(db.upcast()).container,
1929             AssocItem::TypeAlias(it) => it.id.lookup(db.upcast()).container,
1930         };
1931         match container {
1932             ItemContainerId::TraitId(id) => AssocItemContainer::Trait(id.into()),
1933             ItemContainerId::ImplId(id) => AssocItemContainer::Impl(id.into()),
1934             ItemContainerId::ModuleId(_) | ItemContainerId::ExternBlockId(_) => {
1935                 panic!("invalid AssocItem")
1936             }
1937         }
1938     }
1939
1940     pub fn containing_trait(self, db: &dyn HirDatabase) -> Option<Trait> {
1941         match self.container(db) {
1942             AssocItemContainer::Trait(t) => Some(t),
1943             _ => None,
1944         }
1945     }
1946
1947     pub fn containing_trait_impl(self, db: &dyn HirDatabase) -> Option<Trait> {
1948         match self.container(db) {
1949             AssocItemContainer::Impl(i) => i.trait_(db),
1950             _ => None,
1951         }
1952     }
1953
1954     pub fn containing_trait_or_trait_impl(self, db: &dyn HirDatabase) -> Option<Trait> {
1955         match self.container(db) {
1956             AssocItemContainer::Trait(t) => Some(t),
1957             AssocItemContainer::Impl(i) => i.trait_(db),
1958         }
1959     }
1960 }
1961
1962 impl HasVisibility for AssocItem {
1963     fn visibility(&self, db: &dyn HirDatabase) -> Visibility {
1964         match self {
1965             AssocItem::Function(f) => f.visibility(db),
1966             AssocItem::Const(c) => c.visibility(db),
1967             AssocItem::TypeAlias(t) => t.visibility(db),
1968         }
1969     }
1970 }
1971
1972 impl From<AssocItem> for ModuleDef {
1973     fn from(assoc: AssocItem) -> Self {
1974         match assoc {
1975             AssocItem::Function(it) => ModuleDef::Function(it),
1976             AssocItem::Const(it) => ModuleDef::Const(it),
1977             AssocItem::TypeAlias(it) => ModuleDef::TypeAlias(it),
1978         }
1979     }
1980 }
1981
1982 #[derive(Clone, Copy, PartialEq, Eq, Debug, Hash)]
1983 pub enum GenericDef {
1984     Function(Function),
1985     Adt(Adt),
1986     Trait(Trait),
1987     TypeAlias(TypeAlias),
1988     Impl(Impl),
1989     // enum variants cannot have generics themselves, but their parent enums
1990     // can, and this makes some code easier to write
1991     Variant(Variant),
1992     // consts can have type parameters from their parents (i.e. associated consts of traits)
1993     Const(Const),
1994 }
1995 impl_from!(
1996     Function,
1997     Adt(Struct, Enum, Union),
1998     Trait,
1999     TypeAlias,
2000     Impl,
2001     Variant,
2002     Const
2003     for GenericDef
2004 );
2005
2006 impl GenericDef {
2007     pub fn params(self, db: &dyn HirDatabase) -> Vec<GenericParam> {
2008         let generics = db.generic_params(self.into());
2009         let ty_params = generics
2010             .types
2011             .iter()
2012             .map(|(local_id, _)| TypeParam { id: TypeParamId { parent: self.into(), local_id } })
2013             .map(GenericParam::TypeParam);
2014         let lt_params = generics
2015             .lifetimes
2016             .iter()
2017             .map(|(local_id, _)| LifetimeParam {
2018                 id: LifetimeParamId { parent: self.into(), local_id },
2019             })
2020             .map(GenericParam::LifetimeParam);
2021         let const_params = generics
2022             .consts
2023             .iter()
2024             .map(|(local_id, _)| ConstParam { id: ConstParamId { parent: self.into(), local_id } })
2025             .map(GenericParam::ConstParam);
2026         ty_params.chain(lt_params).chain(const_params).collect()
2027     }
2028
2029     pub fn type_params(self, db: &dyn HirDatabase) -> Vec<TypeParam> {
2030         let generics = db.generic_params(self.into());
2031         generics
2032             .types
2033             .iter()
2034             .map(|(local_id, _)| TypeParam { id: TypeParamId { parent: self.into(), local_id } })
2035             .collect()
2036     }
2037 }
2038
2039 #[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)]
2040 pub struct Local {
2041     pub(crate) parent: DefWithBodyId,
2042     pub(crate) pat_id: PatId,
2043 }
2044
2045 impl Local {
2046     pub fn is_param(self, db: &dyn HirDatabase) -> bool {
2047         let src = self.source(db);
2048         match src.value {
2049             Either::Left(bind_pat) => {
2050                 bind_pat.syntax().ancestors().any(|it| ast::Param::can_cast(it.kind()))
2051             }
2052             Either::Right(_self_param) => true,
2053         }
2054     }
2055
2056     pub fn as_self_param(self, db: &dyn HirDatabase) -> Option<SelfParam> {
2057         match self.parent {
2058             DefWithBodyId::FunctionId(func) if self.is_self(db) => Some(SelfParam { func }),
2059             _ => None,
2060         }
2061     }
2062
2063     // FIXME: why is this an option? It shouldn't be?
2064     pub fn name(self, db: &dyn HirDatabase) -> Option<Name> {
2065         let body = db.body(self.parent);
2066         match &body[self.pat_id] {
2067             Pat::Bind { name, .. } => Some(name.clone()),
2068             _ => None,
2069         }
2070     }
2071
2072     pub fn is_self(self, db: &dyn HirDatabase) -> bool {
2073         self.name(db) == Some(name![self])
2074     }
2075
2076     pub fn is_mut(self, db: &dyn HirDatabase) -> bool {
2077         let body = db.body(self.parent);
2078         matches!(&body[self.pat_id], Pat::Bind { mode: BindingAnnotation::Mutable, .. })
2079     }
2080
2081     pub fn is_ref(self, db: &dyn HirDatabase) -> bool {
2082         let body = db.body(self.parent);
2083         matches!(
2084             &body[self.pat_id],
2085             Pat::Bind { mode: BindingAnnotation::Ref | BindingAnnotation::RefMut, .. }
2086         )
2087     }
2088
2089     pub fn parent(self, _db: &dyn HirDatabase) -> DefWithBody {
2090         self.parent.into()
2091     }
2092
2093     pub fn module(self, db: &dyn HirDatabase) -> Module {
2094         self.parent(db).module(db)
2095     }
2096
2097     pub fn ty(self, db: &dyn HirDatabase) -> Type {
2098         let def = self.parent;
2099         let infer = db.infer(def);
2100         let ty = infer[self.pat_id].clone();
2101         let krate = def.module(db.upcast()).krate();
2102         Type::new(db, krate, def, ty)
2103     }
2104
2105     pub fn source(self, db: &dyn HirDatabase) -> InFile<Either<ast::IdentPat, ast::SelfParam>> {
2106         let (_body, source_map) = db.body_with_source_map(self.parent);
2107         let src = source_map.pat_syntax(self.pat_id).unwrap(); // Hmm...
2108         let root = src.file_syntax(db.upcast());
2109         src.map(|ast| {
2110             ast.map_left(|it| it.cast().unwrap().to_node(&root)).map_right(|it| it.to_node(&root))
2111         })
2112     }
2113 }
2114
2115 // FIXME: Wrong name? This is could also be a registered attribute
2116 #[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)]
2117 pub struct BuiltinAttr {
2118     krate: Option<CrateId>,
2119     idx: usize,
2120 }
2121
2122 impl BuiltinAttr {
2123     // FIXME: consider crates\hir_def\src\nameres\attr_resolution.rs?
2124     pub(crate) fn by_name(db: &dyn HirDatabase, krate: Crate, name: &str) -> Option<Self> {
2125         if let builtin @ Some(_) = Self::builtin(name) {
2126             return builtin;
2127         }
2128         let idx = db.crate_def_map(krate.id).registered_attrs().iter().position(|it| it == name)?;
2129         Some(BuiltinAttr { krate: Some(krate.id), idx })
2130     }
2131
2132     pub(crate) fn builtin(name: &str) -> Option<Self> {
2133         hir_def::builtin_attr::INERT_ATTRIBUTES
2134             .iter()
2135             .position(|tool| tool.name == name)
2136             .map(|idx| BuiltinAttr { krate: None, idx })
2137     }
2138
2139     pub fn name(&self, db: &dyn HirDatabase) -> SmolStr {
2140         // FIXME: Return a `Name` here
2141         match self.krate {
2142             Some(krate) => db.crate_def_map(krate).registered_attrs()[self.idx].clone(),
2143             None => SmolStr::new(hir_def::builtin_attr::INERT_ATTRIBUTES[self.idx].name),
2144         }
2145     }
2146
2147     pub fn template(&self, _: &dyn HirDatabase) -> Option<AttributeTemplate> {
2148         match self.krate {
2149             Some(_) => None,
2150             None => Some(hir_def::builtin_attr::INERT_ATTRIBUTES[self.idx].template),
2151         }
2152     }
2153 }
2154
2155 #[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)]
2156 pub struct ToolModule {
2157     krate: Option<CrateId>,
2158     idx: usize,
2159 }
2160
2161 impl ToolModule {
2162     // FIXME: consider crates\hir_def\src\nameres\attr_resolution.rs?
2163     pub(crate) fn by_name(db: &dyn HirDatabase, krate: Crate, name: &str) -> Option<Self> {
2164         if let builtin @ Some(_) = Self::builtin(name) {
2165             return builtin;
2166         }
2167         let idx = db.crate_def_map(krate.id).registered_tools().iter().position(|it| it == name)?;
2168         Some(ToolModule { krate: Some(krate.id), idx })
2169     }
2170
2171     pub(crate) fn builtin(name: &str) -> Option<Self> {
2172         hir_def::builtin_attr::TOOL_MODULES
2173             .iter()
2174             .position(|&tool| tool == name)
2175             .map(|idx| ToolModule { krate: None, idx })
2176     }
2177
2178     pub fn name(&self, db: &dyn HirDatabase) -> SmolStr {
2179         // FIXME: Return a `Name` here
2180         match self.krate {
2181             Some(krate) => db.crate_def_map(krate).registered_tools()[self.idx].clone(),
2182             None => SmolStr::new(hir_def::builtin_attr::TOOL_MODULES[self.idx]),
2183         }
2184     }
2185 }
2186
2187 #[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)]
2188 pub struct Label {
2189     pub(crate) parent: DefWithBodyId,
2190     pub(crate) label_id: LabelId,
2191 }
2192
2193 impl Label {
2194     pub fn module(self, db: &dyn HirDatabase) -> Module {
2195         self.parent(db).module(db)
2196     }
2197
2198     pub fn parent(self, _db: &dyn HirDatabase) -> DefWithBody {
2199         self.parent.into()
2200     }
2201
2202     pub fn name(self, db: &dyn HirDatabase) -> Name {
2203         let body = db.body(self.parent);
2204         body[self.label_id].name.clone()
2205     }
2206
2207     pub fn source(self, db: &dyn HirDatabase) -> InFile<ast::Label> {
2208         let (_body, source_map) = db.body_with_source_map(self.parent);
2209         let src = source_map.label_syntax(self.label_id);
2210         let root = src.file_syntax(db.upcast());
2211         src.map(|ast| ast.to_node(&root))
2212     }
2213 }
2214
2215 #[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)]
2216 pub enum GenericParam {
2217     TypeParam(TypeParam),
2218     LifetimeParam(LifetimeParam),
2219     ConstParam(ConstParam),
2220 }
2221 impl_from!(TypeParam, LifetimeParam, ConstParam for GenericParam);
2222
2223 impl GenericParam {
2224     pub fn module(self, db: &dyn HirDatabase) -> Module {
2225         match self {
2226             GenericParam::TypeParam(it) => it.module(db),
2227             GenericParam::LifetimeParam(it) => it.module(db),
2228             GenericParam::ConstParam(it) => it.module(db),
2229         }
2230     }
2231
2232     pub fn name(self, db: &dyn HirDatabase) -> Name {
2233         match self {
2234             GenericParam::TypeParam(it) => it.name(db),
2235             GenericParam::LifetimeParam(it) => it.name(db),
2236             GenericParam::ConstParam(it) => it.name(db),
2237         }
2238     }
2239 }
2240
2241 #[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)]
2242 pub struct TypeParam {
2243     pub(crate) id: TypeParamId,
2244 }
2245
2246 impl TypeParam {
2247     pub fn name(self, db: &dyn HirDatabase) -> Name {
2248         let params = db.generic_params(self.id.parent);
2249         params.types[self.id.local_id].name.clone().unwrap_or_else(Name::missing)
2250     }
2251
2252     pub fn module(self, db: &dyn HirDatabase) -> Module {
2253         self.id.parent.module(db.upcast()).into()
2254     }
2255
2256     pub fn ty(self, db: &dyn HirDatabase) -> Type {
2257         let resolver = self.id.parent.resolver(db.upcast());
2258         let krate = self.id.parent.module(db.upcast()).krate();
2259         let ty = TyKind::Placeholder(hir_ty::to_placeholder_idx(db, self.id)).intern(Interner);
2260         Type::new_with_resolver_inner(db, krate, &resolver, ty)
2261     }
2262
2263     /// FIXME: this only lists trait bounds from the item defining the type
2264     /// parameter, not additional bounds that might be added e.g. by a method if
2265     /// the parameter comes from an impl!
2266     pub fn trait_bounds(self, db: &dyn HirDatabase) -> Vec<Trait> {
2267         db.generic_predicates_for_param(self.id.parent, self.id, None)
2268             .iter()
2269             .filter_map(|pred| match &pred.skip_binders().skip_binders() {
2270                 hir_ty::WhereClause::Implemented(trait_ref) => {
2271                     Some(Trait::from(trait_ref.hir_trait_id()))
2272                 }
2273                 _ => None,
2274             })
2275             .collect()
2276     }
2277
2278     pub fn default(self, db: &dyn HirDatabase) -> Option<Type> {
2279         let params = db.generic_defaults(self.id.parent);
2280         let local_idx = hir_ty::param_idx(db, self.id)?;
2281         let resolver = self.id.parent.resolver(db.upcast());
2282         let krate = self.id.parent.module(db.upcast()).krate();
2283         let ty = params.get(local_idx)?.clone();
2284         let subst = TyBuilder::type_params_subst(db, self.id.parent);
2285         let ty = ty.substitute(Interner, &subst_prefix(&subst, local_idx));
2286         Some(Type::new_with_resolver_inner(db, krate, &resolver, ty))
2287     }
2288 }
2289
2290 #[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)]
2291 pub struct LifetimeParam {
2292     pub(crate) id: LifetimeParamId,
2293 }
2294
2295 impl LifetimeParam {
2296     pub fn name(self, db: &dyn HirDatabase) -> Name {
2297         let params = db.generic_params(self.id.parent);
2298         params.lifetimes[self.id.local_id].name.clone()
2299     }
2300
2301     pub fn module(self, db: &dyn HirDatabase) -> Module {
2302         self.id.parent.module(db.upcast()).into()
2303     }
2304
2305     pub fn parent(self, _db: &dyn HirDatabase) -> GenericDef {
2306         self.id.parent.into()
2307     }
2308 }
2309
2310 #[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)]
2311 pub struct ConstParam {
2312     pub(crate) id: ConstParamId,
2313 }
2314
2315 impl ConstParam {
2316     pub fn name(self, db: &dyn HirDatabase) -> Name {
2317         let params = db.generic_params(self.id.parent);
2318         params.consts[self.id.local_id].name.clone()
2319     }
2320
2321     pub fn module(self, db: &dyn HirDatabase) -> Module {
2322         self.id.parent.module(db.upcast()).into()
2323     }
2324
2325     pub fn parent(self, _db: &dyn HirDatabase) -> GenericDef {
2326         self.id.parent.into()
2327     }
2328
2329     pub fn ty(self, db: &dyn HirDatabase) -> Type {
2330         let def = self.id.parent;
2331         let krate = def.module(db.upcast()).krate();
2332         Type::new(db, krate, def, db.const_param_ty(self.id))
2333     }
2334 }
2335
2336 #[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
2337 pub struct Impl {
2338     pub(crate) id: ImplId,
2339 }
2340
2341 impl Impl {
2342     pub fn all_in_crate(db: &dyn HirDatabase, krate: Crate) -> Vec<Impl> {
2343         let inherent = db.inherent_impls_in_crate(krate.id);
2344         let trait_ = db.trait_impls_in_crate(krate.id);
2345
2346         inherent.all_impls().chain(trait_.all_impls()).map(Self::from).collect()
2347     }
2348
2349     pub fn all_for_type(db: &dyn HirDatabase, Type { krate, ty, .. }: Type) -> Vec<Impl> {
2350         let def_crates = match method_resolution::def_crates(db, &ty, krate) {
2351             Some(def_crates) => def_crates,
2352             None => return Vec::new(),
2353         };
2354
2355         let filter = |impl_def: &Impl| {
2356             let self_ty = impl_def.self_ty(db);
2357             let rref = self_ty.remove_ref();
2358             ty.equals_ctor(rref.as_ref().map_or(&self_ty.ty, |it| &it.ty))
2359         };
2360
2361         let fp = TyFingerprint::for_inherent_impl(&ty);
2362         let fp = match fp {
2363             Some(fp) => fp,
2364             None => return Vec::new(),
2365         };
2366
2367         let mut all = Vec::new();
2368         def_crates.iter().for_each(|&id| {
2369             all.extend(
2370                 db.inherent_impls_in_crate(id)
2371                     .for_self_ty(&ty)
2372                     .iter()
2373                     .cloned()
2374                     .map(Self::from)
2375                     .filter(filter),
2376             )
2377         });
2378         for id in def_crates
2379             .iter()
2380             .flat_map(|&id| Crate { id }.transitive_reverse_dependencies(db))
2381             .map(|Crate { id }| id)
2382             .chain(def_crates.iter().copied())
2383             .unique()
2384         {
2385             all.extend(
2386                 db.trait_impls_in_crate(id)
2387                     .for_self_ty_without_blanket_impls(fp)
2388                     .map(Self::from)
2389                     .filter(filter),
2390             );
2391         }
2392         all
2393     }
2394
2395     pub fn all_for_trait(db: &dyn HirDatabase, trait_: Trait) -> Vec<Impl> {
2396         let krate = trait_.module(db).krate();
2397         let mut all = Vec::new();
2398         for Crate { id } in krate.transitive_reverse_dependencies(db).into_iter() {
2399             let impls = db.trait_impls_in_crate(id);
2400             all.extend(impls.for_trait(trait_.id).map(Self::from))
2401         }
2402         all
2403     }
2404
2405     // FIXME: the return type is wrong. This should be a hir version of
2406     // `TraitRef` (to account for parameters and qualifiers)
2407     pub fn trait_(self, db: &dyn HirDatabase) -> Option<Trait> {
2408         let trait_ref = db.impl_trait(self.id)?.skip_binders().clone();
2409         let id = hir_ty::from_chalk_trait_id(trait_ref.trait_id);
2410         Some(Trait { id })
2411     }
2412
2413     pub fn self_ty(self, db: &dyn HirDatabase) -> Type {
2414         let impl_data = db.impl_data(self.id);
2415         let resolver = self.id.resolver(db.upcast());
2416         let krate = self.id.lookup(db.upcast()).container.krate();
2417         let ctx = hir_ty::TyLoweringContext::new(db, &resolver);
2418         let ty = ctx.lower_ty(&impl_data.self_ty);
2419         Type::new_with_resolver_inner(db, krate, &resolver, ty)
2420     }
2421
2422     pub fn items(self, db: &dyn HirDatabase) -> Vec<AssocItem> {
2423         db.impl_data(self.id).items.iter().map(|it| (*it).into()).collect()
2424     }
2425
2426     pub fn is_negative(self, db: &dyn HirDatabase) -> bool {
2427         db.impl_data(self.id).is_negative
2428     }
2429
2430     pub fn module(self, db: &dyn HirDatabase) -> Module {
2431         self.id.lookup(db.upcast()).container.into()
2432     }
2433
2434     pub fn is_builtin_derive(self, db: &dyn HirDatabase) -> Option<InFile<ast::Attr>> {
2435         let src = self.source(db)?;
2436         let item = src.file_id.is_builtin_derive(db.upcast())?;
2437         let hygenic = hir_expand::hygiene::Hygiene::new(db.upcast(), item.file_id);
2438
2439         // FIXME: handle `cfg_attr`
2440         let attr = item
2441             .value
2442             .attrs()
2443             .filter_map(|it| {
2444                 let path = ModPath::from_src(db.upcast(), it.path()?, &hygenic)?;
2445                 if path.as_ident()?.to_smol_str() == "derive" {
2446                     Some(it)
2447                 } else {
2448                     None
2449                 }
2450             })
2451             .last()?;
2452
2453         Some(item.with_value(attr))
2454     }
2455 }
2456
2457 #[derive(Clone, PartialEq, Eq, Debug)]
2458 pub struct Type {
2459     krate: CrateId,
2460     env: Arc<TraitEnvironment>,
2461     ty: Ty,
2462 }
2463
2464 impl Type {
2465     pub(crate) fn new_with_resolver(
2466         db: &dyn HirDatabase,
2467         resolver: &Resolver,
2468         ty: Ty,
2469     ) -> Option<Type> {
2470         let krate = resolver.krate()?;
2471         Some(Type::new_with_resolver_inner(db, krate, resolver, ty))
2472     }
2473     pub(crate) fn new_with_resolver_inner(
2474         db: &dyn HirDatabase,
2475         krate: CrateId,
2476         resolver: &Resolver,
2477         ty: Ty,
2478     ) -> Type {
2479         let environment = resolver
2480             .generic_def()
2481             .map_or_else(|| Arc::new(TraitEnvironment::empty(krate)), |d| db.trait_environment(d));
2482         Type { krate, env: environment, ty }
2483     }
2484
2485     fn new(db: &dyn HirDatabase, krate: CrateId, lexical_env: impl HasResolver, ty: Ty) -> Type {
2486         let resolver = lexical_env.resolver(db.upcast());
2487         let environment = resolver
2488             .generic_def()
2489             .map_or_else(|| Arc::new(TraitEnvironment::empty(krate)), |d| db.trait_environment(d));
2490         Type { krate, env: environment, ty }
2491     }
2492
2493     fn from_def(
2494         db: &dyn HirDatabase,
2495         krate: CrateId,
2496         def: impl HasResolver + Into<TyDefId>,
2497     ) -> Type {
2498         let ty = TyBuilder::def_ty(db, def.into()).fill_with_unknown().build();
2499         Type::new(db, krate, def, ty)
2500     }
2501
2502     pub fn new_slice(ty: Type) -> Type {
2503         Type { krate: ty.krate, env: ty.env, ty: TyBuilder::slice(ty.ty) }
2504     }
2505
2506     pub fn is_unit(&self) -> bool {
2507         matches!(self.ty.kind(Interner), TyKind::Tuple(0, ..))
2508     }
2509
2510     pub fn is_bool(&self) -> bool {
2511         matches!(self.ty.kind(Interner), TyKind::Scalar(Scalar::Bool))
2512     }
2513
2514     pub fn is_never(&self) -> bool {
2515         matches!(self.ty.kind(Interner), TyKind::Never)
2516     }
2517
2518     pub fn is_mutable_reference(&self) -> bool {
2519         matches!(self.ty.kind(Interner), TyKind::Ref(hir_ty::Mutability::Mut, ..))
2520     }
2521
2522     pub fn is_reference(&self) -> bool {
2523         matches!(self.ty.kind(Interner), TyKind::Ref(..))
2524     }
2525
2526     pub fn is_usize(&self) -> bool {
2527         matches!(self.ty.kind(Interner), TyKind::Scalar(Scalar::Uint(UintTy::Usize)))
2528     }
2529
2530     pub fn remove_ref(&self) -> Option<Type> {
2531         match &self.ty.kind(Interner) {
2532             TyKind::Ref(.., ty) => Some(self.derived(ty.clone())),
2533             _ => None,
2534         }
2535     }
2536
2537     pub fn strip_references(&self) -> Type {
2538         self.derived(self.ty.strip_references().clone())
2539     }
2540
2541     pub fn is_unknown(&self) -> bool {
2542         self.ty.is_unknown()
2543     }
2544
2545     /// Checks that particular type `ty` implements `std::future::Future`.
2546     /// This function is used in `.await` syntax completion.
2547     pub fn impls_future(&self, db: &dyn HirDatabase) -> bool {
2548         // No special case for the type of async block, since Chalk can figure it out.
2549
2550         let krate = self.krate;
2551
2552         let std_future_trait =
2553             db.lang_item(krate, SmolStr::new_inline("future_trait")).and_then(|it| it.as_trait());
2554         let std_future_trait = match std_future_trait {
2555             Some(it) => it,
2556             None => return false,
2557         };
2558
2559         let canonical_ty =
2560             Canonical { value: self.ty.clone(), binders: CanonicalVarKinds::empty(Interner) };
2561         method_resolution::implements_trait(
2562             &canonical_ty,
2563             db,
2564             self.env.clone(),
2565             krate,
2566             std_future_trait,
2567         )
2568     }
2569
2570     /// Checks that particular type `ty` implements `std::ops::FnOnce`.
2571     ///
2572     /// This function can be used to check if a particular type is callable, since FnOnce is a
2573     /// supertrait of Fn and FnMut, so all callable types implements at least FnOnce.
2574     pub fn impls_fnonce(&self, db: &dyn HirDatabase) -> bool {
2575         let krate = self.krate;
2576
2577         let fnonce_trait = match FnTrait::FnOnce.get_id(db, krate) {
2578             Some(it) => it,
2579             None => return false,
2580         };
2581
2582         let canonical_ty =
2583             Canonical { value: self.ty.clone(), binders: CanonicalVarKinds::empty(Interner) };
2584         method_resolution::implements_trait_unique(
2585             &canonical_ty,
2586             db,
2587             self.env.clone(),
2588             krate,
2589             fnonce_trait,
2590         )
2591     }
2592
2593     pub fn impls_trait(&self, db: &dyn HirDatabase, trait_: Trait, args: &[Type]) -> bool {
2594         let trait_ref = TyBuilder::trait_ref(db, trait_.id)
2595             .push(self.ty.clone())
2596             .fill(args.iter().map(|t| t.ty.clone()))
2597             .build();
2598
2599         let goal = Canonical {
2600             value: hir_ty::InEnvironment::new(&self.env.env, trait_ref.cast(Interner)),
2601             binders: CanonicalVarKinds::empty(Interner),
2602         };
2603
2604         db.trait_solve(self.krate, goal).is_some()
2605     }
2606
2607     pub fn normalize_trait_assoc_type(
2608         &self,
2609         db: &dyn HirDatabase,
2610         args: &[Type],
2611         alias: TypeAlias,
2612     ) -> Option<Type> {
2613         let projection = TyBuilder::assoc_type_projection(db, alias.id)
2614             .push(self.ty.clone())
2615             .fill(args.iter().map(|t| t.ty.clone()))
2616             .build();
2617         let goal = hir_ty::make_canonical(
2618             InEnvironment::new(
2619                 &self.env.env,
2620                 AliasEq {
2621                     alias: AliasTy::Projection(projection),
2622                     ty: TyKind::BoundVar(BoundVar::new(DebruijnIndex::INNERMOST, 0))
2623                         .intern(Interner),
2624                 }
2625                 .cast(Interner),
2626             ),
2627             [TyVariableKind::General].into_iter(),
2628         );
2629
2630         match db.trait_solve(self.krate, goal)? {
2631             Solution::Unique(s) => s
2632                 .value
2633                 .subst
2634                 .as_slice(Interner)
2635                 .first()
2636                 .map(|ty| self.derived(ty.assert_ty_ref(Interner).clone())),
2637             Solution::Ambig(_) => None,
2638         }
2639     }
2640
2641     pub fn is_copy(&self, db: &dyn HirDatabase) -> bool {
2642         let lang_item = db.lang_item(self.krate, SmolStr::new_inline("copy"));
2643         let copy_trait = match lang_item {
2644             Some(LangItemTarget::TraitId(it)) => it,
2645             _ => return false,
2646         };
2647         self.impls_trait(db, copy_trait.into(), &[])
2648     }
2649
2650     pub fn as_callable(&self, db: &dyn HirDatabase) -> Option<Callable> {
2651         let def = self.ty.callable_def(db);
2652
2653         let sig = self.ty.callable_sig(db)?;
2654         Some(Callable { ty: self.clone(), sig, def, is_bound_method: false })
2655     }
2656
2657     pub fn is_closure(&self) -> bool {
2658         matches!(&self.ty.kind(Interner), TyKind::Closure { .. })
2659     }
2660
2661     pub fn is_fn(&self) -> bool {
2662         matches!(&self.ty.kind(Interner), TyKind::FnDef(..) | TyKind::Function { .. })
2663     }
2664
2665     pub fn is_array(&self) -> bool {
2666         matches!(&self.ty.kind(Interner), TyKind::Array(..))
2667     }
2668
2669     pub fn is_packed(&self, db: &dyn HirDatabase) -> bool {
2670         let adt_id = match *self.ty.kind(Interner) {
2671             TyKind::Adt(hir_ty::AdtId(adt_id), ..) => adt_id,
2672             _ => return false,
2673         };
2674
2675         let adt = adt_id.into();
2676         match adt {
2677             Adt::Struct(s) => matches!(s.repr(db), Some(ReprKind::Packed)),
2678             _ => false,
2679         }
2680     }
2681
2682     pub fn is_raw_ptr(&self) -> bool {
2683         matches!(&self.ty.kind(Interner), TyKind::Raw(..))
2684     }
2685
2686     pub fn contains_unknown(&self) -> bool {
2687         return go(&self.ty);
2688
2689         fn go(ty: &Ty) -> bool {
2690             match ty.kind(Interner) {
2691                 TyKind::Error => true,
2692
2693                 TyKind::Adt(_, substs)
2694                 | TyKind::AssociatedType(_, substs)
2695                 | TyKind::Tuple(_, substs)
2696                 | TyKind::OpaqueType(_, substs)
2697                 | TyKind::FnDef(_, substs)
2698                 | TyKind::Closure(_, substs) => {
2699                     substs.iter(Interner).filter_map(|a| a.ty(Interner)).any(go)
2700                 }
2701
2702                 TyKind::Array(_ty, len) if len.is_unknown() => true,
2703                 TyKind::Array(ty, _)
2704                 | TyKind::Slice(ty)
2705                 | TyKind::Raw(_, ty)
2706                 | TyKind::Ref(_, _, ty) => go(ty),
2707
2708                 TyKind::Scalar(_)
2709                 | TyKind::Str
2710                 | TyKind::Never
2711                 | TyKind::Placeholder(_)
2712                 | TyKind::BoundVar(_)
2713                 | TyKind::InferenceVar(_, _)
2714                 | TyKind::Dyn(_)
2715                 | TyKind::Function(_)
2716                 | TyKind::Alias(_)
2717                 | TyKind::Foreign(_)
2718                 | TyKind::Generator(..)
2719                 | TyKind::GeneratorWitness(..) => false,
2720             }
2721         }
2722     }
2723
2724     pub fn fields(&self, db: &dyn HirDatabase) -> Vec<(Field, Type)> {
2725         let (variant_id, substs) = match self.ty.kind(Interner) {
2726             TyKind::Adt(hir_ty::AdtId(AdtId::StructId(s)), substs) => ((*s).into(), substs),
2727             TyKind::Adt(hir_ty::AdtId(AdtId::UnionId(u)), substs) => ((*u).into(), substs),
2728             _ => return Vec::new(),
2729         };
2730
2731         db.field_types(variant_id)
2732             .iter()
2733             .map(|(local_id, ty)| {
2734                 let def = Field { parent: variant_id.into(), id: local_id };
2735                 let ty = ty.clone().substitute(Interner, substs);
2736                 (def, self.derived(ty))
2737             })
2738             .collect()
2739     }
2740
2741     pub fn tuple_fields(&self, _db: &dyn HirDatabase) -> Vec<Type> {
2742         if let TyKind::Tuple(_, substs) = &self.ty.kind(Interner) {
2743             substs
2744                 .iter(Interner)
2745                 .map(|ty| self.derived(ty.assert_ty_ref(Interner).clone()))
2746                 .collect()
2747         } else {
2748             Vec::new()
2749         }
2750     }
2751
2752     pub fn autoderef<'a>(&'a self, db: &'a dyn HirDatabase) -> impl Iterator<Item = Type> + 'a {
2753         self.autoderef_(db).map(move |ty| self.derived(ty))
2754     }
2755
2756     pub fn autoderef_<'a>(&'a self, db: &'a dyn HirDatabase) -> impl Iterator<Item = Ty> + 'a {
2757         // There should be no inference vars in types passed here
2758         let canonical = hir_ty::replace_errors_with_variables(&self.ty);
2759         let environment = self.env.env.clone();
2760         let ty = InEnvironment { goal: canonical, environment };
2761         autoderef(db, Some(self.krate), ty).map(|canonical| canonical.value)
2762     }
2763
2764     // This would be nicer if it just returned an iterator, but that runs into
2765     // lifetime problems, because we need to borrow temp `CrateImplDefs`.
2766     pub fn iterate_assoc_items<T>(
2767         &self,
2768         db: &dyn HirDatabase,
2769         krate: Crate,
2770         mut callback: impl FnMut(AssocItem) -> Option<T>,
2771     ) -> Option<T> {
2772         let mut slot = None;
2773         self.iterate_assoc_items_dyn(db, krate, &mut |assoc_item_id| {
2774             slot = callback(assoc_item_id.into());
2775             slot.is_some()
2776         });
2777         slot
2778     }
2779
2780     fn iterate_assoc_items_dyn(
2781         &self,
2782         db: &dyn HirDatabase,
2783         krate: Crate,
2784         callback: &mut dyn FnMut(AssocItemId) -> bool,
2785     ) {
2786         let def_crates = match method_resolution::def_crates(db, &self.ty, krate.id) {
2787             Some(it) => it,
2788             None => return,
2789         };
2790         for krate in def_crates {
2791             let impls = db.inherent_impls_in_crate(krate);
2792
2793             for impl_def in impls.for_self_ty(&self.ty) {
2794                 for &item in db.impl_data(*impl_def).items.iter() {
2795                     if callback(item) {
2796                         return;
2797                     }
2798                 }
2799             }
2800         }
2801     }
2802
2803     pub fn type_arguments(&self) -> impl Iterator<Item = Type> + '_ {
2804         self.ty
2805             .strip_references()
2806             .as_adt()
2807             .into_iter()
2808             .flat_map(|(_, substs)| substs.iter(Interner))
2809             .filter_map(|arg| arg.ty(Interner).cloned())
2810             .map(move |ty| self.derived(ty))
2811     }
2812
2813     pub fn iterate_method_candidates<T>(
2814         &self,
2815         db: &dyn HirDatabase,
2816         krate: Crate,
2817         traits_in_scope: &FxHashSet<TraitId>,
2818         with_local_impls: Option<Module>,
2819         name: Option<&Name>,
2820         mut callback: impl FnMut(Type, Function) -> Option<T>,
2821     ) -> Option<T> {
2822         let _p = profile::span("iterate_method_candidates");
2823         let mut slot = None;
2824
2825         self.iterate_method_candidates_dyn(
2826             db,
2827             krate,
2828             traits_in_scope,
2829             with_local_impls,
2830             name,
2831             &mut |ty, assoc_item_id| {
2832                 if let AssocItemId::FunctionId(func) = assoc_item_id {
2833                     if let Some(res) = callback(self.derived(ty.clone()), func.into()) {
2834                         slot = Some(res);
2835                         return ControlFlow::Break(());
2836                     }
2837                 }
2838                 ControlFlow::Continue(())
2839             },
2840         );
2841         slot
2842     }
2843
2844     fn iterate_method_candidates_dyn(
2845         &self,
2846         db: &dyn HirDatabase,
2847         krate: Crate,
2848         traits_in_scope: &FxHashSet<TraitId>,
2849         with_local_impls: Option<Module>,
2850         name: Option<&Name>,
2851         callback: &mut dyn FnMut(&Ty, AssocItemId) -> ControlFlow<()>,
2852     ) {
2853         // There should be no inference vars in types passed here
2854         let canonical = hir_ty::replace_errors_with_variables(&self.ty);
2855
2856         let env = self.env.clone();
2857         let krate = krate.id;
2858
2859         method_resolution::iterate_method_candidates_dyn(
2860             &canonical,
2861             db,
2862             env,
2863             krate,
2864             traits_in_scope,
2865             with_local_impls.and_then(|b| b.id.containing_block()).into(),
2866             name,
2867             method_resolution::LookupMode::MethodCall,
2868             &mut |ty, id| callback(&ty.value, id),
2869         );
2870     }
2871
2872     pub fn iterate_path_candidates<T>(
2873         &self,
2874         db: &dyn HirDatabase,
2875         krate: Crate,
2876         traits_in_scope: &FxHashSet<TraitId>,
2877         with_local_impls: Option<Module>,
2878         name: Option<&Name>,
2879         mut callback: impl FnMut(Type, AssocItem) -> Option<T>,
2880     ) -> Option<T> {
2881         let _p = profile::span("iterate_path_candidates");
2882         let mut slot = None;
2883         self.iterate_path_candidates_dyn(
2884             db,
2885             krate,
2886             traits_in_scope,
2887             with_local_impls,
2888             name,
2889             &mut |ty, assoc_item_id| {
2890                 if let Some(res) = callback(self.derived(ty.clone()), assoc_item_id.into()) {
2891                     slot = Some(res);
2892                     return ControlFlow::Break(());
2893                 }
2894                 ControlFlow::Continue(())
2895             },
2896         );
2897         slot
2898     }
2899
2900     fn iterate_path_candidates_dyn(
2901         &self,
2902         db: &dyn HirDatabase,
2903         krate: Crate,
2904         traits_in_scope: &FxHashSet<TraitId>,
2905         with_local_impls: Option<Module>,
2906         name: Option<&Name>,
2907         callback: &mut dyn FnMut(&Ty, AssocItemId) -> ControlFlow<()>,
2908     ) {
2909         let canonical = hir_ty::replace_errors_with_variables(&self.ty);
2910
2911         let env = self.env.clone();
2912         let krate = krate.id;
2913
2914         method_resolution::iterate_method_candidates_dyn(
2915             &canonical,
2916             db,
2917             env,
2918             krate,
2919             traits_in_scope,
2920             with_local_impls.and_then(|b| b.id.containing_block()).into(),
2921             name,
2922             method_resolution::LookupMode::Path,
2923             &mut |ty, id| callback(&ty.value, id),
2924         );
2925     }
2926
2927     pub fn as_adt(&self) -> Option<Adt> {
2928         let (adt, _subst) = self.ty.as_adt()?;
2929         Some(adt.into())
2930     }
2931
2932     pub fn as_builtin(&self) -> Option<BuiltinType> {
2933         self.ty.as_builtin().map(|inner| BuiltinType { inner })
2934     }
2935
2936     pub fn as_dyn_trait(&self) -> Option<Trait> {
2937         self.ty.dyn_trait().map(Into::into)
2938     }
2939
2940     /// If a type can be represented as `dyn Trait`, returns all traits accessible via this type,
2941     /// or an empty iterator otherwise.
2942     pub fn applicable_inherent_traits<'a>(
2943         &'a self,
2944         db: &'a dyn HirDatabase,
2945     ) -> impl Iterator<Item = Trait> + 'a {
2946         let _p = profile::span("applicable_inherent_traits");
2947         self.autoderef_(db)
2948             .filter_map(|ty| ty.dyn_trait())
2949             .flat_map(move |dyn_trait_id| hir_ty::all_super_traits(db.upcast(), dyn_trait_id))
2950             .map(Trait::from)
2951     }
2952
2953     pub fn env_traits<'a>(&'a self, db: &'a dyn HirDatabase) -> impl Iterator<Item = Trait> + 'a {
2954         let _p = profile::span("env_traits");
2955         self.autoderef_(db)
2956             .filter(|ty| matches!(ty.kind(Interner), TyKind::Placeholder(_)))
2957             .flat_map(|ty| {
2958                 self.env
2959                     .traits_in_scope_from_clauses(ty)
2960                     .flat_map(|t| hir_ty::all_super_traits(db.upcast(), t))
2961             })
2962             .map(Trait::from)
2963     }
2964
2965     pub fn as_impl_traits(&self, db: &dyn HirDatabase) -> Option<impl Iterator<Item = Trait>> {
2966         self.ty.impl_trait_bounds(db).map(|it| {
2967             it.into_iter().filter_map(|pred| match pred.skip_binders() {
2968                 hir_ty::WhereClause::Implemented(trait_ref) => {
2969                     Some(Trait::from(trait_ref.hir_trait_id()))
2970                 }
2971                 _ => None,
2972             })
2973         })
2974     }
2975
2976     pub fn as_associated_type_parent_trait(&self, db: &dyn HirDatabase) -> Option<Trait> {
2977         self.ty.associated_type_parent_trait(db).map(Into::into)
2978     }
2979
2980     fn derived(&self, ty: Ty) -> Type {
2981         Type { krate: self.krate, env: self.env.clone(), ty }
2982     }
2983
2984     pub fn walk(&self, db: &dyn HirDatabase, mut cb: impl FnMut(Type)) {
2985         // TypeWalk::walk for a Ty at first visits parameters and only after that the Ty itself.
2986         // We need a different order here.
2987
2988         fn walk_substs(
2989             db: &dyn HirDatabase,
2990             type_: &Type,
2991             substs: &Substitution,
2992             cb: &mut impl FnMut(Type),
2993         ) {
2994             for ty in substs.iter(Interner).filter_map(|a| a.ty(Interner)) {
2995                 walk_type(db, &type_.derived(ty.clone()), cb);
2996             }
2997         }
2998
2999         fn walk_bounds(
3000             db: &dyn HirDatabase,
3001             type_: &Type,
3002             bounds: &[QuantifiedWhereClause],
3003             cb: &mut impl FnMut(Type),
3004         ) {
3005             for pred in bounds {
3006                 if let WhereClause::Implemented(trait_ref) = pred.skip_binders() {
3007                     cb(type_.clone());
3008                     // skip the self type. it's likely the type we just got the bounds from
3009                     for ty in
3010                         trait_ref.substitution.iter(Interner).skip(1).filter_map(|a| a.ty(Interner))
3011                     {
3012                         walk_type(db, &type_.derived(ty.clone()), cb);
3013                     }
3014                 }
3015             }
3016         }
3017
3018         fn walk_type(db: &dyn HirDatabase, type_: &Type, cb: &mut impl FnMut(Type)) {
3019             let ty = type_.ty.strip_references();
3020             match ty.kind(Interner) {
3021                 TyKind::Adt(_, substs) => {
3022                     cb(type_.derived(ty.clone()));
3023                     walk_substs(db, type_, substs, cb);
3024                 }
3025                 TyKind::AssociatedType(_, substs) => {
3026                     if ty.associated_type_parent_trait(db).is_some() {
3027                         cb(type_.derived(ty.clone()));
3028                     }
3029                     walk_substs(db, type_, substs, cb);
3030                 }
3031                 TyKind::OpaqueType(_, subst) => {
3032                     if let Some(bounds) = ty.impl_trait_bounds(db) {
3033                         walk_bounds(db, &type_.derived(ty.clone()), &bounds, cb);
3034                     }
3035
3036                     walk_substs(db, type_, subst, cb);
3037                 }
3038                 TyKind::Alias(AliasTy::Opaque(opaque_ty)) => {
3039                     if let Some(bounds) = ty.impl_trait_bounds(db) {
3040                         walk_bounds(db, &type_.derived(ty.clone()), &bounds, cb);
3041                     }
3042
3043                     walk_substs(db, type_, &opaque_ty.substitution, cb);
3044                 }
3045                 TyKind::Placeholder(_) => {
3046                     if let Some(bounds) = ty.impl_trait_bounds(db) {
3047                         walk_bounds(db, &type_.derived(ty.clone()), &bounds, cb);
3048                     }
3049                 }
3050                 TyKind::Dyn(bounds) => {
3051                     walk_bounds(
3052                         db,
3053                         &type_.derived(ty.clone()),
3054                         bounds.bounds.skip_binders().interned(),
3055                         cb,
3056                     );
3057                 }
3058
3059                 TyKind::Ref(_, _, ty)
3060                 | TyKind::Raw(_, ty)
3061                 | TyKind::Array(ty, _)
3062                 | TyKind::Slice(ty) => {
3063                     walk_type(db, &type_.derived(ty.clone()), cb);
3064                 }
3065
3066                 TyKind::FnDef(_, substs)
3067                 | TyKind::Tuple(_, substs)
3068                 | TyKind::Closure(.., substs) => {
3069                     walk_substs(db, type_, substs, cb);
3070                 }
3071                 TyKind::Function(hir_ty::FnPointer { substitution, .. }) => {
3072                     walk_substs(db, type_, &substitution.0, cb);
3073                 }
3074
3075                 _ => {}
3076             }
3077         }
3078
3079         walk_type(db, self, &mut cb);
3080     }
3081
3082     pub fn could_unify_with(&self, db: &dyn HirDatabase, other: &Type) -> bool {
3083         let tys = hir_ty::replace_errors_with_variables(&(self.ty.clone(), other.ty.clone()));
3084         could_unify(db, self.env.clone(), &tys)
3085     }
3086 }
3087
3088 // FIXME: closures
3089 #[derive(Debug)]
3090 pub struct Callable {
3091     ty: Type,
3092     sig: CallableSig,
3093     def: Option<CallableDefId>,
3094     pub(crate) is_bound_method: bool,
3095 }
3096
3097 pub enum CallableKind {
3098     Function(Function),
3099     TupleStruct(Struct),
3100     TupleEnumVariant(Variant),
3101     Closure,
3102 }
3103
3104 impl Callable {
3105     pub fn kind(&self) -> CallableKind {
3106         match self.def {
3107             Some(CallableDefId::FunctionId(it)) => CallableKind::Function(it.into()),
3108             Some(CallableDefId::StructId(it)) => CallableKind::TupleStruct(it.into()),
3109             Some(CallableDefId::EnumVariantId(it)) => CallableKind::TupleEnumVariant(it.into()),
3110             None => CallableKind::Closure,
3111         }
3112     }
3113     pub fn receiver_param(&self, db: &dyn HirDatabase) -> Option<ast::SelfParam> {
3114         let func = match self.def {
3115             Some(CallableDefId::FunctionId(it)) if self.is_bound_method => it,
3116             _ => return None,
3117         };
3118         let src = func.lookup(db.upcast()).source(db.upcast());
3119         let param_list = src.value.param_list()?;
3120         param_list.self_param()
3121     }
3122     pub fn n_params(&self) -> usize {
3123         self.sig.params().len() - if self.is_bound_method { 1 } else { 0 }
3124     }
3125     pub fn params(
3126         &self,
3127         db: &dyn HirDatabase,
3128     ) -> Vec<(Option<Either<ast::SelfParam, ast::Pat>>, Type)> {
3129         let types = self
3130             .sig
3131             .params()
3132             .iter()
3133             .skip(if self.is_bound_method { 1 } else { 0 })
3134             .map(|ty| self.ty.derived(ty.clone()));
3135         let patterns = match self.def {
3136             Some(CallableDefId::FunctionId(func)) => {
3137                 let src = func.lookup(db.upcast()).source(db.upcast());
3138                 src.value.param_list().map(|param_list| {
3139                     param_list
3140                         .self_param()
3141                         .map(|it| Some(Either::Left(it)))
3142                         .filter(|_| !self.is_bound_method)
3143                         .into_iter()
3144                         .chain(param_list.params().map(|it| it.pat().map(Either::Right)))
3145                 })
3146             }
3147             _ => None,
3148         };
3149         patterns.into_iter().flatten().chain(iter::repeat(None)).zip(types).collect()
3150     }
3151     pub fn return_type(&self) -> Type {
3152         self.ty.derived(self.sig.ret().clone())
3153     }
3154 }
3155
3156 /// For IDE only
3157 #[derive(Copy, Clone, Debug, PartialEq, Eq, Hash)]
3158 pub enum ScopeDef {
3159     ModuleDef(ModuleDef),
3160     MacroDef(MacroDef),
3161     GenericParam(GenericParam),
3162     ImplSelfType(Impl),
3163     AdtSelfType(Adt),
3164     Local(Local),
3165     Label(Label),
3166     Unknown,
3167 }
3168
3169 impl ScopeDef {
3170     pub fn all_items(def: PerNs) -> ArrayVec<Self, 3> {
3171         let mut items = ArrayVec::new();
3172
3173         match (def.take_types(), def.take_values()) {
3174             (Some(m1), None) => items.push(ScopeDef::ModuleDef(m1.into())),
3175             (None, Some(m2)) => items.push(ScopeDef::ModuleDef(m2.into())),
3176             (Some(m1), Some(m2)) => {
3177                 // Some items, like unit structs and enum variants, are
3178                 // returned as both a type and a value. Here we want
3179                 // to de-duplicate them.
3180                 if m1 != m2 {
3181                     items.push(ScopeDef::ModuleDef(m1.into()));
3182                     items.push(ScopeDef::ModuleDef(m2.into()));
3183                 } else {
3184                     items.push(ScopeDef::ModuleDef(m1.into()));
3185                 }
3186             }
3187             (None, None) => {}
3188         };
3189
3190         if let Some(macro_def_id) = def.take_macros() {
3191             items.push(ScopeDef::MacroDef(macro_def_id.into()));
3192         }
3193
3194         if items.is_empty() {
3195             items.push(ScopeDef::Unknown);
3196         }
3197
3198         items
3199     }
3200
3201     pub fn attrs(&self, db: &dyn HirDatabase) -> Option<AttrsWithOwner> {
3202         match self {
3203             ScopeDef::ModuleDef(it) => it.attrs(db),
3204             ScopeDef::MacroDef(it) => Some(it.attrs(db)),
3205             ScopeDef::GenericParam(it) => Some(it.attrs(db)),
3206             ScopeDef::ImplSelfType(_)
3207             | ScopeDef::AdtSelfType(_)
3208             | ScopeDef::Local(_)
3209             | ScopeDef::Label(_)
3210             | ScopeDef::Unknown => None,
3211         }
3212     }
3213
3214     pub fn krate(&self, db: &dyn HirDatabase) -> Option<Crate> {
3215         match self {
3216             ScopeDef::ModuleDef(it) => it.module(db).map(|m| m.krate()),
3217             ScopeDef::MacroDef(it) => it.module(db).map(|m| m.krate()),
3218             ScopeDef::GenericParam(it) => Some(it.module(db).krate()),
3219             ScopeDef::ImplSelfType(_) => None,
3220             ScopeDef::AdtSelfType(it) => Some(it.module(db).krate()),
3221             ScopeDef::Local(it) => Some(it.module(db).krate()),
3222             ScopeDef::Label(it) => Some(it.module(db).krate()),
3223             ScopeDef::Unknown => None,
3224         }
3225     }
3226 }
3227
3228 impl From<ItemInNs> for ScopeDef {
3229     fn from(item: ItemInNs) -> Self {
3230         match item {
3231             ItemInNs::Types(id) => ScopeDef::ModuleDef(id),
3232             ItemInNs::Values(id) => ScopeDef::ModuleDef(id),
3233             ItemInNs::Macros(id) => ScopeDef::MacroDef(id),
3234         }
3235     }
3236 }
3237
3238 pub trait HasVisibility {
3239     fn visibility(&self, db: &dyn HirDatabase) -> Visibility;
3240     fn is_visible_from(&self, db: &dyn HirDatabase, module: Module) -> bool {
3241         let vis = self.visibility(db);
3242         vis.is_visible_from(db.upcast(), module.id)
3243     }
3244 }
3245
3246 /// Trait for obtaining the defining crate of an item.
3247 pub trait HasCrate {
3248     fn krate(&self, db: &dyn HirDatabase) -> Crate;
3249 }
3250
3251 impl<T: hir_def::HasModule> HasCrate for T {
3252     fn krate(&self, db: &dyn HirDatabase) -> Crate {
3253         self.module(db.upcast()).krate().into()
3254     }
3255 }
3256
3257 impl HasCrate for AssocItem {
3258     fn krate(&self, db: &dyn HirDatabase) -> Crate {
3259         self.module(db).krate()
3260     }
3261 }
3262
3263 impl HasCrate for Field {
3264     fn krate(&self, db: &dyn HirDatabase) -> Crate {
3265         self.parent_def(db).module(db).krate()
3266     }
3267 }
3268
3269 impl HasCrate for Function {
3270     fn krate(&self, db: &dyn HirDatabase) -> Crate {
3271         self.module(db).krate()
3272     }
3273 }
3274
3275 impl HasCrate for Const {
3276     fn krate(&self, db: &dyn HirDatabase) -> Crate {
3277         self.module(db).krate()
3278     }
3279 }
3280
3281 impl HasCrate for TypeAlias {
3282     fn krate(&self, db: &dyn HirDatabase) -> Crate {
3283         self.module(db).krate()
3284     }
3285 }
3286
3287 impl HasCrate for Type {
3288     fn krate(&self, _db: &dyn HirDatabase) -> Crate {
3289         self.krate.into()
3290     }
3291 }