1 //! See [`FamousDefs`].
2 use hir::{Crate, Enum, MacroDef, Module, ScopeDef, Semantics, Trait};
4 use crate::RootDatabase;
6 /// Helps with finding well-know things inside the standard library. This is
7 /// somewhat similar to the known paths infra inside hir, but it different; We
8 /// want to make sure that IDE specific paths don't become interesting inside
9 /// the compiler itself as well.
11 /// Note that, by default, rust-analyzer tests **do not** include core or std
12 /// libraries. If you are writing tests for functionality using [`FamousDefs`],
13 /// you'd want to include minicore (see `test_utils::MiniCore`) declaration at
14 /// the start of your tests:
17 /// //- minicore: iterator, ord, derive
19 pub struct FamousDefs<'a, 'b>(pub &'a Semantics<'b, RootDatabase>, pub Option<Crate>);
21 #[allow(non_snake_case)]
22 impl FamousDefs<'_, '_> {
23 pub fn std(&self) -> Option<Crate> {
24 self.find_crate("std")
27 pub fn core(&self) -> Option<Crate> {
28 self.find_crate("core")
31 pub fn core_cmp_Ord(&self) -> Option<Trait> {
32 self.find_trait("core:cmp:Ord")
35 pub fn core_convert_From(&self) -> Option<Trait> {
36 self.find_trait("core:convert:From")
39 pub fn core_convert_Into(&self) -> Option<Trait> {
40 self.find_trait("core:convert:Into")
43 pub fn core_option_Option(&self) -> Option<Enum> {
44 self.find_enum("core:option:Option")
47 pub fn core_result_Result(&self) -> Option<Enum> {
48 self.find_enum("core:result:Result")
51 pub fn core_default_Default(&self) -> Option<Trait> {
52 self.find_trait("core:default:Default")
55 pub fn core_iter_Iterator(&self) -> Option<Trait> {
56 self.find_trait("core:iter:traits:iterator:Iterator")
59 pub fn core_iter_IntoIterator(&self) -> Option<Trait> {
60 self.find_trait("core:iter:traits:collect:IntoIterator")
63 pub fn core_iter(&self) -> Option<Module> {
64 self.find_module("core:iter")
67 pub fn core_ops_Deref(&self) -> Option<Trait> {
68 self.find_trait("core:ops:Deref")
71 pub fn core_convert_AsRef(&self) -> Option<Trait> {
72 self.find_trait("core:convert:AsRef")
75 pub fn core_ops_ControlFlow(&self) -> Option<Enum> {
76 self.find_enum("core:ops:ControlFlow")
79 pub fn core_ops_Drop(&self) -> Option<Trait> {
80 self.find_trait("core:ops:Drop")
83 pub fn core_marker_Copy(&self) -> Option<Trait> {
84 self.find_trait("core:marker:Copy")
87 pub fn core_macros_builtin_derive(&self) -> Option<MacroDef> {
88 self.find_macro("core:macros:builtin:derive")
91 pub fn alloc(&self) -> Option<Crate> {
92 self.find_crate("alloc")
95 pub fn test(&self) -> Option<Crate> {
96 self.find_crate("test")
99 pub fn proc_macro(&self) -> Option<Crate> {
100 self.find_crate("proc_macro")
103 pub fn builtin_crates(&self) -> impl Iterator<Item = Crate> {
104 IntoIterator::into_iter([
114 fn find_trait(&self, path: &str) -> Option<Trait> {
115 match self.find_def(path)? {
116 hir::ScopeDef::ModuleDef(hir::ModuleDef::Trait(it)) => Some(it),
121 fn find_macro(&self, path: &str) -> Option<MacroDef> {
122 match self.find_def(path)? {
123 hir::ScopeDef::MacroDef(it) => Some(it),
128 fn find_enum(&self, path: &str) -> Option<Enum> {
129 match self.find_def(path)? {
130 hir::ScopeDef::ModuleDef(hir::ModuleDef::Adt(hir::Adt::Enum(it))) => Some(it),
135 fn find_module(&self, path: &str) -> Option<Module> {
136 match self.find_def(path)? {
137 hir::ScopeDef::ModuleDef(hir::ModuleDef::Module(it)) => Some(it),
142 fn find_crate(&self, name: &str) -> Option<Crate> {
146 krate.dependencies(db).into_iter().find(|dep| dep.name.to_smol_str() == name)?.krate;
150 fn find_def(&self, path: &str) -> Option<ScopeDef> {
152 let mut path = path.split(':');
153 let trait_ = path.next_back()?;
154 let std_crate = path.next()?;
155 let std_crate = self.find_crate(std_crate)?;
156 let mut module = std_crate.root_module(db);
157 for segment in path {
158 module = module.children(db).find_map(|child| {
159 let name = child.name(db)?;
160 if name.to_smol_str() == segment {
168 module.scope(db, None).into_iter().find(|(name, _def)| name.to_smol_str() == trait_)?.1;