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