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