hir/
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 its
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#![cfg_attr(feature = "in-rust-tree", feature(rustc_private))]
21#![recursion_limit = "512"]
22
23extern crate ra_ap_rustc_type_ir as rustc_type_ir;
24
25mod attrs;
26mod from_id;
27mod has_source;
28mod semantics;
29mod source_analyzer;
30
31pub mod db;
32pub mod diagnostics;
33pub mod symbols;
34pub mod term_search;
35
36mod display;
37
38#[doc(hidden)]
39pub use hir_def::ModuleId;
40
41use std::{
42    fmt,
43    mem::discriminant,
44    ops::{ControlFlow, Not},
45};
46
47use arrayvec::ArrayVec;
48use base_db::{CrateDisplayName, CrateOrigin, LangCrateOrigin};
49use either::Either;
50use hir_def::{
51    AdtId, AssocItemId, AssocItemLoc, CallableDefId, ConstId, ConstParamId, DefWithBodyId, EnumId,
52    EnumVariantId, ExternBlockId, ExternCrateId, FunctionId, GenericDefId, GenericParamId,
53    HasModule, ImplId, ItemContainerId, LifetimeParamId, LocalFieldId, Lookup, MacroExpander,
54    MacroId, StaticId, StructId, SyntheticSyntax, TupleId, TypeAliasId, TypeOrConstParamId,
55    TypeParamId, UnionId,
56    attrs::AttrFlags,
57    expr_store::{ExpressionStoreDiagnostics, ExpressionStoreSourceMap},
58    hir::{
59        BindingAnnotation, BindingId, Expr, ExprId, ExprOrPatId, LabelId, Pat,
60        generics::{LifetimeParamData, TypeOrConstParamData, TypeParamProvenance},
61    },
62    item_tree::ImportAlias,
63    lang_item::LangItemTarget,
64    layout::{self, ReprOptions, TargetDataLayout},
65    nameres::{
66        assoc::TraitItems,
67        diagnostics::{DefDiagnostic, DefDiagnosticKind},
68    },
69    per_ns::PerNs,
70    resolver::{HasResolver, Resolver},
71    signatures::{EnumSignature, ImplFlags, StaticFlags, StructFlags, TraitFlags, VariantFields},
72    src::HasSource as _,
73    visibility::visibility_from_ast,
74};
75use hir_expand::{
76    AstId, MacroCallKind, RenderedExpandError, ValueResult, proc_macro::ProcMacroKind,
77};
78use hir_ty::{
79    GenericPredicates, InferenceResult, ParamEnvAndCrate, TyDefId, TyLoweringDiagnostic,
80    ValueTyDefId, all_super_traits, autoderef, check_orphan_rules,
81    consteval::try_const_usize,
82    db::{InternedClosureId, InternedCoroutineId},
83    diagnostics::BodyValidationDiagnostic,
84    direct_super_traits, known_const_to_ast,
85    layout::{Layout as TyLayout, RustcEnumVariantIdx, RustcFieldIdx, TagEncoding},
86    method_resolution::{
87        self, InherentImpls, MethodResolutionContext, MethodResolutionUnstableFeatures,
88    },
89    mir::{MutBorrowKind, interpret_mir},
90    next_solver::{
91        AliasTy, ClauseKind, ConstKind, DbInterner, ErrorGuaranteed, GenericArg, GenericArgs,
92        ParamEnv, PolyFnSig, Region, SolverDefId, Ty, TyKind, TypingMode,
93        infer::{DbInternerInferExt, InferCtxt},
94    },
95    traits::{self, is_inherent_impl_coherent, structurally_normalize_ty},
96};
97use itertools::Itertools;
98use rustc_hash::FxHashSet;
99use rustc_type_ir::{
100    AliasTyKind, TypeSuperVisitable, TypeVisitable, TypeVisitor, fast_reject,
101    inherent::{AdtDef, GenericArgs as _, IntoKind, SliceLike, Term as _, Ty as _},
102};
103use smallvec::SmallVec;
104use span::{AstIdNode, Edition, FileId};
105use stdx::{format_to, impl_from, never};
106use syntax::{
107    AstNode, AstPtr, SmolStr, SyntaxNode, SyntaxNodePtr, TextRange, ToSmolStr,
108    ast::{self, HasName, HasVisibility as _},
109    format_smolstr,
110};
111use triomphe::{Arc, ThinArc};
112
113use crate::db::{DefDatabase, HirDatabase};
114
115pub use crate::{
116    attrs::{AttrsWithOwner, HasAttrs, resolve_doc_path_on},
117    diagnostics::*,
118    has_source::HasSource,
119    semantics::{
120        LintAttr, PathResolution, PathResolutionPerNs, Semantics, SemanticsImpl, SemanticsScope,
121        TypeInfo, VisibleTraits,
122    },
123};
124
125// Be careful with these re-exports.
126//
127// `hir` is the boundary between the compiler and the IDE. It should try hard to
128// isolate the compiler from the ide, to allow the two to be refactored
129// independently. Re-exporting something from the compiler is the sure way to
130// breach the boundary.
131//
132// Generally, a refactoring which *removes* a name from this list is a good
133// idea!
134pub use {
135    cfg::{CfgAtom, CfgExpr, CfgOptions},
136    hir_def::{
137        Complete,
138        FindPathConfig,
139        attrs::{Docs, IsInnerDoc},
140        find_path::PrefixKind,
141        import_map,
142        lang_item::{LangItemEnum as LangItem, crate_lang_items},
143        nameres::{DefMap, ModuleSource, crate_def_map},
144        per_ns::Namespace,
145        type_ref::{Mutability, TypeRef},
146        visibility::Visibility,
147        // FIXME: This is here since some queries take it as input that are used
148        // outside of hir.
149        {ModuleDefId, TraitId},
150    },
151    hir_expand::{
152        EditionedFileId, ExpandResult, HirFileId, MacroCallId, MacroKind,
153        change::ChangeWithProcMacros,
154        files::{
155            FilePosition, FilePositionWrapper, FileRange, FileRangeWrapper, HirFilePosition,
156            HirFileRange, InFile, InFileWrapper, InMacroFile, InRealFile, MacroFilePosition,
157            MacroFileRange,
158        },
159        inert_attr_macro::AttributeTemplate,
160        mod_path::{ModPath, PathKind, tool_path},
161        name::Name,
162        prettify_macro_expansion,
163        proc_macro::{ProcMacros, ProcMacrosBuilder},
164        tt,
165    },
166    // FIXME: Properly encapsulate mir
167    hir_ty::mir,
168    hir_ty::{
169        CastError, FnAbi, PointerCast, attach_db, attach_db_allow_change,
170        consteval::ConstEvalError,
171        diagnostics::UnsafetyReason,
172        display::{ClosureStyle, DisplayTarget, HirDisplay, HirDisplayError, HirWrite},
173        drop::DropGlue,
174        dyn_compatibility::{DynCompatibilityViolation, MethodViolationCode},
175        layout::LayoutError,
176        mir::{MirEvalError, MirLowerError},
177        next_solver::abi::Safety,
178        next_solver::clear_tls_solver_cache,
179    },
180    // FIXME: These are needed for import assets, properly encapsulate them.
181    hir_ty::{method_resolution::TraitImpls, next_solver::SimplifiedType},
182    intern::{Symbol, sym},
183};
184
185// These are negative re-exports: pub using these names is forbidden, they
186// should remain private to hir internals.
187#[allow(unused)]
188use {
189    hir_def::expr_store::path::Path,
190    hir_expand::{
191        name::AsName,
192        span_map::{ExpansionSpanMap, RealSpanMap, SpanMap, SpanMapRef},
193    },
194};
195
196/// hir::Crate describes a single crate. It's the main interface with which
197/// a crate's dependencies interact. Mostly, it should be just a proxy for the
198/// root module.
199#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
200pub struct Crate {
201    pub(crate) id: base_db::Crate,
202}
203
204#[derive(Debug)]
205pub struct CrateDependency {
206    pub krate: Crate,
207    pub name: Name,
208}
209
210impl Crate {
211    pub fn base(self) -> base_db::Crate {
212        self.id
213    }
214
215    pub fn origin(self, db: &dyn HirDatabase) -> CrateOrigin {
216        self.id.data(db).origin.clone()
217    }
218
219    pub fn is_builtin(self, db: &dyn HirDatabase) -> bool {
220        matches!(self.origin(db), CrateOrigin::Lang(_))
221    }
222
223    pub fn dependencies(self, db: &dyn HirDatabase) -> Vec<CrateDependency> {
224        self.id
225            .data(db)
226            .dependencies
227            .iter()
228            .map(|dep| {
229                let krate = Crate { id: dep.crate_id };
230                let name = dep.as_name();
231                CrateDependency { krate, name }
232            })
233            .collect()
234    }
235
236    pub fn reverse_dependencies(self, db: &dyn HirDatabase) -> Vec<Crate> {
237        let all_crates = db.all_crates();
238        all_crates
239            .iter()
240            .copied()
241            .filter(|&krate| krate.data(db).dependencies.iter().any(|it| it.crate_id == self.id))
242            .map(|id| Crate { id })
243            .collect()
244    }
245
246    pub fn transitive_reverse_dependencies(
247        self,
248        db: &dyn HirDatabase,
249    ) -> impl Iterator<Item = Crate> {
250        self.id.transitive_rev_deps(db).into_iter().map(|id| Crate { id })
251    }
252
253    pub fn notable_traits_in_deps(self, db: &dyn HirDatabase) -> impl Iterator<Item = &TraitId> {
254        self.id
255            .transitive_deps(db)
256            .into_iter()
257            .filter_map(|krate| db.crate_notable_traits(krate))
258            .flatten()
259    }
260
261    pub fn root_module(self, db: &dyn HirDatabase) -> Module {
262        Module { id: crate_def_map(db, self.id).root_module_id() }
263    }
264
265    pub fn modules(self, db: &dyn HirDatabase) -> Vec<Module> {
266        let def_map = crate_def_map(db, self.id);
267        def_map.modules().map(|(id, _)| id.into()).collect()
268    }
269
270    pub fn root_file(self, db: &dyn HirDatabase) -> FileId {
271        self.id.data(db).root_file_id
272    }
273
274    pub fn edition(self, db: &dyn HirDatabase) -> Edition {
275        self.id.data(db).edition
276    }
277
278    pub fn version(self, db: &dyn HirDatabase) -> Option<String> {
279        self.id.extra_data(db).version.clone()
280    }
281
282    pub fn display_name(self, db: &dyn HirDatabase) -> Option<CrateDisplayName> {
283        self.id.extra_data(db).display_name.clone()
284    }
285
286    pub fn query_external_importables(
287        self,
288        db: &dyn DefDatabase,
289        query: import_map::Query,
290    ) -> impl Iterator<Item = (Either<ModuleDef, Macro>, Complete)> {
291        let _p = tracing::info_span!("query_external_importables").entered();
292        import_map::search_dependencies(db, self.into(), &query).into_iter().map(
293            |(item, do_not_complete)| {
294                let item = match ItemInNs::from(item) {
295                    ItemInNs::Types(mod_id) | ItemInNs::Values(mod_id) => Either::Left(mod_id),
296                    ItemInNs::Macros(mac_id) => Either::Right(mac_id),
297                };
298                (item, do_not_complete)
299            },
300        )
301    }
302
303    pub fn all(db: &dyn HirDatabase) -> Vec<Crate> {
304        db.all_crates().iter().map(|&id| Crate { id }).collect()
305    }
306
307    /// Try to get the root URL of the documentation of a crate.
308    pub fn get_html_root_url(self, db: &dyn HirDatabase) -> Option<String> {
309        // Look for #![doc(html_root_url = "...")]
310        let doc_url = AttrFlags::doc_html_root_url(db, self.id);
311        doc_url.as_ref().map(|s| s.trim_matches('"').trim_end_matches('/').to_owned() + "/")
312    }
313
314    pub fn cfg<'db>(&self, db: &'db dyn HirDatabase) -> &'db CfgOptions {
315        self.id.cfg_options(db)
316    }
317
318    pub fn potential_cfg<'db>(&self, db: &'db dyn HirDatabase) -> &'db CfgOptions {
319        let data = self.id.extra_data(db);
320        data.potential_cfg_options.as_ref().unwrap_or_else(|| self.id.cfg_options(db))
321    }
322
323    pub fn to_display_target(self, db: &dyn HirDatabase) -> DisplayTarget {
324        DisplayTarget::from_crate(db, self.id)
325    }
326
327    fn core(db: &dyn HirDatabase) -> Option<Crate> {
328        db.all_crates()
329            .iter()
330            .copied()
331            .find(|&krate| {
332                matches!(krate.data(db).origin, CrateOrigin::Lang(LangCrateOrigin::Core))
333            })
334            .map(Crate::from)
335    }
336}
337
338#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
339pub struct Module {
340    pub(crate) id: ModuleId,
341}
342
343/// The defs which can be visible in the module.
344#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
345pub enum ModuleDef {
346    Module(Module),
347    Function(Function),
348    Adt(Adt),
349    // Can't be directly declared, but can be imported.
350    // FIXME: Rename to `EnumVariant`
351    Variant(Variant),
352    Const(Const),
353    Static(Static),
354    Trait(Trait),
355    TypeAlias(TypeAlias),
356    BuiltinType(BuiltinType),
357    Macro(Macro),
358}
359impl_from!(
360    Module,
361    Function,
362    Adt(Struct, Enum, Union),
363    Variant,
364    Const,
365    Static,
366    Trait,
367    TypeAlias,
368    BuiltinType,
369    Macro
370    for ModuleDef
371);
372
373impl From<VariantDef> for ModuleDef {
374    fn from(var: VariantDef) -> Self {
375        match var {
376            VariantDef::Struct(t) => Adt::from(t).into(),
377            VariantDef::Union(t) => Adt::from(t).into(),
378            VariantDef::Variant(t) => t.into(),
379        }
380    }
381}
382
383impl ModuleDef {
384    pub fn module(self, db: &dyn HirDatabase) -> Option<Module> {
385        match self {
386            ModuleDef::Module(it) => it.parent(db),
387            ModuleDef::Function(it) => Some(it.module(db)),
388            ModuleDef::Adt(it) => Some(it.module(db)),
389            ModuleDef::Variant(it) => Some(it.module(db)),
390            ModuleDef::Const(it) => Some(it.module(db)),
391            ModuleDef::Static(it) => Some(it.module(db)),
392            ModuleDef::Trait(it) => Some(it.module(db)),
393            ModuleDef::TypeAlias(it) => Some(it.module(db)),
394            ModuleDef::Macro(it) => Some(it.module(db)),
395            ModuleDef::BuiltinType(_) => None,
396        }
397    }
398
399    pub fn canonical_path(&self, db: &dyn HirDatabase, edition: Edition) -> Option<String> {
400        let mut segments = vec![self.name(db)?];
401        for m in self.module(db)?.path_to_root(db) {
402            segments.extend(m.name(db))
403        }
404        segments.reverse();
405        Some(segments.iter().map(|it| it.display(db, edition)).join("::"))
406    }
407
408    pub fn canonical_module_path(
409        &self,
410        db: &dyn HirDatabase,
411    ) -> Option<impl Iterator<Item = Module>> {
412        self.module(db).map(|it| it.path_to_root(db).into_iter().rev())
413    }
414
415    pub fn name(self, db: &dyn HirDatabase) -> Option<Name> {
416        let name = match self {
417            ModuleDef::Module(it) => it.name(db)?,
418            ModuleDef::Const(it) => it.name(db)?,
419            ModuleDef::Adt(it) => it.name(db),
420            ModuleDef::Trait(it) => it.name(db),
421            ModuleDef::Function(it) => it.name(db),
422            ModuleDef::Variant(it) => it.name(db),
423            ModuleDef::TypeAlias(it) => it.name(db),
424            ModuleDef::Static(it) => it.name(db),
425            ModuleDef::Macro(it) => it.name(db),
426            ModuleDef::BuiltinType(it) => it.name(),
427        };
428        Some(name)
429    }
430
431    pub fn diagnostics<'db>(
432        self,
433        db: &'db dyn HirDatabase,
434        style_lints: bool,
435    ) -> Vec<AnyDiagnostic<'db>> {
436        let id = match self {
437            ModuleDef::Adt(it) => match it {
438                Adt::Struct(it) => it.id.into(),
439                Adt::Enum(it) => it.id.into(),
440                Adt::Union(it) => it.id.into(),
441            },
442            ModuleDef::Trait(it) => it.id.into(),
443            ModuleDef::Function(it) => it.id.into(),
444            ModuleDef::TypeAlias(it) => it.id.into(),
445            ModuleDef::Module(it) => it.id.into(),
446            ModuleDef::Const(it) => it.id.into(),
447            ModuleDef::Static(it) => it.id.into(),
448            ModuleDef::Variant(it) => it.id.into(),
449            ModuleDef::BuiltinType(_) | ModuleDef::Macro(_) => return Vec::new(),
450        };
451
452        let mut acc = Vec::new();
453
454        match self.as_def_with_body() {
455            Some(def) => {
456                def.diagnostics(db, &mut acc, style_lints);
457            }
458            None => {
459                for diag in hir_ty::diagnostics::incorrect_case(db, id) {
460                    acc.push(diag.into())
461                }
462            }
463        }
464
465        if let Some(def) = self.as_self_generic_def() {
466            def.diagnostics(db, &mut acc);
467        }
468
469        acc
470    }
471
472    pub fn as_def_with_body(self) -> Option<DefWithBody> {
473        match self {
474            ModuleDef::Function(it) => Some(it.into()),
475            ModuleDef::Const(it) => Some(it.into()),
476            ModuleDef::Static(it) => Some(it.into()),
477            ModuleDef::Variant(it) => Some(it.into()),
478
479            ModuleDef::Module(_)
480            | ModuleDef::Adt(_)
481            | ModuleDef::Trait(_)
482            | ModuleDef::TypeAlias(_)
483            | ModuleDef::Macro(_)
484            | ModuleDef::BuiltinType(_) => None,
485        }
486    }
487
488    /// Returns only defs that have generics from themselves, not their parent.
489    pub fn as_self_generic_def(self) -> Option<GenericDef> {
490        match self {
491            ModuleDef::Function(it) => Some(it.into()),
492            ModuleDef::Adt(it) => Some(it.into()),
493            ModuleDef::Trait(it) => Some(it.into()),
494            ModuleDef::TypeAlias(it) => Some(it.into()),
495            ModuleDef::Module(_)
496            | ModuleDef::Variant(_)
497            | ModuleDef::Static(_)
498            | ModuleDef::Const(_)
499            | ModuleDef::BuiltinType(_)
500            | ModuleDef::Macro(_) => None,
501        }
502    }
503
504    pub fn attrs(&self, db: &dyn HirDatabase) -> Option<AttrsWithOwner> {
505        Some(match self {
506            ModuleDef::Module(it) => it.attrs(db),
507            ModuleDef::Function(it) => it.attrs(db),
508            ModuleDef::Adt(it) => it.attrs(db),
509            ModuleDef::Variant(it) => it.attrs(db),
510            ModuleDef::Const(it) => it.attrs(db),
511            ModuleDef::Static(it) => it.attrs(db),
512            ModuleDef::Trait(it) => it.attrs(db),
513            ModuleDef::TypeAlias(it) => it.attrs(db),
514            ModuleDef::Macro(it) => it.attrs(db),
515            ModuleDef::BuiltinType(_) => return None,
516        })
517    }
518}
519
520impl HasCrate for ModuleDef {
521    fn krate(&self, db: &dyn HirDatabase) -> Crate {
522        match self.module(db) {
523            Some(module) => module.krate(db),
524            None => Crate::core(db).unwrap_or_else(|| db.all_crates()[0].into()),
525        }
526    }
527}
528
529impl HasVisibility for ModuleDef {
530    fn visibility(&self, db: &dyn HirDatabase) -> Visibility {
531        match *self {
532            ModuleDef::Module(it) => it.visibility(db),
533            ModuleDef::Function(it) => it.visibility(db),
534            ModuleDef::Adt(it) => it.visibility(db),
535            ModuleDef::Const(it) => it.visibility(db),
536            ModuleDef::Static(it) => it.visibility(db),
537            ModuleDef::Trait(it) => it.visibility(db),
538            ModuleDef::TypeAlias(it) => it.visibility(db),
539            ModuleDef::Variant(it) => it.visibility(db),
540            ModuleDef::Macro(it) => it.visibility(db),
541            ModuleDef::BuiltinType(_) => Visibility::Public,
542        }
543    }
544}
545
546impl Module {
547    /// Name of this module.
548    pub fn name(self, db: &dyn HirDatabase) -> Option<Name> {
549        self.id.name(db)
550    }
551
552    /// Returns the crate this module is part of.
553    pub fn krate(self, db: &dyn HirDatabase) -> Crate {
554        Crate { id: self.id.krate(db) }
555    }
556
557    /// Topmost parent of this module. Every module has a `crate_root`, but some
558    /// might be missing `krate`. This can happen if a module's file is not included
559    /// in the module tree of any target in `Cargo.toml`.
560    pub fn crate_root(self, db: &dyn HirDatabase) -> Module {
561        let def_map = crate_def_map(db, self.id.krate(db));
562        Module { id: def_map.crate_root(db) }
563    }
564
565    pub fn is_crate_root(self, db: &dyn HirDatabase) -> bool {
566        self.crate_root(db) == self
567    }
568
569    /// Iterates over all child modules.
570    pub fn children(self, db: &dyn HirDatabase) -> impl Iterator<Item = Module> {
571        let def_map = self.id.def_map(db);
572        let children = def_map[self.id]
573            .children
574            .values()
575            .map(|module_id| Module { id: *module_id })
576            .collect::<Vec<_>>();
577        children.into_iter()
578    }
579
580    /// Finds a parent module.
581    pub fn parent(self, db: &dyn HirDatabase) -> Option<Module> {
582        let def_map = self.id.def_map(db);
583        let parent_id = def_map.containing_module(self.id)?;
584        Some(Module { id: parent_id })
585    }
586
587    /// Finds nearest non-block ancestor `Module` (`self` included).
588    pub fn nearest_non_block_module(self, db: &dyn HirDatabase) -> Module {
589        let mut id = self.id;
590        while id.is_block_module(db) {
591            id = id.containing_module(db).expect("block without parent module");
592        }
593        Module { id }
594    }
595
596    pub fn path_to_root(self, db: &dyn HirDatabase) -> Vec<Module> {
597        let mut res = vec![self];
598        let mut curr = self;
599        while let Some(next) = curr.parent(db) {
600            res.push(next);
601            curr = next
602        }
603        res
604    }
605
606    /// Returns a `ModuleScope`: a set of items, visible in this module.
607    pub fn scope(
608        self,
609        db: &dyn HirDatabase,
610        visible_from: Option<Module>,
611    ) -> Vec<(Name, ScopeDef)> {
612        self.id.def_map(db)[self.id]
613            .scope
614            .entries()
615            .filter_map(|(name, def)| {
616                if let Some(m) = visible_from {
617                    let filtered = def.filter_visibility(|vis| vis.is_visible_from(db, m.id));
618                    if filtered.is_none() && !def.is_none() { None } else { Some((name, filtered)) }
619                } else {
620                    Some((name, def))
621                }
622            })
623            .flat_map(|(name, def)| {
624                ScopeDef::all_items(def).into_iter().map(move |item| (name.clone(), item))
625            })
626            .collect()
627    }
628
629    pub fn resolve_mod_path(
630        &self,
631        db: &dyn HirDatabase,
632        segments: impl IntoIterator<Item = Name>,
633    ) -> Option<impl Iterator<Item = ItemInNs>> {
634        let items = self
635            .id
636            .resolver(db)
637            .resolve_module_path_in_items(db, &ModPath::from_segments(PathKind::Plain, segments));
638        Some(items.iter_items().map(|(item, _)| item.into()))
639    }
640
641    /// Fills `acc` with the module's diagnostics.
642    pub fn diagnostics<'db>(
643        self,
644        db: &'db dyn HirDatabase,
645        acc: &mut Vec<AnyDiagnostic<'db>>,
646        style_lints: bool,
647    ) {
648        let _p = tracing::info_span!("diagnostics", name = ?self.name(db)).entered();
649        let edition = self.id.krate(db).data(db).edition;
650        let def_map = self.id.def_map(db);
651        for diag in def_map.diagnostics() {
652            if diag.in_module != self.id {
653                // FIXME: This is accidentally quadratic.
654                continue;
655            }
656            emit_def_diagnostic(db, acc, diag, edition, def_map.krate());
657        }
658
659        if !self.id.is_block_module(db) {
660            // These are reported by the body of block modules
661            let scope = &def_map[self.id].scope;
662            scope.all_macro_calls().for_each(|it| macro_call_diagnostics(db, it, acc));
663        }
664
665        for def in self.declarations(db) {
666            match def {
667                ModuleDef::Module(m) => {
668                    // Only add diagnostics from inline modules
669                    if def_map[m.id].origin.is_inline() {
670                        m.diagnostics(db, acc, style_lints)
671                    }
672                    acc.extend(def.diagnostics(db, style_lints))
673                }
674                ModuleDef::Trait(t) => {
675                    let krate = t.krate(db);
676                    for diag in TraitItems::query_with_diagnostics(db, t.id).1.iter() {
677                        emit_def_diagnostic(db, acc, diag, edition, krate.id);
678                    }
679
680                    for item in t.items(db) {
681                        item.diagnostics(db, acc, style_lints);
682                    }
683
684                    t.all_macro_calls(db)
685                        .iter()
686                        .for_each(|&(_ast, call_id)| macro_call_diagnostics(db, call_id, acc));
687
688                    acc.extend(def.diagnostics(db, style_lints))
689                }
690                ModuleDef::Adt(adt) => {
691                    match adt {
692                        Adt::Struct(s) => {
693                            let source_map = db.struct_signature_with_source_map(s.id).1;
694                            expr_store_diagnostics(db, acc, &source_map);
695                            let source_map = &s.id.fields_with_source_map(db).1;
696                            expr_store_diagnostics(db, acc, source_map);
697                            push_ty_diagnostics(
698                                db,
699                                acc,
700                                db.field_types_with_diagnostics(s.id.into()).1,
701                                source_map,
702                            );
703                        }
704                        Adt::Union(u) => {
705                            let source_map = db.union_signature_with_source_map(u.id).1;
706                            expr_store_diagnostics(db, acc, &source_map);
707                            let source_map = &u.id.fields_with_source_map(db).1;
708                            expr_store_diagnostics(db, acc, source_map);
709                            push_ty_diagnostics(
710                                db,
711                                acc,
712                                db.field_types_with_diagnostics(u.id.into()).1,
713                                source_map,
714                            );
715                        }
716                        Adt::Enum(e) => {
717                            let source_map = db.enum_signature_with_source_map(e.id).1;
718                            expr_store_diagnostics(db, acc, &source_map);
719                            let (variants, diagnostics) = e.id.enum_variants_with_diagnostics(db);
720                            let file = e.id.lookup(db).id.file_id;
721                            let ast_id_map = db.ast_id_map(file);
722                            if let Some(diagnostics) = &diagnostics {
723                                for diag in diagnostics.iter() {
724                                    acc.push(
725                                        InactiveCode {
726                                            node: InFile::new(
727                                                file,
728                                                ast_id_map.get(diag.ast_id).syntax_node_ptr(),
729                                            ),
730                                            cfg: diag.cfg.clone(),
731                                            opts: diag.opts.clone(),
732                                        }
733                                        .into(),
734                                    );
735                                }
736                            }
737                            for &(v, _, _) in &variants.variants {
738                                let source_map = &v.fields_with_source_map(db).1;
739                                push_ty_diagnostics(
740                                    db,
741                                    acc,
742                                    db.field_types_with_diagnostics(v.into()).1,
743                                    source_map,
744                                );
745                                expr_store_diagnostics(db, acc, source_map);
746                            }
747                        }
748                    }
749                    acc.extend(def.diagnostics(db, style_lints))
750                }
751                ModuleDef::Macro(m) => emit_macro_def_diagnostics(db, acc, m),
752                ModuleDef::TypeAlias(type_alias) => {
753                    let source_map = db.type_alias_signature_with_source_map(type_alias.id).1;
754                    expr_store_diagnostics(db, acc, &source_map);
755                    push_ty_diagnostics(
756                        db,
757                        acc,
758                        db.type_for_type_alias_with_diagnostics(type_alias.id).1,
759                        &source_map,
760                    );
761                    acc.extend(def.diagnostics(db, style_lints));
762                }
763                _ => acc.extend(def.diagnostics(db, style_lints)),
764            }
765        }
766        self.legacy_macros(db).into_iter().for_each(|m| emit_macro_def_diagnostics(db, acc, m));
767
768        let interner = DbInterner::new_with(db, self.id.krate(db));
769        let infcx = interner.infer_ctxt().build(TypingMode::non_body_analysis());
770
771        let mut impl_assoc_items_scratch = vec![];
772        for impl_def in self.impl_defs(db) {
773            GenericDef::Impl(impl_def).diagnostics(db, acc);
774
775            let loc = impl_def.id.lookup(db);
776            let (impl_signature, source_map) = db.impl_signature_with_source_map(impl_def.id);
777            expr_store_diagnostics(db, acc, &source_map);
778
779            let file_id = loc.id.file_id;
780            if file_id.macro_file().is_some_and(|it| it.kind(db) == MacroKind::DeriveBuiltIn) {
781                // these expansion come from us, diagnosing them is a waste of resources
782                // FIXME: Once we diagnose the inputs to builtin derives, we should at least extract those diagnostics somehow
783                continue;
784            }
785            impl_def
786                .all_macro_calls(db)
787                .iter()
788                .for_each(|&(_ast, call_id)| macro_call_diagnostics(db, call_id, acc));
789
790            let ast_id_map = db.ast_id_map(file_id);
791
792            for diag in impl_def.id.impl_items_with_diagnostics(db).1.iter() {
793                emit_def_diagnostic(db, acc, diag, edition, loc.container.krate(db));
794            }
795
796            if impl_signature.target_trait.is_none()
797                && !is_inherent_impl_coherent(db, def_map, impl_def.id)
798            {
799                acc.push(IncoherentImpl { impl_: ast_id_map.get(loc.id.value), file_id }.into())
800            }
801
802            if !impl_def.check_orphan_rules(db) {
803                acc.push(TraitImplOrphan { impl_: ast_id_map.get(loc.id.value), file_id }.into())
804            }
805
806            let trait_ = impl_def.trait_(db);
807            let mut trait_is_unsafe = trait_.is_some_and(|t| t.is_unsafe(db));
808            let impl_is_negative = impl_def.is_negative(db);
809            let impl_is_unsafe = impl_def.is_unsafe(db);
810
811            let trait_is_unresolved = trait_.is_none() && impl_signature.target_trait.is_some();
812            if trait_is_unresolved {
813                // Ignore trait safety errors when the trait is unresolved, as otherwise we'll treat it as safe,
814                // which may not be correct.
815                trait_is_unsafe = impl_is_unsafe;
816            }
817
818            let drop_maybe_dangle = (|| {
819                // FIXME: This can be simplified a lot by exposing hir-ty's utils.rs::Generics helper
820                let trait_ = trait_?;
821                let drop_trait = interner.lang_items().Drop?;
822                if drop_trait != trait_.into() {
823                    return None;
824                }
825                let parent = impl_def.id.into();
826                let (lifetimes_attrs, type_and_consts_attrs) =
827                    AttrFlags::query_generic_params(db, parent);
828                let res = lifetimes_attrs.values().any(|it| it.contains(AttrFlags::MAY_DANGLE))
829                    || type_and_consts_attrs.values().any(|it| it.contains(AttrFlags::MAY_DANGLE));
830                Some(res)
831            })()
832            .unwrap_or(false);
833
834            match (impl_is_unsafe, trait_is_unsafe, impl_is_negative, drop_maybe_dangle) {
835                // unsafe negative impl
836                (true, _, true, _) |
837                // unsafe impl for safe trait
838                (true, false, _, false) => acc.push(TraitImplIncorrectSafety { impl_: ast_id_map.get(loc.id.value), file_id, should_be_safe: true }.into()),
839                // safe impl for unsafe trait
840                (false, true, false, _) |
841                // safe impl of dangling drop
842                (false, false, _, true) => acc.push(TraitImplIncorrectSafety { impl_: ast_id_map.get(loc.id.value), file_id, should_be_safe: false }.into()),
843                _ => (),
844            };
845
846            // Negative impls can't have items, don't emit missing items diagnostic for them
847            if let (false, Some(trait_)) = (impl_is_negative, trait_) {
848                let items = &trait_.id.trait_items(db).items;
849                let required_items = items.iter().filter(|&(_, assoc)| match *assoc {
850                    AssocItemId::FunctionId(it) => !db.function_signature(it).has_body(),
851                    AssocItemId::ConstId(id) => !db.const_signature(id).has_body(),
852                    AssocItemId::TypeAliasId(it) => db.type_alias_signature(it).ty.is_none(),
853                });
854                impl_assoc_items_scratch.extend(impl_def.id.impl_items(db).items.iter().cloned());
855
856                let redundant = impl_assoc_items_scratch
857                    .iter()
858                    .filter(|(name, id)| {
859                        !items.iter().any(|(impl_name, impl_item)| {
860                            discriminant(impl_item) == discriminant(id) && impl_name == name
861                        })
862                    })
863                    .map(|(name, item)| (name.clone(), AssocItem::from(*item)));
864                for (name, assoc_item) in redundant {
865                    acc.push(
866                        TraitImplRedundantAssocItems {
867                            trait_,
868                            file_id,
869                            impl_: ast_id_map.get(loc.id.value),
870                            assoc_item: (name, assoc_item),
871                        }
872                        .into(),
873                    )
874                }
875
876                let mut missing: Vec<_> = required_items
877                    .filter(|(name, id)| {
878                        !impl_assoc_items_scratch.iter().any(|(impl_name, impl_item)| {
879                            discriminant(impl_item) == discriminant(id) && impl_name == name
880                        })
881                    })
882                    .map(|(name, item)| (name.clone(), AssocItem::from(*item)))
883                    .collect();
884
885                if !missing.is_empty() {
886                    let self_ty = db.impl_self_ty(impl_def.id).instantiate_identity();
887                    let self_ty = structurally_normalize_ty(
888                        &infcx,
889                        self_ty,
890                        db.trait_environment(impl_def.id.into()),
891                    );
892                    let self_ty_is_guaranteed_unsized = matches!(
893                        self_ty.kind(),
894                        TyKind::Dynamic(..) | TyKind::Slice(..) | TyKind::Str
895                    );
896                    if self_ty_is_guaranteed_unsized {
897                        missing.retain(|(_, assoc_item)| {
898                            let assoc_item = match *assoc_item {
899                                AssocItem::Function(it) => it.id.into(),
900                                AssocItem::Const(it) => it.id.into(),
901                                AssocItem::TypeAlias(it) => it.id.into(),
902                            };
903                            !hir_ty::dyn_compatibility::generics_require_sized_self(db, assoc_item)
904                        });
905                    }
906                }
907
908                if !missing.is_empty() {
909                    acc.push(
910                        TraitImplMissingAssocItems {
911                            impl_: ast_id_map.get(loc.id.value),
912                            file_id,
913                            missing,
914                        }
915                        .into(),
916                    )
917                }
918                impl_assoc_items_scratch.clear();
919            }
920
921            push_ty_diagnostics(
922                db,
923                acc,
924                db.impl_self_ty_with_diagnostics(impl_def.id).1,
925                &source_map,
926            );
927            push_ty_diagnostics(
928                db,
929                acc,
930                db.impl_trait_with_diagnostics(impl_def.id).and_then(|it| it.1),
931                &source_map,
932            );
933
934            for &(_, item) in impl_def.id.impl_items(db).items.iter() {
935                AssocItem::from(item).diagnostics(db, acc, style_lints);
936            }
937        }
938    }
939
940    pub fn declarations(self, db: &dyn HirDatabase) -> Vec<ModuleDef> {
941        let def_map = self.id.def_map(db);
942        let scope = &def_map[self.id].scope;
943        scope
944            .declarations()
945            .map(ModuleDef::from)
946            .chain(scope.unnamed_consts().map(|id| ModuleDef::Const(Const::from(id))))
947            .collect()
948    }
949
950    pub fn legacy_macros(self, db: &dyn HirDatabase) -> Vec<Macro> {
951        let def_map = self.id.def_map(db);
952        let scope = &def_map[self.id].scope;
953        scope.legacy_macros().flat_map(|(_, it)| it).map(|&it| it.into()).collect()
954    }
955
956    pub fn impl_defs(self, db: &dyn HirDatabase) -> Vec<Impl> {
957        let def_map = self.id.def_map(db);
958        def_map[self.id].scope.impls().map(Impl::from).collect()
959    }
960
961    /// Finds a path that can be used to refer to the given item from within
962    /// this module, if possible.
963    pub fn find_path(
964        self,
965        db: &dyn DefDatabase,
966        item: impl Into<ItemInNs>,
967        cfg: FindPathConfig,
968    ) -> Option<ModPath> {
969        hir_def::find_path::find_path(
970            db,
971            item.into().into(),
972            self.into(),
973            PrefixKind::Plain,
974            false,
975            cfg,
976        )
977    }
978
979    /// Finds a path that can be used to refer to the given item from within
980    /// this module, if possible. This is used for returning import paths for use-statements.
981    pub fn find_use_path(
982        self,
983        db: &dyn DefDatabase,
984        item: impl Into<ItemInNs>,
985        prefix_kind: PrefixKind,
986        cfg: FindPathConfig,
987    ) -> Option<ModPath> {
988        hir_def::find_path::find_path(db, item.into().into(), self.into(), prefix_kind, true, cfg)
989    }
990
991    #[inline]
992    pub fn doc_keyword(self, db: &dyn HirDatabase) -> Option<Symbol> {
993        AttrFlags::doc_keyword(db, self.id)
994    }
995
996    /// Whether it has `#[path = "..."]` attribute.
997    #[inline]
998    pub fn has_path(&self, db: &dyn HirDatabase) -> bool {
999        self.attrs(db).attrs.contains(AttrFlags::HAS_PATH)
1000    }
1001}
1002
1003fn macro_call_diagnostics<'db>(
1004    db: &'db dyn HirDatabase,
1005    macro_call_id: MacroCallId,
1006    acc: &mut Vec<AnyDiagnostic<'db>>,
1007) {
1008    let Some(e) = db.parse_macro_expansion_error(macro_call_id) else {
1009        return;
1010    };
1011    let ValueResult { value: parse_errors, err } = &*e;
1012    if let Some(err) = err {
1013        let loc = db.lookup_intern_macro_call(macro_call_id);
1014        let file_id = loc.kind.file_id();
1015        let mut range = precise_macro_call_location(&loc.kind, db, loc.krate);
1016        let RenderedExpandError { message, error, kind } = err.render_to_string(db);
1017        if Some(err.span().anchor.file_id) == file_id.file_id().map(|it| it.editioned_file_id(db)) {
1018            range.value = err.span().range
1019                + db.ast_id_map(file_id).get_erased(err.span().anchor.ast_id).text_range().start();
1020        }
1021        acc.push(MacroError { range, message, error, kind }.into());
1022    }
1023
1024    if !parse_errors.is_empty() {
1025        let loc = db.lookup_intern_macro_call(macro_call_id);
1026        let range = precise_macro_call_location(&loc.kind, db, loc.krate);
1027        acc.push(MacroExpansionParseError { range, errors: parse_errors.clone() }.into())
1028    }
1029}
1030
1031fn emit_macro_def_diagnostics<'db>(
1032    db: &'db dyn HirDatabase,
1033    acc: &mut Vec<AnyDiagnostic<'db>>,
1034    m: Macro,
1035) {
1036    let id = db.macro_def(m.id);
1037    if let hir_expand::db::TokenExpander::DeclarativeMacro(expander) = db.macro_expander(id)
1038        && let Some(e) = expander.mac.err()
1039    {
1040        let Some(ast) = id.ast_id().left() else {
1041            never!("declarative expander for non decl-macro: {:?}", e);
1042            return;
1043        };
1044        let krate = HasModule::krate(&m.id, db);
1045        let edition = krate.data(db).edition;
1046        emit_def_diagnostic_(
1047            db,
1048            acc,
1049            &DefDiagnosticKind::MacroDefError { ast, message: e.to_string() },
1050            edition,
1051            m.krate(db).id,
1052        );
1053    }
1054}
1055
1056fn emit_def_diagnostic<'db>(
1057    db: &'db dyn HirDatabase,
1058    acc: &mut Vec<AnyDiagnostic<'db>>,
1059    diag: &DefDiagnostic,
1060    edition: Edition,
1061    krate: base_db::Crate,
1062) {
1063    emit_def_diagnostic_(db, acc, &diag.kind, edition, krate)
1064}
1065
1066fn emit_def_diagnostic_<'db>(
1067    db: &'db dyn HirDatabase,
1068    acc: &mut Vec<AnyDiagnostic<'db>>,
1069    diag: &DefDiagnosticKind,
1070    edition: Edition,
1071    krate: base_db::Crate,
1072) {
1073    match diag {
1074        DefDiagnosticKind::UnresolvedModule { ast: declaration, candidates } => {
1075            let decl = declaration.to_ptr(db);
1076            acc.push(
1077                UnresolvedModule {
1078                    decl: InFile::new(declaration.file_id, decl),
1079                    candidates: candidates.clone(),
1080                }
1081                .into(),
1082            )
1083        }
1084        DefDiagnosticKind::UnresolvedExternCrate { ast } => {
1085            let item = ast.to_ptr(db);
1086            acc.push(UnresolvedExternCrate { decl: InFile::new(ast.file_id, item) }.into());
1087        }
1088
1089        DefDiagnosticKind::MacroError { ast, path, err } => {
1090            let item = ast.to_ptr(db);
1091            let RenderedExpandError { message, error, kind } = err.render_to_string(db);
1092            acc.push(
1093                MacroError {
1094                    range: InFile::new(ast.file_id, item.text_range()),
1095                    message: format!("{}: {message}", path.display(db, edition)),
1096                    error,
1097                    kind,
1098                }
1099                .into(),
1100            )
1101        }
1102        DefDiagnosticKind::UnresolvedImport { id, index } => {
1103            let file_id = id.file_id;
1104
1105            let use_tree = hir_def::src::use_tree_to_ast(db, *id, *index);
1106            acc.push(
1107                UnresolvedImport { decl: InFile::new(file_id, AstPtr::new(&use_tree)) }.into(),
1108            );
1109        }
1110
1111        DefDiagnosticKind::UnconfiguredCode { ast_id, cfg, opts } => {
1112            let ast_id_map = db.ast_id_map(ast_id.file_id);
1113            let ptr = ast_id_map.get_erased(ast_id.value);
1114            acc.push(
1115                InactiveCode {
1116                    node: InFile::new(ast_id.file_id, ptr),
1117                    cfg: cfg.clone(),
1118                    opts: opts.clone(),
1119                }
1120                .into(),
1121            );
1122        }
1123        DefDiagnosticKind::UnresolvedMacroCall { ast, path } => {
1124            let location = precise_macro_call_location(ast, db, krate);
1125            acc.push(
1126                UnresolvedMacroCall {
1127                    range: location,
1128                    path: path.clone(),
1129                    is_bang: matches!(ast, MacroCallKind::FnLike { .. }),
1130                }
1131                .into(),
1132            );
1133        }
1134        DefDiagnosticKind::UnimplementedBuiltinMacro { ast } => {
1135            let node = ast.to_node(db);
1136            // Must have a name, otherwise we wouldn't emit it.
1137            let name = node.name().expect("unimplemented builtin macro with no name");
1138            acc.push(
1139                UnimplementedBuiltinMacro {
1140                    node: ast.with_value(SyntaxNodePtr::from(AstPtr::new(&name))),
1141                }
1142                .into(),
1143            );
1144        }
1145        DefDiagnosticKind::InvalidDeriveTarget { ast, id } => {
1146            let derive = id.find_attr_range(db, krate, *ast).3.path_range();
1147            acc.push(InvalidDeriveTarget { range: ast.with_value(derive) }.into());
1148        }
1149        DefDiagnosticKind::MalformedDerive { ast, id } => {
1150            let derive = id.find_attr_range(db, krate, *ast).2;
1151            acc.push(MalformedDerive { range: ast.with_value(derive) }.into());
1152        }
1153        DefDiagnosticKind::MacroDefError { ast, message } => {
1154            let node = ast.to_node(db);
1155            acc.push(
1156                MacroDefError {
1157                    node: InFile::new(ast.file_id, AstPtr::new(&node)),
1158                    name: node.name().map(|it| it.syntax().text_range()),
1159                    message: message.clone(),
1160                }
1161                .into(),
1162            );
1163        }
1164    }
1165}
1166
1167fn precise_macro_call_location(
1168    ast: &MacroCallKind,
1169    db: &dyn HirDatabase,
1170    krate: base_db::Crate,
1171) -> InFile<TextRange> {
1172    // FIXME: maybe we actually want slightly different ranges for the different macro diagnostics
1173    // - e.g. the full attribute for macro errors, but only the name for name resolution
1174    match ast {
1175        MacroCallKind::FnLike { ast_id, .. } => {
1176            let node = ast_id.to_node(db);
1177            let range = node
1178                .path()
1179                .and_then(|it| it.segment())
1180                .and_then(|it| it.name_ref())
1181                .map(|it| it.syntax().text_range());
1182            let range = range.unwrap_or_else(|| node.syntax().text_range());
1183            ast_id.with_value(range)
1184        }
1185        MacroCallKind::Derive { ast_id, derive_attr_index, derive_index, .. } => {
1186            let range = derive_attr_index.find_derive_range(db, krate, *ast_id, *derive_index);
1187            ast_id.with_value(range)
1188        }
1189        MacroCallKind::Attr { ast_id, censored_attr_ids: attr_ids, .. } => {
1190            let attr_range = attr_ids.invoc_attr().find_attr_range(db, krate, *ast_id).2;
1191            ast_id.with_value(attr_range)
1192        }
1193    }
1194}
1195
1196impl HasVisibility for Module {
1197    fn visibility(&self, db: &dyn HirDatabase) -> Visibility {
1198        let def_map = self.id.def_map(db);
1199        let module_data = &def_map[self.id];
1200        module_data.visibility
1201    }
1202}
1203
1204#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
1205pub struct Field {
1206    pub(crate) parent: VariantDef,
1207    pub(crate) id: LocalFieldId,
1208}
1209
1210#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
1211pub struct InstantiatedField<'db> {
1212    pub(crate) inner: Field,
1213    pub(crate) args: GenericArgs<'db>,
1214}
1215
1216impl<'db> InstantiatedField<'db> {
1217    /// Returns the type as in the signature of the struct.
1218    pub fn ty(&self, db: &'db dyn HirDatabase) -> TypeNs<'db> {
1219        let interner = DbInterner::new_no_crate(db);
1220
1221        let var_id = self.inner.parent.into();
1222        let field = db.field_types(var_id)[self.inner.id];
1223        let ty = field.instantiate(interner, self.args);
1224        TypeNs::new(db, var_id, ty)
1225    }
1226}
1227
1228#[derive(Debug, PartialEq, Eq, Copy, Clone, Hash)]
1229pub struct TupleField {
1230    pub owner: DefWithBodyId,
1231    pub tuple: TupleId,
1232    pub index: u32,
1233}
1234
1235impl TupleField {
1236    pub fn name(&self) -> Name {
1237        Name::new_tuple_field(self.index as usize)
1238    }
1239
1240    pub fn ty<'db>(&self, db: &'db dyn HirDatabase) -> Type<'db> {
1241        let interner = DbInterner::new_no_crate(db);
1242        let ty = InferenceResult::for_body(db, self.owner)
1243            .tuple_field_access_type(self.tuple)
1244            .as_slice()
1245            .get(self.index as usize)
1246            .copied()
1247            .unwrap_or_else(|| Ty::new_error(interner, ErrorGuaranteed));
1248        Type { env: body_param_env_from_has_crate(db, self.owner), ty }
1249    }
1250}
1251
1252#[derive(Debug, PartialEq, Eq)]
1253pub enum FieldSource {
1254    Named(ast::RecordField),
1255    Pos(ast::TupleField),
1256}
1257
1258impl AstNode for FieldSource {
1259    fn can_cast(kind: syntax::SyntaxKind) -> bool
1260    where
1261        Self: Sized,
1262    {
1263        ast::RecordField::can_cast(kind) || ast::TupleField::can_cast(kind)
1264    }
1265
1266    fn cast(syntax: SyntaxNode) -> Option<Self>
1267    where
1268        Self: Sized,
1269    {
1270        if ast::RecordField::can_cast(syntax.kind()) {
1271            <ast::RecordField as AstNode>::cast(syntax).map(FieldSource::Named)
1272        } else if ast::TupleField::can_cast(syntax.kind()) {
1273            <ast::TupleField as AstNode>::cast(syntax).map(FieldSource::Pos)
1274        } else {
1275            None
1276        }
1277    }
1278
1279    fn syntax(&self) -> &SyntaxNode {
1280        match self {
1281            FieldSource::Named(it) => it.syntax(),
1282            FieldSource::Pos(it) => it.syntax(),
1283        }
1284    }
1285}
1286
1287impl Field {
1288    pub fn name(&self, db: &dyn HirDatabase) -> Name {
1289        VariantId::from(self.parent).fields(db).fields()[self.id].name.clone()
1290    }
1291
1292    pub fn index(&self) -> usize {
1293        u32::from(self.id.into_raw()) as usize
1294    }
1295
1296    /// Returns the type as in the signature of the struct. Only use this in the
1297    /// context of the field definition.
1298    pub fn ty<'db>(&self, db: &'db dyn HirDatabase) -> TypeNs<'db> {
1299        let var_id = self.parent.into();
1300        let ty = db.field_types(var_id)[self.id].skip_binder();
1301        TypeNs::new(db, var_id, ty)
1302    }
1303
1304    // FIXME: Find better API to also handle const generics
1305    pub fn ty_with_args<'db>(
1306        &self,
1307        db: &'db dyn HirDatabase,
1308        generics: impl Iterator<Item = Type<'db>>,
1309    ) -> Type<'db> {
1310        let var_id = self.parent.into();
1311        let def_id: AdtId = match self.parent {
1312            VariantDef::Struct(it) => it.id.into(),
1313            VariantDef::Union(it) => it.id.into(),
1314            VariantDef::Variant(it) => it.parent_enum(db).id.into(),
1315        };
1316        let interner = DbInterner::new_no_crate(db);
1317        let args = generic_args_from_tys(interner, def_id.into(), generics.map(|ty| ty.ty));
1318        let ty = db.field_types(var_id)[self.id].instantiate(interner, args);
1319        Type::new(db, var_id, ty)
1320    }
1321
1322    pub fn layout(&self, db: &dyn HirDatabase) -> Result<Layout, LayoutError> {
1323        db.layout_of_ty(
1324            self.ty(db).ty,
1325            param_env_from_has_crate(
1326                db,
1327                match hir_def::VariantId::from(self.parent) {
1328                    hir_def::VariantId::EnumVariantId(id) => {
1329                        GenericDefId::AdtId(id.lookup(db).parent.into())
1330                    }
1331                    hir_def::VariantId::StructId(id) => GenericDefId::AdtId(id.into()),
1332                    hir_def::VariantId::UnionId(id) => GenericDefId::AdtId(id.into()),
1333                },
1334            ),
1335        )
1336        .map(|layout| Layout(layout, db.target_data_layout(self.krate(db).into()).unwrap()))
1337    }
1338
1339    pub fn parent_def(&self, _db: &dyn HirDatabase) -> VariantDef {
1340        self.parent
1341    }
1342}
1343
1344impl HasVisibility for Field {
1345    fn visibility(&self, db: &dyn HirDatabase) -> Visibility {
1346        let variant_data = VariantId::from(self.parent).fields(db);
1347        let visibility = &variant_data.fields()[self.id].visibility;
1348        let parent_id: hir_def::VariantId = self.parent.into();
1349        // FIXME: RawVisibility::Public doesn't need to construct a resolver
1350        Visibility::resolve(db, &parent_id.resolver(db), visibility)
1351    }
1352}
1353
1354#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
1355pub struct Struct {
1356    pub(crate) id: StructId,
1357}
1358
1359impl Struct {
1360    pub fn module(self, db: &dyn HirDatabase) -> Module {
1361        Module { id: self.id.lookup(db).container }
1362    }
1363
1364    pub fn name(self, db: &dyn HirDatabase) -> Name {
1365        db.struct_signature(self.id).name.clone()
1366    }
1367
1368    pub fn fields(self, db: &dyn HirDatabase) -> Vec<Field> {
1369        self.id
1370            .fields(db)
1371            .fields()
1372            .iter()
1373            .map(|(id, _)| Field { parent: self.into(), id })
1374            .collect()
1375    }
1376
1377    pub fn ty(self, db: &dyn HirDatabase) -> Type<'_> {
1378        Type::from_def(db, self.id)
1379    }
1380
1381    pub fn ty_params(self, db: &dyn HirDatabase) -> Type<'_> {
1382        Type::from_def_params(db, self.id)
1383    }
1384
1385    pub fn constructor_ty(self, db: &dyn HirDatabase) -> Type<'_> {
1386        Type::from_value_def(db, self.id)
1387    }
1388
1389    pub fn repr(self, db: &dyn HirDatabase) -> Option<ReprOptions> {
1390        AttrFlags::repr(db, self.id.into())
1391    }
1392
1393    pub fn kind(self, db: &dyn HirDatabase) -> StructKind {
1394        match self.variant_fields(db).shape {
1395            hir_def::item_tree::FieldsShape::Record => StructKind::Record,
1396            hir_def::item_tree::FieldsShape::Tuple => StructKind::Tuple,
1397            hir_def::item_tree::FieldsShape::Unit => StructKind::Unit,
1398        }
1399    }
1400
1401    fn variant_fields(self, db: &dyn HirDatabase) -> &VariantFields {
1402        self.id.fields(db)
1403    }
1404
1405    pub fn is_unstable(self, db: &dyn HirDatabase) -> bool {
1406        AttrFlags::query(db, self.id.into()).contains(AttrFlags::IS_UNSTABLE)
1407    }
1408
1409    pub fn instantiate_infer<'db>(self, infer_ctxt: &InferCtxt<'db>) -> InstantiatedStruct<'db> {
1410        let args = infer_ctxt.fresh_args_for_item(self.id.into());
1411        InstantiatedStruct { inner: self, args }
1412    }
1413}
1414
1415impl HasVisibility for Struct {
1416    fn visibility(&self, db: &dyn HirDatabase) -> Visibility {
1417        let loc = self.id.lookup(db);
1418        let source = loc.source(db);
1419        visibility_from_ast(db, self.id, source.map(|src| src.visibility()))
1420    }
1421}
1422
1423#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
1424pub struct InstantiatedStruct<'db> {
1425    pub(crate) inner: Struct,
1426    pub(crate) args: GenericArgs<'db>,
1427}
1428
1429impl<'db> InstantiatedStruct<'db> {
1430    pub fn fields(self, db: &dyn HirDatabase) -> Vec<InstantiatedField<'db>> {
1431        self.inner
1432            .id
1433            .fields(db)
1434            .fields()
1435            .iter()
1436            .map(|(id, _)| InstantiatedField {
1437                inner: Field { parent: self.inner.into(), id },
1438                args: self.args,
1439            })
1440            .collect()
1441    }
1442
1443    pub fn ty(self, db: &'db dyn HirDatabase) -> TypeNs<'db> {
1444        let interner = DbInterner::new_no_crate(db);
1445
1446        let ty = db.ty(self.inner.id.into());
1447        TypeNs::new(db, self.inner.id, ty.instantiate(interner, self.args))
1448    }
1449}
1450
1451#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
1452pub struct Union {
1453    pub(crate) id: UnionId,
1454}
1455
1456impl Union {
1457    pub fn name(self, db: &dyn HirDatabase) -> Name {
1458        db.union_signature(self.id).name.clone()
1459    }
1460
1461    pub fn module(self, db: &dyn HirDatabase) -> Module {
1462        Module { id: self.id.lookup(db).container }
1463    }
1464
1465    pub fn ty(self, db: &dyn HirDatabase) -> Type<'_> {
1466        Type::from_def(db, self.id)
1467    }
1468
1469    pub fn ty_params(self, db: &dyn HirDatabase) -> Type<'_> {
1470        Type::from_def_params(db, self.id)
1471    }
1472
1473    pub fn constructor_ty(self, db: &dyn HirDatabase) -> Type<'_> {
1474        Type::from_value_def(db, self.id)
1475    }
1476
1477    pub fn kind(self, db: &dyn HirDatabase) -> StructKind {
1478        match self.id.fields(db).shape {
1479            hir_def::item_tree::FieldsShape::Record => StructKind::Record,
1480            hir_def::item_tree::FieldsShape::Tuple => StructKind::Tuple,
1481            hir_def::item_tree::FieldsShape::Unit => StructKind::Unit,
1482        }
1483    }
1484
1485    pub fn fields(self, db: &dyn HirDatabase) -> Vec<Field> {
1486        self.id
1487            .fields(db)
1488            .fields()
1489            .iter()
1490            .map(|(id, _)| Field { parent: self.into(), id })
1491            .collect()
1492    }
1493    pub fn is_unstable(self, db: &dyn HirDatabase) -> bool {
1494        AttrFlags::query(db, self.id.into()).contains(AttrFlags::IS_UNSTABLE)
1495    }
1496}
1497
1498impl HasVisibility for Union {
1499    fn visibility(&self, db: &dyn HirDatabase) -> Visibility {
1500        let loc = self.id.lookup(db);
1501        let source = loc.source(db);
1502        visibility_from_ast(db, self.id, source.map(|src| src.visibility()))
1503    }
1504}
1505
1506#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
1507pub struct Enum {
1508    pub(crate) id: EnumId,
1509}
1510
1511impl Enum {
1512    pub fn module(self, db: &dyn HirDatabase) -> Module {
1513        Module { id: self.id.lookup(db).container }
1514    }
1515
1516    pub fn name(self, db: &dyn HirDatabase) -> Name {
1517        db.enum_signature(self.id).name.clone()
1518    }
1519
1520    pub fn variants(self, db: &dyn HirDatabase) -> Vec<Variant> {
1521        self.id.enum_variants(db).variants.iter().map(|&(id, _, _)| Variant { id }).collect()
1522    }
1523
1524    pub fn num_variants(self, db: &dyn HirDatabase) -> usize {
1525        self.id.enum_variants(db).variants.len()
1526    }
1527
1528    pub fn repr(self, db: &dyn HirDatabase) -> Option<ReprOptions> {
1529        AttrFlags::repr(db, self.id.into())
1530    }
1531
1532    pub fn ty<'db>(self, db: &'db dyn HirDatabase) -> Type<'db> {
1533        Type::from_def(db, self.id)
1534    }
1535
1536    pub fn ty_params<'db>(self, db: &'db dyn HirDatabase) -> Type<'db> {
1537        Type::from_def_params(db, self.id)
1538    }
1539
1540    /// The type of the enum variant bodies.
1541    pub fn variant_body_ty<'db>(self, db: &'db dyn HirDatabase) -> Type<'db> {
1542        let interner = DbInterner::new_no_crate(db);
1543        Type::new_for_crate(
1544            self.id.lookup(db).container.krate(db),
1545            match EnumSignature::variant_body_type(db, self.id) {
1546                layout::IntegerType::Pointer(sign) => match sign {
1547                    true => Ty::new_int(interner, rustc_type_ir::IntTy::Isize),
1548                    false => Ty::new_uint(interner, rustc_type_ir::UintTy::Usize),
1549                },
1550                layout::IntegerType::Fixed(i, sign) => match sign {
1551                    true => Ty::new_int(
1552                        interner,
1553                        match i {
1554                            layout::Integer::I8 => rustc_type_ir::IntTy::I8,
1555                            layout::Integer::I16 => rustc_type_ir::IntTy::I16,
1556                            layout::Integer::I32 => rustc_type_ir::IntTy::I32,
1557                            layout::Integer::I64 => rustc_type_ir::IntTy::I64,
1558                            layout::Integer::I128 => rustc_type_ir::IntTy::I128,
1559                        },
1560                    ),
1561                    false => Ty::new_uint(
1562                        interner,
1563                        match i {
1564                            layout::Integer::I8 => rustc_type_ir::UintTy::U8,
1565                            layout::Integer::I16 => rustc_type_ir::UintTy::U16,
1566                            layout::Integer::I32 => rustc_type_ir::UintTy::U32,
1567                            layout::Integer::I64 => rustc_type_ir::UintTy::U64,
1568                            layout::Integer::I128 => rustc_type_ir::UintTy::U128,
1569                        },
1570                    ),
1571                },
1572            },
1573        )
1574    }
1575
1576    /// Returns true if at least one variant of this enum is a non-unit variant.
1577    pub fn is_data_carrying(self, db: &dyn HirDatabase) -> bool {
1578        self.variants(db).iter().any(|v| !matches!(v.kind(db), StructKind::Unit))
1579    }
1580
1581    pub fn layout(self, db: &dyn HirDatabase) -> Result<Layout, LayoutError> {
1582        Adt::from(self).layout(db)
1583    }
1584
1585    pub fn is_unstable(self, db: &dyn HirDatabase) -> bool {
1586        AttrFlags::query(db, self.id.into()).contains(AttrFlags::IS_UNSTABLE)
1587    }
1588}
1589
1590impl HasVisibility for Enum {
1591    fn visibility(&self, db: &dyn HirDatabase) -> Visibility {
1592        let loc = self.id.lookup(db);
1593        let source = loc.source(db);
1594        visibility_from_ast(db, self.id, source.map(|src| src.visibility()))
1595    }
1596}
1597
1598#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
1599pub struct InstantiatedEnum<'db> {
1600    pub(crate) inner: Enum,
1601    pub(crate) args: GenericArgs<'db>,
1602}
1603
1604impl<'db> InstantiatedEnum<'db> {
1605    pub fn ty(self, db: &'db dyn HirDatabase) -> TypeNs<'db> {
1606        let interner = DbInterner::new_no_crate(db);
1607
1608        let ty = db.ty(self.inner.id.into());
1609        TypeNs::new(db, self.inner.id, ty.instantiate(interner, self.args))
1610    }
1611}
1612
1613impl From<&Variant> for DefWithBodyId {
1614    fn from(&v: &Variant) -> Self {
1615        DefWithBodyId::VariantId(v.into())
1616    }
1617}
1618
1619// FIXME: Rename to `EnumVariant`
1620#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
1621pub struct Variant {
1622    pub(crate) id: EnumVariantId,
1623}
1624
1625impl Variant {
1626    pub fn module(self, db: &dyn HirDatabase) -> Module {
1627        Module { id: self.id.module(db) }
1628    }
1629
1630    pub fn parent_enum(self, db: &dyn HirDatabase) -> Enum {
1631        self.id.lookup(db).parent.into()
1632    }
1633
1634    pub fn constructor_ty(self, db: &dyn HirDatabase) -> Type<'_> {
1635        Type::from_value_def(db, self.id)
1636    }
1637
1638    pub fn name(self, db: &dyn HirDatabase) -> Name {
1639        let lookup = self.id.lookup(db);
1640        let enum_ = lookup.parent;
1641        enum_.enum_variants(db).variants[lookup.index as usize].1.clone()
1642    }
1643
1644    pub fn fields(self, db: &dyn HirDatabase) -> Vec<Field> {
1645        self.id
1646            .fields(db)
1647            .fields()
1648            .iter()
1649            .map(|(id, _)| Field { parent: self.into(), id })
1650            .collect()
1651    }
1652
1653    pub fn kind(self, db: &dyn HirDatabase) -> StructKind {
1654        match self.id.fields(db).shape {
1655            hir_def::item_tree::FieldsShape::Record => StructKind::Record,
1656            hir_def::item_tree::FieldsShape::Tuple => StructKind::Tuple,
1657            hir_def::item_tree::FieldsShape::Unit => StructKind::Unit,
1658        }
1659    }
1660
1661    pub fn value(self, db: &dyn HirDatabase) -> Option<ast::Expr> {
1662        self.source(db)?.value.expr()
1663    }
1664
1665    pub fn eval(self, db: &dyn HirDatabase) -> Result<i128, ConstEvalError<'_>> {
1666        db.const_eval_discriminant(self.into())
1667    }
1668
1669    pub fn layout(&self, db: &dyn HirDatabase) -> Result<Layout, LayoutError> {
1670        let parent_enum = self.parent_enum(db);
1671        let parent_layout = parent_enum.layout(db)?;
1672        Ok(match &parent_layout.0.variants {
1673            layout::Variants::Multiple { variants, .. } => Layout(
1674                {
1675                    let lookup = self.id.lookup(db);
1676                    let rustc_enum_variant_idx = RustcEnumVariantIdx(lookup.index as usize);
1677                    Arc::new(variants[rustc_enum_variant_idx].clone())
1678                },
1679                db.target_data_layout(parent_enum.krate(db).into()).unwrap(),
1680            ),
1681            _ => parent_layout,
1682        })
1683    }
1684
1685    pub fn is_unstable(self, db: &dyn HirDatabase) -> bool {
1686        AttrFlags::query(db, self.id.into()).contains(AttrFlags::IS_UNSTABLE)
1687    }
1688
1689    pub fn instantiate_infer<'db>(self, infer_ctxt: &InferCtxt<'db>) -> InstantiatedVariant<'db> {
1690        let args =
1691            infer_ctxt.fresh_args_for_item(self.parent_enum(infer_ctxt.interner.db()).id.into());
1692        InstantiatedVariant { inner: self, args }
1693    }
1694}
1695
1696// FIXME: Rename to `EnumVariant`
1697#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
1698pub struct InstantiatedVariant<'db> {
1699    pub(crate) inner: Variant,
1700    pub(crate) args: GenericArgs<'db>,
1701}
1702
1703impl<'db> InstantiatedVariant<'db> {
1704    pub fn parent_enum(self, db: &dyn HirDatabase) -> InstantiatedEnum<'db> {
1705        InstantiatedEnum { inner: self.inner.id.lookup(db).parent.into(), args: self.args }
1706    }
1707
1708    pub fn fields(self, db: &dyn HirDatabase) -> Vec<InstantiatedField<'db>> {
1709        self.inner
1710            .id
1711            .fields(db)
1712            .fields()
1713            .iter()
1714            .map(|(id, _)| InstantiatedField {
1715                inner: Field { parent: self.inner.into(), id },
1716                args: self.args,
1717            })
1718            .collect()
1719    }
1720}
1721
1722#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
1723pub enum StructKind {
1724    Record,
1725    Tuple,
1726    Unit,
1727}
1728
1729/// Variants inherit visibility from the parent enum.
1730impl HasVisibility for Variant {
1731    fn visibility(&self, db: &dyn HirDatabase) -> Visibility {
1732        self.parent_enum(db).visibility(db)
1733    }
1734}
1735
1736/// A Data Type
1737#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)]
1738pub enum Adt {
1739    Struct(Struct),
1740    Union(Union),
1741    Enum(Enum),
1742}
1743impl_from!(Struct, Union, Enum for Adt);
1744
1745impl Adt {
1746    pub fn has_non_default_type_params(self, db: &dyn HirDatabase) -> bool {
1747        has_non_default_type_params(db, self.into())
1748    }
1749
1750    pub fn layout(self, db: &dyn HirDatabase) -> Result<Layout, LayoutError> {
1751        let interner = DbInterner::new_no_crate(db);
1752        let adt_id = AdtId::from(self);
1753        let args = GenericArgs::for_item_with_defaults(interner, adt_id.into(), |_, id, _| {
1754            GenericArg::error_from_id(interner, id)
1755        });
1756        db.layout_of_adt(adt_id, args, param_env_from_has_crate(db, adt_id))
1757            .map(|layout| Layout(layout, db.target_data_layout(self.krate(db).id).unwrap()))
1758    }
1759
1760    /// Turns this ADT into a type. Any type parameters of the ADT will be
1761    /// turned into unknown types, which is good for e.g. finding the most
1762    /// general set of completions, but will not look very nice when printed.
1763    pub fn ty(self, db: &dyn HirDatabase) -> Type<'_> {
1764        let id = AdtId::from(self);
1765        Type::from_def(db, id)
1766    }
1767
1768    /// Turns this ADT into a type with the given type parameters. This isn't
1769    /// the greatest API, FIXME find a better one.
1770    pub fn ty_with_args<'db>(
1771        self,
1772        db: &'db dyn HirDatabase,
1773        args: impl IntoIterator<Item = Type<'db>>,
1774    ) -> Type<'db> {
1775        let id = AdtId::from(self);
1776        let interner = DbInterner::new_no_crate(db);
1777        let ty = Ty::new_adt(
1778            interner,
1779            id,
1780            generic_args_from_tys(interner, id.into(), args.into_iter().map(|ty| ty.ty)),
1781        );
1782        Type::new(db, id, ty)
1783    }
1784
1785    pub fn module(self, db: &dyn HirDatabase) -> Module {
1786        match self {
1787            Adt::Struct(s) => s.module(db),
1788            Adt::Union(s) => s.module(db),
1789            Adt::Enum(e) => e.module(db),
1790        }
1791    }
1792
1793    pub fn name(self, db: &dyn HirDatabase) -> Name {
1794        match self {
1795            Adt::Struct(s) => s.name(db),
1796            Adt::Union(u) => u.name(db),
1797            Adt::Enum(e) => e.name(db),
1798        }
1799    }
1800
1801    /// Returns the lifetime of the DataType
1802    pub fn lifetime(&self, db: &dyn HirDatabase) -> Option<LifetimeParamData> {
1803        let resolver = match self {
1804            Adt::Struct(s) => s.id.resolver(db),
1805            Adt::Union(u) => u.id.resolver(db),
1806            Adt::Enum(e) => e.id.resolver(db),
1807        };
1808        resolver
1809            .generic_params()
1810            .and_then(|gp| {
1811                gp.iter_lt()
1812                    // there should only be a single lifetime
1813                    // but `Arena` requires to use an iterator
1814                    .nth(0)
1815            })
1816            .map(|arena| arena.1.clone())
1817    }
1818
1819    pub fn as_struct(&self) -> Option<Struct> {
1820        if let Self::Struct(v) = self { Some(*v) } else { None }
1821    }
1822
1823    pub fn as_enum(&self) -> Option<Enum> {
1824        if let Self::Enum(v) = self { Some(*v) } else { None }
1825    }
1826}
1827
1828impl HasVisibility for Adt {
1829    fn visibility(&self, db: &dyn HirDatabase) -> Visibility {
1830        match self {
1831            Adt::Struct(it) => it.visibility(db),
1832            Adt::Union(it) => it.visibility(db),
1833            Adt::Enum(it) => it.visibility(db),
1834        }
1835    }
1836}
1837
1838#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)]
1839pub enum VariantDef {
1840    Struct(Struct),
1841    Union(Union),
1842    Variant(Variant),
1843}
1844impl_from!(Struct, Union, Variant for VariantDef);
1845
1846impl VariantDef {
1847    pub fn fields(self, db: &dyn HirDatabase) -> Vec<Field> {
1848        match self {
1849            VariantDef::Struct(it) => it.fields(db),
1850            VariantDef::Union(it) => it.fields(db),
1851            VariantDef::Variant(it) => it.fields(db),
1852        }
1853    }
1854
1855    pub fn module(self, db: &dyn HirDatabase) -> Module {
1856        match self {
1857            VariantDef::Struct(it) => it.module(db),
1858            VariantDef::Union(it) => it.module(db),
1859            VariantDef::Variant(it) => it.module(db),
1860        }
1861    }
1862
1863    pub fn name(&self, db: &dyn HirDatabase) -> Name {
1864        match self {
1865            VariantDef::Struct(s) => s.name(db),
1866            VariantDef::Union(u) => u.name(db),
1867            VariantDef::Variant(e) => e.name(db),
1868        }
1869    }
1870}
1871
1872/// The defs which have a body.
1873#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
1874pub enum DefWithBody {
1875    Function(Function),
1876    Static(Static),
1877    Const(Const),
1878    Variant(Variant),
1879}
1880impl_from!(Function, Const, Static, Variant for DefWithBody);
1881
1882impl DefWithBody {
1883    pub fn module(self, db: &dyn HirDatabase) -> Module {
1884        match self {
1885            DefWithBody::Const(c) => c.module(db),
1886            DefWithBody::Function(f) => f.module(db),
1887            DefWithBody::Static(s) => s.module(db),
1888            DefWithBody::Variant(v) => v.module(db),
1889        }
1890    }
1891
1892    pub fn name(self, db: &dyn HirDatabase) -> Option<Name> {
1893        match self {
1894            DefWithBody::Function(f) => Some(f.name(db)),
1895            DefWithBody::Static(s) => Some(s.name(db)),
1896            DefWithBody::Const(c) => c.name(db),
1897            DefWithBody::Variant(v) => Some(v.name(db)),
1898        }
1899    }
1900
1901    /// Returns the type this def's body has to evaluate to.
1902    pub fn body_type(self, db: &dyn HirDatabase) -> Type<'_> {
1903        match self {
1904            DefWithBody::Function(it) => it.ret_type(db),
1905            DefWithBody::Static(it) => it.ty(db),
1906            DefWithBody::Const(it) => it.ty(db),
1907            DefWithBody::Variant(it) => it.parent_enum(db).variant_body_ty(db),
1908        }
1909    }
1910
1911    fn id(&self) -> DefWithBodyId {
1912        match self {
1913            DefWithBody::Function(it) => it.id.into(),
1914            DefWithBody::Static(it) => it.id.into(),
1915            DefWithBody::Const(it) => it.id.into(),
1916            DefWithBody::Variant(it) => it.into(),
1917        }
1918    }
1919
1920    /// A textual representation of the HIR of this def's body for debugging purposes.
1921    pub fn debug_hir(self, db: &dyn HirDatabase) -> String {
1922        let body = db.body(self.id());
1923        body.pretty_print(db, self.id(), Edition::CURRENT)
1924    }
1925
1926    /// A textual representation of the MIR of this def's body for debugging purposes.
1927    pub fn debug_mir(self, db: &dyn HirDatabase) -> String {
1928        let body = db.mir_body(self.id());
1929        match body {
1930            Ok(body) => body.pretty_print(db, self.module(db).krate(db).to_display_target(db)),
1931            Err(e) => format!("error:\n{e:?}"),
1932        }
1933    }
1934
1935    pub fn diagnostics<'db>(
1936        self,
1937        db: &'db dyn HirDatabase,
1938        acc: &mut Vec<AnyDiagnostic<'db>>,
1939        style_lints: bool,
1940    ) {
1941        let krate = self.module(db).id.krate(db);
1942
1943        let (body, source_map) = db.body_with_source_map(self.into());
1944        let sig_source_map = match self {
1945            DefWithBody::Function(id) => db.function_signature_with_source_map(id.into()).1,
1946            DefWithBody::Static(id) => db.static_signature_with_source_map(id.into()).1,
1947            DefWithBody::Const(id) => db.const_signature_with_source_map(id.into()).1,
1948            DefWithBody::Variant(variant) => {
1949                let enum_id = variant.parent_enum(db).id;
1950                db.enum_signature_with_source_map(enum_id).1
1951            }
1952        };
1953
1954        for (_, def_map) in body.blocks(db) {
1955            Module { id: def_map.root_module_id() }.diagnostics(db, acc, style_lints);
1956        }
1957
1958        expr_store_diagnostics(db, acc, &source_map);
1959
1960        let infer = InferenceResult::for_body(db, self.into());
1961        for d in infer.diagnostics() {
1962            acc.extend(AnyDiagnostic::inference_diagnostic(
1963                db,
1964                self.into(),
1965                d,
1966                &source_map,
1967                &sig_source_map,
1968            ));
1969        }
1970
1971        for (pat_or_expr, mismatch) in infer.type_mismatches() {
1972            let expr_or_pat = match pat_or_expr {
1973                ExprOrPatId::ExprId(expr) => source_map.expr_syntax(expr).map(Either::Left),
1974                ExprOrPatId::PatId(pat) => source_map.pat_syntax(pat).map(Either::Right),
1975            };
1976            let expr_or_pat = match expr_or_pat {
1977                Ok(Either::Left(expr)) => expr,
1978                Ok(Either::Right(InFile { file_id, value: pat })) => {
1979                    // cast from Either<Pat, SelfParam> -> Either<_, Pat>
1980                    let Some(ptr) = AstPtr::try_from_raw(pat.syntax_node_ptr()) else {
1981                        continue;
1982                    };
1983                    InFile { file_id, value: ptr }
1984                }
1985                Err(SyntheticSyntax) => continue,
1986            };
1987
1988            acc.push(
1989                TypeMismatch {
1990                    expr_or_pat,
1991                    expected: Type::new(db, DefWithBodyId::from(self), mismatch.expected),
1992                    actual: Type::new(db, DefWithBodyId::from(self), mismatch.actual),
1993                }
1994                .into(),
1995            );
1996        }
1997
1998        let missing_unsafe = hir_ty::diagnostics::missing_unsafe(db, self.into());
1999        for (node, reason) in missing_unsafe.unsafe_exprs {
2000            match source_map.expr_or_pat_syntax(node) {
2001                Ok(node) => acc.push(
2002                    MissingUnsafe {
2003                        node,
2004                        lint: if missing_unsafe.fn_is_unsafe {
2005                            UnsafeLint::UnsafeOpInUnsafeFn
2006                        } else {
2007                            UnsafeLint::HardError
2008                        },
2009                        reason,
2010                    }
2011                    .into(),
2012                ),
2013                Err(SyntheticSyntax) => {
2014                    // FIXME: Here and elsewhere in this file, the `expr` was
2015                    // desugared, report or assert that this doesn't happen.
2016                }
2017            }
2018        }
2019        for node in missing_unsafe.deprecated_safe_calls {
2020            match source_map.expr_syntax(node) {
2021                Ok(node) => acc.push(
2022                    MissingUnsafe {
2023                        node,
2024                        lint: UnsafeLint::DeprecatedSafe2024,
2025                        reason: UnsafetyReason::UnsafeFnCall,
2026                    }
2027                    .into(),
2028                ),
2029                Err(SyntheticSyntax) => never!("synthetic DeprecatedSafe2024"),
2030            }
2031        }
2032
2033        if let Ok(borrowck_results) = db.borrowck(self.into()) {
2034            for borrowck_result in borrowck_results.iter() {
2035                let mir_body = &borrowck_result.mir_body;
2036                for moof in &borrowck_result.moved_out_of_ref {
2037                    let span: InFile<SyntaxNodePtr> = match moof.span {
2038                        mir::MirSpan::ExprId(e) => match source_map.expr_syntax(e) {
2039                            Ok(s) => s.map(|it| it.into()),
2040                            Err(_) => continue,
2041                        },
2042                        mir::MirSpan::PatId(p) => match source_map.pat_syntax(p) {
2043                            Ok(s) => s.map(|it| it.into()),
2044                            Err(_) => continue,
2045                        },
2046                        mir::MirSpan::SelfParam => match source_map.self_param_syntax() {
2047                            Some(s) => s.map(|it| it.into()),
2048                            None => continue,
2049                        },
2050                        mir::MirSpan::BindingId(b) => {
2051                            match source_map
2052                                .patterns_for_binding(b)
2053                                .iter()
2054                                .find_map(|p| source_map.pat_syntax(*p).ok())
2055                            {
2056                                Some(s) => s.map(|it| it.into()),
2057                                None => continue,
2058                            }
2059                        }
2060                        mir::MirSpan::Unknown => continue,
2061                    };
2062                    acc.push(MovedOutOfRef { ty: Type::new_for_crate(krate, moof.ty), span }.into())
2063                }
2064                let mol = &borrowck_result.mutability_of_locals;
2065                for (binding_id, binding_data) in body.bindings() {
2066                    if binding_data.problems.is_some() {
2067                        // We should report specific diagnostics for these problems, not `need-mut` and `unused-mut`.
2068                        continue;
2069                    }
2070                    let Some(&local) = mir_body.binding_locals.get(binding_id) else {
2071                        continue;
2072                    };
2073                    if source_map
2074                        .patterns_for_binding(binding_id)
2075                        .iter()
2076                        .any(|&pat| source_map.pat_syntax(pat).is_err())
2077                    {
2078                        // Skip synthetic bindings
2079                        continue;
2080                    }
2081                    let mut need_mut = &mol[local];
2082                    if body[binding_id].name == sym::self_
2083                        && need_mut == &mir::MutabilityReason::Unused
2084                    {
2085                        need_mut = &mir::MutabilityReason::Not;
2086                    }
2087                    let local = Local { parent: self.into(), binding_id };
2088                    let is_mut = body[binding_id].mode == BindingAnnotation::Mutable;
2089
2090                    match (need_mut, is_mut) {
2091                        (mir::MutabilityReason::Unused, _) => {
2092                            let should_ignore = body[binding_id].name.as_str().starts_with('_');
2093                            if !should_ignore {
2094                                acc.push(UnusedVariable { local }.into())
2095                            }
2096                        }
2097                        (mir::MutabilityReason::Mut { .. }, true)
2098                        | (mir::MutabilityReason::Not, false) => (),
2099                        (mir::MutabilityReason::Mut { spans }, false) => {
2100                            for span in spans {
2101                                let span: InFile<SyntaxNodePtr> = match span {
2102                                    mir::MirSpan::ExprId(e) => match source_map.expr_syntax(*e) {
2103                                        Ok(s) => s.map(|it| it.into()),
2104                                        Err(_) => continue,
2105                                    },
2106                                    mir::MirSpan::PatId(p) => match source_map.pat_syntax(*p) {
2107                                        Ok(s) => s.map(|it| it.into()),
2108                                        Err(_) => continue,
2109                                    },
2110                                    mir::MirSpan::BindingId(b) => {
2111                                        match source_map
2112                                            .patterns_for_binding(*b)
2113                                            .iter()
2114                                            .find_map(|p| source_map.pat_syntax(*p).ok())
2115                                        {
2116                                            Some(s) => s.map(|it| it.into()),
2117                                            None => continue,
2118                                        }
2119                                    }
2120                                    mir::MirSpan::SelfParam => match source_map.self_param_syntax()
2121                                    {
2122                                        Some(s) => s.map(|it| it.into()),
2123                                        None => continue,
2124                                    },
2125                                    mir::MirSpan::Unknown => continue,
2126                                };
2127                                acc.push(NeedMut { local, span }.into());
2128                            }
2129                        }
2130                        (mir::MutabilityReason::Not, true) => {
2131                            if !infer.mutated_bindings_in_closure.contains(&binding_id) {
2132                                let should_ignore = body[binding_id].name.as_str().starts_with('_');
2133                                if !should_ignore {
2134                                    acc.push(UnusedMut { local }.into())
2135                                }
2136                            }
2137                        }
2138                    }
2139                }
2140            }
2141        }
2142
2143        for diagnostic in BodyValidationDiagnostic::collect(db, self.into(), style_lints) {
2144            acc.extend(AnyDiagnostic::body_validation_diagnostic(db, diagnostic, &source_map));
2145        }
2146
2147        let def: ModuleDef = match self {
2148            DefWithBody::Function(it) => it.into(),
2149            DefWithBody::Static(it) => it.into(),
2150            DefWithBody::Const(it) => it.into(),
2151            DefWithBody::Variant(it) => it.into(),
2152        };
2153        for diag in hir_ty::diagnostics::incorrect_case(db, def.into()) {
2154            acc.push(diag.into())
2155        }
2156    }
2157}
2158
2159fn expr_store_diagnostics<'db>(
2160    db: &'db dyn HirDatabase,
2161    acc: &mut Vec<AnyDiagnostic<'db>>,
2162    source_map: &ExpressionStoreSourceMap,
2163) {
2164    for diag in source_map.diagnostics() {
2165        acc.push(match diag {
2166            ExpressionStoreDiagnostics::InactiveCode { node, cfg, opts } => {
2167                InactiveCode { node: *node, cfg: cfg.clone(), opts: opts.clone() }.into()
2168            }
2169            ExpressionStoreDiagnostics::UnresolvedMacroCall { node, path } => UnresolvedMacroCall {
2170                range: node.map(|ptr| ptr.text_range()),
2171                path: path.clone(),
2172                is_bang: true,
2173            }
2174            .into(),
2175            ExpressionStoreDiagnostics::AwaitOutsideOfAsync { node, location } => {
2176                AwaitOutsideOfAsync { node: *node, location: location.clone() }.into()
2177            }
2178            ExpressionStoreDiagnostics::UnreachableLabel { node, name } => {
2179                UnreachableLabel { node: *node, name: name.clone() }.into()
2180            }
2181            ExpressionStoreDiagnostics::UndeclaredLabel { node, name } => {
2182                UndeclaredLabel { node: *node, name: name.clone() }.into()
2183            }
2184        });
2185    }
2186
2187    source_map
2188        .macro_calls()
2189        .for_each(|(_ast_id, call_id)| macro_call_diagnostics(db, call_id, acc));
2190}
2191#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
2192pub struct Function {
2193    pub(crate) id: FunctionId,
2194}
2195
2196impl Function {
2197    pub fn module(self, db: &dyn HirDatabase) -> Module {
2198        self.id.module(db).into()
2199    }
2200
2201    pub fn name(self, db: &dyn HirDatabase) -> Name {
2202        db.function_signature(self.id).name.clone()
2203    }
2204
2205    pub fn ty(self, db: &dyn HirDatabase) -> Type<'_> {
2206        Type::from_value_def(db, self.id)
2207    }
2208
2209    pub fn fn_ptr_type(self, db: &dyn HirDatabase) -> Type<'_> {
2210        let resolver = self.id.resolver(db);
2211        let interner = DbInterner::new_no_crate(db);
2212        // FIXME: This shouldn't be `instantiate_identity()`, we shouldn't leak `TyKind::Param`s.
2213        let callable_sig = db.callable_item_signature(self.id.into()).instantiate_identity();
2214        let ty = Ty::new_fn_ptr(interner, callable_sig);
2215        Type::new_with_resolver_inner(db, &resolver, ty)
2216    }
2217
2218    // FIXME: Find a better API to express all combinations here, perhaps we should have `PreInstantiationType`?
2219
2220    /// Get this function's return type
2221    pub fn ret_type(self, db: &dyn HirDatabase) -> Type<'_> {
2222        let resolver = self.id.resolver(db);
2223        // FIXME: This shouldn't be `instantiate_identity()`, we shouldn't leak `TyKind::Param`s.
2224        let ty = db
2225            .callable_item_signature(self.id.into())
2226            .instantiate_identity()
2227            .skip_binder()
2228            .output();
2229        Type::new_with_resolver_inner(db, &resolver, ty)
2230    }
2231
2232    // FIXME: Find better API to also handle const generics
2233    pub fn ret_type_with_args<'db>(
2234        self,
2235        db: &'db dyn HirDatabase,
2236        generics: impl Iterator<Item = Type<'db>>,
2237    ) -> Type<'db> {
2238        let resolver = self.id.resolver(db);
2239        let interner = DbInterner::new_no_crate(db);
2240        let args = generic_args_from_tys(interner, self.id.into(), generics.map(|ty| ty.ty));
2241
2242        let interner = DbInterner::new_no_crate(db);
2243        let ty = db
2244            .callable_item_signature(self.id.into())
2245            .instantiate(interner, args)
2246            .skip_binder()
2247            .output();
2248        Type::new_with_resolver_inner(db, &resolver, ty)
2249    }
2250
2251    pub fn async_ret_type<'db>(self, db: &'db dyn HirDatabase) -> Option<Type<'db>> {
2252        if !self.is_async(db) {
2253            return None;
2254        }
2255        let resolver = self.id.resolver(db);
2256        // FIXME: This shouldn't be `instantiate_identity()`, we shouldn't leak `TyKind::Param`s.
2257        let ret_ty = db
2258            .callable_item_signature(self.id.into())
2259            .instantiate_identity()
2260            .skip_binder()
2261            .output();
2262        for pred in ret_ty.impl_trait_bounds(db).into_iter().flatten() {
2263            if let ClauseKind::Projection(projection) = pred.kind().skip_binder()
2264                && let Some(output_ty) = projection.term.as_type()
2265            {
2266                return Type::new_with_resolver_inner(db, &resolver, output_ty).into();
2267            }
2268        }
2269        None
2270    }
2271
2272    pub fn has_self_param(self, db: &dyn HirDatabase) -> bool {
2273        db.function_signature(self.id).has_self_param()
2274    }
2275
2276    pub fn self_param(self, db: &dyn HirDatabase) -> Option<SelfParam> {
2277        self.has_self_param(db).then_some(SelfParam { func: self.id })
2278    }
2279
2280    pub fn assoc_fn_params(self, db: &dyn HirDatabase) -> Vec<Param<'_>> {
2281        let environment = param_env_from_has_crate(db, self.id);
2282        // FIXME: This shouldn't be `instantiate_identity()`, we shouldn't leak `TyKind::Param`s.
2283        let callable_sig =
2284            db.callable_item_signature(self.id.into()).instantiate_identity().skip_binder();
2285        callable_sig
2286            .inputs()
2287            .iter()
2288            .enumerate()
2289            .map(|(idx, ty)| {
2290                let ty = Type { env: environment, ty };
2291                Param { func: Callee::Def(CallableDefId::FunctionId(self.id)), ty, idx }
2292            })
2293            .collect()
2294    }
2295
2296    pub fn num_params(self, db: &dyn HirDatabase) -> usize {
2297        db.function_signature(self.id).params.len()
2298    }
2299
2300    pub fn method_params(self, db: &dyn HirDatabase) -> Option<Vec<Param<'_>>> {
2301        self.self_param(db)?;
2302        Some(self.params_without_self(db))
2303    }
2304
2305    pub fn params_without_self(self, db: &dyn HirDatabase) -> Vec<Param<'_>> {
2306        let environment = param_env_from_has_crate(db, self.id);
2307        // FIXME: This shouldn't be `instantiate_identity()`, we shouldn't leak `TyKind::Param`s.
2308        let callable_sig =
2309            db.callable_item_signature(self.id.into()).instantiate_identity().skip_binder();
2310        let skip = if db.function_signature(self.id).has_self_param() { 1 } else { 0 };
2311        callable_sig
2312            .inputs()
2313            .iter()
2314            .enumerate()
2315            .skip(skip)
2316            .map(|(idx, ty)| {
2317                let ty = Type { env: environment, ty };
2318                Param { func: Callee::Def(CallableDefId::FunctionId(self.id)), ty, idx }
2319            })
2320            .collect()
2321    }
2322
2323    // FIXME: Find better API to also handle const generics
2324    pub fn params_without_self_with_args<'db>(
2325        self,
2326        db: &'db dyn HirDatabase,
2327        generics: impl Iterator<Item = Type<'db>>,
2328    ) -> Vec<Param<'db>> {
2329        let environment = param_env_from_has_crate(db, self.id);
2330        let interner = DbInterner::new_no_crate(db);
2331        let args = generic_args_from_tys(interner, self.id.into(), generics.map(|ty| ty.ty));
2332        let callable_sig =
2333            db.callable_item_signature(self.id.into()).instantiate(interner, args).skip_binder();
2334        let skip = if db.function_signature(self.id).has_self_param() { 1 } else { 0 };
2335        callable_sig
2336            .inputs()
2337            .iter()
2338            .enumerate()
2339            .skip(skip)
2340            .map(|(idx, ty)| {
2341                let ty = Type { env: environment, ty };
2342                Param { func: Callee::Def(CallableDefId::FunctionId(self.id)), ty, idx }
2343            })
2344            .collect()
2345    }
2346
2347    pub fn is_const(self, db: &dyn HirDatabase) -> bool {
2348        db.function_signature(self.id).is_const()
2349    }
2350
2351    pub fn is_async(self, db: &dyn HirDatabase) -> bool {
2352        db.function_signature(self.id).is_async()
2353    }
2354
2355    pub fn is_varargs(self, db: &dyn HirDatabase) -> bool {
2356        db.function_signature(self.id).is_varargs()
2357    }
2358
2359    pub fn extern_block(self, db: &dyn HirDatabase) -> Option<ExternBlock> {
2360        match self.id.lookup(db).container {
2361            ItemContainerId::ExternBlockId(id) => Some(ExternBlock { id }),
2362            _ => None,
2363        }
2364    }
2365
2366    pub fn returns_impl_future(self, db: &dyn HirDatabase) -> bool {
2367        if self.is_async(db) {
2368            return true;
2369        }
2370
2371        let ret_type = self.ret_type(db);
2372        let Some(impl_traits) = ret_type.as_impl_traits(db) else { return false };
2373        let lang_items = hir_def::lang_item::lang_items(db, self.krate(db).id);
2374        let Some(future_trait_id) = lang_items.Future else {
2375            return false;
2376        };
2377        let Some(sized_trait_id) = lang_items.Sized else {
2378            return false;
2379        };
2380
2381        let mut has_impl_future = false;
2382        impl_traits
2383            .filter(|t| {
2384                let fut = t.id == future_trait_id;
2385                has_impl_future |= fut;
2386                !fut && t.id != sized_trait_id
2387            })
2388            // all traits but the future trait must be auto traits
2389            .all(|t| t.is_auto(db))
2390            && has_impl_future
2391    }
2392
2393    /// Does this function have `#[test]` attribute?
2394    pub fn is_test(self, db: &dyn HirDatabase) -> bool {
2395        self.attrs(db).is_test()
2396    }
2397
2398    /// is this a `fn main` or a function with an `export_name` of `main`?
2399    pub fn is_main(self, db: &dyn HirDatabase) -> bool {
2400        self.exported_main(db)
2401            || self.module(db).is_crate_root(db) && db.function_signature(self.id).name == sym::main
2402    }
2403
2404    /// Is this a function with an `export_name` of `main`?
2405    pub fn exported_main(self, db: &dyn HirDatabase) -> bool {
2406        AttrFlags::query(db, self.id.into()).contains(AttrFlags::IS_EXPORT_NAME_MAIN)
2407    }
2408
2409    /// Does this function have the ignore attribute?
2410    pub fn is_ignore(self, db: &dyn HirDatabase) -> bool {
2411        AttrFlags::query(db, self.id.into()).contains(AttrFlags::IS_IGNORE)
2412    }
2413
2414    /// Does this function have `#[bench]` attribute?
2415    pub fn is_bench(self, db: &dyn HirDatabase) -> bool {
2416        AttrFlags::query(db, self.id.into()).contains(AttrFlags::IS_BENCH)
2417    }
2418
2419    /// Is this function marked as unstable with `#[feature]` attribute?
2420    pub fn is_unstable(self, db: &dyn HirDatabase) -> bool {
2421        AttrFlags::query(db, self.id.into()).contains(AttrFlags::IS_UNSTABLE)
2422    }
2423
2424    pub fn is_unsafe_to_call(
2425        self,
2426        db: &dyn HirDatabase,
2427        caller: Option<Function>,
2428        call_edition: Edition,
2429    ) -> bool {
2430        let (target_features, target_feature_is_safe_in_target) = caller
2431            .map(|caller| {
2432                let target_features = hir_ty::TargetFeatures::from_fn(db, caller.id);
2433                let target_feature_is_safe_in_target =
2434                    match &caller.krate(db).id.workspace_data(db).target {
2435                        Ok(target) => hir_ty::target_feature_is_safe_in_target(target),
2436                        Err(_) => hir_ty::TargetFeatureIsSafeInTarget::No,
2437                    };
2438                (target_features, target_feature_is_safe_in_target)
2439            })
2440            .unwrap_or_else(|| {
2441                (hir_ty::TargetFeatures::default(), hir_ty::TargetFeatureIsSafeInTarget::No)
2442            });
2443        matches!(
2444            hir_ty::is_fn_unsafe_to_call(
2445                db,
2446                self.id,
2447                &target_features,
2448                call_edition,
2449                target_feature_is_safe_in_target
2450            ),
2451            hir_ty::Unsafety::Unsafe
2452        )
2453    }
2454
2455    /// Whether this function declaration has a definition.
2456    ///
2457    /// This is false in the case of required (not provided) trait methods.
2458    pub fn has_body(self, db: &dyn HirDatabase) -> bool {
2459        db.function_signature(self.id).has_body()
2460    }
2461
2462    pub fn as_proc_macro(self, db: &dyn HirDatabase) -> Option<Macro> {
2463        let def_map = crate_def_map(db, HasModule::krate(&self.id, db));
2464        def_map.fn_as_proc_macro(self.id).map(|id| Macro { id: id.into() })
2465    }
2466
2467    pub fn eval(
2468        self,
2469        db: &dyn HirDatabase,
2470        span_formatter: impl Fn(FileId, TextRange) -> String,
2471    ) -> Result<String, ConstEvalError<'_>> {
2472        let interner = DbInterner::new_no_crate(db);
2473        let body = db.monomorphized_mir_body(
2474            self.id.into(),
2475            GenericArgs::new_from_iter(interner, []),
2476            ParamEnvAndCrate {
2477                param_env: db.trait_environment(self.id.into()),
2478                krate: self.id.module(db).krate(db),
2479            },
2480        )?;
2481        let (result, output) = interpret_mir(db, body, false, None)?;
2482        let mut text = match result {
2483            Ok(_) => "pass".to_owned(),
2484            Err(e) => {
2485                let mut r = String::new();
2486                _ = e.pretty_print(
2487                    &mut r,
2488                    db,
2489                    &span_formatter,
2490                    self.krate(db).to_display_target(db),
2491                );
2492                r
2493            }
2494        };
2495        let stdout = output.stdout().into_owned();
2496        if !stdout.is_empty() {
2497            text += "\n--------- stdout ---------\n";
2498            text += &stdout;
2499        }
2500        let stderr = output.stdout().into_owned();
2501        if !stderr.is_empty() {
2502            text += "\n--------- stderr ---------\n";
2503            text += &stderr;
2504        }
2505        Ok(text)
2506    }
2507}
2508
2509// Note: logically, this belongs to `hir_ty`, but we are not using it there yet.
2510#[derive(Clone, Copy, PartialEq, Eq)]
2511pub enum Access {
2512    Shared,
2513    Exclusive,
2514    Owned,
2515}
2516
2517impl From<hir_ty::next_solver::Mutability> for Access {
2518    fn from(mutability: hir_ty::next_solver::Mutability) -> Access {
2519        match mutability {
2520            hir_ty::next_solver::Mutability::Not => Access::Shared,
2521            hir_ty::next_solver::Mutability::Mut => Access::Exclusive,
2522        }
2523    }
2524}
2525
2526#[derive(Clone, PartialEq, Eq, Hash, Debug)]
2527pub struct Param<'db> {
2528    func: Callee<'db>,
2529    /// The index in parameter list, including self parameter.
2530    idx: usize,
2531    ty: Type<'db>,
2532}
2533
2534impl<'db> Param<'db> {
2535    pub fn parent_fn(&self) -> Option<Function> {
2536        match self.func {
2537            Callee::Def(CallableDefId::FunctionId(f)) => Some(f.into()),
2538            _ => None,
2539        }
2540    }
2541
2542    // pub fn parent_closure(&self) -> Option<Closure> {
2543    //     self.func.as_ref().right().cloned()
2544    // }
2545
2546    pub fn index(&self) -> usize {
2547        self.idx
2548    }
2549
2550    pub fn ty(&self) -> &Type<'db> {
2551        &self.ty
2552    }
2553
2554    pub fn name(&self, db: &dyn HirDatabase) -> Option<Name> {
2555        Some(self.as_local(db)?.name(db))
2556    }
2557
2558    pub fn as_local(&self, db: &dyn HirDatabase) -> Option<Local> {
2559        match self.func {
2560            Callee::Def(CallableDefId::FunctionId(it)) => {
2561                let parent = DefWithBodyId::FunctionId(it);
2562                let body = db.body(parent);
2563                if let Some(self_param) = body.self_param.filter(|_| self.idx == 0) {
2564                    Some(Local { parent, binding_id: self_param })
2565                } else if let Pat::Bind { id, .. } =
2566                    &body[body.params[self.idx - body.self_param.is_some() as usize]]
2567                {
2568                    Some(Local { parent, binding_id: *id })
2569                } else {
2570                    None
2571                }
2572            }
2573            Callee::Closure(closure, _) => {
2574                let c = db.lookup_intern_closure(closure);
2575                let body = db.body(c.0);
2576                if let Expr::Closure { args, .. } = &body[c.1]
2577                    && let Pat::Bind { id, .. } = &body[args[self.idx]]
2578                {
2579                    return Some(Local { parent: c.0, binding_id: *id });
2580                }
2581                None
2582            }
2583            _ => None,
2584        }
2585    }
2586
2587    pub fn pattern_source(self, db: &dyn HirDatabase) -> Option<ast::Pat> {
2588        self.source(db).and_then(|p| p.value.right()?.pat())
2589    }
2590}
2591
2592#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
2593pub struct SelfParam {
2594    func: FunctionId,
2595}
2596
2597impl SelfParam {
2598    pub fn access(self, db: &dyn HirDatabase) -> Access {
2599        let func_data = db.function_signature(self.func);
2600        func_data
2601            .params
2602            .first()
2603            .map(|&param| match &func_data.store[param] {
2604                TypeRef::Reference(ref_) => match ref_.mutability {
2605                    hir_def::type_ref::Mutability::Shared => Access::Shared,
2606                    hir_def::type_ref::Mutability::Mut => Access::Exclusive,
2607                },
2608                _ => Access::Owned,
2609            })
2610            .unwrap_or(Access::Owned)
2611    }
2612
2613    pub fn parent_fn(&self) -> Function {
2614        Function::from(self.func)
2615    }
2616
2617    pub fn ty<'db>(&self, db: &'db dyn HirDatabase) -> Type<'db> {
2618        // FIXME: This shouldn't be `instantiate_identity()`, we shouldn't leak `TyKind::Param`s.
2619        let callable_sig =
2620            db.callable_item_signature(self.func.into()).instantiate_identity().skip_binder();
2621        let environment = param_env_from_has_crate(db, self.func);
2622        let ty = callable_sig.inputs().as_slice()[0];
2623        Type { env: environment, ty }
2624    }
2625
2626    // FIXME: Find better API to also handle const generics
2627    pub fn ty_with_args<'db>(
2628        &self,
2629        db: &'db dyn HirDatabase,
2630        generics: impl Iterator<Item = Type<'db>>,
2631    ) -> Type<'db> {
2632        let interner = DbInterner::new_no_crate(db);
2633        let args = generic_args_from_tys(interner, self.func.into(), generics.map(|ty| ty.ty));
2634        let callable_sig =
2635            db.callable_item_signature(self.func.into()).instantiate(interner, args).skip_binder();
2636        let environment = param_env_from_has_crate(db, self.func);
2637        let ty = callable_sig.inputs().as_slice()[0];
2638        Type { env: environment, ty }
2639    }
2640}
2641
2642impl HasVisibility for Function {
2643    fn visibility(&self, db: &dyn HirDatabase) -> Visibility {
2644        db.assoc_visibility(self.id.into())
2645    }
2646}
2647
2648#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
2649pub struct ExternCrateDecl {
2650    pub(crate) id: ExternCrateId,
2651}
2652
2653impl ExternCrateDecl {
2654    pub fn module(self, db: &dyn HirDatabase) -> Module {
2655        self.id.module(db).into()
2656    }
2657
2658    pub fn resolved_crate(self, db: &dyn HirDatabase) -> Option<Crate> {
2659        let loc = self.id.lookup(db);
2660        let krate = loc.container.krate(db);
2661        let name = self.name(db);
2662        if name == sym::self_ {
2663            Some(krate.into())
2664        } else {
2665            krate.data(db).dependencies.iter().find_map(|dep| {
2666                if dep.name.symbol() == name.symbol() { Some(dep.crate_id.into()) } else { None }
2667            })
2668        }
2669    }
2670
2671    pub fn name(self, db: &dyn HirDatabase) -> Name {
2672        let loc = self.id.lookup(db);
2673        let source = loc.source(db);
2674        as_name_opt(source.value.name_ref())
2675    }
2676
2677    pub fn alias(self, db: &dyn HirDatabase) -> Option<ImportAlias> {
2678        let loc = self.id.lookup(db);
2679        let source = loc.source(db);
2680        let rename = source.value.rename()?;
2681        if let Some(name) = rename.name() {
2682            Some(ImportAlias::Alias(name.as_name()))
2683        } else if rename.underscore_token().is_some() {
2684            Some(ImportAlias::Underscore)
2685        } else {
2686            None
2687        }
2688    }
2689
2690    /// Returns the name under which this crate is made accessible, taking `_` into account.
2691    pub fn alias_or_name(self, db: &dyn HirDatabase) -> Option<Name> {
2692        match self.alias(db) {
2693            Some(ImportAlias::Underscore) => None,
2694            Some(ImportAlias::Alias(alias)) => Some(alias),
2695            None => Some(self.name(db)),
2696        }
2697    }
2698}
2699
2700impl HasVisibility for ExternCrateDecl {
2701    fn visibility(&self, db: &dyn HirDatabase) -> Visibility {
2702        let loc = self.id.lookup(db);
2703        let source = loc.source(db);
2704        visibility_from_ast(db, self.id, source.map(|src| src.visibility()))
2705    }
2706}
2707
2708#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
2709pub struct Const {
2710    pub(crate) id: ConstId,
2711}
2712
2713impl Const {
2714    pub fn module(self, db: &dyn HirDatabase) -> Module {
2715        Module { id: self.id.module(db) }
2716    }
2717
2718    pub fn name(self, db: &dyn HirDatabase) -> Option<Name> {
2719        db.const_signature(self.id).name.clone()
2720    }
2721
2722    pub fn value(self, db: &dyn HirDatabase) -> Option<ast::Expr> {
2723        self.source(db)?.value.body()
2724    }
2725
2726    pub fn ty(self, db: &dyn HirDatabase) -> Type<'_> {
2727        Type::from_value_def(db, self.id)
2728    }
2729
2730    /// Evaluate the constant.
2731    pub fn eval(self, db: &dyn HirDatabase) -> Result<EvaluatedConst<'_>, ConstEvalError<'_>> {
2732        let interner = DbInterner::new_no_crate(db);
2733        let ty = db.value_ty(self.id.into()).unwrap().instantiate_identity();
2734        db.const_eval(self.id, GenericArgs::new_from_iter(interner, []), None)
2735            .map(|it| EvaluatedConst { const_: it, def: self.id.into(), ty })
2736    }
2737}
2738
2739impl HasVisibility for Const {
2740    fn visibility(&self, db: &dyn HirDatabase) -> Visibility {
2741        db.assoc_visibility(self.id.into())
2742    }
2743}
2744
2745pub struct EvaluatedConst<'db> {
2746    def: DefWithBodyId,
2747    const_: hir_ty::next_solver::Const<'db>,
2748    ty: Ty<'db>,
2749}
2750
2751impl<'db> EvaluatedConst<'db> {
2752    pub fn render(&self, db: &dyn HirDatabase, display_target: DisplayTarget) -> String {
2753        format!("{}", self.const_.display(db, display_target))
2754    }
2755
2756    pub fn render_debug(&self, db: &'db dyn HirDatabase) -> Result<String, MirEvalError<'db>> {
2757        let kind = self.const_.kind();
2758        if let ConstKind::Value(c) = kind
2759            && let ty = c.ty.kind()
2760            && let TyKind::Int(_) | TyKind::Uint(_) = ty
2761        {
2762            let b = &c.value.inner().memory;
2763            let value = u128::from_le_bytes(mir::pad16(b, false));
2764            let value_signed = i128::from_le_bytes(mir::pad16(b, matches!(ty, TyKind::Int(_))));
2765            let mut result =
2766                if let TyKind::Int(_) = ty { value_signed.to_string() } else { value.to_string() };
2767            if value >= 10 {
2768                format_to!(result, " ({value:#X})");
2769                return Ok(result);
2770            } else {
2771                return Ok(result);
2772            }
2773        }
2774        mir::render_const_using_debug_impl(db, self.def, self.const_, self.ty)
2775    }
2776}
2777
2778#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
2779pub struct Static {
2780    pub(crate) id: StaticId,
2781}
2782
2783impl Static {
2784    pub fn module(self, db: &dyn HirDatabase) -> Module {
2785        Module { id: self.id.module(db) }
2786    }
2787
2788    pub fn name(self, db: &dyn HirDatabase) -> Name {
2789        db.static_signature(self.id).name.clone()
2790    }
2791
2792    pub fn is_mut(self, db: &dyn HirDatabase) -> bool {
2793        db.static_signature(self.id).flags.contains(StaticFlags::MUTABLE)
2794    }
2795
2796    pub fn value(self, db: &dyn HirDatabase) -> Option<ast::Expr> {
2797        self.source(db)?.value.body()
2798    }
2799
2800    pub fn ty(self, db: &dyn HirDatabase) -> Type<'_> {
2801        Type::from_value_def(db, self.id)
2802    }
2803
2804    pub fn extern_block(self, db: &dyn HirDatabase) -> Option<ExternBlock> {
2805        match self.id.lookup(db).container {
2806            ItemContainerId::ExternBlockId(id) => Some(ExternBlock { id }),
2807            _ => None,
2808        }
2809    }
2810
2811    /// Evaluate the static initializer.
2812    pub fn eval(self, db: &dyn HirDatabase) -> Result<EvaluatedConst<'_>, ConstEvalError<'_>> {
2813        let ty = db.value_ty(self.id.into()).unwrap().instantiate_identity();
2814        db.const_eval_static(self.id).map(|it| EvaluatedConst {
2815            const_: it,
2816            def: self.id.into(),
2817            ty,
2818        })
2819    }
2820}
2821
2822impl HasVisibility for Static {
2823    fn visibility(&self, db: &dyn HirDatabase) -> Visibility {
2824        let loc = self.id.lookup(db);
2825        let source = loc.source(db);
2826        visibility_from_ast(db, self.id, source.map(|src| src.visibility()))
2827    }
2828}
2829
2830#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
2831pub struct Trait {
2832    pub(crate) id: TraitId,
2833}
2834
2835impl Trait {
2836    pub fn lang(db: &dyn HirDatabase, krate: Crate, lang_item: LangItem) -> Option<Trait> {
2837        let lang_items = hir_def::lang_item::lang_items(db, krate.id);
2838        match lang_item.from_lang_items(lang_items)? {
2839            LangItemTarget::TraitId(it) => Some(it.into()),
2840            _ => None,
2841        }
2842    }
2843
2844    pub fn module(self, db: &dyn HirDatabase) -> Module {
2845        Module { id: self.id.lookup(db).container }
2846    }
2847
2848    pub fn name(self, db: &dyn HirDatabase) -> Name {
2849        db.trait_signature(self.id).name.clone()
2850    }
2851
2852    pub fn direct_supertraits(self, db: &dyn HirDatabase) -> Vec<Trait> {
2853        let traits = direct_super_traits(db, self.into());
2854        traits.iter().map(|tr| Trait::from(*tr)).collect()
2855    }
2856
2857    pub fn all_supertraits(self, db: &dyn HirDatabase) -> Vec<Trait> {
2858        let traits = all_super_traits(db, self.into());
2859        traits.iter().map(|tr| Trait::from(*tr)).collect()
2860    }
2861
2862    pub fn function(self, db: &dyn HirDatabase, name: impl PartialEq<Name>) -> Option<Function> {
2863        self.id.trait_items(db).items.iter().find(|(n, _)| name == *n).and_then(|&(_, it)| match it
2864        {
2865            AssocItemId::FunctionId(id) => Some(Function { id }),
2866            _ => None,
2867        })
2868    }
2869
2870    pub fn items(self, db: &dyn HirDatabase) -> Vec<AssocItem> {
2871        self.id.trait_items(db).items.iter().map(|(_name, it)| (*it).into()).collect()
2872    }
2873
2874    pub fn items_with_supertraits(self, db: &dyn HirDatabase) -> Vec<AssocItem> {
2875        self.all_supertraits(db).into_iter().flat_map(|tr| tr.items(db)).collect()
2876    }
2877
2878    pub fn is_auto(self, db: &dyn HirDatabase) -> bool {
2879        db.trait_signature(self.id).flags.contains(TraitFlags::AUTO)
2880    }
2881
2882    pub fn is_unsafe(&self, db: &dyn HirDatabase) -> bool {
2883        db.trait_signature(self.id).flags.contains(TraitFlags::UNSAFE)
2884    }
2885
2886    pub fn type_or_const_param_count(
2887        &self,
2888        db: &dyn HirDatabase,
2889        count_required_only: bool,
2890    ) -> usize {
2891        db.generic_params(self.id.into())
2892            .iter_type_or_consts()
2893            .filter(|(_, ty)| !matches!(ty, TypeOrConstParamData::TypeParamData(ty) if ty.provenance != TypeParamProvenance::TypeParamList))
2894            .filter(|(_, ty)| !count_required_only || !ty.has_default())
2895            .count()
2896    }
2897
2898    pub fn dyn_compatibility(&self, db: &dyn HirDatabase) -> Option<DynCompatibilityViolation> {
2899        hir_ty::dyn_compatibility::dyn_compatibility(db, self.id)
2900    }
2901
2902    pub fn dyn_compatibility_all_violations(
2903        &self,
2904        db: &dyn HirDatabase,
2905    ) -> Option<Vec<DynCompatibilityViolation>> {
2906        let mut violations = vec![];
2907        _ = hir_ty::dyn_compatibility::dyn_compatibility_with_callback(
2908            db,
2909            self.id,
2910            &mut |violation| {
2911                violations.push(violation);
2912                ControlFlow::Continue(())
2913            },
2914        );
2915        violations.is_empty().not().then_some(violations)
2916    }
2917
2918    fn all_macro_calls(&self, db: &dyn HirDatabase) -> Box<[(AstId<ast::Item>, MacroCallId)]> {
2919        self.id.trait_items(db).macro_calls.to_vec().into_boxed_slice()
2920    }
2921
2922    /// `#[rust_analyzer::completions(...)]` mode.
2923    pub fn complete(self, db: &dyn HirDatabase) -> Complete {
2924        Complete::extract(true, self.attrs(db).attrs)
2925    }
2926}
2927
2928impl HasVisibility for Trait {
2929    fn visibility(&self, db: &dyn HirDatabase) -> Visibility {
2930        let loc = self.id.lookup(db);
2931        let source = loc.source(db);
2932        visibility_from_ast(db, self.id, source.map(|src| src.visibility()))
2933    }
2934}
2935
2936#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
2937pub struct TypeAlias {
2938    pub(crate) id: TypeAliasId,
2939}
2940
2941impl TypeAlias {
2942    pub fn has_non_default_type_params(self, db: &dyn HirDatabase) -> bool {
2943        has_non_default_type_params(db, self.id.into())
2944    }
2945
2946    pub fn module(self, db: &dyn HirDatabase) -> Module {
2947        Module { id: self.id.module(db) }
2948    }
2949
2950    pub fn ty(self, db: &dyn HirDatabase) -> Type<'_> {
2951        Type::from_def(db, self.id)
2952    }
2953
2954    pub fn ty_params(self, db: &dyn HirDatabase) -> Type<'_> {
2955        Type::from_def_params(db, self.id)
2956    }
2957
2958    pub fn name(self, db: &dyn HirDatabase) -> Name {
2959        db.type_alias_signature(self.id).name.clone()
2960    }
2961}
2962
2963impl HasVisibility for TypeAlias {
2964    fn visibility(&self, db: &dyn HirDatabase) -> Visibility {
2965        db.assoc_visibility(self.id.into())
2966    }
2967}
2968
2969#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
2970pub struct ExternBlock {
2971    pub(crate) id: ExternBlockId,
2972}
2973
2974impl ExternBlock {
2975    pub fn module(self, db: &dyn HirDatabase) -> Module {
2976        Module { id: self.id.module(db) }
2977    }
2978}
2979
2980#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
2981pub struct StaticLifetime;
2982
2983impl StaticLifetime {
2984    pub fn name(self) -> Name {
2985        Name::new_symbol_root(sym::tick_static)
2986    }
2987}
2988
2989#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
2990pub struct BuiltinType {
2991    pub(crate) inner: hir_def::builtin_type::BuiltinType,
2992}
2993
2994impl BuiltinType {
2995    // Constructors are added on demand, feel free to add more.
2996    pub fn str() -> BuiltinType {
2997        BuiltinType { inner: hir_def::builtin_type::BuiltinType::Str }
2998    }
2999
3000    pub fn i32() -> BuiltinType {
3001        BuiltinType {
3002            inner: hir_def::builtin_type::BuiltinType::Int(hir_ty::primitive::BuiltinInt::I32),
3003        }
3004    }
3005
3006    pub fn ty<'db>(self, db: &'db dyn HirDatabase) -> Type<'db> {
3007        let core = Crate::core(db).map(|core| core.id).unwrap_or_else(|| db.all_crates()[0]);
3008        let interner = DbInterner::new_no_crate(db);
3009        Type::new_for_crate(core, Ty::from_builtin_type(interner, self.inner))
3010    }
3011
3012    pub fn name(self) -> Name {
3013        self.inner.as_name()
3014    }
3015
3016    pub fn is_int(&self) -> bool {
3017        matches!(self.inner, hir_def::builtin_type::BuiltinType::Int(_))
3018    }
3019
3020    pub fn is_uint(&self) -> bool {
3021        matches!(self.inner, hir_def::builtin_type::BuiltinType::Uint(_))
3022    }
3023
3024    pub fn is_float(&self) -> bool {
3025        matches!(self.inner, hir_def::builtin_type::BuiltinType::Float(_))
3026    }
3027
3028    pub fn is_f16(&self) -> bool {
3029        matches!(
3030            self.inner,
3031            hir_def::builtin_type::BuiltinType::Float(hir_def::builtin_type::BuiltinFloat::F16)
3032        )
3033    }
3034
3035    pub fn is_f32(&self) -> bool {
3036        matches!(
3037            self.inner,
3038            hir_def::builtin_type::BuiltinType::Float(hir_def::builtin_type::BuiltinFloat::F32)
3039        )
3040    }
3041
3042    pub fn is_f64(&self) -> bool {
3043        matches!(
3044            self.inner,
3045            hir_def::builtin_type::BuiltinType::Float(hir_def::builtin_type::BuiltinFloat::F64)
3046        )
3047    }
3048
3049    pub fn is_f128(&self) -> bool {
3050        matches!(
3051            self.inner,
3052            hir_def::builtin_type::BuiltinType::Float(hir_def::builtin_type::BuiltinFloat::F128)
3053        )
3054    }
3055
3056    pub fn is_char(&self) -> bool {
3057        matches!(self.inner, hir_def::builtin_type::BuiltinType::Char)
3058    }
3059
3060    pub fn is_bool(&self) -> bool {
3061        matches!(self.inner, hir_def::builtin_type::BuiltinType::Bool)
3062    }
3063
3064    pub fn is_str(&self) -> bool {
3065        matches!(self.inner, hir_def::builtin_type::BuiltinType::Str)
3066    }
3067}
3068
3069#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
3070pub struct Macro {
3071    pub(crate) id: MacroId,
3072}
3073
3074impl Macro {
3075    pub fn module(self, db: &dyn HirDatabase) -> Module {
3076        Module { id: self.id.module(db) }
3077    }
3078
3079    pub fn name(self, db: &dyn HirDatabase) -> Name {
3080        match self.id {
3081            MacroId::Macro2Id(id) => {
3082                let loc = id.lookup(db);
3083                let source = loc.source(db);
3084                as_name_opt(source.value.name())
3085            }
3086            MacroId::MacroRulesId(id) => {
3087                let loc = id.lookup(db);
3088                let source = loc.source(db);
3089                as_name_opt(source.value.name())
3090            }
3091            MacroId::ProcMacroId(id) => {
3092                let loc = id.lookup(db);
3093                let source = loc.source(db);
3094                match loc.kind {
3095                    ProcMacroKind::CustomDerive => AttrFlags::derive_info(db, self.id).map_or_else(
3096                        || as_name_opt(source.value.name()),
3097                        |info| Name::new_symbol_root(info.trait_name.clone()),
3098                    ),
3099                    ProcMacroKind::Bang | ProcMacroKind::Attr => as_name_opt(source.value.name()),
3100                }
3101            }
3102        }
3103    }
3104
3105    pub fn is_macro_export(self, db: &dyn HirDatabase) -> bool {
3106        matches!(self.id, MacroId::MacroRulesId(_) if AttrFlags::query(db, self.id.into()).contains(AttrFlags::IS_MACRO_EXPORT))
3107    }
3108
3109    pub fn is_proc_macro(self) -> bool {
3110        matches!(self.id, MacroId::ProcMacroId(_))
3111    }
3112
3113    pub fn kind(&self, db: &dyn HirDatabase) -> MacroKind {
3114        match self.id {
3115            MacroId::Macro2Id(it) => match it.lookup(db).expander {
3116                MacroExpander::Declarative { .. } => MacroKind::Declarative,
3117                MacroExpander::BuiltIn(_) | MacroExpander::BuiltInEager(_) => {
3118                    MacroKind::DeclarativeBuiltIn
3119                }
3120                MacroExpander::BuiltInAttr(_) => MacroKind::AttrBuiltIn,
3121                MacroExpander::BuiltInDerive(_) => MacroKind::DeriveBuiltIn,
3122            },
3123            MacroId::MacroRulesId(it) => match it.lookup(db).expander {
3124                MacroExpander::Declarative { .. } => MacroKind::Declarative,
3125                MacroExpander::BuiltIn(_) | MacroExpander::BuiltInEager(_) => {
3126                    MacroKind::DeclarativeBuiltIn
3127                }
3128                MacroExpander::BuiltInAttr(_) => MacroKind::AttrBuiltIn,
3129                MacroExpander::BuiltInDerive(_) => MacroKind::DeriveBuiltIn,
3130            },
3131            MacroId::ProcMacroId(it) => match it.lookup(db).kind {
3132                ProcMacroKind::CustomDerive => MacroKind::Derive,
3133                ProcMacroKind::Bang => MacroKind::ProcMacro,
3134                ProcMacroKind::Attr => MacroKind::Attr,
3135            },
3136        }
3137    }
3138
3139    pub fn is_fn_like(&self, db: &dyn HirDatabase) -> bool {
3140        matches!(
3141            self.kind(db),
3142            MacroKind::Declarative | MacroKind::DeclarativeBuiltIn | MacroKind::ProcMacro
3143        )
3144    }
3145
3146    pub fn is_builtin_derive(&self, db: &dyn HirDatabase) -> bool {
3147        match self.id {
3148            MacroId::Macro2Id(it) => {
3149                matches!(it.lookup(db).expander, MacroExpander::BuiltInDerive(_))
3150            }
3151            MacroId::MacroRulesId(it) => {
3152                matches!(it.lookup(db).expander, MacroExpander::BuiltInDerive(_))
3153            }
3154            MacroId::ProcMacroId(_) => false,
3155        }
3156    }
3157
3158    pub fn is_env_or_option_env(&self, db: &dyn HirDatabase) -> bool {
3159        match self.id {
3160            MacroId::Macro2Id(it) => {
3161                matches!(it.lookup(db).expander, MacroExpander::BuiltInEager(eager) if eager.is_env_or_option_env())
3162            }
3163            MacroId::MacroRulesId(it) => {
3164                matches!(it.lookup(db).expander, MacroExpander::BuiltInEager(eager) if eager.is_env_or_option_env())
3165            }
3166            MacroId::ProcMacroId(_) => false,
3167        }
3168    }
3169
3170    /// Is this `asm!()`, or a variant of it (e.g. `global_asm!()`)?
3171    pub fn is_asm_like(&self, db: &dyn HirDatabase) -> bool {
3172        match self.id {
3173            MacroId::Macro2Id(it) => {
3174                matches!(it.lookup(db).expander, MacroExpander::BuiltIn(m) if m.is_asm())
3175            }
3176            MacroId::MacroRulesId(it) => {
3177                matches!(it.lookup(db).expander, MacroExpander::BuiltIn(m) if m.is_asm())
3178            }
3179            MacroId::ProcMacroId(_) => false,
3180        }
3181    }
3182
3183    pub fn is_attr(&self, db: &dyn HirDatabase) -> bool {
3184        matches!(self.kind(db), MacroKind::Attr | MacroKind::AttrBuiltIn)
3185    }
3186
3187    pub fn is_derive(&self, db: &dyn HirDatabase) -> bool {
3188        matches!(self.kind(db), MacroKind::Derive | MacroKind::DeriveBuiltIn)
3189    }
3190}
3191
3192impl HasVisibility for Macro {
3193    fn visibility(&self, db: &dyn HirDatabase) -> Visibility {
3194        match self.id {
3195            MacroId::Macro2Id(id) => {
3196                let loc = id.lookup(db);
3197                let source = loc.source(db);
3198                visibility_from_ast(db, id, source.map(|src| src.visibility()))
3199            }
3200            MacroId::MacroRulesId(_) => Visibility::Public,
3201            MacroId::ProcMacroId(_) => Visibility::Public,
3202        }
3203    }
3204}
3205
3206#[derive(Clone, Copy, PartialEq, Eq, Debug, Hash)]
3207pub enum ItemInNs {
3208    Types(ModuleDef),
3209    Values(ModuleDef),
3210    Macros(Macro),
3211}
3212
3213impl From<Macro> for ItemInNs {
3214    fn from(it: Macro) -> Self {
3215        Self::Macros(it)
3216    }
3217}
3218
3219impl From<ModuleDef> for ItemInNs {
3220    fn from(module_def: ModuleDef) -> Self {
3221        match module_def {
3222            ModuleDef::Static(_) | ModuleDef::Const(_) | ModuleDef::Function(_) => {
3223                ItemInNs::Values(module_def)
3224            }
3225            ModuleDef::Macro(it) => ItemInNs::Macros(it),
3226            _ => ItemInNs::Types(module_def),
3227        }
3228    }
3229}
3230
3231impl ItemInNs {
3232    pub fn into_module_def(self) -> ModuleDef {
3233        match self {
3234            ItemInNs::Types(id) | ItemInNs::Values(id) => id,
3235            ItemInNs::Macros(id) => ModuleDef::Macro(id),
3236        }
3237    }
3238
3239    /// Returns the crate defining this item (or `None` if `self` is built-in).
3240    pub fn krate(&self, db: &dyn HirDatabase) -> Option<Crate> {
3241        match self {
3242            ItemInNs::Types(did) | ItemInNs::Values(did) => did.module(db).map(|m| m.krate(db)),
3243            ItemInNs::Macros(id) => Some(id.module(db).krate(db)),
3244        }
3245    }
3246
3247    pub fn attrs(&self, db: &dyn HirDatabase) -> Option<AttrsWithOwner> {
3248        match self {
3249            ItemInNs::Types(it) | ItemInNs::Values(it) => it.attrs(db),
3250            ItemInNs::Macros(it) => Some(it.attrs(db)),
3251        }
3252    }
3253}
3254
3255/// Invariant: `inner.as_extern_assoc_item(db).is_some()`
3256/// We do not actively enforce this invariant.
3257#[derive(Debug, Copy, Clone, PartialEq, Eq, Hash)]
3258pub enum ExternAssocItem {
3259    Function(Function),
3260    Static(Static),
3261    TypeAlias(TypeAlias),
3262}
3263
3264pub trait AsExternAssocItem {
3265    fn as_extern_assoc_item(self, db: &dyn HirDatabase) -> Option<ExternAssocItem>;
3266}
3267
3268impl AsExternAssocItem for Function {
3269    fn as_extern_assoc_item(self, db: &dyn HirDatabase) -> Option<ExternAssocItem> {
3270        as_extern_assoc_item(db, ExternAssocItem::Function, self.id)
3271    }
3272}
3273
3274impl AsExternAssocItem for Static {
3275    fn as_extern_assoc_item(self, db: &dyn HirDatabase) -> Option<ExternAssocItem> {
3276        as_extern_assoc_item(db, ExternAssocItem::Static, self.id)
3277    }
3278}
3279
3280impl AsExternAssocItem for TypeAlias {
3281    fn as_extern_assoc_item(self, db: &dyn HirDatabase) -> Option<ExternAssocItem> {
3282        as_extern_assoc_item(db, ExternAssocItem::TypeAlias, self.id)
3283    }
3284}
3285
3286/// Invariant: `inner.as_assoc_item(db).is_some()`
3287/// We do not actively enforce this invariant.
3288#[derive(Debug, Copy, Clone, PartialEq, Eq, Hash)]
3289pub enum AssocItem {
3290    Function(Function),
3291    Const(Const),
3292    TypeAlias(TypeAlias),
3293}
3294
3295impl From<method_resolution::CandidateId> for AssocItem {
3296    fn from(value: method_resolution::CandidateId) -> Self {
3297        match value {
3298            method_resolution::CandidateId::FunctionId(id) => AssocItem::Function(Function { id }),
3299            method_resolution::CandidateId::ConstId(id) => AssocItem::Const(Const { id }),
3300        }
3301    }
3302}
3303
3304#[derive(Debug, Clone)]
3305pub enum AssocItemContainer {
3306    Trait(Trait),
3307    Impl(Impl),
3308}
3309
3310pub trait AsAssocItem {
3311    fn as_assoc_item(self, db: &dyn HirDatabase) -> Option<AssocItem>;
3312}
3313
3314impl AsAssocItem for Function {
3315    fn as_assoc_item(self, db: &dyn HirDatabase) -> Option<AssocItem> {
3316        as_assoc_item(db, AssocItem::Function, self.id)
3317    }
3318}
3319
3320impl AsAssocItem for Const {
3321    fn as_assoc_item(self, db: &dyn HirDatabase) -> Option<AssocItem> {
3322        as_assoc_item(db, AssocItem::Const, self.id)
3323    }
3324}
3325
3326impl AsAssocItem for TypeAlias {
3327    fn as_assoc_item(self, db: &dyn HirDatabase) -> Option<AssocItem> {
3328        as_assoc_item(db, AssocItem::TypeAlias, self.id)
3329    }
3330}
3331
3332impl AsAssocItem for ModuleDef {
3333    fn as_assoc_item(self, db: &dyn HirDatabase) -> Option<AssocItem> {
3334        match self {
3335            ModuleDef::Function(it) => it.as_assoc_item(db),
3336            ModuleDef::Const(it) => it.as_assoc_item(db),
3337            ModuleDef::TypeAlias(it) => it.as_assoc_item(db),
3338            _ => None,
3339        }
3340    }
3341}
3342
3343impl AsAssocItem for DefWithBody {
3344    fn as_assoc_item(self, db: &dyn HirDatabase) -> Option<AssocItem> {
3345        match self {
3346            DefWithBody::Function(it) => it.as_assoc_item(db),
3347            DefWithBody::Const(it) => it.as_assoc_item(db),
3348            DefWithBody::Static(_) | DefWithBody::Variant(_) => None,
3349        }
3350    }
3351}
3352
3353fn as_assoc_item<'db, ID, DEF, LOC>(
3354    db: &(dyn HirDatabase + 'db),
3355    ctor: impl FnOnce(DEF) -> AssocItem,
3356    id: ID,
3357) -> Option<AssocItem>
3358where
3359    ID: Lookup<Database = dyn DefDatabase, Data = AssocItemLoc<LOC>>,
3360    DEF: From<ID>,
3361    LOC: AstIdNode,
3362{
3363    match id.lookup(db).container {
3364        ItemContainerId::TraitId(_) | ItemContainerId::ImplId(_) => Some(ctor(DEF::from(id))),
3365        ItemContainerId::ModuleId(_) | ItemContainerId::ExternBlockId(_) => None,
3366    }
3367}
3368
3369fn as_extern_assoc_item<'db, ID, DEF, LOC>(
3370    db: &(dyn HirDatabase + 'db),
3371    ctor: impl FnOnce(DEF) -> ExternAssocItem,
3372    id: ID,
3373) -> Option<ExternAssocItem>
3374where
3375    ID: Lookup<Database = dyn DefDatabase, Data = AssocItemLoc<LOC>>,
3376    DEF: From<ID>,
3377    LOC: AstIdNode,
3378{
3379    match id.lookup(db).container {
3380        ItemContainerId::ExternBlockId(_) => Some(ctor(DEF::from(id))),
3381        ItemContainerId::TraitId(_) | ItemContainerId::ImplId(_) | ItemContainerId::ModuleId(_) => {
3382            None
3383        }
3384    }
3385}
3386
3387impl ExternAssocItem {
3388    pub fn name(self, db: &dyn HirDatabase) -> Name {
3389        match self {
3390            Self::Function(it) => it.name(db),
3391            Self::Static(it) => it.name(db),
3392            Self::TypeAlias(it) => it.name(db),
3393        }
3394    }
3395
3396    pub fn module(self, db: &dyn HirDatabase) -> Module {
3397        match self {
3398            Self::Function(f) => f.module(db),
3399            Self::Static(c) => c.module(db),
3400            Self::TypeAlias(t) => t.module(db),
3401        }
3402    }
3403
3404    pub fn as_function(self) -> Option<Function> {
3405        match self {
3406            Self::Function(v) => Some(v),
3407            _ => None,
3408        }
3409    }
3410
3411    pub fn as_static(self) -> Option<Static> {
3412        match self {
3413            Self::Static(v) => Some(v),
3414            _ => None,
3415        }
3416    }
3417
3418    pub fn as_type_alias(self) -> Option<TypeAlias> {
3419        match self {
3420            Self::TypeAlias(v) => Some(v),
3421            _ => None,
3422        }
3423    }
3424}
3425
3426impl AssocItem {
3427    pub fn name(self, db: &dyn HirDatabase) -> Option<Name> {
3428        match self {
3429            AssocItem::Function(it) => Some(it.name(db)),
3430            AssocItem::Const(it) => it.name(db),
3431            AssocItem::TypeAlias(it) => Some(it.name(db)),
3432        }
3433    }
3434
3435    pub fn module(self, db: &dyn HirDatabase) -> Module {
3436        match self {
3437            AssocItem::Function(f) => f.module(db),
3438            AssocItem::Const(c) => c.module(db),
3439            AssocItem::TypeAlias(t) => t.module(db),
3440        }
3441    }
3442
3443    pub fn container(self, db: &dyn HirDatabase) -> AssocItemContainer {
3444        let container = match self {
3445            AssocItem::Function(it) => it.id.lookup(db).container,
3446            AssocItem::Const(it) => it.id.lookup(db).container,
3447            AssocItem::TypeAlias(it) => it.id.lookup(db).container,
3448        };
3449        match container {
3450            ItemContainerId::TraitId(id) => AssocItemContainer::Trait(id.into()),
3451            ItemContainerId::ImplId(id) => AssocItemContainer::Impl(id.into()),
3452            ItemContainerId::ModuleId(_) | ItemContainerId::ExternBlockId(_) => {
3453                panic!("invalid AssocItem")
3454            }
3455        }
3456    }
3457
3458    pub fn container_trait(self, db: &dyn HirDatabase) -> Option<Trait> {
3459        match self.container(db) {
3460            AssocItemContainer::Trait(t) => Some(t),
3461            _ => None,
3462        }
3463    }
3464
3465    pub fn implemented_trait(self, db: &dyn HirDatabase) -> Option<Trait> {
3466        match self.container(db) {
3467            AssocItemContainer::Impl(i) => i.trait_(db),
3468            _ => None,
3469        }
3470    }
3471
3472    pub fn container_or_implemented_trait(self, db: &dyn HirDatabase) -> Option<Trait> {
3473        match self.container(db) {
3474            AssocItemContainer::Trait(t) => Some(t),
3475            AssocItemContainer::Impl(i) => i.trait_(db),
3476        }
3477    }
3478
3479    pub fn implementing_ty(self, db: &dyn HirDatabase) -> Option<Type<'_>> {
3480        match self.container(db) {
3481            AssocItemContainer::Impl(i) => Some(i.self_ty(db)),
3482            _ => None,
3483        }
3484    }
3485
3486    pub fn as_function(self) -> Option<Function> {
3487        match self {
3488            Self::Function(v) => Some(v),
3489            _ => None,
3490        }
3491    }
3492
3493    pub fn as_const(self) -> Option<Const> {
3494        match self {
3495            Self::Const(v) => Some(v),
3496            _ => None,
3497        }
3498    }
3499
3500    pub fn as_type_alias(self) -> Option<TypeAlias> {
3501        match self {
3502            Self::TypeAlias(v) => Some(v),
3503            _ => None,
3504        }
3505    }
3506
3507    pub fn diagnostics<'db>(
3508        self,
3509        db: &'db dyn HirDatabase,
3510        acc: &mut Vec<AnyDiagnostic<'db>>,
3511        style_lints: bool,
3512    ) {
3513        match self {
3514            AssocItem::Function(func) => {
3515                GenericDef::Function(func).diagnostics(db, acc);
3516                DefWithBody::from(func).diagnostics(db, acc, style_lints);
3517            }
3518            AssocItem::Const(const_) => {
3519                GenericDef::Const(const_).diagnostics(db, acc);
3520                DefWithBody::from(const_).diagnostics(db, acc, style_lints);
3521            }
3522            AssocItem::TypeAlias(type_alias) => {
3523                GenericDef::TypeAlias(type_alias).diagnostics(db, acc);
3524                push_ty_diagnostics(
3525                    db,
3526                    acc,
3527                    db.type_for_type_alias_with_diagnostics(type_alias.id).1,
3528                    &db.type_alias_signature_with_source_map(type_alias.id).1,
3529                );
3530                for diag in hir_ty::diagnostics::incorrect_case(db, type_alias.id.into()) {
3531                    acc.push(diag.into());
3532                }
3533            }
3534        }
3535    }
3536}
3537
3538impl HasVisibility for AssocItem {
3539    fn visibility(&self, db: &dyn HirDatabase) -> Visibility {
3540        match self {
3541            AssocItem::Function(f) => f.visibility(db),
3542            AssocItem::Const(c) => c.visibility(db),
3543            AssocItem::TypeAlias(t) => t.visibility(db),
3544        }
3545    }
3546}
3547
3548impl From<AssocItem> for ModuleDef {
3549    fn from(assoc: AssocItem) -> Self {
3550        match assoc {
3551            AssocItem::Function(it) => ModuleDef::Function(it),
3552            AssocItem::Const(it) => ModuleDef::Const(it),
3553            AssocItem::TypeAlias(it) => ModuleDef::TypeAlias(it),
3554        }
3555    }
3556}
3557
3558#[derive(Clone, Copy, PartialEq, Eq, Debug, Hash)]
3559pub enum GenericDef {
3560    Function(Function),
3561    Adt(Adt),
3562    Trait(Trait),
3563    TypeAlias(TypeAlias),
3564    Impl(Impl),
3565    // consts can have type parameters from their parents (i.e. associated consts of traits)
3566    Const(Const),
3567    Static(Static),
3568}
3569impl_from!(
3570    Function,
3571    Adt(Struct, Enum, Union),
3572    Trait,
3573    TypeAlias,
3574    Impl,
3575    Const,
3576    Static
3577    for GenericDef
3578);
3579
3580impl GenericDef {
3581    pub fn params(self, db: &dyn HirDatabase) -> Vec<GenericParam> {
3582        let generics = db.generic_params(self.into());
3583        let ty_params = generics.iter_type_or_consts().map(|(local_id, _)| {
3584            let toc = TypeOrConstParam { id: TypeOrConstParamId { parent: self.into(), local_id } };
3585            match toc.split(db) {
3586                Either::Left(it) => GenericParam::ConstParam(it),
3587                Either::Right(it) => GenericParam::TypeParam(it),
3588            }
3589        });
3590        self.lifetime_params(db)
3591            .into_iter()
3592            .map(GenericParam::LifetimeParam)
3593            .chain(ty_params)
3594            .collect()
3595    }
3596
3597    pub fn lifetime_params(self, db: &dyn HirDatabase) -> Vec<LifetimeParam> {
3598        let generics = db.generic_params(self.into());
3599        generics
3600            .iter_lt()
3601            .map(|(local_id, _)| LifetimeParam {
3602                id: LifetimeParamId { parent: self.into(), local_id },
3603            })
3604            .collect()
3605    }
3606
3607    pub fn type_or_const_params(self, db: &dyn HirDatabase) -> Vec<TypeOrConstParam> {
3608        let generics = db.generic_params(self.into());
3609        generics
3610            .iter_type_or_consts()
3611            .map(|(local_id, _)| TypeOrConstParam {
3612                id: TypeOrConstParamId { parent: self.into(), local_id },
3613            })
3614            .collect()
3615    }
3616
3617    fn id(self) -> GenericDefId {
3618        match self {
3619            GenericDef::Function(it) => it.id.into(),
3620            GenericDef::Adt(it) => it.into(),
3621            GenericDef::Trait(it) => it.id.into(),
3622            GenericDef::TypeAlias(it) => it.id.into(),
3623            GenericDef::Impl(it) => it.id.into(),
3624            GenericDef::Const(it) => it.id.into(),
3625            GenericDef::Static(it) => it.id.into(),
3626        }
3627    }
3628
3629    pub fn diagnostics<'db>(self, db: &'db dyn HirDatabase, acc: &mut Vec<AnyDiagnostic<'db>>) {
3630        let def = self.id();
3631
3632        let generics = db.generic_params(def);
3633
3634        if generics.is_empty() && generics.has_no_predicates() {
3635            return;
3636        }
3637
3638        let source_map = match def {
3639            GenericDefId::AdtId(AdtId::EnumId(it)) => db.enum_signature_with_source_map(it).1,
3640            GenericDefId::AdtId(AdtId::StructId(it)) => db.struct_signature_with_source_map(it).1,
3641            GenericDefId::AdtId(AdtId::UnionId(it)) => db.union_signature_with_source_map(it).1,
3642            GenericDefId::ConstId(_) => return,
3643            GenericDefId::FunctionId(it) => db.function_signature_with_source_map(it).1,
3644            GenericDefId::ImplId(it) => db.impl_signature_with_source_map(it).1,
3645            GenericDefId::StaticId(_) => return,
3646            GenericDefId::TraitId(it) => db.trait_signature_with_source_map(it).1,
3647            GenericDefId::TypeAliasId(it) => db.type_alias_signature_with_source_map(it).1,
3648        };
3649
3650        expr_store_diagnostics(db, acc, &source_map);
3651        push_ty_diagnostics(db, acc, db.generic_defaults_with_diagnostics(def).1, &source_map);
3652        push_ty_diagnostics(
3653            db,
3654            acc,
3655            GenericPredicates::query_with_diagnostics(db, def).1.clone(),
3656            &source_map,
3657        );
3658        for (param_id, param) in generics.iter_type_or_consts() {
3659            if let TypeOrConstParamData::ConstParamData(_) = param {
3660                push_ty_diagnostics(
3661                    db,
3662                    acc,
3663                    db.const_param_ty_with_diagnostics(ConstParamId::from_unchecked(
3664                        TypeOrConstParamId { parent: def, local_id: param_id },
3665                    ))
3666                    .1,
3667                    &source_map,
3668                );
3669            }
3670        }
3671    }
3672
3673    /// Returns a string describing the kind of this type.
3674    #[inline]
3675    pub fn description(self) -> &'static str {
3676        match self {
3677            GenericDef::Function(_) => "function",
3678            GenericDef::Adt(Adt::Struct(_)) => "struct",
3679            GenericDef::Adt(Adt::Enum(_)) => "enum",
3680            GenericDef::Adt(Adt::Union(_)) => "union",
3681            GenericDef::Trait(_) => "trait",
3682            GenericDef::TypeAlias(_) => "type alias",
3683            GenericDef::Impl(_) => "impl",
3684            GenericDef::Const(_) => "constant",
3685            GenericDef::Static(_) => "static",
3686        }
3687    }
3688}
3689
3690// We cannot call this `Substitution` unfortunately...
3691#[derive(Debug)]
3692pub struct GenericSubstitution<'db> {
3693    def: GenericDefId,
3694    subst: GenericArgs<'db>,
3695    env: ParamEnvAndCrate<'db>,
3696}
3697
3698impl<'db> GenericSubstitution<'db> {
3699    fn new(def: GenericDefId, subst: GenericArgs<'db>, env: ParamEnvAndCrate<'db>) -> Self {
3700        Self { def, subst, env }
3701    }
3702
3703    pub fn types(&self, db: &'db dyn HirDatabase) -> Vec<(Symbol, Type<'db>)> {
3704        let container = match self.def {
3705            GenericDefId::ConstId(id) => Some(id.lookup(db).container),
3706            GenericDefId::FunctionId(id) => Some(id.lookup(db).container),
3707            GenericDefId::TypeAliasId(id) => Some(id.lookup(db).container),
3708            _ => None,
3709        };
3710        let container_type_params = container
3711            .and_then(|container| match container {
3712                ItemContainerId::ImplId(container) => Some(container.into()),
3713                ItemContainerId::TraitId(container) => Some(container.into()),
3714                _ => None,
3715            })
3716            .map(|container| {
3717                db.generic_params(container)
3718                    .iter_type_or_consts()
3719                    .filter_map(|param| match param.1 {
3720                        TypeOrConstParamData::TypeParamData(param) => Some(param.name.clone()),
3721                        TypeOrConstParamData::ConstParamData(_) => None,
3722                    })
3723                    .collect::<Vec<_>>()
3724            });
3725        let generics = db.generic_params(self.def);
3726        let type_params = generics.iter_type_or_consts().filter_map(|param| match param.1 {
3727            TypeOrConstParamData::TypeParamData(param) => Some(param.name.clone()),
3728            TypeOrConstParamData::ConstParamData(_) => None,
3729        });
3730        let parent_len = self.subst.len()
3731            - generics
3732                .iter_type_or_consts()
3733                .filter(|g| matches!(g.1, TypeOrConstParamData::TypeParamData(..)))
3734                .count();
3735        let container_params = self.subst.as_slice()[..parent_len]
3736            .iter()
3737            .filter_map(|param| param.ty())
3738            .zip(container_type_params.into_iter().flatten());
3739        let self_params = self.subst.as_slice()[parent_len..]
3740            .iter()
3741            .filter_map(|param| param.ty())
3742            .zip(type_params);
3743        container_params
3744            .chain(self_params)
3745            .filter_map(|(ty, name)| Some((name?.symbol().clone(), Type { ty, env: self.env })))
3746            .collect()
3747    }
3748}
3749
3750/// A single local definition.
3751#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)]
3752pub struct Local {
3753    pub(crate) parent: DefWithBodyId,
3754    pub(crate) binding_id: BindingId,
3755}
3756
3757pub struct LocalSource {
3758    pub local: Local,
3759    pub source: InFile<Either<ast::IdentPat, ast::SelfParam>>,
3760}
3761
3762impl LocalSource {
3763    pub fn as_ident_pat(&self) -> Option<&ast::IdentPat> {
3764        match &self.source.value {
3765            Either::Left(it) => Some(it),
3766            Either::Right(_) => None,
3767        }
3768    }
3769
3770    pub fn into_ident_pat(self) -> Option<ast::IdentPat> {
3771        match self.source.value {
3772            Either::Left(it) => Some(it),
3773            Either::Right(_) => None,
3774        }
3775    }
3776
3777    pub fn original_file(&self, db: &dyn HirDatabase) -> EditionedFileId {
3778        self.source.file_id.original_file(db)
3779    }
3780
3781    pub fn file(&self) -> HirFileId {
3782        self.source.file_id
3783    }
3784
3785    pub fn name(&self) -> Option<InFile<ast::Name>> {
3786        self.source.as_ref().map(|it| it.name()).transpose()
3787    }
3788
3789    pub fn syntax(&self) -> &SyntaxNode {
3790        self.source.value.syntax()
3791    }
3792
3793    pub fn syntax_ptr(self) -> InFile<SyntaxNodePtr> {
3794        self.source.map(|it| SyntaxNodePtr::new(it.syntax()))
3795    }
3796}
3797
3798impl Local {
3799    pub fn is_param(self, db: &dyn HirDatabase) -> bool {
3800        // FIXME: This parses!
3801        let src = self.primary_source(db);
3802        match src.source.value {
3803            Either::Left(pat) => pat
3804                .syntax()
3805                .ancestors()
3806                .map(|it| it.kind())
3807                .take_while(|&kind| ast::Pat::can_cast(kind) || ast::Param::can_cast(kind))
3808                .any(ast::Param::can_cast),
3809            Either::Right(_) => true,
3810        }
3811    }
3812
3813    pub fn as_self_param(self, db: &dyn HirDatabase) -> Option<SelfParam> {
3814        match self.parent {
3815            DefWithBodyId::FunctionId(func) if self.is_self(db) => Some(SelfParam { func }),
3816            _ => None,
3817        }
3818    }
3819
3820    pub fn name(self, db: &dyn HirDatabase) -> Name {
3821        let body = db.body(self.parent);
3822        body[self.binding_id].name.clone()
3823    }
3824
3825    pub fn is_self(self, db: &dyn HirDatabase) -> bool {
3826        self.name(db) == sym::self_
3827    }
3828
3829    pub fn is_mut(self, db: &dyn HirDatabase) -> bool {
3830        let body = db.body(self.parent);
3831        body[self.binding_id].mode == BindingAnnotation::Mutable
3832    }
3833
3834    pub fn is_ref(self, db: &dyn HirDatabase) -> bool {
3835        let body = db.body(self.parent);
3836        matches!(body[self.binding_id].mode, BindingAnnotation::Ref | BindingAnnotation::RefMut)
3837    }
3838
3839    pub fn parent(self, _db: &dyn HirDatabase) -> DefWithBody {
3840        self.parent.into()
3841    }
3842
3843    pub fn module(self, db: &dyn HirDatabase) -> Module {
3844        self.parent(db).module(db)
3845    }
3846
3847    pub fn ty(self, db: &dyn HirDatabase) -> Type<'_> {
3848        let def = self.parent;
3849        let infer = InferenceResult::for_body(db, def);
3850        let ty = infer[self.binding_id];
3851        Type::new(db, def, ty)
3852    }
3853
3854    /// All definitions for this local. Example: `let (a$0, _) | (_, a$0) = it;`
3855    pub fn sources(self, db: &dyn HirDatabase) -> Vec<LocalSource> {
3856        let (body, source_map) = db.body_with_source_map(self.parent);
3857        match body.self_param.zip(source_map.self_param_syntax()) {
3858            Some((param, source)) if param == self.binding_id => {
3859                let root = source.file_syntax(db);
3860                vec![LocalSource {
3861                    local: self,
3862                    source: source.map(|ast| Either::Right(ast.to_node(&root))),
3863                }]
3864            }
3865            _ => source_map
3866                .patterns_for_binding(self.binding_id)
3867                .iter()
3868                .map(|&definition| {
3869                    let src = source_map.pat_syntax(definition).unwrap(); // Hmm...
3870                    let root = src.file_syntax(db);
3871                    LocalSource {
3872                        local: self,
3873                        source: src.map(|ast| match ast.to_node(&root) {
3874                            Either::Right(ast::Pat::IdentPat(it)) => Either::Left(it),
3875                            _ => unreachable!("local with non ident-pattern"),
3876                        }),
3877                    }
3878                })
3879                .collect(),
3880        }
3881    }
3882
3883    /// The leftmost definition for this local. Example: `let (a$0, _) | (_, a) = it;`
3884    pub fn primary_source(self, db: &dyn HirDatabase) -> LocalSource {
3885        let (body, source_map) = db.body_with_source_map(self.parent);
3886        match body.self_param.zip(source_map.self_param_syntax()) {
3887            Some((param, source)) if param == self.binding_id => {
3888                let root = source.file_syntax(db);
3889                LocalSource {
3890                    local: self,
3891                    source: source.map(|ast| Either::Right(ast.to_node(&root))),
3892                }
3893            }
3894            _ => source_map
3895                .patterns_for_binding(self.binding_id)
3896                .first()
3897                .map(|&definition| {
3898                    let src = source_map.pat_syntax(definition).unwrap(); // Hmm...
3899                    let root = src.file_syntax(db);
3900                    LocalSource {
3901                        local: self,
3902                        source: src.map(|ast| match ast.to_node(&root) {
3903                            Either::Right(ast::Pat::IdentPat(it)) => Either::Left(it),
3904                            _ => unreachable!("local with non ident-pattern"),
3905                        }),
3906                    }
3907                })
3908                .unwrap(),
3909        }
3910    }
3911}
3912
3913impl PartialOrd for Local {
3914    fn partial_cmp(&self, other: &Self) -> Option<std::cmp::Ordering> {
3915        Some(self.cmp(other))
3916    }
3917}
3918
3919impl Ord for Local {
3920    fn cmp(&self, other: &Self) -> std::cmp::Ordering {
3921        self.binding_id.cmp(&other.binding_id)
3922    }
3923}
3924
3925#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)]
3926pub struct DeriveHelper {
3927    pub(crate) derive: MacroId,
3928    pub(crate) idx: u32,
3929}
3930
3931impl DeriveHelper {
3932    pub fn derive(&self) -> Macro {
3933        Macro { id: self.derive }
3934    }
3935
3936    pub fn name(&self, db: &dyn HirDatabase) -> Name {
3937        AttrFlags::derive_info(db, self.derive)
3938            .and_then(|it| it.helpers.get(self.idx as usize))
3939            .map(|helper| Name::new_symbol_root(helper.clone()))
3940            .unwrap_or_else(Name::missing)
3941    }
3942}
3943
3944#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)]
3945pub struct BuiltinAttr {
3946    idx: u32,
3947}
3948
3949impl BuiltinAttr {
3950    fn builtin(name: &str) -> Option<Self> {
3951        hir_expand::inert_attr_macro::find_builtin_attr_idx(&Symbol::intern(name))
3952            .map(|idx| BuiltinAttr { idx: idx as u32 })
3953    }
3954
3955    pub fn name(&self) -> Name {
3956        Name::new_symbol_root(Symbol::intern(
3957            hir_expand::inert_attr_macro::INERT_ATTRIBUTES[self.idx as usize].name,
3958        ))
3959    }
3960
3961    pub fn template(&self) -> Option<AttributeTemplate> {
3962        Some(hir_expand::inert_attr_macro::INERT_ATTRIBUTES[self.idx as usize].template)
3963    }
3964}
3965
3966#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)]
3967pub struct ToolModule {
3968    krate: base_db::Crate,
3969    idx: u32,
3970}
3971
3972impl ToolModule {
3973    pub(crate) fn by_name(db: &dyn HirDatabase, krate: Crate, name: &str) -> Option<Self> {
3974        let krate = krate.id;
3975        let idx =
3976            crate_def_map(db, krate).registered_tools().iter().position(|it| it.as_str() == name)?
3977                as u32;
3978        Some(ToolModule { krate, idx })
3979    }
3980
3981    pub fn name(&self, db: &dyn HirDatabase) -> Name {
3982        Name::new_symbol_root(
3983            crate_def_map(db, self.krate).registered_tools()[self.idx as usize].clone(),
3984        )
3985    }
3986
3987    pub fn krate(&self) -> Crate {
3988        Crate { id: self.krate }
3989    }
3990}
3991
3992#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)]
3993pub struct Label {
3994    pub(crate) parent: DefWithBodyId,
3995    pub(crate) label_id: LabelId,
3996}
3997
3998impl Label {
3999    pub fn module(self, db: &dyn HirDatabase) -> Module {
4000        self.parent(db).module(db)
4001    }
4002
4003    pub fn parent(self, _db: &dyn HirDatabase) -> DefWithBody {
4004        self.parent.into()
4005    }
4006
4007    pub fn name(self, db: &dyn HirDatabase) -> Name {
4008        let body = db.body(self.parent);
4009        body[self.label_id].name.clone()
4010    }
4011}
4012
4013#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)]
4014pub enum GenericParam {
4015    TypeParam(TypeParam),
4016    ConstParam(ConstParam),
4017    LifetimeParam(LifetimeParam),
4018}
4019impl_from!(TypeParam, ConstParam, LifetimeParam for GenericParam);
4020
4021impl GenericParam {
4022    pub fn module(self, db: &dyn HirDatabase) -> Module {
4023        match self {
4024            GenericParam::TypeParam(it) => it.module(db),
4025            GenericParam::ConstParam(it) => it.module(db),
4026            GenericParam::LifetimeParam(it) => it.module(db),
4027        }
4028    }
4029
4030    pub fn name(self, db: &dyn HirDatabase) -> Name {
4031        match self {
4032            GenericParam::TypeParam(it) => it.name(db),
4033            GenericParam::ConstParam(it) => it.name(db),
4034            GenericParam::LifetimeParam(it) => it.name(db),
4035        }
4036    }
4037
4038    pub fn parent(self) -> GenericDef {
4039        match self {
4040            GenericParam::TypeParam(it) => it.id.parent().into(),
4041            GenericParam::ConstParam(it) => it.id.parent().into(),
4042            GenericParam::LifetimeParam(it) => it.id.parent.into(),
4043        }
4044    }
4045
4046    pub fn variance(self, db: &dyn HirDatabase) -> Option<Variance> {
4047        let parent = match self {
4048            GenericParam::TypeParam(it) => it.id.parent(),
4049            // const parameters are always invariant
4050            GenericParam::ConstParam(_) => return None,
4051            GenericParam::LifetimeParam(it) => it.id.parent,
4052        };
4053        let generics = hir_ty::generics::generics(db, parent);
4054        let index = match self {
4055            GenericParam::TypeParam(it) => generics.type_or_const_param_idx(it.id.into())?,
4056            GenericParam::ConstParam(_) => return None,
4057            GenericParam::LifetimeParam(it) => generics.lifetime_idx(it.id)?,
4058        };
4059        db.variances_of(parent).get(index).map(Into::into)
4060    }
4061}
4062
4063#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
4064pub enum Variance {
4065    Bivariant,
4066    Covariant,
4067    Contravariant,
4068    Invariant,
4069}
4070
4071impl From<rustc_type_ir::Variance> for Variance {
4072    #[inline]
4073    fn from(value: rustc_type_ir::Variance) -> Self {
4074        match value {
4075            rustc_type_ir::Variance::Covariant => Variance::Covariant,
4076            rustc_type_ir::Variance::Invariant => Variance::Invariant,
4077            rustc_type_ir::Variance::Contravariant => Variance::Contravariant,
4078            rustc_type_ir::Variance::Bivariant => Variance::Bivariant,
4079        }
4080    }
4081}
4082
4083impl fmt::Display for Variance {
4084    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
4085        let description = match self {
4086            Variance::Bivariant => "bivariant",
4087            Variance::Covariant => "covariant",
4088            Variance::Contravariant => "contravariant",
4089            Variance::Invariant => "invariant",
4090        };
4091        f.pad(description)
4092    }
4093}
4094
4095#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)]
4096pub struct TypeParam {
4097    pub(crate) id: TypeParamId,
4098}
4099
4100impl TypeParam {
4101    pub fn merge(self) -> TypeOrConstParam {
4102        TypeOrConstParam { id: self.id.into() }
4103    }
4104
4105    pub fn name(self, db: &dyn HirDatabase) -> Name {
4106        self.merge().name(db)
4107    }
4108
4109    pub fn parent(self, _db: &dyn HirDatabase) -> GenericDef {
4110        self.id.parent().into()
4111    }
4112
4113    pub fn module(self, db: &dyn HirDatabase) -> Module {
4114        self.id.parent().module(db).into()
4115    }
4116
4117    /// Is this type parameter implicitly introduced (eg. `Self` in a trait or an `impl Trait`
4118    /// argument)?
4119    pub fn is_implicit(self, db: &dyn HirDatabase) -> bool {
4120        let params = db.generic_params(self.id.parent());
4121        let data = &params[self.id.local_id()];
4122        match data.type_param().unwrap().provenance {
4123            TypeParamProvenance::TypeParamList => false,
4124            TypeParamProvenance::TraitSelf | TypeParamProvenance::ArgumentImplTrait => true,
4125        }
4126    }
4127
4128    pub fn ty(self, db: &dyn HirDatabase) -> Type<'_> {
4129        let resolver = self.id.parent().resolver(db);
4130        let interner = DbInterner::new_no_crate(db);
4131        let index = hir_ty::param_idx(db, self.id.into()).unwrap();
4132        let ty = Ty::new_param(interner, self.id, index as u32);
4133        Type::new_with_resolver_inner(db, &resolver, ty)
4134    }
4135
4136    /// FIXME: this only lists trait bounds from the item defining the type
4137    /// parameter, not additional bounds that might be added e.g. by a method if
4138    /// the parameter comes from an impl!
4139    pub fn trait_bounds(self, db: &dyn HirDatabase) -> Vec<Trait> {
4140        let self_ty = self.ty(db).ty;
4141        GenericPredicates::query_explicit(db, self.id.parent())
4142            .iter_identity_copied()
4143            .filter_map(|pred| match &pred.kind().skip_binder() {
4144                ClauseKind::Trait(trait_ref) if trait_ref.self_ty() == self_ty => {
4145                    Some(Trait::from(trait_ref.def_id().0))
4146                }
4147                _ => None,
4148            })
4149            .collect()
4150    }
4151
4152    pub fn default(self, db: &dyn HirDatabase) -> Option<Type<'_>> {
4153        let ty = generic_arg_from_param(db, self.id.into())?;
4154        let resolver = self.id.parent().resolver(db);
4155        match ty {
4156            GenericArg::Ty(it) if !it.is_ty_error() => {
4157                Some(Type::new_with_resolver_inner(db, &resolver, it))
4158            }
4159            _ => None,
4160        }
4161    }
4162
4163    pub fn is_unstable(self, db: &dyn HirDatabase) -> bool {
4164        self.attrs(db).is_unstable()
4165    }
4166}
4167
4168#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)]
4169pub struct LifetimeParam {
4170    pub(crate) id: LifetimeParamId,
4171}
4172
4173impl LifetimeParam {
4174    pub fn name(self, db: &dyn HirDatabase) -> Name {
4175        let params = db.generic_params(self.id.parent);
4176        params[self.id.local_id].name.clone()
4177    }
4178
4179    pub fn module(self, db: &dyn HirDatabase) -> Module {
4180        self.id.parent.module(db).into()
4181    }
4182
4183    pub fn parent(self, _db: &dyn HirDatabase) -> GenericDef {
4184        self.id.parent.into()
4185    }
4186}
4187
4188#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)]
4189pub struct ConstParam {
4190    pub(crate) id: ConstParamId,
4191}
4192
4193impl ConstParam {
4194    pub fn merge(self) -> TypeOrConstParam {
4195        TypeOrConstParam { id: self.id.into() }
4196    }
4197
4198    pub fn name(self, db: &dyn HirDatabase) -> Name {
4199        let params = db.generic_params(self.id.parent());
4200        match params[self.id.local_id()].name() {
4201            Some(it) => it.clone(),
4202            None => {
4203                never!();
4204                Name::missing()
4205            }
4206        }
4207    }
4208
4209    pub fn module(self, db: &dyn HirDatabase) -> Module {
4210        self.id.parent().module(db).into()
4211    }
4212
4213    pub fn parent(self, _db: &dyn HirDatabase) -> GenericDef {
4214        self.id.parent().into()
4215    }
4216
4217    pub fn ty(self, db: &dyn HirDatabase) -> Type<'_> {
4218        Type::new(db, self.id.parent(), db.const_param_ty_ns(self.id))
4219    }
4220
4221    pub fn default(
4222        self,
4223        db: &dyn HirDatabase,
4224        display_target: DisplayTarget,
4225    ) -> Option<ast::ConstArg> {
4226        let arg = generic_arg_from_param(db, self.id.into())?;
4227        known_const_to_ast(arg.konst()?, db, display_target)
4228    }
4229}
4230
4231fn generic_arg_from_param(db: &dyn HirDatabase, id: TypeOrConstParamId) -> Option<GenericArg<'_>> {
4232    let local_idx = hir_ty::param_idx(db, id)?;
4233    let defaults = db.generic_defaults(id.parent);
4234    let ty = defaults.get(local_idx)?;
4235    // FIXME: This shouldn't be `instantiate_identity()`, we shouldn't leak `TyKind::Param`s.
4236    Some(ty.instantiate_identity())
4237}
4238
4239#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)]
4240pub struct TypeOrConstParam {
4241    pub(crate) id: TypeOrConstParamId,
4242}
4243
4244impl TypeOrConstParam {
4245    pub fn name(self, db: &dyn HirDatabase) -> Name {
4246        let params = db.generic_params(self.id.parent);
4247        match params[self.id.local_id].name() {
4248            Some(n) => n.clone(),
4249            _ => Name::missing(),
4250        }
4251    }
4252
4253    pub fn module(self, db: &dyn HirDatabase) -> Module {
4254        self.id.parent.module(db).into()
4255    }
4256
4257    pub fn parent(self, _db: &dyn HirDatabase) -> GenericDef {
4258        self.id.parent.into()
4259    }
4260
4261    pub fn split(self, db: &dyn HirDatabase) -> Either<ConstParam, TypeParam> {
4262        let params = db.generic_params(self.id.parent);
4263        match &params[self.id.local_id] {
4264            TypeOrConstParamData::TypeParamData(_) => {
4265                Either::Right(TypeParam { id: TypeParamId::from_unchecked(self.id) })
4266            }
4267            TypeOrConstParamData::ConstParamData(_) => {
4268                Either::Left(ConstParam { id: ConstParamId::from_unchecked(self.id) })
4269            }
4270        }
4271    }
4272
4273    pub fn ty(self, db: &dyn HirDatabase) -> Type<'_> {
4274        match self.split(db) {
4275            Either::Left(it) => it.ty(db),
4276            Either::Right(it) => it.ty(db),
4277        }
4278    }
4279
4280    pub fn as_type_param(self, db: &dyn HirDatabase) -> Option<TypeParam> {
4281        let params = db.generic_params(self.id.parent);
4282        match &params[self.id.local_id] {
4283            TypeOrConstParamData::TypeParamData(_) => {
4284                Some(TypeParam { id: TypeParamId::from_unchecked(self.id) })
4285            }
4286            TypeOrConstParamData::ConstParamData(_) => None,
4287        }
4288    }
4289
4290    pub fn as_const_param(self, db: &dyn HirDatabase) -> Option<ConstParam> {
4291        let params = db.generic_params(self.id.parent);
4292        match &params[self.id.local_id] {
4293            TypeOrConstParamData::TypeParamData(_) => None,
4294            TypeOrConstParamData::ConstParamData(_) => {
4295                Some(ConstParam { id: ConstParamId::from_unchecked(self.id) })
4296            }
4297        }
4298    }
4299}
4300
4301#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
4302pub struct Impl {
4303    pub(crate) id: ImplId,
4304}
4305
4306impl Impl {
4307    pub fn all_in_crate(db: &dyn HirDatabase, krate: Crate) -> Vec<Impl> {
4308        let mut result = Vec::new();
4309        extend_with_def_map(db, crate_def_map(db, krate.id), &mut result);
4310        return result;
4311
4312        fn extend_with_def_map(db: &dyn HirDatabase, def_map: &DefMap, result: &mut Vec<Impl>) {
4313            for (_, module) in def_map.modules() {
4314                result.extend(module.scope.impls().map(Impl::from));
4315
4316                for unnamed_const in module.scope.unnamed_consts() {
4317                    for (_, block_def_map) in db.body(unnamed_const.into()).blocks(db) {
4318                        extend_with_def_map(db, block_def_map, result);
4319                    }
4320                }
4321            }
4322        }
4323    }
4324
4325    pub fn all_in_module(db: &dyn HirDatabase, module: Module) -> Vec<Impl> {
4326        module.id.def_map(db)[module.id].scope.impls().map(Into::into).collect()
4327    }
4328
4329    /// **Note:** This is an **approximation** that strives to give the *human-perceived notion* of an "impl for type",
4330    /// **not** answer the technical question "what are all impls applying to this type". In particular, it excludes
4331    /// blanket impls, and only does a shallow type constructor check. In fact, this should've probably been on `Adt`
4332    /// etc., and not on `Type`. If you would want to create a precise list of all impls applying to a type,
4333    /// you would need to include blanket impls, and try to prove to predicates for each candidate.
4334    pub fn all_for_type<'db>(db: &'db dyn HirDatabase, Type { ty, env }: Type<'db>) -> Vec<Impl> {
4335        let mut result = Vec::new();
4336        let interner = DbInterner::new_no_crate(db);
4337        let Some(simplified_ty) =
4338            fast_reject::simplify_type(interner, ty, fast_reject::TreatParams::AsRigid)
4339        else {
4340            return Vec::new();
4341        };
4342        let mut extend_with_impls =
4343            |impls: &[ImplId]| result.extend(impls.iter().copied().map(Impl::from));
4344        method_resolution::with_incoherent_inherent_impls(
4345            db,
4346            env.krate,
4347            &simplified_ty,
4348            &mut extend_with_impls,
4349        );
4350        if let Some(module) = method_resolution::simplified_type_module(db, &simplified_ty) {
4351            InherentImpls::for_each_crate_and_block(
4352                db,
4353                module.krate(db),
4354                module.block(db),
4355                &mut |impls| extend_with_impls(impls.for_self_ty(&simplified_ty)),
4356            );
4357            std::iter::successors(module.block(db), |block| block.loc(db).module.block(db))
4358                .filter_map(|block| TraitImpls::for_block(db, block).as_deref())
4359                .for_each(|impls| impls.for_self_ty(&simplified_ty, &mut extend_with_impls));
4360            for &krate in &**db.all_crates() {
4361                TraitImpls::for_crate(db, krate)
4362                    .for_self_ty(&simplified_ty, &mut extend_with_impls);
4363            }
4364        } else {
4365            for &krate in &**db.all_crates() {
4366                TraitImpls::for_crate(db, krate)
4367                    .for_self_ty(&simplified_ty, &mut extend_with_impls);
4368            }
4369        }
4370        result
4371    }
4372
4373    pub fn all_for_trait(db: &dyn HirDatabase, trait_: Trait) -> Vec<Impl> {
4374        let module = trait_.module(db).id;
4375        let mut all = Vec::new();
4376        let mut handle_impls = |impls: &TraitImpls| {
4377            impls.for_trait(trait_.id, |impls| all.extend(impls.iter().copied().map(Impl::from)));
4378        };
4379        for krate in module.krate(db).transitive_rev_deps(db) {
4380            handle_impls(TraitImpls::for_crate(db, krate));
4381        }
4382        if let Some(block) = module.block(db)
4383            && let Some(impls) = TraitImpls::for_block(db, block)
4384        {
4385            handle_impls(impls);
4386        }
4387        all
4388    }
4389
4390    pub fn trait_(self, db: &dyn HirDatabase) -> Option<Trait> {
4391        let trait_ref = db.impl_trait(self.id)?;
4392        let id = trait_ref.skip_binder().def_id;
4393        Some(Trait { id: id.0 })
4394    }
4395
4396    pub fn trait_ref(self, db: &dyn HirDatabase) -> Option<TraitRef<'_>> {
4397        let trait_ref = db.impl_trait(self.id)?.instantiate_identity();
4398        let resolver = self.id.resolver(db);
4399        Some(TraitRef::new_with_resolver(db, &resolver, trait_ref))
4400    }
4401
4402    pub fn self_ty(self, db: &dyn HirDatabase) -> Type<'_> {
4403        let resolver = self.id.resolver(db);
4404        // FIXME: This shouldn't be `instantiate_identity()`, we shouldn't leak `TyKind::Param`s.
4405        let ty = db.impl_self_ty(self.id).instantiate_identity();
4406        Type::new_with_resolver_inner(db, &resolver, ty)
4407    }
4408
4409    pub fn items(self, db: &dyn HirDatabase) -> Vec<AssocItem> {
4410        self.id.impl_items(db).items.iter().map(|&(_, it)| it.into()).collect()
4411    }
4412
4413    pub fn is_negative(self, db: &dyn HirDatabase) -> bool {
4414        db.impl_signature(self.id).flags.contains(ImplFlags::NEGATIVE)
4415    }
4416
4417    pub fn is_unsafe(self, db: &dyn HirDatabase) -> bool {
4418        db.impl_signature(self.id).flags.contains(ImplFlags::UNSAFE)
4419    }
4420
4421    pub fn module(self, db: &dyn HirDatabase) -> Module {
4422        self.id.lookup(db).container.into()
4423    }
4424
4425    pub fn as_builtin_derive_path(self, db: &dyn HirDatabase) -> Option<InMacroFile<ast::Path>> {
4426        let src = self.source(db)?;
4427
4428        let macro_file = src.file_id.macro_file()?;
4429        let loc = macro_file.lookup(db);
4430        let (derive_attr, derive_index) = match loc.kind {
4431            MacroCallKind::Derive { ast_id, derive_attr_index, derive_index, .. } => {
4432                let module_id = self.id.lookup(db).container;
4433                (
4434                    module_id.def_map(db)[module_id]
4435                        .scope
4436                        .derive_macro_invoc(ast_id, derive_attr_index)?,
4437                    derive_index,
4438                )
4439            }
4440            _ => return None,
4441        };
4442        let path = db
4443            .parse_macro_expansion(derive_attr)
4444            .value
4445            .0
4446            .syntax_node()
4447            .children()
4448            .nth(derive_index as usize)
4449            .and_then(<ast::Attr as AstNode>::cast)
4450            .and_then(|it| it.path())?;
4451        Some(InMacroFile { file_id: derive_attr, value: path })
4452    }
4453
4454    pub fn check_orphan_rules(self, db: &dyn HirDatabase) -> bool {
4455        check_orphan_rules(db, self.id)
4456    }
4457
4458    fn all_macro_calls(&self, db: &dyn HirDatabase) -> Box<[(AstId<ast::Item>, MacroCallId)]> {
4459        self.id.impl_items(db).macro_calls.to_vec().into_boxed_slice()
4460    }
4461}
4462
4463#[derive(Clone, PartialEq, Eq, Debug, Hash)]
4464pub struct TraitRef<'db> {
4465    env: ParamEnvAndCrate<'db>,
4466    trait_ref: hir_ty::next_solver::TraitRef<'db>,
4467}
4468
4469impl<'db> TraitRef<'db> {
4470    pub(crate) fn new_with_resolver(
4471        db: &'db dyn HirDatabase,
4472        resolver: &Resolver<'_>,
4473        trait_ref: hir_ty::next_solver::TraitRef<'db>,
4474    ) -> Self {
4475        let env = param_env_from_resolver(db, resolver);
4476        TraitRef { env, trait_ref }
4477    }
4478
4479    pub fn trait_(&self) -> Trait {
4480        Trait { id: self.trait_ref.def_id.0 }
4481    }
4482
4483    pub fn self_ty(&self) -> TypeNs<'_> {
4484        let ty = self.trait_ref.self_ty();
4485        TypeNs { env: self.env, ty }
4486    }
4487
4488    /// Returns `idx`-th argument of this trait reference if it is a type argument. Note that the
4489    /// first argument is the `Self` type.
4490    pub fn get_type_argument(&self, idx: usize) -> Option<TypeNs<'db>> {
4491        self.trait_ref
4492            .args
4493            .as_slice()
4494            .get(idx)
4495            .and_then(|arg| arg.ty())
4496            .map(|ty| TypeNs { env: self.env, ty })
4497    }
4498}
4499
4500#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)]
4501enum AnyClosureId {
4502    ClosureId(InternedClosureId),
4503    CoroutineClosureId(InternedCoroutineId),
4504}
4505
4506#[derive(Clone, Debug, PartialEq, Eq, Hash)]
4507pub struct Closure<'db> {
4508    id: AnyClosureId,
4509    subst: GenericArgs<'db>,
4510}
4511
4512impl<'db> Closure<'db> {
4513    fn as_ty(&self, db: &'db dyn HirDatabase) -> Ty<'db> {
4514        let interner = DbInterner::new_no_crate(db);
4515        match self.id {
4516            AnyClosureId::ClosureId(id) => Ty::new_closure(interner, id.into(), self.subst),
4517            AnyClosureId::CoroutineClosureId(id) => {
4518                Ty::new_coroutine_closure(interner, id.into(), self.subst)
4519            }
4520        }
4521    }
4522
4523    pub fn display_with_id(&self, db: &dyn HirDatabase, display_target: DisplayTarget) -> String {
4524        self.as_ty(db)
4525            .display(db, display_target)
4526            .with_closure_style(ClosureStyle::ClosureWithId)
4527            .to_string()
4528    }
4529
4530    pub fn display_with_impl(&self, db: &dyn HirDatabase, display_target: DisplayTarget) -> String {
4531        self.as_ty(db)
4532            .display(db, display_target)
4533            .with_closure_style(ClosureStyle::ImplFn)
4534            .to_string()
4535    }
4536
4537    pub fn captured_items(&self, db: &'db dyn HirDatabase) -> Vec<ClosureCapture<'db>> {
4538        let AnyClosureId::ClosureId(id) = self.id else {
4539            // FIXME: Infer coroutine closures' captures.
4540            return Vec::new();
4541        };
4542        let owner = db.lookup_intern_closure(id).0;
4543        let infer = InferenceResult::for_body(db, owner);
4544        let info = infer.closure_info(id);
4545        info.0
4546            .iter()
4547            .cloned()
4548            .map(|capture| ClosureCapture { owner, closure: id, capture })
4549            .collect()
4550    }
4551
4552    pub fn capture_types(&self, db: &'db dyn HirDatabase) -> Vec<Type<'db>> {
4553        let AnyClosureId::ClosureId(id) = self.id else {
4554            // FIXME: Infer coroutine closures' captures.
4555            return Vec::new();
4556        };
4557        let owner = db.lookup_intern_closure(id).0;
4558        let infer = InferenceResult::for_body(db, owner);
4559        let (captures, _) = infer.closure_info(id);
4560        let env = body_param_env_from_has_crate(db, owner);
4561        captures.iter().map(|capture| Type { env, ty: capture.ty(db, self.subst) }).collect()
4562    }
4563
4564    pub fn fn_trait(&self, db: &dyn HirDatabase) -> FnTrait {
4565        match self.id {
4566            AnyClosureId::ClosureId(id) => {
4567                let owner = db.lookup_intern_closure(id).0;
4568                let infer = InferenceResult::for_body(db, owner);
4569                let info = infer.closure_info(id);
4570                info.1.into()
4571            }
4572            AnyClosureId::CoroutineClosureId(_id) => {
4573                // FIXME: Infer kind for coroutine closures.
4574                match self.subst.as_coroutine_closure().kind() {
4575                    rustc_type_ir::ClosureKind::Fn => FnTrait::AsyncFn,
4576                    rustc_type_ir::ClosureKind::FnMut => FnTrait::AsyncFnMut,
4577                    rustc_type_ir::ClosureKind::FnOnce => FnTrait::AsyncFnOnce,
4578                }
4579            }
4580        }
4581    }
4582}
4583
4584#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
4585pub enum FnTrait {
4586    FnOnce,
4587    FnMut,
4588    Fn,
4589
4590    AsyncFnOnce,
4591    AsyncFnMut,
4592    AsyncFn,
4593}
4594
4595impl From<traits::FnTrait> for FnTrait {
4596    fn from(value: traits::FnTrait) -> Self {
4597        match value {
4598            traits::FnTrait::FnOnce => FnTrait::FnOnce,
4599            traits::FnTrait::FnMut => FnTrait::FnMut,
4600            traits::FnTrait::Fn => FnTrait::Fn,
4601            traits::FnTrait::AsyncFnOnce => FnTrait::AsyncFnOnce,
4602            traits::FnTrait::AsyncFnMut => FnTrait::AsyncFnMut,
4603            traits::FnTrait::AsyncFn => FnTrait::AsyncFn,
4604        }
4605    }
4606}
4607
4608impl fmt::Display for FnTrait {
4609    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
4610        match self {
4611            FnTrait::FnOnce => write!(f, "FnOnce"),
4612            FnTrait::FnMut => write!(f, "FnMut"),
4613            FnTrait::Fn => write!(f, "Fn"),
4614            FnTrait::AsyncFnOnce => write!(f, "AsyncFnOnce"),
4615            FnTrait::AsyncFnMut => write!(f, "AsyncFnMut"),
4616            FnTrait::AsyncFn => write!(f, "AsyncFn"),
4617        }
4618    }
4619}
4620
4621impl FnTrait {
4622    pub const fn function_name(&self) -> &'static str {
4623        match self {
4624            FnTrait::FnOnce => "call_once",
4625            FnTrait::FnMut => "call_mut",
4626            FnTrait::Fn => "call",
4627            FnTrait::AsyncFnOnce => "async_call_once",
4628            FnTrait::AsyncFnMut => "async_call_mut",
4629            FnTrait::AsyncFn => "async_call",
4630        }
4631    }
4632
4633    pub fn lang_item(self) -> LangItem {
4634        match self {
4635            FnTrait::FnOnce => LangItem::FnOnce,
4636            FnTrait::FnMut => LangItem::FnMut,
4637            FnTrait::Fn => LangItem::Fn,
4638            FnTrait::AsyncFnOnce => LangItem::AsyncFnOnce,
4639            FnTrait::AsyncFnMut => LangItem::AsyncFnMut,
4640            FnTrait::AsyncFn => LangItem::AsyncFn,
4641        }
4642    }
4643
4644    pub fn get_id(self, db: &dyn HirDatabase, krate: Crate) -> Option<Trait> {
4645        Trait::lang(db, krate, self.lang_item())
4646    }
4647}
4648
4649#[derive(Clone, Debug, PartialEq, Eq)]
4650pub struct ClosureCapture<'db> {
4651    owner: DefWithBodyId,
4652    closure: InternedClosureId,
4653    capture: hir_ty::CapturedItem<'db>,
4654}
4655
4656impl<'db> ClosureCapture<'db> {
4657    pub fn local(&self) -> Local {
4658        Local { parent: self.owner, binding_id: self.capture.local() }
4659    }
4660
4661    /// Returns whether this place has any field (aka. non-deref) projections.
4662    pub fn has_field_projections(&self) -> bool {
4663        self.capture.has_field_projections()
4664    }
4665
4666    pub fn usages(&self) -> CaptureUsages {
4667        CaptureUsages { parent: self.owner, spans: self.capture.spans() }
4668    }
4669
4670    pub fn kind(&self) -> CaptureKind {
4671        match self.capture.kind() {
4672            hir_ty::CaptureKind::ByRef(
4673                hir_ty::mir::BorrowKind::Shallow | hir_ty::mir::BorrowKind::Shared,
4674            ) => CaptureKind::SharedRef,
4675            hir_ty::CaptureKind::ByRef(hir_ty::mir::BorrowKind::Mut {
4676                kind: MutBorrowKind::ClosureCapture,
4677            }) => CaptureKind::UniqueSharedRef,
4678            hir_ty::CaptureKind::ByRef(hir_ty::mir::BorrowKind::Mut {
4679                kind: MutBorrowKind::Default | MutBorrowKind::TwoPhasedBorrow,
4680            }) => CaptureKind::MutableRef,
4681            hir_ty::CaptureKind::ByValue => CaptureKind::Move,
4682        }
4683    }
4684
4685    /// Converts the place to a name that can be inserted into source code.
4686    pub fn place_to_name(&self, db: &dyn HirDatabase) -> String {
4687        self.capture.place_to_name(self.owner, db)
4688    }
4689
4690    pub fn display_place_source_code(&self, db: &dyn HirDatabase) -> String {
4691        self.capture.display_place_source_code(self.owner, db)
4692    }
4693
4694    pub fn display_place(&self, db: &dyn HirDatabase) -> String {
4695        self.capture.display_place(self.owner, db)
4696    }
4697}
4698
4699#[derive(Clone, Copy, PartialEq, Eq)]
4700pub enum CaptureKind {
4701    SharedRef,
4702    UniqueSharedRef,
4703    MutableRef,
4704    Move,
4705}
4706
4707#[derive(Debug, Clone)]
4708pub struct CaptureUsages {
4709    parent: DefWithBodyId,
4710    spans: SmallVec<[mir::MirSpan; 3]>,
4711}
4712
4713impl CaptureUsages {
4714    pub fn sources(&self, db: &dyn HirDatabase) -> Vec<CaptureUsageSource> {
4715        let (body, source_map) = db.body_with_source_map(self.parent);
4716
4717        let mut result = Vec::with_capacity(self.spans.len());
4718        for &span in self.spans.iter() {
4719            let is_ref = span.is_ref_span(&body);
4720            match span {
4721                mir::MirSpan::ExprId(expr) => {
4722                    if let Ok(expr) = source_map.expr_syntax(expr) {
4723                        result.push(CaptureUsageSource { is_ref, source: expr })
4724                    }
4725                }
4726                mir::MirSpan::PatId(pat) => {
4727                    if let Ok(pat) = source_map.pat_syntax(pat) {
4728                        result.push(CaptureUsageSource { is_ref, source: pat });
4729                    }
4730                }
4731                mir::MirSpan::BindingId(binding) => result.extend(
4732                    source_map
4733                        .patterns_for_binding(binding)
4734                        .iter()
4735                        .filter_map(|&pat| source_map.pat_syntax(pat).ok())
4736                        .map(|pat| CaptureUsageSource { is_ref, source: pat }),
4737                ),
4738                mir::MirSpan::SelfParam | mir::MirSpan::Unknown => {
4739                    unreachable!("invalid capture usage span")
4740                }
4741            }
4742        }
4743        result
4744    }
4745}
4746
4747#[derive(Debug)]
4748pub struct CaptureUsageSource {
4749    is_ref: bool,
4750    source: InFile<AstPtr<Either<ast::Expr, ast::Pat>>>,
4751}
4752
4753impl CaptureUsageSource {
4754    pub fn source(&self) -> AstPtr<Either<ast::Expr, ast::Pat>> {
4755        self.source.value
4756    }
4757
4758    pub fn file_id(&self) -> HirFileId {
4759        self.source.file_id
4760    }
4761
4762    pub fn is_ref(&self) -> bool {
4763        self.is_ref
4764    }
4765}
4766
4767#[derive(Clone, PartialEq, Eq, Debug, Hash)]
4768pub struct Type<'db> {
4769    env: ParamEnvAndCrate<'db>,
4770    ty: Ty<'db>,
4771}
4772
4773impl<'db> Type<'db> {
4774    pub(crate) fn new_with_resolver(
4775        db: &'db dyn HirDatabase,
4776        resolver: &Resolver<'_>,
4777        ty: Ty<'db>,
4778    ) -> Self {
4779        Type::new_with_resolver_inner(db, resolver, ty)
4780    }
4781
4782    pub(crate) fn new_with_resolver_inner(
4783        db: &'db dyn HirDatabase,
4784        resolver: &Resolver<'_>,
4785        ty: Ty<'db>,
4786    ) -> Self {
4787        let environment = param_env_from_resolver(db, resolver);
4788        Type { env: environment, ty }
4789    }
4790
4791    pub(crate) fn new_for_crate(krate: base_db::Crate, ty: Ty<'db>) -> Self {
4792        Type { env: empty_param_env(krate), ty }
4793    }
4794
4795    fn new(db: &'db dyn HirDatabase, lexical_env: impl HasResolver, ty: Ty<'db>) -> Self {
4796        let resolver = lexical_env.resolver(db);
4797        let environment = param_env_from_resolver(db, &resolver);
4798        Type { env: environment, ty }
4799    }
4800
4801    fn from_def(db: &'db dyn HirDatabase, def: impl Into<TyDefId> + HasResolver) -> Self {
4802        let interner = DbInterner::new_no_crate(db);
4803        let ty = db.ty(def.into());
4804        let def = match def.into() {
4805            TyDefId::AdtId(it) => GenericDefId::AdtId(it),
4806            TyDefId::TypeAliasId(it) => GenericDefId::TypeAliasId(it),
4807            TyDefId::BuiltinType(_) => {
4808                return Type::new(db, def, ty.skip_binder());
4809            }
4810        };
4811        let args = GenericArgs::error_for_item(interner, def.into());
4812        Type::new(db, def, ty.instantiate(interner, args))
4813    }
4814
4815    // FIXME: We shouldn't leak `TyKind::Param`s.
4816    fn from_def_params(db: &'db dyn HirDatabase, def: impl Into<TyDefId> + HasResolver) -> Self {
4817        let ty = db.ty(def.into());
4818        Type::new(db, def, ty.instantiate_identity())
4819    }
4820
4821    fn from_value_def(
4822        db: &'db dyn HirDatabase,
4823        def: impl Into<ValueTyDefId> + HasResolver,
4824    ) -> Self {
4825        let interner = DbInterner::new_no_crate(db);
4826        let Some(ty) = db.value_ty(def.into()) else {
4827            return Type::new(db, def, Ty::new_error(interner, ErrorGuaranteed));
4828        };
4829        let def = match def.into() {
4830            ValueTyDefId::ConstId(it) => GenericDefId::ConstId(it),
4831            ValueTyDefId::FunctionId(it) => GenericDefId::FunctionId(it),
4832            ValueTyDefId::StructId(it) => GenericDefId::AdtId(AdtId::StructId(it)),
4833            ValueTyDefId::UnionId(it) => GenericDefId::AdtId(AdtId::UnionId(it)),
4834            ValueTyDefId::EnumVariantId(it) => {
4835                GenericDefId::AdtId(AdtId::EnumId(it.lookup(db).parent))
4836            }
4837            ValueTyDefId::StaticId(_) => {
4838                return Type::new(db, def, ty.skip_binder());
4839            }
4840        };
4841        let args = GenericArgs::error_for_item(interner, def.into());
4842        Type::new(db, def, ty.instantiate(interner, args))
4843    }
4844
4845    pub fn new_slice(ty: Self) -> Self {
4846        let interner = DbInterner::conjure();
4847        Type { env: ty.env, ty: Ty::new_slice(interner, ty.ty) }
4848    }
4849
4850    pub fn new_tuple(krate: base_db::Crate, tys: &[Self]) -> Self {
4851        let tys = tys.iter().map(|it| it.ty);
4852        let interner = DbInterner::conjure();
4853        Type { env: empty_param_env(krate), ty: Ty::new_tup_from_iter(interner, tys) }
4854    }
4855
4856    pub fn is_unit(&self) -> bool {
4857        self.ty.is_unit()
4858    }
4859
4860    pub fn is_bool(&self) -> bool {
4861        matches!(self.ty.kind(), TyKind::Bool)
4862    }
4863
4864    pub fn is_str(&self) -> bool {
4865        matches!(self.ty.kind(), TyKind::Str)
4866    }
4867
4868    pub fn is_never(&self) -> bool {
4869        matches!(self.ty.kind(), TyKind::Never)
4870    }
4871
4872    pub fn is_mutable_reference(&self) -> bool {
4873        matches!(self.ty.kind(), TyKind::Ref(.., hir_ty::next_solver::Mutability::Mut))
4874    }
4875
4876    pub fn is_reference(&self) -> bool {
4877        matches!(self.ty.kind(), TyKind::Ref(..))
4878    }
4879
4880    pub fn contains_reference(&self, db: &'db dyn HirDatabase) -> bool {
4881        let interner = DbInterner::new_no_crate(db);
4882        return self.ty.visit_with(&mut Visitor { interner }).is_break();
4883
4884        fn is_phantom_data(db: &dyn HirDatabase, adt_id: AdtId) -> bool {
4885            match adt_id {
4886                AdtId::StructId(s) => {
4887                    let flags = db.struct_signature(s).flags;
4888                    flags.contains(StructFlags::IS_PHANTOM_DATA)
4889                }
4890                AdtId::UnionId(_) | AdtId::EnumId(_) => false,
4891            }
4892        }
4893
4894        struct Visitor<'db> {
4895            interner: DbInterner<'db>,
4896        }
4897
4898        impl<'db> TypeVisitor<DbInterner<'db>> for Visitor<'db> {
4899            type Result = ControlFlow<()>;
4900
4901            fn visit_ty(&mut self, ty: Ty<'db>) -> Self::Result {
4902                match ty.kind() {
4903                    // Reference itself
4904                    TyKind::Ref(..) => ControlFlow::Break(()),
4905
4906                    // For non-phantom_data adts we check variants/fields as well as generic parameters
4907                    TyKind::Adt(adt_def, args)
4908                        if !is_phantom_data(self.interner.db(), adt_def.def_id().0) =>
4909                    {
4910                        let _variant_id_to_fields = |id: VariantId| {
4911                            let variant_data = &id.fields(self.interner.db());
4912                            if variant_data.fields().is_empty() {
4913                                vec![]
4914                            } else {
4915                                let field_types = self.interner.db().field_types(id);
4916                                variant_data
4917                                    .fields()
4918                                    .iter()
4919                                    .map(|(idx, _)| {
4920                                        field_types[idx].instantiate(self.interner, args)
4921                                    })
4922                                    .filter(|it| !it.references_non_lt_error())
4923                                    .collect()
4924                            }
4925                        };
4926                        let variant_id_to_fields = |_: VariantId| vec![];
4927
4928                        let variants: Vec<Vec<Ty<'db>>> = match adt_def.def_id().0 {
4929                            AdtId::StructId(id) => {
4930                                vec![variant_id_to_fields(id.into())]
4931                            }
4932                            AdtId::EnumId(id) => id
4933                                .enum_variants(self.interner.db())
4934                                .variants
4935                                .iter()
4936                                .map(|&(variant_id, _, _)| variant_id_to_fields(variant_id.into()))
4937                                .collect(),
4938                            AdtId::UnionId(id) => {
4939                                vec![variant_id_to_fields(id.into())]
4940                            }
4941                        };
4942
4943                        variants
4944                            .into_iter()
4945                            .flat_map(|variant| variant.into_iter())
4946                            .try_for_each(|ty| ty.visit_with(self))?;
4947                        args.visit_with(self)
4948                    }
4949                    // And for `PhantomData<T>`, we check `T`.
4950                    _ => ty.super_visit_with(self),
4951                }
4952            }
4953        }
4954    }
4955
4956    pub fn as_reference(&self) -> Option<(Type<'db>, Mutability)> {
4957        let TyKind::Ref(_lt, ty, m) = self.ty.kind() else { return None };
4958        let m = Mutability::from_mutable(matches!(m, hir_ty::next_solver::Mutability::Mut));
4959        Some((self.derived(ty), m))
4960    }
4961
4962    pub fn add_reference(&self, mutability: Mutability) -> Self {
4963        let interner = DbInterner::conjure();
4964        let ty_mutability = match mutability {
4965            Mutability::Shared => hir_ty::next_solver::Mutability::Not,
4966            Mutability::Mut => hir_ty::next_solver::Mutability::Mut,
4967        };
4968        self.derived(Ty::new_ref(interner, Region::error(interner), self.ty, ty_mutability))
4969    }
4970
4971    pub fn is_slice(&self) -> bool {
4972        matches!(self.ty.kind(), TyKind::Slice(..))
4973    }
4974
4975    pub fn is_usize(&self) -> bool {
4976        matches!(self.ty.kind(), TyKind::Uint(rustc_type_ir::UintTy::Usize))
4977    }
4978
4979    pub fn is_float(&self) -> bool {
4980        matches!(self.ty.kind(), TyKind::Float(_))
4981    }
4982
4983    pub fn is_char(&self) -> bool {
4984        matches!(self.ty.kind(), TyKind::Char)
4985    }
4986
4987    pub fn is_int_or_uint(&self) -> bool {
4988        matches!(self.ty.kind(), TyKind::Int(_) | TyKind::Uint(_))
4989    }
4990
4991    pub fn is_scalar(&self) -> bool {
4992        matches!(
4993            self.ty.kind(),
4994            TyKind::Bool | TyKind::Char | TyKind::Int(_) | TyKind::Uint(_) | TyKind::Float(_)
4995        )
4996    }
4997
4998    pub fn is_tuple(&self) -> bool {
4999        matches!(self.ty.kind(), TyKind::Tuple(..))
5000    }
5001
5002    pub fn remove_ref(&self) -> Option<Type<'db>> {
5003        match self.ty.kind() {
5004            TyKind::Ref(_, ty, _) => Some(self.derived(ty)),
5005            _ => None,
5006        }
5007    }
5008
5009    pub fn as_slice(&self) -> Option<Type<'db>> {
5010        match self.ty.kind() {
5011            TyKind::Slice(ty) => Some(self.derived(ty)),
5012            _ => None,
5013        }
5014    }
5015
5016    pub fn strip_references(&self) -> Self {
5017        self.derived(self.ty.strip_references())
5018    }
5019
5020    // FIXME: This is the same as `remove_ref()`, remove one of these methods.
5021    pub fn strip_reference(&self) -> Self {
5022        self.derived(self.ty.strip_reference())
5023    }
5024
5025    pub fn is_unknown(&self) -> bool {
5026        self.ty.is_ty_error()
5027    }
5028
5029    /// Checks that particular type `ty` implements `std::future::IntoFuture` or
5030    /// `std::future::Future` and returns the `Output` associated type.
5031    /// This function is used in `.await` syntax completion.
5032    pub fn into_future_output(&self, db: &'db dyn HirDatabase) -> Option<Type<'db>> {
5033        let lang_items = hir_def::lang_item::lang_items(db, self.env.krate);
5034        let trait_ = lang_items
5035            .IntoFutureIntoFuture
5036            .and_then(|into_future_fn| {
5037                let assoc_item = as_assoc_item(db, AssocItem::Function, into_future_fn)?;
5038                let into_future_trait = assoc_item.container_or_implemented_trait(db)?;
5039                Some(into_future_trait.id)
5040            })
5041            .or(lang_items.Future)?;
5042
5043        if !traits::implements_trait_unique(self.ty, db, self.env, trait_) {
5044            return None;
5045        }
5046
5047        let output_assoc_type =
5048            trait_.trait_items(db).associated_type_by_name(&Name::new_symbol_root(sym::Output))?;
5049        self.normalize_trait_assoc_type(db, &[], output_assoc_type.into())
5050    }
5051
5052    /// This does **not** resolve `IntoFuture`, only `Future`.
5053    pub fn future_output(self, db: &'db dyn HirDatabase) -> Option<Type<'db>> {
5054        let lang_items = hir_def::lang_item::lang_items(db, self.env.krate);
5055        let future_output = lang_items.FutureOutput?;
5056        self.normalize_trait_assoc_type(db, &[], future_output.into())
5057    }
5058
5059    /// This does **not** resolve `IntoIterator`, only `Iterator`.
5060    pub fn iterator_item(self, db: &'db dyn HirDatabase) -> Option<Type<'db>> {
5061        let lang_items = hir_def::lang_item::lang_items(db, self.env.krate);
5062        let iterator_trait = lang_items.Iterator?;
5063        let iterator_item = iterator_trait
5064            .trait_items(db)
5065            .associated_type_by_name(&Name::new_symbol_root(sym::Item))?;
5066        self.normalize_trait_assoc_type(db, &[], iterator_item.into())
5067    }
5068
5069    pub fn impls_iterator(self, db: &'db dyn HirDatabase) -> bool {
5070        let lang_items = hir_def::lang_item::lang_items(db, self.env.krate);
5071        let Some(iterator_trait) = lang_items.Iterator else {
5072            return false;
5073        };
5074        traits::implements_trait_unique(self.ty, db, self.env, iterator_trait)
5075    }
5076
5077    /// Resolves the projection `<Self as IntoIterator>::IntoIter` and returns the resulting type
5078    pub fn into_iterator_iter(self, db: &'db dyn HirDatabase) -> Option<Type<'db>> {
5079        let lang_items = hir_def::lang_item::lang_items(db, self.env.krate);
5080        let trait_ = lang_items.IntoIterIntoIter.and_then(|into_iter_fn| {
5081            let assoc_item = as_assoc_item(db, AssocItem::Function, into_iter_fn)?;
5082            let into_iter_trait = assoc_item.container_or_implemented_trait(db)?;
5083            Some(into_iter_trait.id)
5084        })?;
5085
5086        if !traits::implements_trait_unique(self.ty, db, self.env, trait_) {
5087            return None;
5088        }
5089
5090        let into_iter_assoc_type = trait_
5091            .trait_items(db)
5092            .associated_type_by_name(&Name::new_symbol_root(sym::IntoIter))?;
5093        self.normalize_trait_assoc_type(db, &[], into_iter_assoc_type.into())
5094    }
5095
5096    /// Checks that particular type `ty` implements `std::ops::FnOnce`.
5097    ///
5098    /// This function can be used to check if a particular type is callable, since FnOnce is a
5099    /// supertrait of Fn and FnMut, so all callable types implements at least FnOnce.
5100    pub fn impls_fnonce(&self, db: &'db dyn HirDatabase) -> bool {
5101        let lang_items = hir_def::lang_item::lang_items(db, self.env.krate);
5102        let fnonce_trait = match lang_items.FnOnce {
5103            Some(it) => it,
5104            None => return false,
5105        };
5106
5107        traits::implements_trait_unique(self.ty, db, self.env, fnonce_trait)
5108    }
5109
5110    // FIXME: Find better API that also handles const generics
5111    pub fn impls_trait(&self, db: &'db dyn HirDatabase, trait_: Trait, args: &[Type<'db>]) -> bool {
5112        let interner = DbInterner::new_no_crate(db);
5113        let args = generic_args_from_tys(
5114            interner,
5115            trait_.id.into(),
5116            std::iter::once(self.ty).chain(args.iter().map(|ty| ty.ty)),
5117        );
5118        traits::implements_trait_unique_with_args(db, self.env, trait_.id, args)
5119    }
5120
5121    pub fn normalize_trait_assoc_type(
5122        &self,
5123        db: &'db dyn HirDatabase,
5124        args: &[Type<'db>],
5125        alias: TypeAlias,
5126    ) -> Option<Type<'db>> {
5127        let interner = DbInterner::new_with(db, self.env.krate);
5128        let args = generic_args_from_tys(
5129            interner,
5130            alias.id.into(),
5131            std::iter::once(self.ty).chain(args.iter().map(|ty| ty.ty)),
5132        );
5133        // FIXME: We don't handle GATs yet.
5134        let projection = Ty::new_alias(
5135            interner,
5136            AliasTyKind::Projection,
5137            AliasTy::new(interner, alias.id.into(), args),
5138        );
5139
5140        let infcx = interner.infer_ctxt().build(TypingMode::PostAnalysis);
5141        let ty = structurally_normalize_ty(&infcx, projection, self.env.param_env);
5142        if ty.is_ty_error() { None } else { Some(self.derived(ty)) }
5143    }
5144
5145    pub fn is_copy(&self, db: &'db dyn HirDatabase) -> bool {
5146        let lang_items = hir_def::lang_item::lang_items(db, self.env.krate);
5147        let Some(copy_trait) = lang_items.Copy else {
5148            return false;
5149        };
5150        self.impls_trait(db, copy_trait.into(), &[])
5151    }
5152
5153    pub fn as_callable(&self, db: &'db dyn HirDatabase) -> Option<Callable<'db>> {
5154        let interner = DbInterner::new_no_crate(db);
5155        let callee = match self.ty.kind() {
5156            TyKind::Closure(id, subst) => Callee::Closure(id.0, subst),
5157            TyKind::CoroutineClosure(id, subst) => Callee::CoroutineClosure(id.0, subst),
5158            TyKind::FnPtr(..) => Callee::FnPtr,
5159            TyKind::FnDef(id, _) => Callee::Def(id.0),
5160            // This will happen when it implements fn or fn mut, since we add an autoborrow adjustment
5161            TyKind::Ref(_, inner_ty, _) => return self.derived(inner_ty).as_callable(db),
5162            _ => {
5163                let (fn_trait, sig) = hir_ty::callable_sig_from_fn_trait(self.ty, self.env, db)?;
5164                return Some(Callable {
5165                    ty: self.clone(),
5166                    sig,
5167                    callee: Callee::FnImpl(fn_trait),
5168                    is_bound_method: false,
5169                });
5170            }
5171        };
5172
5173        let sig = self.ty.callable_sig(interner)?;
5174        Some(Callable { ty: self.clone(), sig, callee, is_bound_method: false })
5175    }
5176
5177    pub fn is_closure(&self) -> bool {
5178        matches!(self.ty.kind(), TyKind::Closure { .. })
5179    }
5180
5181    pub fn as_closure(&self) -> Option<Closure<'db>> {
5182        match self.ty.kind() {
5183            TyKind::Closure(id, subst) => {
5184                Some(Closure { id: AnyClosureId::ClosureId(id.0), subst })
5185            }
5186            TyKind::CoroutineClosure(id, subst) => {
5187                Some(Closure { id: AnyClosureId::CoroutineClosureId(id.0), subst })
5188            }
5189            _ => None,
5190        }
5191    }
5192
5193    pub fn is_fn(&self) -> bool {
5194        matches!(self.ty.kind(), TyKind::FnDef(..) | TyKind::FnPtr { .. })
5195    }
5196
5197    pub fn is_array(&self) -> bool {
5198        matches!(self.ty.kind(), TyKind::Array(..))
5199    }
5200
5201    pub fn is_packed(&self, db: &'db dyn HirDatabase) -> bool {
5202        let adt_id = match self.ty.kind() {
5203            TyKind::Adt(adt_def, ..) => adt_def.def_id().0,
5204            _ => return false,
5205        };
5206
5207        let adt = adt_id.into();
5208        match adt {
5209            Adt::Struct(s) => s.repr(db).unwrap_or_default().pack.is_some(),
5210            _ => false,
5211        }
5212    }
5213
5214    pub fn is_raw_ptr(&self) -> bool {
5215        matches!(self.ty.kind(), TyKind::RawPtr(..))
5216    }
5217
5218    pub fn remove_raw_ptr(&self) -> Option<Type<'db>> {
5219        if let TyKind::RawPtr(ty, _) = self.ty.kind() { Some(self.derived(ty)) } else { None }
5220    }
5221
5222    pub fn contains_unknown(&self) -> bool {
5223        self.ty.references_non_lt_error()
5224    }
5225
5226    pub fn fields(&self, db: &'db dyn HirDatabase) -> Vec<(Field, Self)> {
5227        let interner = DbInterner::new_no_crate(db);
5228        let (variant_id, substs) = match self.ty.kind() {
5229            TyKind::Adt(adt_def, substs) => {
5230                let id = match adt_def.def_id().0 {
5231                    AdtId::StructId(id) => id.into(),
5232                    AdtId::UnionId(id) => id.into(),
5233                    AdtId::EnumId(_) => return Vec::new(),
5234                };
5235                (id, substs)
5236            }
5237            _ => return Vec::new(),
5238        };
5239
5240        db.field_types(variant_id)
5241            .iter()
5242            .map(|(local_id, ty)| {
5243                let def = Field { parent: variant_id.into(), id: local_id };
5244                let ty = ty.instantiate(interner, substs);
5245                (def, self.derived(ty))
5246            })
5247            .collect()
5248    }
5249
5250    pub fn tuple_fields(&self, _db: &'db dyn HirDatabase) -> Vec<Self> {
5251        if let TyKind::Tuple(substs) = self.ty.kind() {
5252            substs.iter().map(|ty| self.derived(ty)).collect()
5253        } else {
5254            Vec::new()
5255        }
5256    }
5257
5258    pub fn as_array(&self, db: &'db dyn HirDatabase) -> Option<(Self, usize)> {
5259        if let TyKind::Array(ty, len) = self.ty.kind() {
5260            try_const_usize(db, len).map(|it| (self.derived(ty), it as usize))
5261        } else {
5262            None
5263        }
5264    }
5265
5266    pub fn fingerprint_for_trait_impl(&self) -> Option<SimplifiedType> {
5267        fast_reject::simplify_type(
5268            DbInterner::conjure(),
5269            self.ty,
5270            fast_reject::TreatParams::AsRigid,
5271        )
5272    }
5273
5274    /// Returns types that this type dereferences to (including this type itself). The returned
5275    /// iterator won't yield the same type more than once even if the deref chain contains a cycle.
5276    pub fn autoderef(
5277        &self,
5278        db: &'db dyn HirDatabase,
5279    ) -> impl Iterator<Item = Type<'db>> + use<'_, 'db> {
5280        self.autoderef_(db).map(move |ty| self.derived(ty))
5281    }
5282
5283    fn autoderef_(&self, db: &'db dyn HirDatabase) -> impl Iterator<Item = Ty<'db>> {
5284        let interner = DbInterner::new_no_crate(db);
5285        // There should be no inference vars in types passed here
5286        let canonical = hir_ty::replace_errors_with_variables(interner, &self.ty);
5287        autoderef(db, self.env, canonical)
5288    }
5289
5290    // This would be nicer if it just returned an iterator, but that runs into
5291    // lifetime problems, because we need to borrow temp `CrateImplDefs`.
5292    pub fn iterate_assoc_items<T>(
5293        &self,
5294        db: &'db dyn HirDatabase,
5295        mut callback: impl FnMut(AssocItem) -> Option<T>,
5296    ) -> Option<T> {
5297        let mut slot = None;
5298        self.iterate_assoc_items_dyn(db, &mut |assoc_item_id| {
5299            slot = callback(assoc_item_id.into());
5300            slot.is_some()
5301        });
5302        slot
5303    }
5304
5305    fn iterate_assoc_items_dyn(
5306        &self,
5307        db: &'db dyn HirDatabase,
5308        callback: &mut dyn FnMut(AssocItemId) -> bool,
5309    ) {
5310        let mut handle_impls = |impls: &[ImplId]| {
5311            for &impl_def in impls {
5312                for &(_, item) in impl_def.impl_items(db).items.iter() {
5313                    if callback(item) {
5314                        return;
5315                    }
5316                }
5317            }
5318        };
5319
5320        let interner = DbInterner::new_no_crate(db);
5321        let Some(simplified_type) =
5322            fast_reject::simplify_type(interner, self.ty, fast_reject::TreatParams::AsRigid)
5323        else {
5324            return;
5325        };
5326
5327        method_resolution::with_incoherent_inherent_impls(
5328            db,
5329            self.env.krate,
5330            &simplified_type,
5331            &mut handle_impls,
5332        );
5333
5334        if let Some(module) = method_resolution::simplified_type_module(db, &simplified_type) {
5335            InherentImpls::for_each_crate_and_block(
5336                db,
5337                module.krate(db),
5338                module.block(db),
5339                &mut |impls| {
5340                    handle_impls(impls.for_self_ty(&simplified_type));
5341                },
5342            );
5343        }
5344    }
5345
5346    /// Iterates its type arguments
5347    ///
5348    /// It iterates the actual type arguments when concrete types are used
5349    /// and otherwise the generic names.
5350    /// It does not include `const` arguments.
5351    ///
5352    /// For code, such as:
5353    /// ```text
5354    /// struct Foo<T, U>
5355    ///
5356    /// impl<U> Foo<String, U>
5357    /// ```
5358    ///
5359    /// It iterates:
5360    /// ```text
5361    /// - "String"
5362    /// - "U"
5363    /// ```
5364    pub fn type_arguments(&self) -> impl Iterator<Item = Type<'db>> + '_ {
5365        match self.ty.strip_references().kind() {
5366            TyKind::Adt(_, substs) => Either::Left(substs.types().map(move |ty| self.derived(ty))),
5367            TyKind::Tuple(substs) => {
5368                Either::Right(Either::Left(substs.iter().map(move |ty| self.derived(ty))))
5369            }
5370            _ => Either::Right(Either::Right(std::iter::empty())),
5371        }
5372    }
5373
5374    /// Iterates its type and const arguments
5375    ///
5376    /// It iterates the actual type and const arguments when concrete types
5377    /// are used and otherwise the generic names.
5378    ///
5379    /// For code, such as:
5380    /// ```text
5381    /// struct Foo<T, const U: usize, const X: usize>
5382    ///
5383    /// impl<U> Foo<String, U, 12>
5384    /// ```
5385    ///
5386    /// It iterates:
5387    /// ```text
5388    /// - "String"
5389    /// - "U"
5390    /// - "12"
5391    /// ```
5392    pub fn type_and_const_arguments<'a>(
5393        &'a self,
5394        db: &'a dyn HirDatabase,
5395        display_target: DisplayTarget,
5396    ) -> impl Iterator<Item = SmolStr> + 'a {
5397        self.ty
5398            .strip_references()
5399            .as_adt()
5400            .into_iter()
5401            .flat_map(|(_, substs)| substs.iter())
5402            .filter_map(move |arg| match arg {
5403                GenericArg::Ty(ty) => Some(format_smolstr!("{}", ty.display(db, display_target))),
5404                GenericArg::Const(const_) => {
5405                    Some(format_smolstr!("{}", const_.display(db, display_target)))
5406                }
5407                GenericArg::Lifetime(_) => None,
5408            })
5409    }
5410
5411    /// Combines lifetime indicators, type and constant parameters into a single `Iterator`
5412    pub fn generic_parameters<'a>(
5413        &'a self,
5414        db: &'a dyn HirDatabase,
5415        display_target: DisplayTarget,
5416    ) -> impl Iterator<Item = SmolStr> + 'a {
5417        // iterate the lifetime
5418        self.as_adt()
5419            .and_then(|a| {
5420                // Lifetimes do not need edition-specific handling as they cannot be escaped.
5421                a.lifetime(db).map(|lt| lt.name.display_no_db(Edition::Edition2015).to_smolstr())
5422            })
5423            .into_iter()
5424            // add the type and const parameters
5425            .chain(self.type_and_const_arguments(db, display_target))
5426    }
5427
5428    pub fn iterate_method_candidates_with_traits<T>(
5429        &self,
5430        db: &'db dyn HirDatabase,
5431        scope: &SemanticsScope<'_>,
5432        traits_in_scope: &FxHashSet<TraitId>,
5433        name: Option<&Name>,
5434        mut callback: impl FnMut(Function) -> Option<T>,
5435    ) -> Option<T> {
5436        let _p = tracing::info_span!("iterate_method_candidates_with_traits").entered();
5437        let mut slot = None;
5438        self.iterate_method_candidates_split_inherent(db, scope, traits_in_scope, name, |f| {
5439            match callback(f) {
5440                it @ Some(_) => {
5441                    slot = it;
5442                    ControlFlow::Break(())
5443                }
5444                None => ControlFlow::Continue(()),
5445            }
5446        });
5447        slot
5448    }
5449
5450    pub fn iterate_method_candidates<T>(
5451        &self,
5452        db: &'db dyn HirDatabase,
5453        scope: &SemanticsScope<'_>,
5454        name: Option<&Name>,
5455        callback: impl FnMut(Function) -> Option<T>,
5456    ) -> Option<T> {
5457        self.iterate_method_candidates_with_traits(
5458            db,
5459            scope,
5460            &scope.visible_traits().0,
5461            name,
5462            callback,
5463        )
5464    }
5465
5466    fn with_method_resolution<R>(
5467        &self,
5468        db: &'db dyn HirDatabase,
5469        resolver: &Resolver<'db>,
5470        traits_in_scope: &FxHashSet<TraitId>,
5471        f: impl FnOnce(&MethodResolutionContext<'_, 'db>) -> R,
5472    ) -> R {
5473        let module = resolver.module();
5474        let interner = DbInterner::new_with(db, module.krate(db));
5475        let infcx = interner.infer_ctxt().build(TypingMode::PostAnalysis);
5476        let unstable_features =
5477            MethodResolutionUnstableFeatures::from_def_map(resolver.top_level_def_map());
5478        let environment = param_env_from_resolver(db, resolver);
5479        let ctx = MethodResolutionContext {
5480            infcx: &infcx,
5481            resolver,
5482            param_env: environment.param_env,
5483            traits_in_scope,
5484            edition: resolver.krate().data(db).edition,
5485            unstable_features: &unstable_features,
5486        };
5487        f(&ctx)
5488    }
5489
5490    /// Allows you to treat inherent and non-inherent methods differently.
5491    ///
5492    /// Note that inherent methods may actually be trait methods! For example, in `dyn Trait`, the trait's methods
5493    /// are considered inherent methods.
5494    pub fn iterate_method_candidates_split_inherent(
5495        &self,
5496        db: &'db dyn HirDatabase,
5497        scope: &SemanticsScope<'_>,
5498        traits_in_scope: &FxHashSet<TraitId>,
5499        name: Option<&Name>,
5500        mut callback: impl MethodCandidateCallback,
5501    ) {
5502        let _p = tracing::info_span!(
5503            "iterate_method_candidates_split_inherent",
5504            traits_in_scope = traits_in_scope.len(),
5505            ?name,
5506        )
5507        .entered();
5508
5509        self.with_method_resolution(db, scope.resolver(), traits_in_scope, |ctx| {
5510            // There should be no inference vars in types passed here
5511            let canonical = hir_ty::replace_errors_with_variables(ctx.infcx.interner, &self.ty);
5512            let (self_ty, _) = ctx.infcx.instantiate_canonical(&canonical);
5513
5514            match name {
5515                Some(name) => {
5516                    match ctx.probe_for_name(
5517                        method_resolution::Mode::MethodCall,
5518                        name.clone(),
5519                        self_ty,
5520                    ) {
5521                        Ok(candidate)
5522                        | Err(method_resolution::MethodError::PrivateMatch(candidate)) => {
5523                            let method_resolution::CandidateId::FunctionId(id) = candidate.item
5524                            else {
5525                                unreachable!("`Mode::MethodCall` can only return functions");
5526                            };
5527                            let id = Function { id };
5528                            match candidate.kind {
5529                                method_resolution::PickKind::InherentImplPick(_)
5530                                | method_resolution::PickKind::ObjectPick(..)
5531                                | method_resolution::PickKind::WhereClausePick(..) => {
5532                                    // Candidates from where clauses and trait objects are considered inherent.
5533                                    _ = callback.on_inherent_method(id);
5534                                }
5535                                method_resolution::PickKind::TraitPick(..) => {
5536                                    _ = callback.on_trait_method(id);
5537                                }
5538                            }
5539                        }
5540                        Err(_) => {}
5541                    };
5542                }
5543                None => {
5544                    _ = ctx.probe_all(method_resolution::Mode::MethodCall, self_ty).try_for_each(
5545                        |candidate| {
5546                            let method_resolution::CandidateId::FunctionId(id) =
5547                                candidate.candidate.item
5548                            else {
5549                                unreachable!("`Mode::MethodCall` can only return functions");
5550                            };
5551                            let id = Function { id };
5552                            match candidate.candidate.kind {
5553                                method_resolution::CandidateKind::InherentImplCandidate {
5554                                    ..
5555                                }
5556                                | method_resolution::CandidateKind::ObjectCandidate(..)
5557                                | method_resolution::CandidateKind::WhereClauseCandidate(..) => {
5558                                    // Candidates from where clauses and trait objects are considered inherent.
5559                                    callback.on_inherent_method(id)
5560                                }
5561                                method_resolution::CandidateKind::TraitCandidate(..) => {
5562                                    callback.on_trait_method(id)
5563                                }
5564                            }
5565                        },
5566                    );
5567                }
5568            }
5569        })
5570    }
5571
5572    #[tracing::instrument(skip_all, fields(name = ?name))]
5573    pub fn iterate_path_candidates<T>(
5574        &self,
5575        db: &'db dyn HirDatabase,
5576        scope: &SemanticsScope<'_>,
5577        traits_in_scope: &FxHashSet<TraitId>,
5578        name: Option<&Name>,
5579        mut callback: impl FnMut(AssocItem) -> Option<T>,
5580    ) -> Option<T> {
5581        let _p = tracing::info_span!("iterate_path_candidates").entered();
5582        let mut slot = None;
5583
5584        self.iterate_path_candidates_split_inherent(db, scope, traits_in_scope, name, |item| {
5585            match callback(item) {
5586                it @ Some(_) => {
5587                    slot = it;
5588                    ControlFlow::Break(())
5589                }
5590                None => ControlFlow::Continue(()),
5591            }
5592        });
5593        slot
5594    }
5595
5596    /// Iterates over inherent methods.
5597    ///
5598    /// In some circumstances, inherent methods methods may actually be trait methods!
5599    /// For example, when `dyn Trait` is a receiver, _trait_'s methods would be considered
5600    /// to be inherent methods.
5601    #[tracing::instrument(skip_all, fields(name = ?name))]
5602    pub fn iterate_path_candidates_split_inherent(
5603        &self,
5604        db: &'db dyn HirDatabase,
5605        scope: &SemanticsScope<'_>,
5606        traits_in_scope: &FxHashSet<TraitId>,
5607        name: Option<&Name>,
5608        mut callback: impl PathCandidateCallback,
5609    ) {
5610        let _p = tracing::info_span!(
5611            "iterate_path_candidates_split_inherent",
5612            traits_in_scope = traits_in_scope.len(),
5613            ?name,
5614        )
5615        .entered();
5616
5617        self.with_method_resolution(db, scope.resolver(), traits_in_scope, |ctx| {
5618            // There should be no inference vars in types passed here
5619            let canonical = hir_ty::replace_errors_with_variables(ctx.infcx.interner, &self.ty);
5620            let (self_ty, _) = ctx.infcx.instantiate_canonical(&canonical);
5621
5622            match name {
5623                Some(name) => {
5624                    match ctx.probe_for_name(
5625                        method_resolution::Mode::MethodCall,
5626                        name.clone(),
5627                        self_ty,
5628                    ) {
5629                        Ok(candidate)
5630                        | Err(method_resolution::MethodError::PrivateMatch(candidate)) => {
5631                            let id = candidate.item.into();
5632                            match candidate.kind {
5633                                method_resolution::PickKind::InherentImplPick(_)
5634                                | method_resolution::PickKind::ObjectPick(..)
5635                                | method_resolution::PickKind::WhereClausePick(..) => {
5636                                    // Candidates from where clauses and trait objects are considered inherent.
5637                                    _ = callback.on_inherent_item(id);
5638                                }
5639                                method_resolution::PickKind::TraitPick(..) => {
5640                                    _ = callback.on_trait_item(id);
5641                                }
5642                            }
5643                        }
5644                        Err(_) => {}
5645                    };
5646                }
5647                None => {
5648                    _ = ctx.probe_all(method_resolution::Mode::Path, self_ty).try_for_each(
5649                        |candidate| {
5650                            let id = candidate.candidate.item.into();
5651                            match candidate.candidate.kind {
5652                                method_resolution::CandidateKind::InherentImplCandidate {
5653                                    ..
5654                                }
5655                                | method_resolution::CandidateKind::ObjectCandidate(..)
5656                                | method_resolution::CandidateKind::WhereClauseCandidate(..) => {
5657                                    // Candidates from where clauses and trait objects are considered inherent.
5658                                    callback.on_inherent_item(id)
5659                                }
5660                                method_resolution::CandidateKind::TraitCandidate(..) => {
5661                                    callback.on_trait_item(id)
5662                                }
5663                            }
5664                        },
5665                    );
5666                }
5667            }
5668        })
5669    }
5670
5671    pub fn as_adt(&self) -> Option<Adt> {
5672        let (adt, _subst) = self.ty.as_adt()?;
5673        Some(adt.into())
5674    }
5675
5676    pub fn as_builtin(&self) -> Option<BuiltinType> {
5677        self.ty.as_builtin().map(|inner| BuiltinType { inner })
5678    }
5679
5680    pub fn as_dyn_trait(&self) -> Option<Trait> {
5681        self.ty.dyn_trait().map(Into::into)
5682    }
5683
5684    /// If a type can be represented as `dyn Trait`, returns all traits accessible via this type,
5685    /// or an empty iterator otherwise.
5686    pub fn applicable_inherent_traits(
5687        &self,
5688        db: &'db dyn HirDatabase,
5689    ) -> impl Iterator<Item = Trait> {
5690        let _p = tracing::info_span!("applicable_inherent_traits").entered();
5691        self.autoderef_(db)
5692            .filter_map(|ty| ty.dyn_trait())
5693            .flat_map(move |dyn_trait_id| hir_ty::all_super_traits(db, dyn_trait_id))
5694            .map(Trait::from)
5695    }
5696
5697    pub fn env_traits(&self, db: &'db dyn HirDatabase) -> impl Iterator<Item = Trait> {
5698        let _p = tracing::info_span!("env_traits").entered();
5699        self.autoderef_(db)
5700            .filter(|ty| matches!(ty.kind(), TyKind::Param(_)))
5701            .flat_map(|ty| {
5702                self.env
5703                    .param_env
5704                    .clauses()
5705                    .iter()
5706                    .filter_map(move |pred| match pred.kind().skip_binder() {
5707                        ClauseKind::Trait(tr) if tr.self_ty() == ty => Some(tr.def_id().0),
5708                        _ => None,
5709                    })
5710                    .flat_map(|t| hir_ty::all_super_traits(db, t))
5711            })
5712            .map(Trait::from)
5713    }
5714
5715    pub fn as_impl_traits(&self, db: &'db dyn HirDatabase) -> Option<impl Iterator<Item = Trait>> {
5716        self.ty.impl_trait_bounds(db).map(|it| {
5717            it.into_iter().filter_map(|pred| match pred.kind().skip_binder() {
5718                ClauseKind::Trait(trait_ref) => Some(Trait::from(trait_ref.def_id().0)),
5719                _ => None,
5720            })
5721        })
5722    }
5723
5724    pub fn as_associated_type_parent_trait(&self, db: &'db dyn HirDatabase) -> Option<Trait> {
5725        let TyKind::Alias(AliasTyKind::Projection, alias) = self.ty.kind() else { return None };
5726        match alias.def_id.expect_type_alias().loc(db).container {
5727            ItemContainerId::TraitId(id) => Some(Trait { id }),
5728            _ => None,
5729        }
5730    }
5731
5732    fn derived(&self, ty: Ty<'db>) -> Self {
5733        Type { env: self.env, ty }
5734    }
5735
5736    /// Visits every type, including generic arguments, in this type. `callback` is called with type
5737    /// itself first, and then with its generic arguments.
5738    pub fn walk(&self, db: &'db dyn HirDatabase, callback: impl FnMut(Type<'db>)) {
5739        struct Visitor<'db, F> {
5740            db: &'db dyn HirDatabase,
5741            env: ParamEnvAndCrate<'db>,
5742            callback: F,
5743            visited: FxHashSet<Ty<'db>>,
5744        }
5745        impl<'db, F> TypeVisitor<DbInterner<'db>> for Visitor<'db, F>
5746        where
5747            F: FnMut(Type<'db>),
5748        {
5749            type Result = ();
5750
5751            fn visit_ty(&mut self, ty: Ty<'db>) -> Self::Result {
5752                if !self.visited.insert(ty) {
5753                    return;
5754                }
5755
5756                (self.callback)(Type { env: self.env, ty });
5757
5758                if let Some(bounds) = ty.impl_trait_bounds(self.db) {
5759                    bounds.visit_with(self);
5760                }
5761
5762                ty.super_visit_with(self);
5763            }
5764        }
5765
5766        let mut visitor = Visitor { db, env: self.env, callback, visited: FxHashSet::default() };
5767        self.ty.visit_with(&mut visitor);
5768    }
5769    /// Check if type unifies with another type.
5770    ///
5771    /// Note that we consider placeholder types to unify with everything.
5772    /// For example `Option<T>` and `Option<U>` unify although there is unresolved goal `T = U`.
5773    pub fn could_unify_with(&self, db: &'db dyn HirDatabase, other: &Type<'db>) -> bool {
5774        let interner = DbInterner::new_no_crate(db);
5775        let tys = hir_ty::replace_errors_with_variables(interner, &(self.ty, other.ty));
5776        hir_ty::could_unify(db, self.env, &tys)
5777    }
5778
5779    /// Check if type unifies with another type eagerly making sure there are no unresolved goals.
5780    ///
5781    /// This means that placeholder types are not considered to unify if there are any bounds set on
5782    /// them. For example `Option<T>` and `Option<U>` do not unify as we cannot show that `T = U`
5783    pub fn could_unify_with_deeply(&self, db: &'db dyn HirDatabase, other: &Type<'db>) -> bool {
5784        let interner = DbInterner::new_no_crate(db);
5785        let tys = hir_ty::replace_errors_with_variables(interner, &(self.ty, other.ty));
5786        hir_ty::could_unify_deeply(db, self.env, &tys)
5787    }
5788
5789    pub fn could_coerce_to(&self, db: &'db dyn HirDatabase, to: &Type<'db>) -> bool {
5790        let interner = DbInterner::new_no_crate(db);
5791        let tys = hir_ty::replace_errors_with_variables(interner, &(self.ty, to.ty));
5792        hir_ty::could_coerce(db, self.env, &tys)
5793    }
5794
5795    pub fn as_type_param(&self, _db: &'db dyn HirDatabase) -> Option<TypeParam> {
5796        match self.ty.kind() {
5797            TyKind::Param(param) => Some(TypeParam { id: param.id }),
5798            _ => None,
5799        }
5800    }
5801
5802    /// Returns unique `GenericParam`s contained in this type.
5803    pub fn generic_params(&self, db: &'db dyn HirDatabase) -> FxHashSet<GenericParam> {
5804        hir_ty::collect_params(&self.ty)
5805            .into_iter()
5806            .map(|id| TypeOrConstParam { id }.split(db).either_into())
5807            .collect()
5808    }
5809
5810    pub fn layout(&self, db: &'db dyn HirDatabase) -> Result<Layout, LayoutError> {
5811        db.layout_of_ty(self.ty, self.env)
5812            .map(|layout| Layout(layout, db.target_data_layout(self.env.krate).unwrap()))
5813    }
5814
5815    pub fn drop_glue(&self, db: &'db dyn HirDatabase) -> DropGlue {
5816        let interner = DbInterner::new_with(db, self.env.krate);
5817        let infcx = interner.infer_ctxt().build(TypingMode::PostAnalysis);
5818        hir_ty::drop::has_drop_glue(&infcx, self.ty, self.env.param_env)
5819    }
5820}
5821
5822#[derive(Clone, PartialEq, Eq, Debug, Hash)]
5823pub struct TypeNs<'db> {
5824    env: ParamEnvAndCrate<'db>,
5825    ty: Ty<'db>,
5826}
5827
5828impl<'db> TypeNs<'db> {
5829    fn new(db: &'db dyn HirDatabase, lexical_env: impl HasResolver, ty: Ty<'db>) -> Self {
5830        let resolver = lexical_env.resolver(db);
5831        let environment = param_env_from_resolver(db, &resolver);
5832        TypeNs { env: environment, ty }
5833    }
5834
5835    pub fn to_type(&self, _db: &'db dyn HirDatabase) -> Type<'db> {
5836        Type { env: self.env, ty: self.ty }
5837    }
5838
5839    // FIXME: Find better API that also handles const generics
5840    pub fn impls_trait(&self, infcx: InferCtxt<'db>, trait_: Trait, args: &[TypeNs<'db>]) -> bool {
5841        let args = GenericArgs::new_from_iter(
5842            infcx.interner,
5843            [self.ty].into_iter().chain(args.iter().map(|t| t.ty)).map(|t| t.into()),
5844        );
5845        let trait_ref = hir_ty::next_solver::TraitRef::new(infcx.interner, trait_.id.into(), args);
5846
5847        let pred_kind = rustc_type_ir::Binder::dummy(rustc_type_ir::PredicateKind::Clause(
5848            rustc_type_ir::ClauseKind::Trait(rustc_type_ir::TraitPredicate {
5849                trait_ref,
5850                polarity: rustc_type_ir::PredicatePolarity::Positive,
5851            }),
5852        ));
5853        let predicate = hir_ty::next_solver::Predicate::new(infcx.interner, pred_kind);
5854        let goal = hir_ty::next_solver::Goal::new(
5855            infcx.interner,
5856            hir_ty::next_solver::ParamEnv::empty(),
5857            predicate,
5858        );
5859        let res = hir_ty::traits::next_trait_solve_in_ctxt(&infcx, goal);
5860        res.map_or(false, |res| matches!(res.1, rustc_type_ir::solve::Certainty::Yes))
5861    }
5862
5863    pub fn is_bool(&self) -> bool {
5864        matches!(self.ty.kind(), rustc_type_ir::TyKind::Bool)
5865    }
5866}
5867
5868#[derive(Debug, PartialEq, Eq, Copy, Clone, Hash)]
5869pub struct InlineAsmOperand {
5870    owner: DefWithBodyId,
5871    expr: ExprId,
5872    index: usize,
5873}
5874
5875impl InlineAsmOperand {
5876    pub fn parent(self, _db: &dyn HirDatabase) -> DefWithBody {
5877        self.owner.into()
5878    }
5879
5880    pub fn name(&self, db: &dyn HirDatabase) -> Option<Name> {
5881        match &db.body(self.owner)[self.expr] {
5882            hir_def::hir::Expr::InlineAsm(e) => e.operands.get(self.index)?.0.clone(),
5883            _ => None,
5884        }
5885    }
5886}
5887
5888// FIXME: Document this
5889#[derive(Debug)]
5890pub struct Callable<'db> {
5891    ty: Type<'db>,
5892    sig: PolyFnSig<'db>,
5893    callee: Callee<'db>,
5894    /// Whether this is a method that was called with method call syntax.
5895    is_bound_method: bool,
5896}
5897
5898#[derive(Clone, PartialEq, Eq, Hash, Debug)]
5899enum Callee<'db> {
5900    Def(CallableDefId),
5901    Closure(InternedClosureId, GenericArgs<'db>),
5902    CoroutineClosure(InternedCoroutineId, GenericArgs<'db>),
5903    FnPtr,
5904    FnImpl(traits::FnTrait),
5905}
5906
5907pub enum CallableKind<'db> {
5908    Function(Function),
5909    TupleStruct(Struct),
5910    TupleEnumVariant(Variant),
5911    Closure(Closure<'db>),
5912    FnPtr,
5913    FnImpl(FnTrait),
5914}
5915
5916impl<'db> Callable<'db> {
5917    pub fn kind(&self) -> CallableKind<'db> {
5918        match self.callee {
5919            Callee::Def(CallableDefId::FunctionId(it)) => CallableKind::Function(it.into()),
5920            Callee::Def(CallableDefId::StructId(it)) => CallableKind::TupleStruct(it.into()),
5921            Callee::Def(CallableDefId::EnumVariantId(it)) => {
5922                CallableKind::TupleEnumVariant(it.into())
5923            }
5924            Callee::Closure(id, subst) => {
5925                CallableKind::Closure(Closure { id: AnyClosureId::ClosureId(id), subst })
5926            }
5927            Callee::CoroutineClosure(id, subst) => {
5928                CallableKind::Closure(Closure { id: AnyClosureId::CoroutineClosureId(id), subst })
5929            }
5930            Callee::FnPtr => CallableKind::FnPtr,
5931            Callee::FnImpl(fn_) => CallableKind::FnImpl(fn_.into()),
5932        }
5933    }
5934    pub fn receiver_param(&self, db: &'db dyn HirDatabase) -> Option<(SelfParam, Type<'db>)> {
5935        let func = match self.callee {
5936            Callee::Def(CallableDefId::FunctionId(it)) if self.is_bound_method => it,
5937            _ => return None,
5938        };
5939        let func = Function { id: func };
5940        Some((
5941            func.self_param(db)?,
5942            self.ty.derived(self.sig.skip_binder().inputs_and_output.inputs()[0]),
5943        ))
5944    }
5945    pub fn n_params(&self) -> usize {
5946        self.sig.skip_binder().inputs_and_output.inputs().len()
5947            - if self.is_bound_method { 1 } else { 0 }
5948    }
5949    pub fn params(&self) -> Vec<Param<'db>> {
5950        self.sig
5951            .skip_binder()
5952            .inputs_and_output
5953            .inputs()
5954            .iter()
5955            .enumerate()
5956            .skip(if self.is_bound_method { 1 } else { 0 })
5957            .map(|(idx, ty)| (idx, self.ty.derived(*ty)))
5958            .map(|(idx, ty)| Param { func: self.callee.clone(), idx, ty })
5959            .collect()
5960    }
5961    pub fn return_type(&self) -> Type<'db> {
5962        self.ty.derived(self.sig.skip_binder().output())
5963    }
5964    pub fn sig(&self) -> impl Eq {
5965        &self.sig
5966    }
5967
5968    pub fn ty(&self) -> &Type<'db> {
5969        &self.ty
5970    }
5971}
5972
5973#[derive(Clone, Debug, Eq, PartialEq)]
5974pub struct Layout(Arc<TyLayout>, Arc<TargetDataLayout>);
5975
5976impl Layout {
5977    pub fn size(&self) -> u64 {
5978        self.0.size.bytes()
5979    }
5980
5981    pub fn align(&self) -> u64 {
5982        self.0.align.bytes()
5983    }
5984
5985    pub fn niches(&self) -> Option<u128> {
5986        Some(self.0.largest_niche?.available(&*self.1))
5987    }
5988
5989    pub fn field_offset(&self, field: Field) -> Option<u64> {
5990        match self.0.fields {
5991            layout::FieldsShape::Primitive => None,
5992            layout::FieldsShape::Union(_) => Some(0),
5993            layout::FieldsShape::Array { stride, count } => {
5994                let i = u64::try_from(field.index()).ok()?;
5995                (i < count).then_some((stride * i).bytes())
5996            }
5997            layout::FieldsShape::Arbitrary { ref offsets, .. } => {
5998                Some(offsets.get(RustcFieldIdx(field.id))?.bytes())
5999            }
6000        }
6001    }
6002
6003    pub fn tuple_field_offset(&self, field: usize) -> Option<u64> {
6004        match self.0.fields {
6005            layout::FieldsShape::Primitive => None,
6006            layout::FieldsShape::Union(_) => Some(0),
6007            layout::FieldsShape::Array { stride, count } => {
6008                let i = u64::try_from(field).ok()?;
6009                (i < count).then_some((stride * i).bytes())
6010            }
6011            layout::FieldsShape::Arbitrary { ref offsets, .. } => {
6012                Some(offsets.get(RustcFieldIdx::new(field))?.bytes())
6013            }
6014        }
6015    }
6016
6017    pub fn tail_padding(&self, field_size: &mut impl FnMut(usize) -> Option<u64>) -> Option<u64> {
6018        match self.0.fields {
6019            layout::FieldsShape::Primitive => None,
6020            layout::FieldsShape::Union(_) => None,
6021            layout::FieldsShape::Array { stride, count } => count.checked_sub(1).and_then(|tail| {
6022                let tail_field_size = field_size(tail as usize)?;
6023                let offset = stride.bytes() * tail;
6024                self.0.size.bytes().checked_sub(offset)?.checked_sub(tail_field_size)
6025            }),
6026            layout::FieldsShape::Arbitrary { ref offsets, ref memory_index } => {
6027                let tail = memory_index.last_index()?;
6028                let tail_field_size = field_size(tail.0.into_raw().into_u32() as usize)?;
6029                let offset = offsets.get(tail)?.bytes();
6030                self.0.size.bytes().checked_sub(offset)?.checked_sub(tail_field_size)
6031            }
6032        }
6033    }
6034
6035    pub fn largest_padding(
6036        &self,
6037        field_size: &mut impl FnMut(usize) -> Option<u64>,
6038    ) -> Option<u64> {
6039        match self.0.fields {
6040            layout::FieldsShape::Primitive => None,
6041            layout::FieldsShape::Union(_) => None,
6042            layout::FieldsShape::Array { stride: _, count: 0 } => None,
6043            layout::FieldsShape::Array { stride, .. } => {
6044                let size = field_size(0)?;
6045                stride.bytes().checked_sub(size)
6046            }
6047            layout::FieldsShape::Arbitrary { ref offsets, ref memory_index } => {
6048                let mut reverse_index = vec![None; memory_index.len()];
6049                for (src, (mem, offset)) in memory_index.iter().zip(offsets.iter()).enumerate() {
6050                    reverse_index[*mem as usize] = Some((src, offset.bytes()));
6051                }
6052                if reverse_index.iter().any(|it| it.is_none()) {
6053                    stdx::never!();
6054                    return None;
6055                }
6056                reverse_index
6057                    .into_iter()
6058                    .flatten()
6059                    .chain(std::iter::once((0, self.0.size.bytes())))
6060                    .tuple_windows()
6061                    .filter_map(|((i, start), (_, end))| {
6062                        let size = field_size(i)?;
6063                        end.checked_sub(start)?.checked_sub(size)
6064                    })
6065                    .max()
6066            }
6067        }
6068    }
6069
6070    pub fn enum_tag_size(&self) -> Option<usize> {
6071        let tag_size =
6072            if let layout::Variants::Multiple { tag, tag_encoding, .. } = &self.0.variants {
6073                match tag_encoding {
6074                    TagEncoding::Direct => tag.size(&*self.1).bytes_usize(),
6075                    TagEncoding::Niche { .. } => 0,
6076                }
6077            } else {
6078                return None;
6079            };
6080        Some(tag_size)
6081    }
6082}
6083
6084#[derive(Copy, Clone, Debug, Eq, PartialEq)]
6085pub enum BindingMode {
6086    Move,
6087    Ref(Mutability),
6088}
6089
6090/// For IDE only
6091#[derive(Copy, Clone, Debug, PartialEq, Eq, Hash)]
6092pub enum ScopeDef {
6093    ModuleDef(ModuleDef),
6094    GenericParam(GenericParam),
6095    ImplSelfType(Impl),
6096    AdtSelfType(Adt),
6097    Local(Local),
6098    Label(Label),
6099    Unknown,
6100}
6101
6102impl ScopeDef {
6103    pub fn all_items(def: PerNs) -> ArrayVec<Self, 3> {
6104        let mut items = ArrayVec::new();
6105
6106        match (def.take_types(), def.take_values()) {
6107            (Some(m1), None) => items.push(ScopeDef::ModuleDef(m1.into())),
6108            (None, Some(m2)) => items.push(ScopeDef::ModuleDef(m2.into())),
6109            (Some(m1), Some(m2)) => {
6110                // Some items, like unit structs and enum variants, are
6111                // returned as both a type and a value. Here we want
6112                // to de-duplicate them.
6113                if m1 != m2 {
6114                    items.push(ScopeDef::ModuleDef(m1.into()));
6115                    items.push(ScopeDef::ModuleDef(m2.into()));
6116                } else {
6117                    items.push(ScopeDef::ModuleDef(m1.into()));
6118                }
6119            }
6120            (None, None) => {}
6121        };
6122
6123        if let Some(macro_def_id) = def.take_macros() {
6124            items.push(ScopeDef::ModuleDef(ModuleDef::Macro(macro_def_id.into())));
6125        }
6126
6127        if items.is_empty() {
6128            items.push(ScopeDef::Unknown);
6129        }
6130
6131        items
6132    }
6133
6134    pub fn attrs(&self, db: &dyn HirDatabase) -> Option<AttrsWithOwner> {
6135        match self {
6136            ScopeDef::ModuleDef(it) => it.attrs(db),
6137            ScopeDef::GenericParam(it) => Some(it.attrs(db)),
6138            ScopeDef::ImplSelfType(_)
6139            | ScopeDef::AdtSelfType(_)
6140            | ScopeDef::Local(_)
6141            | ScopeDef::Label(_)
6142            | ScopeDef::Unknown => None,
6143        }
6144    }
6145
6146    pub fn krate(&self, db: &dyn HirDatabase) -> Option<Crate> {
6147        match self {
6148            ScopeDef::ModuleDef(it) => it.module(db).map(|m| m.krate(db)),
6149            ScopeDef::GenericParam(it) => Some(it.module(db).krate(db)),
6150            ScopeDef::ImplSelfType(_) => None,
6151            ScopeDef::AdtSelfType(it) => Some(it.module(db).krate(db)),
6152            ScopeDef::Local(it) => Some(it.module(db).krate(db)),
6153            ScopeDef::Label(it) => Some(it.module(db).krate(db)),
6154            ScopeDef::Unknown => None,
6155        }
6156    }
6157}
6158
6159impl From<ItemInNs> for ScopeDef {
6160    fn from(item: ItemInNs) -> Self {
6161        match item {
6162            ItemInNs::Types(id) => ScopeDef::ModuleDef(id),
6163            ItemInNs::Values(id) => ScopeDef::ModuleDef(id),
6164            ItemInNs::Macros(id) => ScopeDef::ModuleDef(ModuleDef::Macro(id)),
6165        }
6166    }
6167}
6168
6169#[derive(Clone, Debug, PartialEq, Eq)]
6170pub struct Adjustment<'db> {
6171    pub source: Type<'db>,
6172    pub target: Type<'db>,
6173    pub kind: Adjust,
6174}
6175
6176#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)]
6177pub enum Adjust {
6178    /// Go from ! to any type.
6179    NeverToAny,
6180    /// Dereference once, producing a place.
6181    Deref(Option<OverloadedDeref>),
6182    /// Take the address and produce either a `&` or `*` pointer.
6183    Borrow(AutoBorrow),
6184    Pointer(PointerCast),
6185}
6186
6187#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)]
6188pub enum AutoBorrow {
6189    /// Converts from T to &T.
6190    Ref(Mutability),
6191    /// Converts from T to *T.
6192    RawPtr(Mutability),
6193}
6194
6195#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)]
6196pub struct OverloadedDeref(pub Mutability);
6197
6198pub trait HasVisibility {
6199    fn visibility(&self, db: &dyn HirDatabase) -> Visibility;
6200    fn is_visible_from(&self, db: &dyn HirDatabase, module: Module) -> bool {
6201        let vis = self.visibility(db);
6202        vis.is_visible_from(db, module.id)
6203    }
6204}
6205
6206/// Trait for obtaining the defining crate of an item.
6207pub trait HasCrate {
6208    fn krate(&self, db: &dyn HirDatabase) -> Crate;
6209}
6210
6211impl<T: hir_def::HasModule> HasCrate for T {
6212    fn krate(&self, db: &dyn HirDatabase) -> Crate {
6213        self.module(db).krate(db).into()
6214    }
6215}
6216
6217impl HasCrate for AssocItem {
6218    fn krate(&self, db: &dyn HirDatabase) -> Crate {
6219        self.module(db).krate(db)
6220    }
6221}
6222
6223impl HasCrate for Struct {
6224    fn krate(&self, db: &dyn HirDatabase) -> Crate {
6225        self.module(db).krate(db)
6226    }
6227}
6228
6229impl HasCrate for Union {
6230    fn krate(&self, db: &dyn HirDatabase) -> Crate {
6231        self.module(db).krate(db)
6232    }
6233}
6234
6235impl HasCrate for Enum {
6236    fn krate(&self, db: &dyn HirDatabase) -> Crate {
6237        self.module(db).krate(db)
6238    }
6239}
6240
6241impl HasCrate for Field {
6242    fn krate(&self, db: &dyn HirDatabase) -> Crate {
6243        self.parent_def(db).module(db).krate(db)
6244    }
6245}
6246
6247impl HasCrate for Variant {
6248    fn krate(&self, db: &dyn HirDatabase) -> Crate {
6249        self.module(db).krate(db)
6250    }
6251}
6252
6253impl HasCrate for Function {
6254    fn krate(&self, db: &dyn HirDatabase) -> Crate {
6255        self.module(db).krate(db)
6256    }
6257}
6258
6259impl HasCrate for Const {
6260    fn krate(&self, db: &dyn HirDatabase) -> Crate {
6261        self.module(db).krate(db)
6262    }
6263}
6264
6265impl HasCrate for TypeAlias {
6266    fn krate(&self, db: &dyn HirDatabase) -> Crate {
6267        self.module(db).krate(db)
6268    }
6269}
6270
6271impl HasCrate for Type<'_> {
6272    fn krate(&self, _db: &dyn HirDatabase) -> Crate {
6273        self.env.krate.into()
6274    }
6275}
6276
6277impl HasCrate for Macro {
6278    fn krate(&self, db: &dyn HirDatabase) -> Crate {
6279        self.module(db).krate(db)
6280    }
6281}
6282
6283impl HasCrate for Trait {
6284    fn krate(&self, db: &dyn HirDatabase) -> Crate {
6285        self.module(db).krate(db)
6286    }
6287}
6288
6289impl HasCrate for Static {
6290    fn krate(&self, db: &dyn HirDatabase) -> Crate {
6291        self.module(db).krate(db)
6292    }
6293}
6294
6295impl HasCrate for Adt {
6296    fn krate(&self, db: &dyn HirDatabase) -> Crate {
6297        self.module(db).krate(db)
6298    }
6299}
6300
6301impl HasCrate for Impl {
6302    fn krate(&self, db: &dyn HirDatabase) -> Crate {
6303        self.module(db).krate(db)
6304    }
6305}
6306
6307impl HasCrate for Module {
6308    fn krate(&self, db: &dyn HirDatabase) -> Crate {
6309        Module::krate(*self, db)
6310    }
6311}
6312
6313pub trait HasContainer {
6314    fn container(&self, db: &dyn HirDatabase) -> ItemContainer;
6315}
6316
6317impl HasContainer for ExternCrateDecl {
6318    fn container(&self, db: &dyn HirDatabase) -> ItemContainer {
6319        container_id_to_hir(self.id.lookup(db).container.into())
6320    }
6321}
6322
6323impl HasContainer for Module {
6324    fn container(&self, db: &dyn HirDatabase) -> ItemContainer {
6325        // FIXME: handle block expressions as modules (their parent is in a different DefMap)
6326        let def_map = self.id.def_map(db);
6327        match def_map[self.id].parent {
6328            Some(parent_id) => ItemContainer::Module(Module { id: parent_id }),
6329            None => ItemContainer::Crate(def_map.krate().into()),
6330        }
6331    }
6332}
6333
6334impl HasContainer for Function {
6335    fn container(&self, db: &dyn HirDatabase) -> ItemContainer {
6336        container_id_to_hir(self.id.lookup(db).container)
6337    }
6338}
6339
6340impl HasContainer for Struct {
6341    fn container(&self, db: &dyn HirDatabase) -> ItemContainer {
6342        ItemContainer::Module(Module { id: self.id.lookup(db).container })
6343    }
6344}
6345
6346impl HasContainer for Union {
6347    fn container(&self, db: &dyn HirDatabase) -> ItemContainer {
6348        ItemContainer::Module(Module { id: self.id.lookup(db).container })
6349    }
6350}
6351
6352impl HasContainer for Enum {
6353    fn container(&self, db: &dyn HirDatabase) -> ItemContainer {
6354        ItemContainer::Module(Module { id: self.id.lookup(db).container })
6355    }
6356}
6357
6358impl HasContainer for TypeAlias {
6359    fn container(&self, db: &dyn HirDatabase) -> ItemContainer {
6360        container_id_to_hir(self.id.lookup(db).container)
6361    }
6362}
6363
6364impl HasContainer for Const {
6365    fn container(&self, db: &dyn HirDatabase) -> ItemContainer {
6366        container_id_to_hir(self.id.lookup(db).container)
6367    }
6368}
6369
6370impl HasContainer for Static {
6371    fn container(&self, db: &dyn HirDatabase) -> ItemContainer {
6372        container_id_to_hir(self.id.lookup(db).container)
6373    }
6374}
6375
6376impl HasContainer for Trait {
6377    fn container(&self, db: &dyn HirDatabase) -> ItemContainer {
6378        ItemContainer::Module(Module { id: self.id.lookup(db).container })
6379    }
6380}
6381
6382impl HasContainer for ExternBlock {
6383    fn container(&self, db: &dyn HirDatabase) -> ItemContainer {
6384        ItemContainer::Module(Module { id: self.id.lookup(db).container })
6385    }
6386}
6387
6388fn container_id_to_hir(c: ItemContainerId) -> ItemContainer {
6389    match c {
6390        ItemContainerId::ExternBlockId(id) => ItemContainer::ExternBlock(ExternBlock { id }),
6391        ItemContainerId::ModuleId(id) => ItemContainer::Module(Module { id }),
6392        ItemContainerId::ImplId(id) => ItemContainer::Impl(Impl { id }),
6393        ItemContainerId::TraitId(id) => ItemContainer::Trait(Trait { id }),
6394    }
6395}
6396
6397#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
6398pub enum ItemContainer {
6399    Trait(Trait),
6400    Impl(Impl),
6401    Module(Module),
6402    ExternBlock(ExternBlock),
6403    Crate(Crate),
6404}
6405
6406/// Subset of `ide_db::Definition` that doc links can resolve to.
6407pub enum DocLinkDef {
6408    ModuleDef(ModuleDef),
6409    Field(Field),
6410    SelfType(Trait),
6411}
6412
6413fn push_ty_diagnostics<'db>(
6414    db: &'db dyn HirDatabase,
6415    acc: &mut Vec<AnyDiagnostic<'db>>,
6416    diagnostics: Option<ThinArc<(), TyLoweringDiagnostic>>,
6417    source_map: &ExpressionStoreSourceMap,
6418) {
6419    if let Some(diagnostics) = diagnostics {
6420        acc.extend(
6421            diagnostics
6422                .slice
6423                .iter()
6424                .filter_map(|diagnostic| AnyDiagnostic::ty_diagnostic(diagnostic, source_map, db)),
6425        );
6426    }
6427}
6428
6429pub trait MethodCandidateCallback {
6430    fn on_inherent_method(&mut self, f: Function) -> ControlFlow<()>;
6431
6432    fn on_trait_method(&mut self, f: Function) -> ControlFlow<()>;
6433}
6434
6435impl<F> MethodCandidateCallback for F
6436where
6437    F: FnMut(Function) -> ControlFlow<()>,
6438{
6439    fn on_inherent_method(&mut self, f: Function) -> ControlFlow<()> {
6440        self(f)
6441    }
6442
6443    fn on_trait_method(&mut self, f: Function) -> ControlFlow<()> {
6444        self(f)
6445    }
6446}
6447
6448pub trait PathCandidateCallback {
6449    fn on_inherent_item(&mut self, item: AssocItem) -> ControlFlow<()>;
6450
6451    fn on_trait_item(&mut self, item: AssocItem) -> ControlFlow<()>;
6452}
6453
6454impl<F> PathCandidateCallback for F
6455where
6456    F: FnMut(AssocItem) -> ControlFlow<()>,
6457{
6458    fn on_inherent_item(&mut self, item: AssocItem) -> ControlFlow<()> {
6459        self(item)
6460    }
6461
6462    fn on_trait_item(&mut self, item: AssocItem) -> ControlFlow<()> {
6463        self(item)
6464    }
6465}
6466
6467pub fn resolve_absolute_path<'a, I: Iterator<Item = Symbol> + Clone + 'a>(
6468    db: &'a dyn HirDatabase,
6469    mut segments: I,
6470) -> impl Iterator<Item = ItemInNs> + use<'a, I> {
6471    segments
6472        .next()
6473        .into_iter()
6474        .flat_map(move |crate_name| {
6475            db.all_crates()
6476                .iter()
6477                .filter(|&krate| {
6478                    krate
6479                        .extra_data(db)
6480                        .display_name
6481                        .as_ref()
6482                        .is_some_and(|name| *name.crate_name().symbol() == crate_name)
6483                })
6484                .filter_map(|&krate| {
6485                    let segments = segments.clone();
6486                    let mut def_map = crate_def_map(db, krate);
6487                    let mut module = &def_map[def_map.root_module_id()];
6488                    let mut segments = segments.with_position().peekable();
6489                    while let Some((_, segment)) = segments.next_if(|&(position, _)| {
6490                        !matches!(position, itertools::Position::Last | itertools::Position::Only)
6491                    }) {
6492                        let res = module
6493                            .scope
6494                            .get(&Name::new_symbol_root(segment))
6495                            .take_types()
6496                            .and_then(|res| match res {
6497                                ModuleDefId::ModuleId(it) => Some(it),
6498                                _ => None,
6499                            })?;
6500                        def_map = res.def_map(db);
6501                        module = &def_map[res];
6502                    }
6503                    let (_, item_name) = segments.next()?;
6504                    let res = module.scope.get(&Name::new_symbol_root(item_name));
6505                    Some(res.iter_items().map(|(item, _)| item.into()))
6506                })
6507                .collect::<Vec<_>>()
6508        })
6509        .flatten()
6510}
6511
6512fn as_name_opt(name: Option<impl AsName>) -> Name {
6513    name.map_or_else(Name::missing, |name| name.as_name())
6514}
6515
6516fn generic_args_from_tys<'db>(
6517    interner: DbInterner<'db>,
6518    def_id: SolverDefId,
6519    args: impl IntoIterator<Item = Ty<'db>>,
6520) -> GenericArgs<'db> {
6521    let mut args = args.into_iter();
6522    GenericArgs::for_item(interner, def_id, |_, id, _| {
6523        if matches!(id, GenericParamId::TypeParamId(_))
6524            && let Some(arg) = args.next()
6525        {
6526            arg.into()
6527        } else {
6528            next_solver::GenericArg::error_from_id(interner, id)
6529        }
6530    })
6531}
6532
6533fn has_non_default_type_params(db: &dyn HirDatabase, generic_def: GenericDefId) -> bool {
6534    let params = db.generic_params(generic_def);
6535    let defaults = db.generic_defaults(generic_def);
6536    params
6537        .iter_type_or_consts()
6538        .filter(|(_, param)| matches!(param, TypeOrConstParamData::TypeParamData(_)))
6539        .map(|(local_id, _)| TypeOrConstParamId { parent: generic_def, local_id })
6540        .any(|param| {
6541            let Some(param) = hir_ty::param_idx(db, param) else {
6542                return false;
6543            };
6544            defaults.get(param).is_none()
6545        })
6546}
6547
6548fn param_env_from_resolver<'db>(
6549    db: &'db dyn HirDatabase,
6550    resolver: &Resolver<'_>,
6551) -> ParamEnvAndCrate<'db> {
6552    ParamEnvAndCrate {
6553        param_env: resolver
6554            .generic_def()
6555            .map_or_else(ParamEnv::empty, |generic_def| db.trait_environment(generic_def)),
6556        krate: resolver.krate(),
6557    }
6558}
6559
6560fn param_env_from_has_crate<'db>(
6561    db: &'db dyn HirDatabase,
6562    id: impl hir_def::HasModule + Into<GenericDefId> + Copy,
6563) -> ParamEnvAndCrate<'db> {
6564    ParamEnvAndCrate { param_env: db.trait_environment(id.into()), krate: id.krate(db) }
6565}
6566
6567fn body_param_env_from_has_crate<'db>(
6568    db: &'db dyn HirDatabase,
6569    id: impl hir_def::HasModule + Into<DefWithBodyId> + Copy,
6570) -> ParamEnvAndCrate<'db> {
6571    ParamEnvAndCrate { param_env: db.trait_environment_for_body(id.into()), krate: id.krate(db) }
6572}
6573
6574fn empty_param_env<'db>(krate: base_db::Crate) -> ParamEnvAndCrate<'db> {
6575    ParamEnvAndCrate { param_env: ParamEnv::empty(), krate }
6576}
6577
6578pub use hir_ty::next_solver;
6579pub use hir_ty::setup_tracing;