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>(
4335        db: &'db dyn HirDatabase,
4336        Type { ty, env: _ }: Type<'db>,
4337    ) -> Vec<Impl> {
4338        let mut result = Vec::new();
4339        let interner = DbInterner::new_no_crate(db);
4340        let Some(simplified_ty) =
4341            fast_reject::simplify_type(interner, ty, fast_reject::TreatParams::AsRigid)
4342        else {
4343            return Vec::new();
4344        };
4345        let mut extend_with_impls =
4346            |impls: &[ImplId]| result.extend(impls.iter().copied().map(Impl::from));
4347        extend_with_impls(method_resolution::incoherent_inherent_impls(db, simplified_ty));
4348        if let Some(module) = method_resolution::simplified_type_module(db, &simplified_ty) {
4349            InherentImpls::for_each_crate_and_block(
4350                db,
4351                module.krate(db),
4352                module.block(db),
4353                &mut |impls| extend_with_impls(impls.for_self_ty(&simplified_ty)),
4354            );
4355            std::iter::successors(module.block(db), |block| block.loc(db).module.block(db))
4356                .filter_map(|block| TraitImpls::for_block(db, block).as_deref())
4357                .for_each(|impls| impls.for_self_ty(&simplified_ty, &mut extend_with_impls));
4358            for &krate in &**db.all_crates() {
4359                TraitImpls::for_crate(db, krate)
4360                    .for_self_ty(&simplified_ty, &mut extend_with_impls);
4361            }
4362        } else {
4363            for &krate in &**db.all_crates() {
4364                TraitImpls::for_crate(db, krate)
4365                    .for_self_ty(&simplified_ty, &mut extend_with_impls);
4366            }
4367        }
4368        result
4369    }
4370
4371    pub fn all_for_trait(db: &dyn HirDatabase, trait_: Trait) -> Vec<Impl> {
4372        let module = trait_.module(db).id;
4373        let mut all = Vec::new();
4374        let mut handle_impls = |impls: &TraitImpls| {
4375            impls.for_trait(trait_.id, |impls| all.extend(impls.iter().copied().map(Impl::from)));
4376        };
4377        for krate in module.krate(db).transitive_rev_deps(db) {
4378            handle_impls(TraitImpls::for_crate(db, krate));
4379        }
4380        if let Some(block) = module.block(db)
4381            && let Some(impls) = TraitImpls::for_block(db, block)
4382        {
4383            handle_impls(impls);
4384        }
4385        all
4386    }
4387
4388    pub fn trait_(self, db: &dyn HirDatabase) -> Option<Trait> {
4389        let trait_ref = db.impl_trait(self.id)?;
4390        let id = trait_ref.skip_binder().def_id;
4391        Some(Trait { id: id.0 })
4392    }
4393
4394    pub fn trait_ref(self, db: &dyn HirDatabase) -> Option<TraitRef<'_>> {
4395        let trait_ref = db.impl_trait(self.id)?.instantiate_identity();
4396        let resolver = self.id.resolver(db);
4397        Some(TraitRef::new_with_resolver(db, &resolver, trait_ref))
4398    }
4399
4400    pub fn self_ty(self, db: &dyn HirDatabase) -> Type<'_> {
4401        let resolver = self.id.resolver(db);
4402        // FIXME: This shouldn't be `instantiate_identity()`, we shouldn't leak `TyKind::Param`s.
4403        let ty = db.impl_self_ty(self.id).instantiate_identity();
4404        Type::new_with_resolver_inner(db, &resolver, ty)
4405    }
4406
4407    pub fn items(self, db: &dyn HirDatabase) -> Vec<AssocItem> {
4408        self.id.impl_items(db).items.iter().map(|&(_, it)| it.into()).collect()
4409    }
4410
4411    pub fn is_negative(self, db: &dyn HirDatabase) -> bool {
4412        db.impl_signature(self.id).flags.contains(ImplFlags::NEGATIVE)
4413    }
4414
4415    pub fn is_unsafe(self, db: &dyn HirDatabase) -> bool {
4416        db.impl_signature(self.id).flags.contains(ImplFlags::UNSAFE)
4417    }
4418
4419    pub fn module(self, db: &dyn HirDatabase) -> Module {
4420        self.id.lookup(db).container.into()
4421    }
4422
4423    pub fn as_builtin_derive_path(self, db: &dyn HirDatabase) -> Option<InMacroFile<ast::Path>> {
4424        let src = self.source(db)?;
4425
4426        let macro_file = src.file_id.macro_file()?;
4427        let loc = macro_file.lookup(db);
4428        let (derive_attr, derive_index) = match loc.kind {
4429            MacroCallKind::Derive { ast_id, derive_attr_index, derive_index, .. } => {
4430                let module_id = self.id.lookup(db).container;
4431                (
4432                    module_id.def_map(db)[module_id]
4433                        .scope
4434                        .derive_macro_invoc(ast_id, derive_attr_index)?,
4435                    derive_index,
4436                )
4437            }
4438            _ => return None,
4439        };
4440        let path = db
4441            .parse_macro_expansion(derive_attr)
4442            .value
4443            .0
4444            .syntax_node()
4445            .children()
4446            .nth(derive_index as usize)
4447            .and_then(<ast::Attr as AstNode>::cast)
4448            .and_then(|it| it.path())?;
4449        Some(InMacroFile { file_id: derive_attr, value: path })
4450    }
4451
4452    pub fn check_orphan_rules(self, db: &dyn HirDatabase) -> bool {
4453        check_orphan_rules(db, self.id)
4454    }
4455
4456    fn all_macro_calls(&self, db: &dyn HirDatabase) -> Box<[(AstId<ast::Item>, MacroCallId)]> {
4457        self.id.impl_items(db).macro_calls.to_vec().into_boxed_slice()
4458    }
4459}
4460
4461#[derive(Clone, PartialEq, Eq, Debug, Hash)]
4462pub struct TraitRef<'db> {
4463    env: ParamEnvAndCrate<'db>,
4464    trait_ref: hir_ty::next_solver::TraitRef<'db>,
4465}
4466
4467impl<'db> TraitRef<'db> {
4468    pub(crate) fn new_with_resolver(
4469        db: &'db dyn HirDatabase,
4470        resolver: &Resolver<'_>,
4471        trait_ref: hir_ty::next_solver::TraitRef<'db>,
4472    ) -> Self {
4473        let env = param_env_from_resolver(db, resolver);
4474        TraitRef { env, trait_ref }
4475    }
4476
4477    pub fn trait_(&self) -> Trait {
4478        Trait { id: self.trait_ref.def_id.0 }
4479    }
4480
4481    pub fn self_ty(&self) -> TypeNs<'_> {
4482        let ty = self.trait_ref.self_ty();
4483        TypeNs { env: self.env, ty }
4484    }
4485
4486    /// Returns `idx`-th argument of this trait reference if it is a type argument. Note that the
4487    /// first argument is the `Self` type.
4488    pub fn get_type_argument(&self, idx: usize) -> Option<TypeNs<'db>> {
4489        self.trait_ref
4490            .args
4491            .as_slice()
4492            .get(idx)
4493            .and_then(|arg| arg.ty())
4494            .map(|ty| TypeNs { env: self.env, ty })
4495    }
4496}
4497
4498#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)]
4499enum AnyClosureId {
4500    ClosureId(InternedClosureId),
4501    CoroutineClosureId(InternedCoroutineId),
4502}
4503
4504#[derive(Clone, Debug, PartialEq, Eq, Hash)]
4505pub struct Closure<'db> {
4506    id: AnyClosureId,
4507    subst: GenericArgs<'db>,
4508}
4509
4510impl<'db> Closure<'db> {
4511    fn as_ty(&self, db: &'db dyn HirDatabase) -> Ty<'db> {
4512        let interner = DbInterner::new_no_crate(db);
4513        match self.id {
4514            AnyClosureId::ClosureId(id) => Ty::new_closure(interner, id.into(), self.subst),
4515            AnyClosureId::CoroutineClosureId(id) => {
4516                Ty::new_coroutine_closure(interner, id.into(), self.subst)
4517            }
4518        }
4519    }
4520
4521    pub fn display_with_id(&self, db: &dyn HirDatabase, display_target: DisplayTarget) -> String {
4522        self.as_ty(db)
4523            .display(db, display_target)
4524            .with_closure_style(ClosureStyle::ClosureWithId)
4525            .to_string()
4526    }
4527
4528    pub fn display_with_impl(&self, db: &dyn HirDatabase, display_target: DisplayTarget) -> String {
4529        self.as_ty(db)
4530            .display(db, display_target)
4531            .with_closure_style(ClosureStyle::ImplFn)
4532            .to_string()
4533    }
4534
4535    pub fn captured_items(&self, db: &'db dyn HirDatabase) -> Vec<ClosureCapture<'db>> {
4536        let AnyClosureId::ClosureId(id) = self.id else {
4537            // FIXME: Infer coroutine closures' captures.
4538            return Vec::new();
4539        };
4540        let owner = db.lookup_intern_closure(id).0;
4541        let infer = InferenceResult::for_body(db, owner);
4542        let info = infer.closure_info(id);
4543        info.0
4544            .iter()
4545            .cloned()
4546            .map(|capture| ClosureCapture { owner, closure: id, capture })
4547            .collect()
4548    }
4549
4550    pub fn capture_types(&self, db: &'db dyn HirDatabase) -> Vec<Type<'db>> {
4551        let AnyClosureId::ClosureId(id) = self.id else {
4552            // FIXME: Infer coroutine closures' captures.
4553            return Vec::new();
4554        };
4555        let owner = db.lookup_intern_closure(id).0;
4556        let infer = InferenceResult::for_body(db, owner);
4557        let (captures, _) = infer.closure_info(id);
4558        let env = body_param_env_from_has_crate(db, owner);
4559        captures.iter().map(|capture| Type { env, ty: capture.ty(db, self.subst) }).collect()
4560    }
4561
4562    pub fn fn_trait(&self, db: &dyn HirDatabase) -> FnTrait {
4563        match self.id {
4564            AnyClosureId::ClosureId(id) => {
4565                let owner = db.lookup_intern_closure(id).0;
4566                let infer = InferenceResult::for_body(db, owner);
4567                let info = infer.closure_info(id);
4568                info.1.into()
4569            }
4570            AnyClosureId::CoroutineClosureId(_id) => {
4571                // FIXME: Infer kind for coroutine closures.
4572                match self.subst.as_coroutine_closure().kind() {
4573                    rustc_type_ir::ClosureKind::Fn => FnTrait::AsyncFn,
4574                    rustc_type_ir::ClosureKind::FnMut => FnTrait::AsyncFnMut,
4575                    rustc_type_ir::ClosureKind::FnOnce => FnTrait::AsyncFnOnce,
4576                }
4577            }
4578        }
4579    }
4580}
4581
4582#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
4583pub enum FnTrait {
4584    FnOnce,
4585    FnMut,
4586    Fn,
4587
4588    AsyncFnOnce,
4589    AsyncFnMut,
4590    AsyncFn,
4591}
4592
4593impl From<traits::FnTrait> for FnTrait {
4594    fn from(value: traits::FnTrait) -> Self {
4595        match value {
4596            traits::FnTrait::FnOnce => FnTrait::FnOnce,
4597            traits::FnTrait::FnMut => FnTrait::FnMut,
4598            traits::FnTrait::Fn => FnTrait::Fn,
4599            traits::FnTrait::AsyncFnOnce => FnTrait::AsyncFnOnce,
4600            traits::FnTrait::AsyncFnMut => FnTrait::AsyncFnMut,
4601            traits::FnTrait::AsyncFn => FnTrait::AsyncFn,
4602        }
4603    }
4604}
4605
4606impl fmt::Display for FnTrait {
4607    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
4608        match self {
4609            FnTrait::FnOnce => write!(f, "FnOnce"),
4610            FnTrait::FnMut => write!(f, "FnMut"),
4611            FnTrait::Fn => write!(f, "Fn"),
4612            FnTrait::AsyncFnOnce => write!(f, "AsyncFnOnce"),
4613            FnTrait::AsyncFnMut => write!(f, "AsyncFnMut"),
4614            FnTrait::AsyncFn => write!(f, "AsyncFn"),
4615        }
4616    }
4617}
4618
4619impl FnTrait {
4620    pub const fn function_name(&self) -> &'static str {
4621        match self {
4622            FnTrait::FnOnce => "call_once",
4623            FnTrait::FnMut => "call_mut",
4624            FnTrait::Fn => "call",
4625            FnTrait::AsyncFnOnce => "async_call_once",
4626            FnTrait::AsyncFnMut => "async_call_mut",
4627            FnTrait::AsyncFn => "async_call",
4628        }
4629    }
4630
4631    pub fn lang_item(self) -> LangItem {
4632        match self {
4633            FnTrait::FnOnce => LangItem::FnOnce,
4634            FnTrait::FnMut => LangItem::FnMut,
4635            FnTrait::Fn => LangItem::Fn,
4636            FnTrait::AsyncFnOnce => LangItem::AsyncFnOnce,
4637            FnTrait::AsyncFnMut => LangItem::AsyncFnMut,
4638            FnTrait::AsyncFn => LangItem::AsyncFn,
4639        }
4640    }
4641
4642    pub fn get_id(self, db: &dyn HirDatabase, krate: Crate) -> Option<Trait> {
4643        Trait::lang(db, krate, self.lang_item())
4644    }
4645}
4646
4647#[derive(Clone, Debug, PartialEq, Eq)]
4648pub struct ClosureCapture<'db> {
4649    owner: DefWithBodyId,
4650    closure: InternedClosureId,
4651    capture: hir_ty::CapturedItem<'db>,
4652}
4653
4654impl<'db> ClosureCapture<'db> {
4655    pub fn local(&self) -> Local {
4656        Local { parent: self.owner, binding_id: self.capture.local() }
4657    }
4658
4659    /// Returns whether this place has any field (aka. non-deref) projections.
4660    pub fn has_field_projections(&self) -> bool {
4661        self.capture.has_field_projections()
4662    }
4663
4664    pub fn usages(&self) -> CaptureUsages {
4665        CaptureUsages { parent: self.owner, spans: self.capture.spans() }
4666    }
4667
4668    pub fn kind(&self) -> CaptureKind {
4669        match self.capture.kind() {
4670            hir_ty::CaptureKind::ByRef(
4671                hir_ty::mir::BorrowKind::Shallow | hir_ty::mir::BorrowKind::Shared,
4672            ) => CaptureKind::SharedRef,
4673            hir_ty::CaptureKind::ByRef(hir_ty::mir::BorrowKind::Mut {
4674                kind: MutBorrowKind::ClosureCapture,
4675            }) => CaptureKind::UniqueSharedRef,
4676            hir_ty::CaptureKind::ByRef(hir_ty::mir::BorrowKind::Mut {
4677                kind: MutBorrowKind::Default | MutBorrowKind::TwoPhasedBorrow,
4678            }) => CaptureKind::MutableRef,
4679            hir_ty::CaptureKind::ByValue => CaptureKind::Move,
4680        }
4681    }
4682
4683    /// Converts the place to a name that can be inserted into source code.
4684    pub fn place_to_name(&self, db: &dyn HirDatabase) -> String {
4685        self.capture.place_to_name(self.owner, db)
4686    }
4687
4688    pub fn display_place_source_code(&self, db: &dyn HirDatabase) -> String {
4689        self.capture.display_place_source_code(self.owner, db)
4690    }
4691
4692    pub fn display_place(&self, db: &dyn HirDatabase) -> String {
4693        self.capture.display_place(self.owner, db)
4694    }
4695}
4696
4697#[derive(Clone, Copy, PartialEq, Eq)]
4698pub enum CaptureKind {
4699    SharedRef,
4700    UniqueSharedRef,
4701    MutableRef,
4702    Move,
4703}
4704
4705#[derive(Debug, Clone)]
4706pub struct CaptureUsages {
4707    parent: DefWithBodyId,
4708    spans: SmallVec<[mir::MirSpan; 3]>,
4709}
4710
4711impl CaptureUsages {
4712    pub fn sources(&self, db: &dyn HirDatabase) -> Vec<CaptureUsageSource> {
4713        let (body, source_map) = db.body_with_source_map(self.parent);
4714
4715        let mut result = Vec::with_capacity(self.spans.len());
4716        for &span in self.spans.iter() {
4717            let is_ref = span.is_ref_span(&body);
4718            match span {
4719                mir::MirSpan::ExprId(expr) => {
4720                    if let Ok(expr) = source_map.expr_syntax(expr) {
4721                        result.push(CaptureUsageSource { is_ref, source: expr })
4722                    }
4723                }
4724                mir::MirSpan::PatId(pat) => {
4725                    if let Ok(pat) = source_map.pat_syntax(pat) {
4726                        result.push(CaptureUsageSource { is_ref, source: pat });
4727                    }
4728                }
4729                mir::MirSpan::BindingId(binding) => result.extend(
4730                    source_map
4731                        .patterns_for_binding(binding)
4732                        .iter()
4733                        .filter_map(|&pat| source_map.pat_syntax(pat).ok())
4734                        .map(|pat| CaptureUsageSource { is_ref, source: pat }),
4735                ),
4736                mir::MirSpan::SelfParam | mir::MirSpan::Unknown => {
4737                    unreachable!("invalid capture usage span")
4738                }
4739            }
4740        }
4741        result
4742    }
4743}
4744
4745#[derive(Debug)]
4746pub struct CaptureUsageSource {
4747    is_ref: bool,
4748    source: InFile<AstPtr<Either<ast::Expr, ast::Pat>>>,
4749}
4750
4751impl CaptureUsageSource {
4752    pub fn source(&self) -> AstPtr<Either<ast::Expr, ast::Pat>> {
4753        self.source.value
4754    }
4755
4756    pub fn file_id(&self) -> HirFileId {
4757        self.source.file_id
4758    }
4759
4760    pub fn is_ref(&self) -> bool {
4761        self.is_ref
4762    }
4763}
4764
4765#[derive(Clone, PartialEq, Eq, Debug, Hash)]
4766pub struct Type<'db> {
4767    env: ParamEnvAndCrate<'db>,
4768    ty: Ty<'db>,
4769}
4770
4771impl<'db> Type<'db> {
4772    pub(crate) fn new_with_resolver(
4773        db: &'db dyn HirDatabase,
4774        resolver: &Resolver<'_>,
4775        ty: Ty<'db>,
4776    ) -> Self {
4777        Type::new_with_resolver_inner(db, resolver, ty)
4778    }
4779
4780    pub(crate) fn new_with_resolver_inner(
4781        db: &'db dyn HirDatabase,
4782        resolver: &Resolver<'_>,
4783        ty: Ty<'db>,
4784    ) -> Self {
4785        let environment = param_env_from_resolver(db, resolver);
4786        Type { env: environment, ty }
4787    }
4788
4789    pub(crate) fn new_for_crate(krate: base_db::Crate, ty: Ty<'db>) -> Self {
4790        Type { env: empty_param_env(krate), ty }
4791    }
4792
4793    fn new(db: &'db dyn HirDatabase, lexical_env: impl HasResolver, ty: Ty<'db>) -> Self {
4794        let resolver = lexical_env.resolver(db);
4795        let environment = param_env_from_resolver(db, &resolver);
4796        Type { env: environment, ty }
4797    }
4798
4799    fn from_def(db: &'db dyn HirDatabase, def: impl Into<TyDefId> + HasResolver) -> Self {
4800        let interner = DbInterner::new_no_crate(db);
4801        let ty = db.ty(def.into());
4802        let def = match def.into() {
4803            TyDefId::AdtId(it) => GenericDefId::AdtId(it),
4804            TyDefId::TypeAliasId(it) => GenericDefId::TypeAliasId(it),
4805            TyDefId::BuiltinType(_) => {
4806                return Type::new(db, def, ty.skip_binder());
4807            }
4808        };
4809        let args = GenericArgs::error_for_item(interner, def.into());
4810        Type::new(db, def, ty.instantiate(interner, args))
4811    }
4812
4813    // FIXME: We shouldn't leak `TyKind::Param`s.
4814    fn from_def_params(db: &'db dyn HirDatabase, def: impl Into<TyDefId> + HasResolver) -> Self {
4815        let ty = db.ty(def.into());
4816        Type::new(db, def, ty.instantiate_identity())
4817    }
4818
4819    fn from_value_def(
4820        db: &'db dyn HirDatabase,
4821        def: impl Into<ValueTyDefId> + HasResolver,
4822    ) -> Self {
4823        let interner = DbInterner::new_no_crate(db);
4824        let Some(ty) = db.value_ty(def.into()) else {
4825            return Type::new(db, def, Ty::new_error(interner, ErrorGuaranteed));
4826        };
4827        let def = match def.into() {
4828            ValueTyDefId::ConstId(it) => GenericDefId::ConstId(it),
4829            ValueTyDefId::FunctionId(it) => GenericDefId::FunctionId(it),
4830            ValueTyDefId::StructId(it) => GenericDefId::AdtId(AdtId::StructId(it)),
4831            ValueTyDefId::UnionId(it) => GenericDefId::AdtId(AdtId::UnionId(it)),
4832            ValueTyDefId::EnumVariantId(it) => {
4833                GenericDefId::AdtId(AdtId::EnumId(it.lookup(db).parent))
4834            }
4835            ValueTyDefId::StaticId(_) => {
4836                return Type::new(db, def, ty.skip_binder());
4837            }
4838        };
4839        let args = GenericArgs::error_for_item(interner, def.into());
4840        Type::new(db, def, ty.instantiate(interner, args))
4841    }
4842
4843    pub fn new_slice(ty: Self) -> Self {
4844        let interner = DbInterner::conjure();
4845        Type { env: ty.env, ty: Ty::new_slice(interner, ty.ty) }
4846    }
4847
4848    pub fn new_tuple(krate: base_db::Crate, tys: &[Self]) -> Self {
4849        let tys = tys.iter().map(|it| it.ty);
4850        let interner = DbInterner::conjure();
4851        Type { env: empty_param_env(krate), ty: Ty::new_tup_from_iter(interner, tys) }
4852    }
4853
4854    pub fn is_unit(&self) -> bool {
4855        self.ty.is_unit()
4856    }
4857
4858    pub fn is_bool(&self) -> bool {
4859        matches!(self.ty.kind(), TyKind::Bool)
4860    }
4861
4862    pub fn is_str(&self) -> bool {
4863        matches!(self.ty.kind(), TyKind::Str)
4864    }
4865
4866    pub fn is_never(&self) -> bool {
4867        matches!(self.ty.kind(), TyKind::Never)
4868    }
4869
4870    pub fn is_mutable_reference(&self) -> bool {
4871        matches!(self.ty.kind(), TyKind::Ref(.., hir_ty::next_solver::Mutability::Mut))
4872    }
4873
4874    pub fn is_reference(&self) -> bool {
4875        matches!(self.ty.kind(), TyKind::Ref(..))
4876    }
4877
4878    pub fn contains_reference(&self, db: &'db dyn HirDatabase) -> bool {
4879        let interner = DbInterner::new_no_crate(db);
4880        return self.ty.visit_with(&mut Visitor { interner }).is_break();
4881
4882        fn is_phantom_data(db: &dyn HirDatabase, adt_id: AdtId) -> bool {
4883            match adt_id {
4884                AdtId::StructId(s) => {
4885                    let flags = db.struct_signature(s).flags;
4886                    flags.contains(StructFlags::IS_PHANTOM_DATA)
4887                }
4888                AdtId::UnionId(_) | AdtId::EnumId(_) => false,
4889            }
4890        }
4891
4892        struct Visitor<'db> {
4893            interner: DbInterner<'db>,
4894        }
4895
4896        impl<'db> TypeVisitor<DbInterner<'db>> for Visitor<'db> {
4897            type Result = ControlFlow<()>;
4898
4899            fn visit_ty(&mut self, ty: Ty<'db>) -> Self::Result {
4900                match ty.kind() {
4901                    // Reference itself
4902                    TyKind::Ref(..) => ControlFlow::Break(()),
4903
4904                    // For non-phantom_data adts we check variants/fields as well as generic parameters
4905                    TyKind::Adt(adt_def, args)
4906                        if !is_phantom_data(self.interner.db(), adt_def.def_id().0) =>
4907                    {
4908                        let _variant_id_to_fields = |id: VariantId| {
4909                            let variant_data = &id.fields(self.interner.db());
4910                            if variant_data.fields().is_empty() {
4911                                vec![]
4912                            } else {
4913                                let field_types = self.interner.db().field_types(id);
4914                                variant_data
4915                                    .fields()
4916                                    .iter()
4917                                    .map(|(idx, _)| {
4918                                        field_types[idx].instantiate(self.interner, args)
4919                                    })
4920                                    .filter(|it| !it.references_non_lt_error())
4921                                    .collect()
4922                            }
4923                        };
4924                        let variant_id_to_fields = |_: VariantId| vec![];
4925
4926                        let variants: Vec<Vec<Ty<'db>>> = match adt_def.def_id().0 {
4927                            AdtId::StructId(id) => {
4928                                vec![variant_id_to_fields(id.into())]
4929                            }
4930                            AdtId::EnumId(id) => id
4931                                .enum_variants(self.interner.db())
4932                                .variants
4933                                .iter()
4934                                .map(|&(variant_id, _, _)| variant_id_to_fields(variant_id.into()))
4935                                .collect(),
4936                            AdtId::UnionId(id) => {
4937                                vec![variant_id_to_fields(id.into())]
4938                            }
4939                        };
4940
4941                        variants
4942                            .into_iter()
4943                            .flat_map(|variant| variant.into_iter())
4944                            .try_for_each(|ty| ty.visit_with(self))?;
4945                        args.visit_with(self)
4946                    }
4947                    // And for `PhantomData<T>`, we check `T`.
4948                    _ => ty.super_visit_with(self),
4949                }
4950            }
4951        }
4952    }
4953
4954    pub fn as_reference(&self) -> Option<(Type<'db>, Mutability)> {
4955        let TyKind::Ref(_lt, ty, m) = self.ty.kind() else { return None };
4956        let m = Mutability::from_mutable(matches!(m, hir_ty::next_solver::Mutability::Mut));
4957        Some((self.derived(ty), m))
4958    }
4959
4960    pub fn add_reference(&self, mutability: Mutability) -> Self {
4961        let interner = DbInterner::conjure();
4962        let ty_mutability = match mutability {
4963            Mutability::Shared => hir_ty::next_solver::Mutability::Not,
4964            Mutability::Mut => hir_ty::next_solver::Mutability::Mut,
4965        };
4966        self.derived(Ty::new_ref(interner, Region::error(interner), self.ty, ty_mutability))
4967    }
4968
4969    pub fn is_slice(&self) -> bool {
4970        matches!(self.ty.kind(), TyKind::Slice(..))
4971    }
4972
4973    pub fn is_usize(&self) -> bool {
4974        matches!(self.ty.kind(), TyKind::Uint(rustc_type_ir::UintTy::Usize))
4975    }
4976
4977    pub fn is_float(&self) -> bool {
4978        matches!(self.ty.kind(), TyKind::Float(_))
4979    }
4980
4981    pub fn is_char(&self) -> bool {
4982        matches!(self.ty.kind(), TyKind::Char)
4983    }
4984
4985    pub fn is_int_or_uint(&self) -> bool {
4986        matches!(self.ty.kind(), TyKind::Int(_) | TyKind::Uint(_))
4987    }
4988
4989    pub fn is_scalar(&self) -> bool {
4990        matches!(
4991            self.ty.kind(),
4992            TyKind::Bool | TyKind::Char | TyKind::Int(_) | TyKind::Uint(_) | TyKind::Float(_)
4993        )
4994    }
4995
4996    pub fn is_tuple(&self) -> bool {
4997        matches!(self.ty.kind(), TyKind::Tuple(..))
4998    }
4999
5000    pub fn remove_ref(&self) -> Option<Type<'db>> {
5001        match self.ty.kind() {
5002            TyKind::Ref(_, ty, _) => Some(self.derived(ty)),
5003            _ => None,
5004        }
5005    }
5006
5007    pub fn as_slice(&self) -> Option<Type<'db>> {
5008        match self.ty.kind() {
5009            TyKind::Slice(ty) => Some(self.derived(ty)),
5010            _ => None,
5011        }
5012    }
5013
5014    pub fn strip_references(&self) -> Self {
5015        self.derived(self.ty.strip_references())
5016    }
5017
5018    // FIXME: This is the same as `remove_ref()`, remove one of these methods.
5019    pub fn strip_reference(&self) -> Self {
5020        self.derived(self.ty.strip_reference())
5021    }
5022
5023    pub fn is_unknown(&self) -> bool {
5024        self.ty.is_ty_error()
5025    }
5026
5027    /// Checks that particular type `ty` implements `std::future::IntoFuture` or
5028    /// `std::future::Future` and returns the `Output` associated type.
5029    /// This function is used in `.await` syntax completion.
5030    pub fn into_future_output(&self, db: &'db dyn HirDatabase) -> Option<Type<'db>> {
5031        let lang_items = hir_def::lang_item::lang_items(db, self.env.krate);
5032        let trait_ = lang_items
5033            .IntoFutureIntoFuture
5034            .and_then(|into_future_fn| {
5035                let assoc_item = as_assoc_item(db, AssocItem::Function, into_future_fn)?;
5036                let into_future_trait = assoc_item.container_or_implemented_trait(db)?;
5037                Some(into_future_trait.id)
5038            })
5039            .or(lang_items.Future)?;
5040
5041        if !traits::implements_trait_unique(self.ty, db, self.env, trait_) {
5042            return None;
5043        }
5044
5045        let output_assoc_type =
5046            trait_.trait_items(db).associated_type_by_name(&Name::new_symbol_root(sym::Output))?;
5047        self.normalize_trait_assoc_type(db, &[], output_assoc_type.into())
5048    }
5049
5050    /// This does **not** resolve `IntoFuture`, only `Future`.
5051    pub fn future_output(self, db: &'db dyn HirDatabase) -> Option<Type<'db>> {
5052        let lang_items = hir_def::lang_item::lang_items(db, self.env.krate);
5053        let future_output = lang_items.FutureOutput?;
5054        self.normalize_trait_assoc_type(db, &[], future_output.into())
5055    }
5056
5057    /// This does **not** resolve `IntoIterator`, only `Iterator`.
5058    pub fn iterator_item(self, db: &'db dyn HirDatabase) -> Option<Type<'db>> {
5059        let lang_items = hir_def::lang_item::lang_items(db, self.env.krate);
5060        let iterator_trait = lang_items.Iterator?;
5061        let iterator_item = iterator_trait
5062            .trait_items(db)
5063            .associated_type_by_name(&Name::new_symbol_root(sym::Item))?;
5064        self.normalize_trait_assoc_type(db, &[], iterator_item.into())
5065    }
5066
5067    pub fn impls_iterator(self, db: &'db dyn HirDatabase) -> bool {
5068        let lang_items = hir_def::lang_item::lang_items(db, self.env.krate);
5069        let Some(iterator_trait) = lang_items.Iterator else {
5070            return false;
5071        };
5072        traits::implements_trait_unique(self.ty, db, self.env, iterator_trait)
5073    }
5074
5075    /// Resolves the projection `<Self as IntoIterator>::IntoIter` and returns the resulting type
5076    pub fn into_iterator_iter(self, db: &'db dyn HirDatabase) -> Option<Type<'db>> {
5077        let lang_items = hir_def::lang_item::lang_items(db, self.env.krate);
5078        let trait_ = lang_items.IntoIterIntoIter.and_then(|into_iter_fn| {
5079            let assoc_item = as_assoc_item(db, AssocItem::Function, into_iter_fn)?;
5080            let into_iter_trait = assoc_item.container_or_implemented_trait(db)?;
5081            Some(into_iter_trait.id)
5082        })?;
5083
5084        if !traits::implements_trait_unique(self.ty, db, self.env, trait_) {
5085            return None;
5086        }
5087
5088        let into_iter_assoc_type = trait_
5089            .trait_items(db)
5090            .associated_type_by_name(&Name::new_symbol_root(sym::IntoIter))?;
5091        self.normalize_trait_assoc_type(db, &[], into_iter_assoc_type.into())
5092    }
5093
5094    /// Checks that particular type `ty` implements `std::ops::FnOnce`.
5095    ///
5096    /// This function can be used to check if a particular type is callable, since FnOnce is a
5097    /// supertrait of Fn and FnMut, so all callable types implements at least FnOnce.
5098    pub fn impls_fnonce(&self, db: &'db dyn HirDatabase) -> bool {
5099        let lang_items = hir_def::lang_item::lang_items(db, self.env.krate);
5100        let fnonce_trait = match lang_items.FnOnce {
5101            Some(it) => it,
5102            None => return false,
5103        };
5104
5105        traits::implements_trait_unique(self.ty, db, self.env, fnonce_trait)
5106    }
5107
5108    // FIXME: Find better API that also handles const generics
5109    pub fn impls_trait(&self, db: &'db dyn HirDatabase, trait_: Trait, args: &[Type<'db>]) -> bool {
5110        let interner = DbInterner::new_no_crate(db);
5111        let args = generic_args_from_tys(
5112            interner,
5113            trait_.id.into(),
5114            std::iter::once(self.ty).chain(args.iter().map(|ty| ty.ty)),
5115        );
5116        traits::implements_trait_unique_with_args(db, self.env, trait_.id, args)
5117    }
5118
5119    pub fn normalize_trait_assoc_type(
5120        &self,
5121        db: &'db dyn HirDatabase,
5122        args: &[Type<'db>],
5123        alias: TypeAlias,
5124    ) -> Option<Type<'db>> {
5125        let interner = DbInterner::new_with(db, self.env.krate);
5126        let args = generic_args_from_tys(
5127            interner,
5128            alias.id.into(),
5129            std::iter::once(self.ty).chain(args.iter().map(|ty| ty.ty)),
5130        );
5131        // FIXME: We don't handle GATs yet.
5132        let projection = Ty::new_alias(
5133            interner,
5134            AliasTyKind::Projection,
5135            AliasTy::new(interner, alias.id.into(), args),
5136        );
5137
5138        let infcx = interner.infer_ctxt().build(TypingMode::PostAnalysis);
5139        let ty = structurally_normalize_ty(&infcx, projection, self.env.param_env);
5140        if ty.is_ty_error() { None } else { Some(self.derived(ty)) }
5141    }
5142
5143    pub fn is_copy(&self, db: &'db dyn HirDatabase) -> bool {
5144        let lang_items = hir_def::lang_item::lang_items(db, self.env.krate);
5145        let Some(copy_trait) = lang_items.Copy else {
5146            return false;
5147        };
5148        self.impls_trait(db, copy_trait.into(), &[])
5149    }
5150
5151    pub fn as_callable(&self, db: &'db dyn HirDatabase) -> Option<Callable<'db>> {
5152        let interner = DbInterner::new_no_crate(db);
5153        let callee = match self.ty.kind() {
5154            TyKind::Closure(id, subst) => Callee::Closure(id.0, subst),
5155            TyKind::CoroutineClosure(id, subst) => Callee::CoroutineClosure(id.0, subst),
5156            TyKind::FnPtr(..) => Callee::FnPtr,
5157            TyKind::FnDef(id, _) => Callee::Def(id.0),
5158            // This will happen when it implements fn or fn mut, since we add an autoborrow adjustment
5159            TyKind::Ref(_, inner_ty, _) => return self.derived(inner_ty).as_callable(db),
5160            _ => {
5161                let (fn_trait, sig) = hir_ty::callable_sig_from_fn_trait(self.ty, self.env, db)?;
5162                return Some(Callable {
5163                    ty: self.clone(),
5164                    sig,
5165                    callee: Callee::FnImpl(fn_trait),
5166                    is_bound_method: false,
5167                });
5168            }
5169        };
5170
5171        let sig = self.ty.callable_sig(interner)?;
5172        Some(Callable { ty: self.clone(), sig, callee, is_bound_method: false })
5173    }
5174
5175    pub fn is_closure(&self) -> bool {
5176        matches!(self.ty.kind(), TyKind::Closure { .. })
5177    }
5178
5179    pub fn as_closure(&self) -> Option<Closure<'db>> {
5180        match self.ty.kind() {
5181            TyKind::Closure(id, subst) => {
5182                Some(Closure { id: AnyClosureId::ClosureId(id.0), subst })
5183            }
5184            TyKind::CoroutineClosure(id, subst) => {
5185                Some(Closure { id: AnyClosureId::CoroutineClosureId(id.0), subst })
5186            }
5187            _ => None,
5188        }
5189    }
5190
5191    pub fn is_fn(&self) -> bool {
5192        matches!(self.ty.kind(), TyKind::FnDef(..) | TyKind::FnPtr { .. })
5193    }
5194
5195    pub fn is_array(&self) -> bool {
5196        matches!(self.ty.kind(), TyKind::Array(..))
5197    }
5198
5199    pub fn is_packed(&self, db: &'db dyn HirDatabase) -> bool {
5200        let adt_id = match self.ty.kind() {
5201            TyKind::Adt(adt_def, ..) => adt_def.def_id().0,
5202            _ => return false,
5203        };
5204
5205        let adt = adt_id.into();
5206        match adt {
5207            Adt::Struct(s) => s.repr(db).unwrap_or_default().pack.is_some(),
5208            _ => false,
5209        }
5210    }
5211
5212    pub fn is_raw_ptr(&self) -> bool {
5213        matches!(self.ty.kind(), TyKind::RawPtr(..))
5214    }
5215
5216    pub fn remove_raw_ptr(&self) -> Option<Type<'db>> {
5217        if let TyKind::RawPtr(ty, _) = self.ty.kind() { Some(self.derived(ty)) } else { None }
5218    }
5219
5220    pub fn contains_unknown(&self) -> bool {
5221        self.ty.references_non_lt_error()
5222    }
5223
5224    pub fn fields(&self, db: &'db dyn HirDatabase) -> Vec<(Field, Self)> {
5225        let interner = DbInterner::new_no_crate(db);
5226        let (variant_id, substs) = match self.ty.kind() {
5227            TyKind::Adt(adt_def, substs) => {
5228                let id = match adt_def.def_id().0 {
5229                    AdtId::StructId(id) => id.into(),
5230                    AdtId::UnionId(id) => id.into(),
5231                    AdtId::EnumId(_) => return Vec::new(),
5232                };
5233                (id, substs)
5234            }
5235            _ => return Vec::new(),
5236        };
5237
5238        db.field_types(variant_id)
5239            .iter()
5240            .map(|(local_id, ty)| {
5241                let def = Field { parent: variant_id.into(), id: local_id };
5242                let ty = ty.instantiate(interner, substs);
5243                (def, self.derived(ty))
5244            })
5245            .collect()
5246    }
5247
5248    pub fn tuple_fields(&self, _db: &'db dyn HirDatabase) -> Vec<Self> {
5249        if let TyKind::Tuple(substs) = self.ty.kind() {
5250            substs.iter().map(|ty| self.derived(ty)).collect()
5251        } else {
5252            Vec::new()
5253        }
5254    }
5255
5256    pub fn as_array(&self, db: &'db dyn HirDatabase) -> Option<(Self, usize)> {
5257        if let TyKind::Array(ty, len) = self.ty.kind() {
5258            try_const_usize(db, len).map(|it| (self.derived(ty), it as usize))
5259        } else {
5260            None
5261        }
5262    }
5263
5264    pub fn fingerprint_for_trait_impl(&self) -> Option<SimplifiedType> {
5265        fast_reject::simplify_type(
5266            DbInterner::conjure(),
5267            self.ty,
5268            fast_reject::TreatParams::AsRigid,
5269        )
5270    }
5271
5272    /// Returns types that this type dereferences to (including this type itself). The returned
5273    /// iterator won't yield the same type more than once even if the deref chain contains a cycle.
5274    pub fn autoderef(
5275        &self,
5276        db: &'db dyn HirDatabase,
5277    ) -> impl Iterator<Item = Type<'db>> + use<'_, 'db> {
5278        self.autoderef_(db).map(move |ty| self.derived(ty))
5279    }
5280
5281    fn autoderef_(&self, db: &'db dyn HirDatabase) -> impl Iterator<Item = Ty<'db>> {
5282        let interner = DbInterner::new_no_crate(db);
5283        // There should be no inference vars in types passed here
5284        let canonical = hir_ty::replace_errors_with_variables(interner, &self.ty);
5285        autoderef(db, self.env, canonical)
5286    }
5287
5288    // This would be nicer if it just returned an iterator, but that runs into
5289    // lifetime problems, because we need to borrow temp `CrateImplDefs`.
5290    pub fn iterate_assoc_items<T>(
5291        &self,
5292        db: &'db dyn HirDatabase,
5293        mut callback: impl FnMut(AssocItem) -> Option<T>,
5294    ) -> Option<T> {
5295        let mut slot = None;
5296        self.iterate_assoc_items_dyn(db, &mut |assoc_item_id| {
5297            slot = callback(assoc_item_id.into());
5298            slot.is_some()
5299        });
5300        slot
5301    }
5302
5303    fn iterate_assoc_items_dyn(
5304        &self,
5305        db: &'db dyn HirDatabase,
5306        callback: &mut dyn FnMut(AssocItemId) -> bool,
5307    ) {
5308        let mut handle_impls = |impls: &[ImplId]| {
5309            for &impl_def in impls {
5310                for &(_, item) in impl_def.impl_items(db).items.iter() {
5311                    if callback(item) {
5312                        return;
5313                    }
5314                }
5315            }
5316        };
5317
5318        let interner = DbInterner::new_no_crate(db);
5319        let Some(simplified_type) =
5320            fast_reject::simplify_type(interner, self.ty, fast_reject::TreatParams::AsRigid)
5321        else {
5322            return;
5323        };
5324
5325        handle_impls(method_resolution::incoherent_inherent_impls(db, simplified_type));
5326
5327        if let Some(module) = method_resolution::simplified_type_module(db, &simplified_type) {
5328            InherentImpls::for_each_crate_and_block(
5329                db,
5330                module.krate(db),
5331                module.block(db),
5332                &mut |impls| {
5333                    handle_impls(impls.for_self_ty(&simplified_type));
5334                },
5335            );
5336        }
5337    }
5338
5339    /// Iterates its type arguments
5340    ///
5341    /// It iterates the actual type arguments when concrete types are used
5342    /// and otherwise the generic names.
5343    /// It does not include `const` arguments.
5344    ///
5345    /// For code, such as:
5346    /// ```text
5347    /// struct Foo<T, U>
5348    ///
5349    /// impl<U> Foo<String, U>
5350    /// ```
5351    ///
5352    /// It iterates:
5353    /// ```text
5354    /// - "String"
5355    /// - "U"
5356    /// ```
5357    pub fn type_arguments(&self) -> impl Iterator<Item = Type<'db>> + '_ {
5358        match self.ty.strip_references().kind() {
5359            TyKind::Adt(_, substs) => Either::Left(substs.types().map(move |ty| self.derived(ty))),
5360            TyKind::Tuple(substs) => {
5361                Either::Right(Either::Left(substs.iter().map(move |ty| self.derived(ty))))
5362            }
5363            _ => Either::Right(Either::Right(std::iter::empty())),
5364        }
5365    }
5366
5367    /// Iterates its type and const arguments
5368    ///
5369    /// It iterates the actual type and const arguments when concrete types
5370    /// are used and otherwise the generic names.
5371    ///
5372    /// For code, such as:
5373    /// ```text
5374    /// struct Foo<T, const U: usize, const X: usize>
5375    ///
5376    /// impl<U> Foo<String, U, 12>
5377    /// ```
5378    ///
5379    /// It iterates:
5380    /// ```text
5381    /// - "String"
5382    /// - "U"
5383    /// - "12"
5384    /// ```
5385    pub fn type_and_const_arguments<'a>(
5386        &'a self,
5387        db: &'a dyn HirDatabase,
5388        display_target: DisplayTarget,
5389    ) -> impl Iterator<Item = SmolStr> + 'a {
5390        self.ty
5391            .strip_references()
5392            .as_adt()
5393            .into_iter()
5394            .flat_map(|(_, substs)| substs.iter())
5395            .filter_map(move |arg| match arg {
5396                GenericArg::Ty(ty) => Some(format_smolstr!("{}", ty.display(db, display_target))),
5397                GenericArg::Const(const_) => {
5398                    Some(format_smolstr!("{}", const_.display(db, display_target)))
5399                }
5400                GenericArg::Lifetime(_) => None,
5401            })
5402    }
5403
5404    /// Combines lifetime indicators, type and constant parameters into a single `Iterator`
5405    pub fn generic_parameters<'a>(
5406        &'a self,
5407        db: &'a dyn HirDatabase,
5408        display_target: DisplayTarget,
5409    ) -> impl Iterator<Item = SmolStr> + 'a {
5410        // iterate the lifetime
5411        self.as_adt()
5412            .and_then(|a| {
5413                // Lifetimes do not need edition-specific handling as they cannot be escaped.
5414                a.lifetime(db).map(|lt| lt.name.display_no_db(Edition::Edition2015).to_smolstr())
5415            })
5416            .into_iter()
5417            // add the type and const parameters
5418            .chain(self.type_and_const_arguments(db, display_target))
5419    }
5420
5421    pub fn iterate_method_candidates_with_traits<T>(
5422        &self,
5423        db: &'db dyn HirDatabase,
5424        scope: &SemanticsScope<'_>,
5425        traits_in_scope: &FxHashSet<TraitId>,
5426        name: Option<&Name>,
5427        mut callback: impl FnMut(Function) -> Option<T>,
5428    ) -> Option<T> {
5429        let _p = tracing::info_span!("iterate_method_candidates_with_traits").entered();
5430        let mut slot = None;
5431        self.iterate_method_candidates_split_inherent(db, scope, traits_in_scope, name, |f| {
5432            match callback(f) {
5433                it @ Some(_) => {
5434                    slot = it;
5435                    ControlFlow::Break(())
5436                }
5437                None => ControlFlow::Continue(()),
5438            }
5439        });
5440        slot
5441    }
5442
5443    pub fn iterate_method_candidates<T>(
5444        &self,
5445        db: &'db dyn HirDatabase,
5446        scope: &SemanticsScope<'_>,
5447        name: Option<&Name>,
5448        callback: impl FnMut(Function) -> Option<T>,
5449    ) -> Option<T> {
5450        self.iterate_method_candidates_with_traits(
5451            db,
5452            scope,
5453            &scope.visible_traits().0,
5454            name,
5455            callback,
5456        )
5457    }
5458
5459    fn with_method_resolution<R>(
5460        &self,
5461        db: &'db dyn HirDatabase,
5462        resolver: &Resolver<'db>,
5463        traits_in_scope: &FxHashSet<TraitId>,
5464        f: impl FnOnce(&MethodResolutionContext<'_, 'db>) -> R,
5465    ) -> R {
5466        let module = resolver.module();
5467        let interner = DbInterner::new_with(db, module.krate(db));
5468        let infcx = interner.infer_ctxt().build(TypingMode::PostAnalysis);
5469        let unstable_features =
5470            MethodResolutionUnstableFeatures::from_def_map(resolver.top_level_def_map());
5471        let environment = param_env_from_resolver(db, resolver);
5472        let ctx = MethodResolutionContext {
5473            infcx: &infcx,
5474            resolver,
5475            param_env: environment.param_env,
5476            traits_in_scope,
5477            edition: resolver.krate().data(db).edition,
5478            unstable_features: &unstable_features,
5479        };
5480        f(&ctx)
5481    }
5482
5483    /// Allows you to treat inherent and non-inherent methods differently.
5484    ///
5485    /// Note that inherent methods may actually be trait methods! For example, in `dyn Trait`, the trait's methods
5486    /// are considered inherent methods.
5487    pub fn iterate_method_candidates_split_inherent(
5488        &self,
5489        db: &'db dyn HirDatabase,
5490        scope: &SemanticsScope<'_>,
5491        traits_in_scope: &FxHashSet<TraitId>,
5492        name: Option<&Name>,
5493        mut callback: impl MethodCandidateCallback,
5494    ) {
5495        let _p = tracing::info_span!(
5496            "iterate_method_candidates_split_inherent",
5497            traits_in_scope = traits_in_scope.len(),
5498            ?name,
5499        )
5500        .entered();
5501
5502        self.with_method_resolution(db, scope.resolver(), traits_in_scope, |ctx| {
5503            // There should be no inference vars in types passed here
5504            let canonical = hir_ty::replace_errors_with_variables(ctx.infcx.interner, &self.ty);
5505            let (self_ty, _) = ctx.infcx.instantiate_canonical(&canonical);
5506
5507            match name {
5508                Some(name) => {
5509                    match ctx.probe_for_name(
5510                        method_resolution::Mode::MethodCall,
5511                        name.clone(),
5512                        self_ty,
5513                    ) {
5514                        Ok(candidate)
5515                        | Err(method_resolution::MethodError::PrivateMatch(candidate)) => {
5516                            let method_resolution::CandidateId::FunctionId(id) = candidate.item
5517                            else {
5518                                unreachable!("`Mode::MethodCall` can only return functions");
5519                            };
5520                            let id = Function { id };
5521                            match candidate.kind {
5522                                method_resolution::PickKind::InherentImplPick(_)
5523                                | method_resolution::PickKind::ObjectPick(..)
5524                                | method_resolution::PickKind::WhereClausePick(..) => {
5525                                    // Candidates from where clauses and trait objects are considered inherent.
5526                                    _ = callback.on_inherent_method(id);
5527                                }
5528                                method_resolution::PickKind::TraitPick(..) => {
5529                                    _ = callback.on_trait_method(id);
5530                                }
5531                            }
5532                        }
5533                        Err(_) => {}
5534                    };
5535                }
5536                None => {
5537                    _ = ctx.probe_all(method_resolution::Mode::MethodCall, self_ty).try_for_each(
5538                        |candidate| {
5539                            let method_resolution::CandidateId::FunctionId(id) =
5540                                candidate.candidate.item
5541                            else {
5542                                unreachable!("`Mode::MethodCall` can only return functions");
5543                            };
5544                            let id = Function { id };
5545                            match candidate.candidate.kind {
5546                                method_resolution::CandidateKind::InherentImplCandidate {
5547                                    ..
5548                                }
5549                                | method_resolution::CandidateKind::ObjectCandidate(..)
5550                                | method_resolution::CandidateKind::WhereClauseCandidate(..) => {
5551                                    // Candidates from where clauses and trait objects are considered inherent.
5552                                    callback.on_inherent_method(id)
5553                                }
5554                                method_resolution::CandidateKind::TraitCandidate(..) => {
5555                                    callback.on_trait_method(id)
5556                                }
5557                            }
5558                        },
5559                    );
5560                }
5561            }
5562        })
5563    }
5564
5565    #[tracing::instrument(skip_all, fields(name = ?name))]
5566    pub fn iterate_path_candidates<T>(
5567        &self,
5568        db: &'db dyn HirDatabase,
5569        scope: &SemanticsScope<'_>,
5570        traits_in_scope: &FxHashSet<TraitId>,
5571        name: Option<&Name>,
5572        mut callback: impl FnMut(AssocItem) -> Option<T>,
5573    ) -> Option<T> {
5574        let _p = tracing::info_span!("iterate_path_candidates").entered();
5575        let mut slot = None;
5576
5577        self.iterate_path_candidates_split_inherent(db, scope, traits_in_scope, name, |item| {
5578            match callback(item) {
5579                it @ Some(_) => {
5580                    slot = it;
5581                    ControlFlow::Break(())
5582                }
5583                None => ControlFlow::Continue(()),
5584            }
5585        });
5586        slot
5587    }
5588
5589    /// Iterates over inherent methods.
5590    ///
5591    /// In some circumstances, inherent methods methods may actually be trait methods!
5592    /// For example, when `dyn Trait` is a receiver, _trait_'s methods would be considered
5593    /// to be inherent methods.
5594    #[tracing::instrument(skip_all, fields(name = ?name))]
5595    pub fn iterate_path_candidates_split_inherent(
5596        &self,
5597        db: &'db dyn HirDatabase,
5598        scope: &SemanticsScope<'_>,
5599        traits_in_scope: &FxHashSet<TraitId>,
5600        name: Option<&Name>,
5601        mut callback: impl PathCandidateCallback,
5602    ) {
5603        let _p = tracing::info_span!(
5604            "iterate_path_candidates_split_inherent",
5605            traits_in_scope = traits_in_scope.len(),
5606            ?name,
5607        )
5608        .entered();
5609
5610        self.with_method_resolution(db, scope.resolver(), traits_in_scope, |ctx| {
5611            // There should be no inference vars in types passed here
5612            let canonical = hir_ty::replace_errors_with_variables(ctx.infcx.interner, &self.ty);
5613            let (self_ty, _) = ctx.infcx.instantiate_canonical(&canonical);
5614
5615            match name {
5616                Some(name) => {
5617                    match ctx.probe_for_name(
5618                        method_resolution::Mode::MethodCall,
5619                        name.clone(),
5620                        self_ty,
5621                    ) {
5622                        Ok(candidate)
5623                        | Err(method_resolution::MethodError::PrivateMatch(candidate)) => {
5624                            let id = candidate.item.into();
5625                            match candidate.kind {
5626                                method_resolution::PickKind::InherentImplPick(_)
5627                                | method_resolution::PickKind::ObjectPick(..)
5628                                | method_resolution::PickKind::WhereClausePick(..) => {
5629                                    // Candidates from where clauses and trait objects are considered inherent.
5630                                    _ = callback.on_inherent_item(id);
5631                                }
5632                                method_resolution::PickKind::TraitPick(..) => {
5633                                    _ = callback.on_trait_item(id);
5634                                }
5635                            }
5636                        }
5637                        Err(_) => {}
5638                    };
5639                }
5640                None => {
5641                    _ = ctx.probe_all(method_resolution::Mode::Path, self_ty).try_for_each(
5642                        |candidate| {
5643                            let id = candidate.candidate.item.into();
5644                            match candidate.candidate.kind {
5645                                method_resolution::CandidateKind::InherentImplCandidate {
5646                                    ..
5647                                }
5648                                | method_resolution::CandidateKind::ObjectCandidate(..)
5649                                | method_resolution::CandidateKind::WhereClauseCandidate(..) => {
5650                                    // Candidates from where clauses and trait objects are considered inherent.
5651                                    callback.on_inherent_item(id)
5652                                }
5653                                method_resolution::CandidateKind::TraitCandidate(..) => {
5654                                    callback.on_trait_item(id)
5655                                }
5656                            }
5657                        },
5658                    );
5659                }
5660            }
5661        })
5662    }
5663
5664    pub fn as_adt(&self) -> Option<Adt> {
5665        let (adt, _subst) = self.ty.as_adt()?;
5666        Some(adt.into())
5667    }
5668
5669    pub fn as_builtin(&self) -> Option<BuiltinType> {
5670        self.ty.as_builtin().map(|inner| BuiltinType { inner })
5671    }
5672
5673    pub fn as_dyn_trait(&self) -> Option<Trait> {
5674        self.ty.dyn_trait().map(Into::into)
5675    }
5676
5677    /// If a type can be represented as `dyn Trait`, returns all traits accessible via this type,
5678    /// or an empty iterator otherwise.
5679    pub fn applicable_inherent_traits(
5680        &self,
5681        db: &'db dyn HirDatabase,
5682    ) -> impl Iterator<Item = Trait> {
5683        let _p = tracing::info_span!("applicable_inherent_traits").entered();
5684        self.autoderef_(db)
5685            .filter_map(|ty| ty.dyn_trait())
5686            .flat_map(move |dyn_trait_id| hir_ty::all_super_traits(db, dyn_trait_id))
5687            .map(Trait::from)
5688    }
5689
5690    pub fn env_traits(&self, db: &'db dyn HirDatabase) -> impl Iterator<Item = Trait> {
5691        let _p = tracing::info_span!("env_traits").entered();
5692        self.autoderef_(db)
5693            .filter(|ty| matches!(ty.kind(), TyKind::Param(_)))
5694            .flat_map(|ty| {
5695                self.env
5696                    .param_env
5697                    .clauses()
5698                    .iter()
5699                    .filter_map(move |pred| match pred.kind().skip_binder() {
5700                        ClauseKind::Trait(tr) if tr.self_ty() == ty => Some(tr.def_id().0),
5701                        _ => None,
5702                    })
5703                    .flat_map(|t| hir_ty::all_super_traits(db, t))
5704            })
5705            .map(Trait::from)
5706    }
5707
5708    pub fn as_impl_traits(&self, db: &'db dyn HirDatabase) -> Option<impl Iterator<Item = Trait>> {
5709        self.ty.impl_trait_bounds(db).map(|it| {
5710            it.into_iter().filter_map(|pred| match pred.kind().skip_binder() {
5711                ClauseKind::Trait(trait_ref) => Some(Trait::from(trait_ref.def_id().0)),
5712                _ => None,
5713            })
5714        })
5715    }
5716
5717    pub fn as_associated_type_parent_trait(&self, db: &'db dyn HirDatabase) -> Option<Trait> {
5718        let TyKind::Alias(AliasTyKind::Projection, alias) = self.ty.kind() else { return None };
5719        match alias.def_id.expect_type_alias().loc(db).container {
5720            ItemContainerId::TraitId(id) => Some(Trait { id }),
5721            _ => None,
5722        }
5723    }
5724
5725    fn derived(&self, ty: Ty<'db>) -> Self {
5726        Type { env: self.env, ty }
5727    }
5728
5729    /// Visits every type, including generic arguments, in this type. `callback` is called with type
5730    /// itself first, and then with its generic arguments.
5731    pub fn walk(&self, db: &'db dyn HirDatabase, callback: impl FnMut(Type<'db>)) {
5732        struct Visitor<'db, F> {
5733            db: &'db dyn HirDatabase,
5734            env: ParamEnvAndCrate<'db>,
5735            callback: F,
5736            visited: FxHashSet<Ty<'db>>,
5737        }
5738        impl<'db, F> TypeVisitor<DbInterner<'db>> for Visitor<'db, F>
5739        where
5740            F: FnMut(Type<'db>),
5741        {
5742            type Result = ();
5743
5744            fn visit_ty(&mut self, ty: Ty<'db>) -> Self::Result {
5745                if !self.visited.insert(ty) {
5746                    return;
5747                }
5748
5749                (self.callback)(Type { env: self.env, ty });
5750
5751                if let Some(bounds) = ty.impl_trait_bounds(self.db) {
5752                    bounds.visit_with(self);
5753                }
5754
5755                ty.super_visit_with(self);
5756            }
5757        }
5758
5759        let mut visitor = Visitor { db, env: self.env, callback, visited: FxHashSet::default() };
5760        self.ty.visit_with(&mut visitor);
5761    }
5762    /// Check if type unifies with another type.
5763    ///
5764    /// Note that we consider placeholder types to unify with everything.
5765    /// For example `Option<T>` and `Option<U>` unify although there is unresolved goal `T = U`.
5766    pub fn could_unify_with(&self, db: &'db dyn HirDatabase, other: &Type<'db>) -> bool {
5767        let interner = DbInterner::new_no_crate(db);
5768        let tys = hir_ty::replace_errors_with_variables(interner, &(self.ty, other.ty));
5769        hir_ty::could_unify(db, self.env, &tys)
5770    }
5771
5772    /// Check if type unifies with another type eagerly making sure there are no unresolved goals.
5773    ///
5774    /// This means that placeholder types are not considered to unify if there are any bounds set on
5775    /// them. For example `Option<T>` and `Option<U>` do not unify as we cannot show that `T = U`
5776    pub fn could_unify_with_deeply(&self, db: &'db dyn HirDatabase, other: &Type<'db>) -> bool {
5777        let interner = DbInterner::new_no_crate(db);
5778        let tys = hir_ty::replace_errors_with_variables(interner, &(self.ty, other.ty));
5779        hir_ty::could_unify_deeply(db, self.env, &tys)
5780    }
5781
5782    pub fn could_coerce_to(&self, db: &'db dyn HirDatabase, to: &Type<'db>) -> bool {
5783        let interner = DbInterner::new_no_crate(db);
5784        let tys = hir_ty::replace_errors_with_variables(interner, &(self.ty, to.ty));
5785        hir_ty::could_coerce(db, self.env, &tys)
5786    }
5787
5788    pub fn as_type_param(&self, _db: &'db dyn HirDatabase) -> Option<TypeParam> {
5789        match self.ty.kind() {
5790            TyKind::Param(param) => Some(TypeParam { id: param.id }),
5791            _ => None,
5792        }
5793    }
5794
5795    /// Returns unique `GenericParam`s contained in this type.
5796    pub fn generic_params(&self, db: &'db dyn HirDatabase) -> FxHashSet<GenericParam> {
5797        hir_ty::collect_params(&self.ty)
5798            .into_iter()
5799            .map(|id| TypeOrConstParam { id }.split(db).either_into())
5800            .collect()
5801    }
5802
5803    pub fn layout(&self, db: &'db dyn HirDatabase) -> Result<Layout, LayoutError> {
5804        db.layout_of_ty(self.ty, self.env)
5805            .map(|layout| Layout(layout, db.target_data_layout(self.env.krate).unwrap()))
5806    }
5807
5808    pub fn drop_glue(&self, db: &'db dyn HirDatabase) -> DropGlue {
5809        let interner = DbInterner::new_with(db, self.env.krate);
5810        let infcx = interner.infer_ctxt().build(TypingMode::PostAnalysis);
5811        hir_ty::drop::has_drop_glue(&infcx, self.ty, self.env.param_env)
5812    }
5813}
5814
5815#[derive(Clone, PartialEq, Eq, Debug, Hash)]
5816pub struct TypeNs<'db> {
5817    env: ParamEnvAndCrate<'db>,
5818    ty: Ty<'db>,
5819}
5820
5821impl<'db> TypeNs<'db> {
5822    fn new(db: &'db dyn HirDatabase, lexical_env: impl HasResolver, ty: Ty<'db>) -> Self {
5823        let resolver = lexical_env.resolver(db);
5824        let environment = param_env_from_resolver(db, &resolver);
5825        TypeNs { env: environment, ty }
5826    }
5827
5828    pub fn to_type(&self, _db: &'db dyn HirDatabase) -> Type<'db> {
5829        Type { env: self.env, ty: self.ty }
5830    }
5831
5832    // FIXME: Find better API that also handles const generics
5833    pub fn impls_trait(&self, infcx: InferCtxt<'db>, trait_: Trait, args: &[TypeNs<'db>]) -> bool {
5834        let args = GenericArgs::new_from_iter(
5835            infcx.interner,
5836            [self.ty].into_iter().chain(args.iter().map(|t| t.ty)).map(|t| t.into()),
5837        );
5838        let trait_ref = hir_ty::next_solver::TraitRef::new(infcx.interner, trait_.id.into(), args);
5839
5840        let pred_kind = rustc_type_ir::Binder::dummy(rustc_type_ir::PredicateKind::Clause(
5841            rustc_type_ir::ClauseKind::Trait(rustc_type_ir::TraitPredicate {
5842                trait_ref,
5843                polarity: rustc_type_ir::PredicatePolarity::Positive,
5844            }),
5845        ));
5846        let predicate = hir_ty::next_solver::Predicate::new(infcx.interner, pred_kind);
5847        let goal = hir_ty::next_solver::Goal::new(
5848            infcx.interner,
5849            hir_ty::next_solver::ParamEnv::empty(),
5850            predicate,
5851        );
5852        let res = hir_ty::traits::next_trait_solve_in_ctxt(&infcx, goal);
5853        res.map_or(false, |res| matches!(res.1, rustc_type_ir::solve::Certainty::Yes))
5854    }
5855
5856    pub fn is_bool(&self) -> bool {
5857        matches!(self.ty.kind(), rustc_type_ir::TyKind::Bool)
5858    }
5859}
5860
5861#[derive(Debug, PartialEq, Eq, Copy, Clone, Hash)]
5862pub struct InlineAsmOperand {
5863    owner: DefWithBodyId,
5864    expr: ExprId,
5865    index: usize,
5866}
5867
5868impl InlineAsmOperand {
5869    pub fn parent(self, _db: &dyn HirDatabase) -> DefWithBody {
5870        self.owner.into()
5871    }
5872
5873    pub fn name(&self, db: &dyn HirDatabase) -> Option<Name> {
5874        match &db.body(self.owner)[self.expr] {
5875            hir_def::hir::Expr::InlineAsm(e) => e.operands.get(self.index)?.0.clone(),
5876            _ => None,
5877        }
5878    }
5879}
5880
5881// FIXME: Document this
5882#[derive(Debug)]
5883pub struct Callable<'db> {
5884    ty: Type<'db>,
5885    sig: PolyFnSig<'db>,
5886    callee: Callee<'db>,
5887    /// Whether this is a method that was called with method call syntax.
5888    is_bound_method: bool,
5889}
5890
5891#[derive(Clone, PartialEq, Eq, Hash, Debug)]
5892enum Callee<'db> {
5893    Def(CallableDefId),
5894    Closure(InternedClosureId, GenericArgs<'db>),
5895    CoroutineClosure(InternedCoroutineId, GenericArgs<'db>),
5896    FnPtr,
5897    FnImpl(traits::FnTrait),
5898}
5899
5900pub enum CallableKind<'db> {
5901    Function(Function),
5902    TupleStruct(Struct),
5903    TupleEnumVariant(Variant),
5904    Closure(Closure<'db>),
5905    FnPtr,
5906    FnImpl(FnTrait),
5907}
5908
5909impl<'db> Callable<'db> {
5910    pub fn kind(&self) -> CallableKind<'db> {
5911        match self.callee {
5912            Callee::Def(CallableDefId::FunctionId(it)) => CallableKind::Function(it.into()),
5913            Callee::Def(CallableDefId::StructId(it)) => CallableKind::TupleStruct(it.into()),
5914            Callee::Def(CallableDefId::EnumVariantId(it)) => {
5915                CallableKind::TupleEnumVariant(it.into())
5916            }
5917            Callee::Closure(id, subst) => {
5918                CallableKind::Closure(Closure { id: AnyClosureId::ClosureId(id), subst })
5919            }
5920            Callee::CoroutineClosure(id, subst) => {
5921                CallableKind::Closure(Closure { id: AnyClosureId::CoroutineClosureId(id), subst })
5922            }
5923            Callee::FnPtr => CallableKind::FnPtr,
5924            Callee::FnImpl(fn_) => CallableKind::FnImpl(fn_.into()),
5925        }
5926    }
5927    pub fn receiver_param(&self, db: &'db dyn HirDatabase) -> Option<(SelfParam, Type<'db>)> {
5928        let func = match self.callee {
5929            Callee::Def(CallableDefId::FunctionId(it)) if self.is_bound_method => it,
5930            _ => return None,
5931        };
5932        let func = Function { id: func };
5933        Some((
5934            func.self_param(db)?,
5935            self.ty.derived(self.sig.skip_binder().inputs_and_output.inputs()[0]),
5936        ))
5937    }
5938    pub fn n_params(&self) -> usize {
5939        self.sig.skip_binder().inputs_and_output.inputs().len()
5940            - if self.is_bound_method { 1 } else { 0 }
5941    }
5942    pub fn params(&self) -> Vec<Param<'db>> {
5943        self.sig
5944            .skip_binder()
5945            .inputs_and_output
5946            .inputs()
5947            .iter()
5948            .enumerate()
5949            .skip(if self.is_bound_method { 1 } else { 0 })
5950            .map(|(idx, ty)| (idx, self.ty.derived(*ty)))
5951            .map(|(idx, ty)| Param { func: self.callee.clone(), idx, ty })
5952            .collect()
5953    }
5954    pub fn return_type(&self) -> Type<'db> {
5955        self.ty.derived(self.sig.skip_binder().output())
5956    }
5957    pub fn sig(&self) -> impl Eq {
5958        &self.sig
5959    }
5960
5961    pub fn ty(&self) -> &Type<'db> {
5962        &self.ty
5963    }
5964}
5965
5966#[derive(Clone, Debug, Eq, PartialEq)]
5967pub struct Layout(Arc<TyLayout>, Arc<TargetDataLayout>);
5968
5969impl Layout {
5970    pub fn size(&self) -> u64 {
5971        self.0.size.bytes()
5972    }
5973
5974    pub fn align(&self) -> u64 {
5975        self.0.align.bytes()
5976    }
5977
5978    pub fn niches(&self) -> Option<u128> {
5979        Some(self.0.largest_niche?.available(&*self.1))
5980    }
5981
5982    pub fn field_offset(&self, field: Field) -> Option<u64> {
5983        match self.0.fields {
5984            layout::FieldsShape::Primitive => None,
5985            layout::FieldsShape::Union(_) => Some(0),
5986            layout::FieldsShape::Array { stride, count } => {
5987                let i = u64::try_from(field.index()).ok()?;
5988                (i < count).then_some((stride * i).bytes())
5989            }
5990            layout::FieldsShape::Arbitrary { ref offsets, .. } => {
5991                Some(offsets.get(RustcFieldIdx(field.id))?.bytes())
5992            }
5993        }
5994    }
5995
5996    pub fn tuple_field_offset(&self, field: usize) -> Option<u64> {
5997        match self.0.fields {
5998            layout::FieldsShape::Primitive => None,
5999            layout::FieldsShape::Union(_) => Some(0),
6000            layout::FieldsShape::Array { stride, count } => {
6001                let i = u64::try_from(field).ok()?;
6002                (i < count).then_some((stride * i).bytes())
6003            }
6004            layout::FieldsShape::Arbitrary { ref offsets, .. } => {
6005                Some(offsets.get(RustcFieldIdx::new(field))?.bytes())
6006            }
6007        }
6008    }
6009
6010    pub fn tail_padding(&self, field_size: &mut impl FnMut(usize) -> Option<u64>) -> Option<u64> {
6011        match self.0.fields {
6012            layout::FieldsShape::Primitive => None,
6013            layout::FieldsShape::Union(_) => None,
6014            layout::FieldsShape::Array { stride, count } => count.checked_sub(1).and_then(|tail| {
6015                let tail_field_size = field_size(tail as usize)?;
6016                let offset = stride.bytes() * tail;
6017                self.0.size.bytes().checked_sub(offset)?.checked_sub(tail_field_size)
6018            }),
6019            layout::FieldsShape::Arbitrary { ref offsets, ref memory_index } => {
6020                let tail = memory_index.last_index()?;
6021                let tail_field_size = field_size(tail.0.into_raw().into_u32() as usize)?;
6022                let offset = offsets.get(tail)?.bytes();
6023                self.0.size.bytes().checked_sub(offset)?.checked_sub(tail_field_size)
6024            }
6025        }
6026    }
6027
6028    pub fn largest_padding(
6029        &self,
6030        field_size: &mut impl FnMut(usize) -> Option<u64>,
6031    ) -> Option<u64> {
6032        match self.0.fields {
6033            layout::FieldsShape::Primitive => None,
6034            layout::FieldsShape::Union(_) => None,
6035            layout::FieldsShape::Array { stride: _, count: 0 } => None,
6036            layout::FieldsShape::Array { stride, .. } => {
6037                let size = field_size(0)?;
6038                stride.bytes().checked_sub(size)
6039            }
6040            layout::FieldsShape::Arbitrary { ref offsets, ref memory_index } => {
6041                let mut reverse_index = vec![None; memory_index.len()];
6042                for (src, (mem, offset)) in memory_index.iter().zip(offsets.iter()).enumerate() {
6043                    reverse_index[*mem as usize] = Some((src, offset.bytes()));
6044                }
6045                if reverse_index.iter().any(|it| it.is_none()) {
6046                    stdx::never!();
6047                    return None;
6048                }
6049                reverse_index
6050                    .into_iter()
6051                    .flatten()
6052                    .chain(std::iter::once((0, self.0.size.bytes())))
6053                    .tuple_windows()
6054                    .filter_map(|((i, start), (_, end))| {
6055                        let size = field_size(i)?;
6056                        end.checked_sub(start)?.checked_sub(size)
6057                    })
6058                    .max()
6059            }
6060        }
6061    }
6062
6063    pub fn enum_tag_size(&self) -> Option<usize> {
6064        let tag_size =
6065            if let layout::Variants::Multiple { tag, tag_encoding, .. } = &self.0.variants {
6066                match tag_encoding {
6067                    TagEncoding::Direct => tag.size(&*self.1).bytes_usize(),
6068                    TagEncoding::Niche { .. } => 0,
6069                }
6070            } else {
6071                return None;
6072            };
6073        Some(tag_size)
6074    }
6075}
6076
6077#[derive(Copy, Clone, Debug, Eq, PartialEq)]
6078pub enum BindingMode {
6079    Move,
6080    Ref(Mutability),
6081}
6082
6083/// For IDE only
6084#[derive(Copy, Clone, Debug, PartialEq, Eq, Hash)]
6085pub enum ScopeDef {
6086    ModuleDef(ModuleDef),
6087    GenericParam(GenericParam),
6088    ImplSelfType(Impl),
6089    AdtSelfType(Adt),
6090    Local(Local),
6091    Label(Label),
6092    Unknown,
6093}
6094
6095impl ScopeDef {
6096    pub fn all_items(def: PerNs) -> ArrayVec<Self, 3> {
6097        let mut items = ArrayVec::new();
6098
6099        match (def.take_types(), def.take_values()) {
6100            (Some(m1), None) => items.push(ScopeDef::ModuleDef(m1.into())),
6101            (None, Some(m2)) => items.push(ScopeDef::ModuleDef(m2.into())),
6102            (Some(m1), Some(m2)) => {
6103                // Some items, like unit structs and enum variants, are
6104                // returned as both a type and a value. Here we want
6105                // to de-duplicate them.
6106                if m1 != m2 {
6107                    items.push(ScopeDef::ModuleDef(m1.into()));
6108                    items.push(ScopeDef::ModuleDef(m2.into()));
6109                } else {
6110                    items.push(ScopeDef::ModuleDef(m1.into()));
6111                }
6112            }
6113            (None, None) => {}
6114        };
6115
6116        if let Some(macro_def_id) = def.take_macros() {
6117            items.push(ScopeDef::ModuleDef(ModuleDef::Macro(macro_def_id.into())));
6118        }
6119
6120        if items.is_empty() {
6121            items.push(ScopeDef::Unknown);
6122        }
6123
6124        items
6125    }
6126
6127    pub fn attrs(&self, db: &dyn HirDatabase) -> Option<AttrsWithOwner> {
6128        match self {
6129            ScopeDef::ModuleDef(it) => it.attrs(db),
6130            ScopeDef::GenericParam(it) => Some(it.attrs(db)),
6131            ScopeDef::ImplSelfType(_)
6132            | ScopeDef::AdtSelfType(_)
6133            | ScopeDef::Local(_)
6134            | ScopeDef::Label(_)
6135            | ScopeDef::Unknown => None,
6136        }
6137    }
6138
6139    pub fn krate(&self, db: &dyn HirDatabase) -> Option<Crate> {
6140        match self {
6141            ScopeDef::ModuleDef(it) => it.module(db).map(|m| m.krate(db)),
6142            ScopeDef::GenericParam(it) => Some(it.module(db).krate(db)),
6143            ScopeDef::ImplSelfType(_) => None,
6144            ScopeDef::AdtSelfType(it) => Some(it.module(db).krate(db)),
6145            ScopeDef::Local(it) => Some(it.module(db).krate(db)),
6146            ScopeDef::Label(it) => Some(it.module(db).krate(db)),
6147            ScopeDef::Unknown => None,
6148        }
6149    }
6150}
6151
6152impl From<ItemInNs> for ScopeDef {
6153    fn from(item: ItemInNs) -> Self {
6154        match item {
6155            ItemInNs::Types(id) => ScopeDef::ModuleDef(id),
6156            ItemInNs::Values(id) => ScopeDef::ModuleDef(id),
6157            ItemInNs::Macros(id) => ScopeDef::ModuleDef(ModuleDef::Macro(id)),
6158        }
6159    }
6160}
6161
6162#[derive(Clone, Debug, PartialEq, Eq)]
6163pub struct Adjustment<'db> {
6164    pub source: Type<'db>,
6165    pub target: Type<'db>,
6166    pub kind: Adjust,
6167}
6168
6169#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)]
6170pub enum Adjust {
6171    /// Go from ! to any type.
6172    NeverToAny,
6173    /// Dereference once, producing a place.
6174    Deref(Option<OverloadedDeref>),
6175    /// Take the address and produce either a `&` or `*` pointer.
6176    Borrow(AutoBorrow),
6177    Pointer(PointerCast),
6178}
6179
6180#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)]
6181pub enum AutoBorrow {
6182    /// Converts from T to &T.
6183    Ref(Mutability),
6184    /// Converts from T to *T.
6185    RawPtr(Mutability),
6186}
6187
6188#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)]
6189pub struct OverloadedDeref(pub Mutability);
6190
6191pub trait HasVisibility {
6192    fn visibility(&self, db: &dyn HirDatabase) -> Visibility;
6193    fn is_visible_from(&self, db: &dyn HirDatabase, module: Module) -> bool {
6194        let vis = self.visibility(db);
6195        vis.is_visible_from(db, module.id)
6196    }
6197}
6198
6199/// Trait for obtaining the defining crate of an item.
6200pub trait HasCrate {
6201    fn krate(&self, db: &dyn HirDatabase) -> Crate;
6202}
6203
6204impl<T: hir_def::HasModule> HasCrate for T {
6205    fn krate(&self, db: &dyn HirDatabase) -> Crate {
6206        self.module(db).krate(db).into()
6207    }
6208}
6209
6210impl HasCrate for AssocItem {
6211    fn krate(&self, db: &dyn HirDatabase) -> Crate {
6212        self.module(db).krate(db)
6213    }
6214}
6215
6216impl HasCrate for Struct {
6217    fn krate(&self, db: &dyn HirDatabase) -> Crate {
6218        self.module(db).krate(db)
6219    }
6220}
6221
6222impl HasCrate for Union {
6223    fn krate(&self, db: &dyn HirDatabase) -> Crate {
6224        self.module(db).krate(db)
6225    }
6226}
6227
6228impl HasCrate for Enum {
6229    fn krate(&self, db: &dyn HirDatabase) -> Crate {
6230        self.module(db).krate(db)
6231    }
6232}
6233
6234impl HasCrate for Field {
6235    fn krate(&self, db: &dyn HirDatabase) -> Crate {
6236        self.parent_def(db).module(db).krate(db)
6237    }
6238}
6239
6240impl HasCrate for Variant {
6241    fn krate(&self, db: &dyn HirDatabase) -> Crate {
6242        self.module(db).krate(db)
6243    }
6244}
6245
6246impl HasCrate for Function {
6247    fn krate(&self, db: &dyn HirDatabase) -> Crate {
6248        self.module(db).krate(db)
6249    }
6250}
6251
6252impl HasCrate for Const {
6253    fn krate(&self, db: &dyn HirDatabase) -> Crate {
6254        self.module(db).krate(db)
6255    }
6256}
6257
6258impl HasCrate for TypeAlias {
6259    fn krate(&self, db: &dyn HirDatabase) -> Crate {
6260        self.module(db).krate(db)
6261    }
6262}
6263
6264impl HasCrate for Type<'_> {
6265    fn krate(&self, _db: &dyn HirDatabase) -> Crate {
6266        self.env.krate.into()
6267    }
6268}
6269
6270impl HasCrate for Macro {
6271    fn krate(&self, db: &dyn HirDatabase) -> Crate {
6272        self.module(db).krate(db)
6273    }
6274}
6275
6276impl HasCrate for Trait {
6277    fn krate(&self, db: &dyn HirDatabase) -> Crate {
6278        self.module(db).krate(db)
6279    }
6280}
6281
6282impl HasCrate for Static {
6283    fn krate(&self, db: &dyn HirDatabase) -> Crate {
6284        self.module(db).krate(db)
6285    }
6286}
6287
6288impl HasCrate for Adt {
6289    fn krate(&self, db: &dyn HirDatabase) -> Crate {
6290        self.module(db).krate(db)
6291    }
6292}
6293
6294impl HasCrate for Impl {
6295    fn krate(&self, db: &dyn HirDatabase) -> Crate {
6296        self.module(db).krate(db)
6297    }
6298}
6299
6300impl HasCrate for Module {
6301    fn krate(&self, db: &dyn HirDatabase) -> Crate {
6302        Module::krate(*self, db)
6303    }
6304}
6305
6306pub trait HasContainer {
6307    fn container(&self, db: &dyn HirDatabase) -> ItemContainer;
6308}
6309
6310impl HasContainer for ExternCrateDecl {
6311    fn container(&self, db: &dyn HirDatabase) -> ItemContainer {
6312        container_id_to_hir(self.id.lookup(db).container.into())
6313    }
6314}
6315
6316impl HasContainer for Module {
6317    fn container(&self, db: &dyn HirDatabase) -> ItemContainer {
6318        // FIXME: handle block expressions as modules (their parent is in a different DefMap)
6319        let def_map = self.id.def_map(db);
6320        match def_map[self.id].parent {
6321            Some(parent_id) => ItemContainer::Module(Module { id: parent_id }),
6322            None => ItemContainer::Crate(def_map.krate().into()),
6323        }
6324    }
6325}
6326
6327impl HasContainer for Function {
6328    fn container(&self, db: &dyn HirDatabase) -> ItemContainer {
6329        container_id_to_hir(self.id.lookup(db).container)
6330    }
6331}
6332
6333impl HasContainer for Struct {
6334    fn container(&self, db: &dyn HirDatabase) -> ItemContainer {
6335        ItemContainer::Module(Module { id: self.id.lookup(db).container })
6336    }
6337}
6338
6339impl HasContainer for Union {
6340    fn container(&self, db: &dyn HirDatabase) -> ItemContainer {
6341        ItemContainer::Module(Module { id: self.id.lookup(db).container })
6342    }
6343}
6344
6345impl HasContainer for Enum {
6346    fn container(&self, db: &dyn HirDatabase) -> ItemContainer {
6347        ItemContainer::Module(Module { id: self.id.lookup(db).container })
6348    }
6349}
6350
6351impl HasContainer for TypeAlias {
6352    fn container(&self, db: &dyn HirDatabase) -> ItemContainer {
6353        container_id_to_hir(self.id.lookup(db).container)
6354    }
6355}
6356
6357impl HasContainer for Const {
6358    fn container(&self, db: &dyn HirDatabase) -> ItemContainer {
6359        container_id_to_hir(self.id.lookup(db).container)
6360    }
6361}
6362
6363impl HasContainer for Static {
6364    fn container(&self, db: &dyn HirDatabase) -> ItemContainer {
6365        container_id_to_hir(self.id.lookup(db).container)
6366    }
6367}
6368
6369impl HasContainer for Trait {
6370    fn container(&self, db: &dyn HirDatabase) -> ItemContainer {
6371        ItemContainer::Module(Module { id: self.id.lookup(db).container })
6372    }
6373}
6374
6375impl HasContainer for ExternBlock {
6376    fn container(&self, db: &dyn HirDatabase) -> ItemContainer {
6377        ItemContainer::Module(Module { id: self.id.lookup(db).container })
6378    }
6379}
6380
6381fn container_id_to_hir(c: ItemContainerId) -> ItemContainer {
6382    match c {
6383        ItemContainerId::ExternBlockId(id) => ItemContainer::ExternBlock(ExternBlock { id }),
6384        ItemContainerId::ModuleId(id) => ItemContainer::Module(Module { id }),
6385        ItemContainerId::ImplId(id) => ItemContainer::Impl(Impl { id }),
6386        ItemContainerId::TraitId(id) => ItemContainer::Trait(Trait { id }),
6387    }
6388}
6389
6390#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
6391pub enum ItemContainer {
6392    Trait(Trait),
6393    Impl(Impl),
6394    Module(Module),
6395    ExternBlock(ExternBlock),
6396    Crate(Crate),
6397}
6398
6399/// Subset of `ide_db::Definition` that doc links can resolve to.
6400pub enum DocLinkDef {
6401    ModuleDef(ModuleDef),
6402    Field(Field),
6403    SelfType(Trait),
6404}
6405
6406fn push_ty_diagnostics<'db>(
6407    db: &'db dyn HirDatabase,
6408    acc: &mut Vec<AnyDiagnostic<'db>>,
6409    diagnostics: Option<ThinArc<(), TyLoweringDiagnostic>>,
6410    source_map: &ExpressionStoreSourceMap,
6411) {
6412    if let Some(diagnostics) = diagnostics {
6413        acc.extend(
6414            diagnostics
6415                .slice
6416                .iter()
6417                .filter_map(|diagnostic| AnyDiagnostic::ty_diagnostic(diagnostic, source_map, db)),
6418        );
6419    }
6420}
6421
6422pub trait MethodCandidateCallback {
6423    fn on_inherent_method(&mut self, f: Function) -> ControlFlow<()>;
6424
6425    fn on_trait_method(&mut self, f: Function) -> ControlFlow<()>;
6426}
6427
6428impl<F> MethodCandidateCallback for F
6429where
6430    F: FnMut(Function) -> ControlFlow<()>,
6431{
6432    fn on_inherent_method(&mut self, f: Function) -> ControlFlow<()> {
6433        self(f)
6434    }
6435
6436    fn on_trait_method(&mut self, f: Function) -> ControlFlow<()> {
6437        self(f)
6438    }
6439}
6440
6441pub trait PathCandidateCallback {
6442    fn on_inherent_item(&mut self, item: AssocItem) -> ControlFlow<()>;
6443
6444    fn on_trait_item(&mut self, item: AssocItem) -> ControlFlow<()>;
6445}
6446
6447impl<F> PathCandidateCallback for F
6448where
6449    F: FnMut(AssocItem) -> ControlFlow<()>,
6450{
6451    fn on_inherent_item(&mut self, item: AssocItem) -> ControlFlow<()> {
6452        self(item)
6453    }
6454
6455    fn on_trait_item(&mut self, item: AssocItem) -> ControlFlow<()> {
6456        self(item)
6457    }
6458}
6459
6460pub fn resolve_absolute_path<'a, I: Iterator<Item = Symbol> + Clone + 'a>(
6461    db: &'a dyn HirDatabase,
6462    mut segments: I,
6463) -> impl Iterator<Item = ItemInNs> + use<'a, I> {
6464    segments
6465        .next()
6466        .into_iter()
6467        .flat_map(move |crate_name| {
6468            db.all_crates()
6469                .iter()
6470                .filter(|&krate| {
6471                    krate
6472                        .extra_data(db)
6473                        .display_name
6474                        .as_ref()
6475                        .is_some_and(|name| *name.crate_name().symbol() == crate_name)
6476                })
6477                .filter_map(|&krate| {
6478                    let segments = segments.clone();
6479                    let mut def_map = crate_def_map(db, krate);
6480                    let mut module = &def_map[def_map.root_module_id()];
6481                    let mut segments = segments.with_position().peekable();
6482                    while let Some((_, segment)) = segments.next_if(|&(position, _)| {
6483                        !matches!(position, itertools::Position::Last | itertools::Position::Only)
6484                    }) {
6485                        let res = module
6486                            .scope
6487                            .get(&Name::new_symbol_root(segment))
6488                            .take_types()
6489                            .and_then(|res| match res {
6490                                ModuleDefId::ModuleId(it) => Some(it),
6491                                _ => None,
6492                            })?;
6493                        def_map = res.def_map(db);
6494                        module = &def_map[res];
6495                    }
6496                    let (_, item_name) = segments.next()?;
6497                    let res = module.scope.get(&Name::new_symbol_root(item_name));
6498                    Some(res.iter_items().map(|(item, _)| item.into()))
6499                })
6500                .collect::<Vec<_>>()
6501        })
6502        .flatten()
6503}
6504
6505fn as_name_opt(name: Option<impl AsName>) -> Name {
6506    name.map_or_else(Name::missing, |name| name.as_name())
6507}
6508
6509fn generic_args_from_tys<'db>(
6510    interner: DbInterner<'db>,
6511    def_id: SolverDefId,
6512    args: impl IntoIterator<Item = Ty<'db>>,
6513) -> GenericArgs<'db> {
6514    let mut args = args.into_iter();
6515    GenericArgs::for_item(interner, def_id, |_, id, _| {
6516        if matches!(id, GenericParamId::TypeParamId(_))
6517            && let Some(arg) = args.next()
6518        {
6519            arg.into()
6520        } else {
6521            next_solver::GenericArg::error_from_id(interner, id)
6522        }
6523    })
6524}
6525
6526fn has_non_default_type_params(db: &dyn HirDatabase, generic_def: GenericDefId) -> bool {
6527    let params = db.generic_params(generic_def);
6528    let defaults = db.generic_defaults(generic_def);
6529    params
6530        .iter_type_or_consts()
6531        .filter(|(_, param)| matches!(param, TypeOrConstParamData::TypeParamData(_)))
6532        .map(|(local_id, _)| TypeOrConstParamId { parent: generic_def, local_id })
6533        .any(|param| {
6534            let Some(param) = hir_ty::param_idx(db, param) else {
6535                return false;
6536            };
6537            defaults.get(param).is_none()
6538        })
6539}
6540
6541fn param_env_from_resolver<'db>(
6542    db: &'db dyn HirDatabase,
6543    resolver: &Resolver<'_>,
6544) -> ParamEnvAndCrate<'db> {
6545    ParamEnvAndCrate {
6546        param_env: resolver
6547            .generic_def()
6548            .map_or_else(ParamEnv::empty, |generic_def| db.trait_environment(generic_def)),
6549        krate: resolver.krate(),
6550    }
6551}
6552
6553fn param_env_from_has_crate<'db>(
6554    db: &'db dyn HirDatabase,
6555    id: impl hir_def::HasModule + Into<GenericDefId> + Copy,
6556) -> ParamEnvAndCrate<'db> {
6557    ParamEnvAndCrate { param_env: db.trait_environment(id.into()), krate: id.krate(db) }
6558}
6559
6560fn body_param_env_from_has_crate<'db>(
6561    db: &'db dyn HirDatabase,
6562    id: impl hir_def::HasModule + Into<DefWithBodyId> + Copy,
6563) -> ParamEnvAndCrate<'db> {
6564    ParamEnvAndCrate { param_env: db.trait_environment_for_body(id.into()), krate: id.krate(db) }
6565}
6566
6567fn empty_param_env<'db>(krate: base_db::Crate) -> ParamEnvAndCrate<'db> {
6568    ParamEnvAndCrate { param_env: ParamEnv::empty(), krate }
6569}
6570
6571pub use hir_ty::next_solver;
6572pub use hir_ty::setup_tracing;