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