1 //! FIXME: write short doc here
7 builtin_type::BuiltinType,
9 expr::{BindingAnnotation, Pat, PatId},
10 nameres::ModuleSource,
12 resolver::HasResolver,
13 type_ref::{Mutability, TypeRef},
14 AdtId, ConstId, DefWithBodyId, EnumId, FunctionId, HasModule, ImplId, LocalEnumVariantId,
15 LocalModuleId, LocalStructFieldId, Lookup, ModuleId, StaticId, StructId, TraitId, TypeAliasId,
19 diagnostics::DiagnosticSink,
24 autoderef, display::HirFormatter, expr::ExprValidator, ApplicationTy, Canonical, InEnvironment,
25 TraitEnvironment, Ty, TyDefId, TypeCtor, TypeWalk,
27 use ra_db::{CrateId, Edition, FileId};
31 db::{DefDatabase, HirDatabase},
32 CallableDef, HirDisplay, InFile, Name,
35 /// hir::Crate describes a single crate. It's the main interface with which
36 /// a crate's dependencies interact. Mostly, it should be just a proxy for the
38 #[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
40 pub(crate) id: CrateId,
44 pub struct CrateDependency {
50 pub fn dependencies(self, db: &impl DefDatabase) -> Vec<CrateDependency> {
52 .dependencies(self.id)
54 let krate = Crate { id: dep.crate_id() };
55 let name = dep.as_name();
56 CrateDependency { krate, name }
61 // FIXME: add `transitive_reverse_dependencies`.
62 pub fn reverse_dependencies(self, db: &impl DefDatabase) -> Vec<Crate> {
63 let crate_graph = db.crate_graph();
66 .filter(|&krate| crate_graph.dependencies(krate).any(|it| it.crate_id == self.id))
67 .map(|id| Crate { id })
71 pub fn root_module(self, db: &impl DefDatabase) -> Option<Module> {
72 let module_id = db.crate_def_map(self.id).root;
73 Some(Module::new(self, module_id))
76 pub fn root_file(self, db: &impl DefDatabase) -> FileId {
77 db.crate_graph().crate_root(self.id)
80 pub fn edition(self, db: &impl DefDatabase) -> Edition {
81 let crate_graph = db.crate_graph();
82 crate_graph.edition(self.id)
85 pub fn all(db: &impl DefDatabase) -> Vec<Crate> {
86 db.crate_graph().iter().map(|id| Crate { id }).collect()
90 #[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
92 pub(crate) id: ModuleId,
95 /// The defs which can be visible in the module.
96 #[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
101 // Can't be directly declared, but can be imported.
102 EnumVariant(EnumVariant),
106 TypeAlias(TypeAlias),
107 BuiltinType(BuiltinType),
112 Adt(Struct, Enum, Union),
121 pub use hir_def::attr::Attrs;
124 pub(crate) fn new(krate: Crate, crate_module_id: LocalModuleId) -> Module {
125 Module { id: ModuleId { krate: krate.id, local_id: crate_module_id } }
128 /// Name of this module.
129 pub fn name(self, db: &impl DefDatabase) -> Option<Name> {
130 let def_map = db.crate_def_map(self.id.krate);
131 let parent = def_map[self.id.local_id].parent?;
132 def_map[parent].children.iter().find_map(|(name, module_id)| {
133 if *module_id == self.id.local_id {
141 /// Returns the crate this module is part of.
142 pub fn krate(self) -> Crate {
143 Crate { id: self.id.krate }
146 /// Topmost parent of this module. Every module has a `crate_root`, but some
147 /// might be missing `krate`. This can happen if a module's file is not included
148 /// in the module tree of any target in `Cargo.toml`.
149 pub fn crate_root(self, db: &impl DefDatabase) -> Module {
150 let def_map = db.crate_def_map(self.id.krate);
151 self.with_module_id(def_map.root)
154 /// Iterates over all child modules.
155 pub fn children(self, db: &impl DefDatabase) -> impl Iterator<Item = Module> {
156 let def_map = db.crate_def_map(self.id.krate);
157 let children = def_map[self.id.local_id]
160 .map(|(_, module_id)| self.with_module_id(*module_id))
161 .collect::<Vec<_>>();
165 /// Finds a parent module.
166 pub fn parent(self, db: &impl DefDatabase) -> Option<Module> {
167 let def_map = db.crate_def_map(self.id.krate);
168 let parent_id = def_map[self.id.local_id].parent?;
169 Some(self.with_module_id(parent_id))
172 pub fn path_to_root(self, db: &impl HirDatabase) -> Vec<Module> {
173 let mut res = vec![self];
175 while let Some(next) = curr.parent(db) {
182 /// Returns a `ModuleScope`: a set of items, visible in this module.
183 pub fn scope(self, db: &impl HirDatabase) -> Vec<(Name, ScopeDef)> {
184 db.crate_def_map(self.id.krate)[self.id.local_id]
187 .map(|(name, res)| (name.clone(), res.def.into()))
191 pub fn diagnostics(self, db: &impl HirDatabase, sink: &mut DiagnosticSink) {
192 db.crate_def_map(self.id.krate).add_diagnostics(db, self.id.local_id, sink);
193 for decl in self.declarations(db) {
195 crate::ModuleDef::Function(f) => f.diagnostics(db, sink),
196 crate::ModuleDef::Module(m) => {
197 // Only add diagnostics from inline modules
198 if let ModuleSource::Module(_) = m.definition_source(db).value {
199 m.diagnostics(db, sink)
206 for impl_block in self.impl_blocks(db) {
207 for item in impl_block.items(db) {
208 if let AssocItem::Function(f) = item {
209 f.diagnostics(db, sink);
215 pub fn declarations(self, db: &impl DefDatabase) -> Vec<ModuleDef> {
216 let def_map = db.crate_def_map(self.id.krate);
217 def_map[self.id.local_id].scope.declarations().map(ModuleDef::from).collect()
220 pub fn impl_blocks(self, db: &impl DefDatabase) -> Vec<ImplBlock> {
221 let def_map = db.crate_def_map(self.id.krate);
222 def_map[self.id.local_id].scope.impls().map(ImplBlock::from).collect()
225 pub(crate) fn with_module_id(self, module_id: LocalModuleId) -> Module {
226 Module::new(self.krate(), module_id)
230 #[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
231 pub struct StructField {
232 pub(crate) parent: VariantDef,
233 pub(crate) id: LocalStructFieldId,
236 #[derive(Debug, PartialEq, Eq)]
237 pub enum FieldSource {
238 Named(ast::RecordFieldDef),
239 Pos(ast::TupleFieldDef),
243 pub fn name(&self, db: &impl HirDatabase) -> Name {
244 self.parent.variant_data(db).fields()[self.id].name.clone()
247 pub fn ty(&self, db: &impl HirDatabase) -> Type {
248 let var_id = self.parent.into();
249 let ty = db.field_types(var_id)[self.id].clone();
250 Type::new(db, self.parent.module(db).id.krate.into(), var_id, ty)
253 pub fn parent_def(&self, _db: &impl HirDatabase) -> VariantDef {
258 #[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
260 pub(crate) id: StructId,
264 pub fn module(self, db: &impl DefDatabase) -> Module {
265 Module { id: self.id.lookup(db).container.module(db) }
268 pub fn krate(self, db: &impl DefDatabase) -> Option<Crate> {
269 Some(self.module(db).krate())
272 pub fn name(self, db: &impl DefDatabase) -> Name {
273 db.struct_data(self.id.into()).name.clone()
276 pub fn fields(self, db: &impl HirDatabase) -> Vec<StructField> {
277 db.struct_data(self.id.into())
281 .map(|(id, _)| StructField { parent: self.into(), id })
285 pub fn ty(self, db: &impl HirDatabase) -> Type {
286 Type::from_def(db, self.id.lookup(db).container.module(db).krate, self.id)
289 fn variant_data(self, db: &impl DefDatabase) -> Arc<VariantData> {
290 db.struct_data(self.id.into()).variant_data.clone()
294 #[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
296 pub(crate) id: UnionId,
300 pub fn name(self, db: &impl DefDatabase) -> Name {
301 db.union_data(self.id).name.clone()
304 pub fn module(self, db: &impl DefDatabase) -> Module {
305 Module { id: self.id.lookup(db).container.module(db) }
308 pub fn ty(self, db: &impl HirDatabase) -> Type {
309 Type::from_def(db, self.id.lookup(db).container.module(db).krate, self.id)
312 pub fn fields(self, db: &impl HirDatabase) -> Vec<StructField> {
313 db.union_data(self.id)
317 .map(|(id, _)| StructField { parent: self.into(), id })
321 fn variant_data(self, db: &impl DefDatabase) -> Arc<VariantData> {
322 db.union_data(self.id).variant_data.clone()
326 #[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
328 pub(crate) id: EnumId,
332 pub fn module(self, db: &impl DefDatabase) -> Module {
333 Module { id: self.id.lookup(db).container.module(db) }
336 pub fn krate(self, db: &impl DefDatabase) -> Option<Crate> {
337 Some(self.module(db).krate())
340 pub fn name(self, db: &impl DefDatabase) -> Name {
341 db.enum_data(self.id).name.clone()
344 pub fn variants(self, db: &impl DefDatabase) -> Vec<EnumVariant> {
345 db.enum_data(self.id)
348 .map(|(id, _)| EnumVariant { parent: self, id })
352 pub fn ty(self, db: &impl HirDatabase) -> Type {
353 Type::from_def(db, self.id.lookup(db).container.module(db).krate, self.id)
357 #[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
358 pub struct EnumVariant {
359 pub(crate) parent: Enum,
360 pub(crate) id: LocalEnumVariantId,
364 pub fn module(self, db: &impl HirDatabase) -> Module {
365 self.parent.module(db)
367 pub fn parent_enum(self, _db: &impl DefDatabase) -> Enum {
371 pub fn name(self, db: &impl DefDatabase) -> Name {
372 db.enum_data(self.parent.id).variants[self.id].name.clone()
375 pub fn fields(self, db: &impl HirDatabase) -> Vec<StructField> {
376 self.variant_data(db)
379 .map(|(id, _)| StructField { parent: self.into(), id })
383 pub(crate) fn variant_data(self, db: &impl DefDatabase) -> Arc<VariantData> {
384 db.enum_data(self.parent.id).variants[self.id].variant_data.clone()
389 #[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)]
395 impl_froms!(Adt: Struct, Union, Enum);
398 pub fn has_non_default_type_params(self, db: &impl HirDatabase) -> bool {
399 let subst = db.generic_defaults(self.into());
400 subst.iter().any(|ty| ty == &Ty::Unknown)
402 pub fn ty(self, db: &impl HirDatabase) -> Type {
403 let id = AdtId::from(self);
404 Type::from_def(db, id.module(db).krate, id)
407 pub fn module(self, db: &impl DefDatabase) -> Module {
409 Adt::Struct(s) => s.module(db),
410 Adt::Union(s) => s.module(db),
411 Adt::Enum(e) => e.module(db),
415 pub fn krate(self, db: &impl HirDatabase) -> Option<Crate> {
416 Some(self.module(db).krate())
420 #[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)]
421 pub enum VariantDef {
424 EnumVariant(EnumVariant),
426 impl_froms!(VariantDef: Struct, Union, EnumVariant);
429 pub fn fields(self, db: &impl HirDatabase) -> Vec<StructField> {
431 VariantDef::Struct(it) => it.fields(db),
432 VariantDef::Union(it) => it.fields(db),
433 VariantDef::EnumVariant(it) => it.fields(db),
437 pub fn module(self, db: &impl HirDatabase) -> Module {
439 VariantDef::Struct(it) => it.module(db),
440 VariantDef::Union(it) => it.module(db),
441 VariantDef::EnumVariant(it) => it.module(db),
445 pub(crate) fn variant_data(self, db: &impl DefDatabase) -> Arc<VariantData> {
447 VariantDef::Struct(it) => it.variant_data(db),
448 VariantDef::Union(it) => it.variant_data(db),
449 VariantDef::EnumVariant(it) => it.variant_data(db),
454 /// The defs which have a body.
455 #[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
456 pub enum DefWithBody {
462 impl_froms!(DefWithBody: Function, Const, Static);
465 pub fn module(self, db: &impl HirDatabase) -> Module {
467 DefWithBody::Const(c) => c.module(db),
468 DefWithBody::Function(f) => f.module(db),
469 DefWithBody::Static(s) => s.module(db),
474 #[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
475 pub struct Function {
476 pub(crate) id: FunctionId,
480 pub fn module(self, db: &impl DefDatabase) -> Module {
481 self.id.lookup(db).module(db).into()
484 pub fn krate(self, db: &impl DefDatabase) -> Option<Crate> {
485 Some(self.module(db).krate())
488 pub fn name(self, db: &impl HirDatabase) -> Name {
489 db.function_data(self.id).name.clone()
492 pub fn has_self_param(self, db: &impl HirDatabase) -> bool {
493 db.function_data(self.id).has_self_param
496 pub fn params(self, db: &impl HirDatabase) -> Vec<TypeRef> {
497 db.function_data(self.id).params.clone()
500 pub fn diagnostics(self, db: &impl HirDatabase, sink: &mut DiagnosticSink) {
501 let infer = db.infer(self.id.into());
502 infer.add_diagnostics(db, self.id, sink);
503 let mut validator = ExprValidator::new(self.id, infer, sink);
504 validator.validate_body(db);
508 #[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
510 pub(crate) id: ConstId,
514 pub fn module(self, db: &impl DefDatabase) -> Module {
515 Module { id: self.id.lookup(db).module(db) }
518 pub fn krate(self, db: &impl DefDatabase) -> Option<Crate> {
519 Some(self.module(db).krate())
522 pub fn name(self, db: &impl HirDatabase) -> Option<Name> {
523 db.const_data(self.id).name.clone()
527 #[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
529 pub(crate) id: StaticId,
533 pub fn module(self, db: &impl DefDatabase) -> Module {
534 Module { id: self.id.lookup(db).module(db) }
537 pub fn krate(self, db: &impl DefDatabase) -> Option<Crate> {
538 Some(self.module(db).krate())
542 #[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
544 pub(crate) id: TraitId,
548 pub fn module(self, db: &impl DefDatabase) -> Module {
549 Module { id: self.id.lookup(db).container.module(db) }
552 pub fn name(self, db: &impl DefDatabase) -> Name {
553 db.trait_data(self.id).name.clone()
556 pub fn items(self, db: &impl DefDatabase) -> Vec<AssocItem> {
557 db.trait_data(self.id).items.iter().map(|(_name, it)| (*it).into()).collect()
560 pub fn is_auto(self, db: &impl DefDatabase) -> bool {
561 db.trait_data(self.id).auto
565 #[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
566 pub struct TypeAlias {
567 pub(crate) id: TypeAliasId,
571 pub fn has_non_default_type_params(self, db: &impl HirDatabase) -> bool {
572 let subst = db.generic_defaults(self.id.into());
573 subst.iter().any(|ty| ty == &Ty::Unknown)
576 pub fn module(self, db: &impl DefDatabase) -> Module {
577 Module { id: self.id.lookup(db).module(db) }
580 pub fn krate(self, db: &impl DefDatabase) -> Option<Crate> {
581 Some(self.module(db).krate())
584 pub fn type_ref(self, db: &impl DefDatabase) -> Option<TypeRef> {
585 db.type_alias_data(self.id).type_ref.clone()
588 pub fn ty(self, db: &impl HirDatabase) -> Type {
589 Type::from_def(db, self.id.lookup(db).module(db).krate, self.id)
592 pub fn name(self, db: &impl DefDatabase) -> Name {
593 db.type_alias_data(self.id).name.clone()
597 #[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
598 pub struct MacroDef {
599 pub(crate) id: MacroDefId,
602 #[derive(Debug, Copy, Clone, PartialEq, Eq, Hash)]
606 TypeAlias(TypeAlias),
608 // FIXME: not every function, ... is actually an assoc item. maybe we should make
609 // sure that you can only turn actual assoc items into AssocItems. This would
610 // require not implementing From, and instead having some checked way of
611 // casting them, and somehow making the constructors private, which would be annoying.
612 impl_froms!(AssocItem: Function, Const, TypeAlias);
615 pub fn module(self, db: &impl DefDatabase) -> Module {
617 AssocItem::Function(f) => f.module(db),
618 AssocItem::Const(c) => c.module(db),
619 AssocItem::TypeAlias(t) => t.module(db),
624 #[derive(Clone, Copy, PartialEq, Eq, Debug, Hash)]
625 pub enum GenericDef {
629 TypeAlias(TypeAlias),
630 ImplBlock(ImplBlock),
631 // enum variants cannot have generics themselves, but their parent enums
632 // can, and this makes some code easier to write
633 EnumVariant(EnumVariant),
634 // consts can have type parameters from their parents (i.e. associated consts of traits)
638 GenericDef: Function,
639 Adt(Struct, Enum, Union),
647 #[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)]
649 pub(crate) parent: DefWithBody,
650 pub(crate) pat_id: PatId,
654 pub fn name(self, db: &impl HirDatabase) -> Option<Name> {
655 let body = db.body(self.parent.into());
656 match &body[self.pat_id] {
657 Pat::Bind { name, .. } => Some(name.clone()),
662 pub fn is_self(self, db: &impl HirDatabase) -> bool {
663 self.name(db) == Some(name![self])
666 pub fn is_mut(self, db: &impl HirDatabase) -> bool {
667 let body = db.body(self.parent.into());
668 match &body[self.pat_id] {
669 Pat::Bind { mode, .. } => match mode {
670 BindingAnnotation::Mutable | BindingAnnotation::RefMut => true,
677 pub fn parent(self, _db: &impl HirDatabase) -> DefWithBody {
681 pub fn module(self, db: &impl HirDatabase) -> Module {
682 self.parent.module(db)
685 pub fn ty(self, db: &impl HirDatabase) -> Type {
686 let def = DefWithBodyId::from(self.parent);
687 let infer = db.infer(def);
688 let ty = infer[self.pat_id].clone();
689 let resolver = def.resolver(db);
690 let krate = def.module(db).krate;
691 let environment = TraitEnvironment::lower(db, &resolver);
692 Type { krate, ty: InEnvironment { value: ty, environment } }
695 pub fn source(self, db: &impl HirDatabase) -> InFile<Either<ast::BindPat, ast::SelfParam>> {
696 let (_body, source_map) = db.body_with_source_map(self.parent.into());
697 let src = source_map.pat_syntax(self.pat_id).unwrap(); // Hmm...
698 let root = src.file_syntax(db);
700 ast.map_left(|it| it.cast().unwrap().to_node(&root)).map_right(|it| it.to_node(&root))
705 #[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)]
706 pub struct TypeParam {
707 pub(crate) id: TypeParamId,
711 pub fn name(self, db: &impl HirDatabase) -> Name {
712 let params = db.generic_params(self.id.parent);
713 params.types[self.id.local_id].name.clone()
716 pub fn module(self, db: &impl HirDatabase) -> Module {
717 self.id.parent.module(db).into()
721 #[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
722 pub struct ImplBlock {
723 pub(crate) id: ImplId,
727 pub fn all_in_crate(db: &impl HirDatabase, krate: Crate) -> Vec<ImplBlock> {
728 let impls = db.impls_in_crate(krate.id);
729 impls.all_impls().map(Self::from).collect()
731 pub fn for_trait(db: &impl HirDatabase, krate: Crate, trait_: Trait) -> Vec<ImplBlock> {
732 let impls = db.impls_in_crate(krate.id);
733 impls.lookup_impl_blocks_for_trait(trait_.id).map(Self::from).collect()
736 pub fn target_trait(&self, db: &impl DefDatabase) -> Option<TypeRef> {
737 db.impl_data(self.id).target_trait.clone()
740 pub fn target_type(&self, db: &impl DefDatabase) -> TypeRef {
741 db.impl_data(self.id).target_type.clone()
744 pub fn target_ty(&self, db: &impl HirDatabase) -> Type {
745 let impl_data = db.impl_data(self.id);
746 let resolver = self.id.resolver(db);
747 let environment = TraitEnvironment::lower(db, &resolver);
748 let ty = Ty::from_hir(db, &resolver, &impl_data.target_type);
750 krate: self.id.lookup(db).container.module(db).krate,
751 ty: InEnvironment { value: ty, environment },
755 pub fn items(&self, db: &impl DefDatabase) -> Vec<AssocItem> {
756 db.impl_data(self.id).items.iter().map(|it| (*it).into()).collect()
759 pub fn is_negative(&self, db: &impl DefDatabase) -> bool {
760 db.impl_data(self.id).is_negative
763 pub fn module(&self, db: &impl DefDatabase) -> Module {
764 self.id.lookup(db).container.module(db).into()
767 pub fn krate(&self, db: &impl DefDatabase) -> Crate {
768 Crate { id: self.module(db).id.krate }
772 #[derive(Clone, PartialEq, Eq, Debug)]
774 pub(crate) krate: CrateId,
775 pub(crate) ty: InEnvironment<Ty>,
779 fn new(db: &impl HirDatabase, krate: CrateId, lexical_env: impl HasResolver, ty: Ty) -> Type {
780 let resolver = lexical_env.resolver(db);
781 let environment = TraitEnvironment::lower(db, &resolver);
782 Type { krate, ty: InEnvironment { value: ty, environment } }
786 db: &impl HirDatabase,
788 def: impl HasResolver + Into<TyDefId>,
790 let ty = db.ty(def.into());
791 Type::new(db, krate, def, ty)
794 pub fn is_bool(&self) -> bool {
795 match &self.ty.value {
796 Ty::Apply(a_ty) => match a_ty.ctor {
797 TypeCtor::Bool => true,
804 pub fn is_mutable_reference(&self) -> bool {
805 match &self.ty.value {
806 Ty::Apply(a_ty) => match a_ty.ctor {
807 TypeCtor::Ref(Mutability::Mut) => true,
814 pub fn is_unknown(&self) -> bool {
815 match &self.ty.value {
821 // FIXME: this method is broken, as it doesn't take closures into account.
822 pub fn as_callable(&self) -> Option<CallableDef> {
823 Some(self.ty.value.as_callable()?.0)
826 pub fn contains_unknown(&self) -> bool {
827 return go(&self.ty.value);
829 fn go(ty: &Ty) -> bool {
832 Ty::Apply(a_ty) => a_ty.parameters.iter().any(go),
838 pub fn fields(&self, db: &impl HirDatabase) -> Vec<(StructField, Type)> {
839 if let Ty::Apply(a_ty) = &self.ty.value {
841 TypeCtor::Adt(AdtId::StructId(s)) => {
842 let var_def = s.into();
844 .field_types(var_def)
846 .map(|(local_id, ty)| {
847 let def = StructField { parent: var_def.into(), id: local_id };
848 let ty = ty.clone().subst(&a_ty.parameters);
849 (def, self.derived(ty))
859 pub fn tuple_fields(&self, _db: &impl HirDatabase) -> Vec<Type> {
860 let mut res = Vec::new();
861 if let Ty::Apply(a_ty) = &self.ty.value {
863 TypeCtor::Tuple { .. } => {
864 for ty in a_ty.parameters.iter() {
865 let ty = ty.clone().subst(&a_ty.parameters);
866 res.push(self.derived(ty));
875 pub fn variant_fields(
877 db: &impl HirDatabase,
879 ) -> Vec<(StructField, Type)> {
880 // FIXME: check that ty and def match
881 match &self.ty.value {
883 let field_types = db.field_types(def.into());
887 let ty = field_types[it.id].clone().subst(&a_ty.parameters);
888 (it, self.derived(ty))
896 pub fn autoderef<'a>(&'a self, db: &'a impl HirDatabase) -> impl Iterator<Item = Type> + 'a {
897 // There should be no inference vars in types passed here
899 let canonical = Canonical { value: self.ty.value.clone(), num_vars: 0 };
900 let environment = self.ty.environment.clone();
901 let ty = InEnvironment { value: canonical, environment: environment.clone() };
902 autoderef(db, Some(self.krate), ty)
903 .map(|canonical| canonical.value)
904 .map(move |ty| self.derived(ty))
907 // This would be nicer if it just returned an iterator, but that runs into
908 // lifetime problems, because we need to borrow temp `CrateImplBlocks`.
909 pub fn iterate_impl_items<T>(
911 db: &impl HirDatabase,
913 mut callback: impl FnMut(AssocItem) -> Option<T>,
915 for krate in self.ty.value.def_crates(db, krate.id)? {
916 let impls = db.impls_in_crate(krate);
918 for impl_block in impls.lookup_impl_blocks(&self.ty.value) {
919 for &item in db.impl_data(impl_block).items.iter() {
920 if let Some(result) = callback(item.into()) {
929 pub fn as_adt(&self) -> Option<Adt> {
930 let (adt, _subst) = self.ty.value.as_adt()?;
934 // FIXME: provide required accessors such that it becomes implementable from outside.
935 pub fn is_equal_for_find_impls(&self, other: &Type) -> bool {
936 match (&self.ty.value, &other.ty.value) {
937 (Ty::Apply(a_original_ty), Ty::Apply(ApplicationTy { ctor, parameters })) => match ctor
939 TypeCtor::Ref(..) => match parameters.as_single() {
940 Ty::Apply(a_ty) => a_original_ty.ctor == a_ty.ctor,
943 _ => a_original_ty.ctor == *ctor,
949 fn derived(&self, ty: Ty) -> Type {
952 ty: InEnvironment { value: ty, environment: self.ty.environment.clone() },
957 impl HirDisplay for Type {
958 fn hir_fmt(&self, f: &mut HirFormatter<impl HirDatabase>) -> std::fmt::Result {
959 self.ty.value.hir_fmt(f)
965 ModuleDef(ModuleDef),
967 GenericParam(TypeParam),
968 ImplSelfType(ImplBlock),
974 impl From<PerNs> for ScopeDef {
975 fn from(def: PerNs) -> Self {
977 .or_else(|| def.take_values())
978 .map(|module_def_id| ScopeDef::ModuleDef(module_def_id.into()))
980 def.take_macros().map(|macro_def_id| ScopeDef::MacroDef(macro_def_id.into()))
982 .unwrap_or(ScopeDef::Unknown)
986 #[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)]
989 StructField(StructField),
992 EnumVariant(EnumVariant),
996 TypeAlias(TypeAlias),
1003 Adt(Struct, Enum, Union),
1013 pub trait HasAttrs {
1014 fn attrs(self, db: &impl DefDatabase) -> Attrs;
1017 impl<T: Into<AttrDef>> HasAttrs for T {
1018 fn attrs(self, db: &impl DefDatabase) -> Attrs {
1019 let def: AttrDef = self.into();
1020 db.attrs(def.into())
1025 fn docs(&self, db: &impl HirDatabase) -> Option<Documentation>;
1027 impl<T: Into<AttrDef> + Copy> Docs for T {
1028 fn docs(&self, db: &impl HirDatabase) -> Option<Documentation> {
1029 let def: AttrDef = (*self).into();
1030 db.documentation(def.into())