internal: Refactor FamousDefs builtin crate search

This commit is contained in:
Lukas Wirth 2022-03-26 21:22:35 +01:00
parent 259182b50b
commit ef92453dfe
16 changed files with 146 additions and 105 deletions

View file

@ -10,7 +10,7 @@ use tt::Subtree;
use vfs::{file_set::FileSet, VfsPath}; use vfs::{file_set::FileSet, VfsPath};
use crate::{ use crate::{
input::{CrateName, CrateOrigin}, input::{CrateName, CrateOrigin, LangCrateOrigin},
Change, CrateDisplayName, CrateGraph, CrateId, Dependency, Edition, Env, FileId, FilePosition, Change, CrateDisplayName, CrateGraph, CrateId, Dependency, Edition, Env, FileId, FilePosition,
FileRange, ProcMacro, ProcMacroExpander, ProcMacroExpansionError, SourceDatabaseExt, FileRange, ProcMacro, ProcMacroExpander, ProcMacroExpansionError, SourceDatabaseExt,
SourceRoot, SourceRootId, SourceRoot, SourceRootId,
@ -196,7 +196,7 @@ impl ChangeFixture {
Env::default(), Env::default(),
Default::default(), Default::default(),
false, false,
Default::default(), CrateOrigin::CratesIo { repo: None },
); );
} else { } else {
for (from, to, prelude) in crate_deps { for (from, to, prelude) in crate_deps {
@ -233,7 +233,7 @@ impl ChangeFixture {
Env::default(), Env::default(),
Vec::new(), Vec::new(),
false, false,
CrateOrigin::Lang, CrateOrigin::Lang(LangCrateOrigin::Core),
); );
for krate in all_crates { for krate in all_crates {
@ -270,7 +270,7 @@ impl ChangeFixture {
Env::default(), Env::default(),
proc_macro, proc_macro,
true, true,
CrateOrigin::Lang, CrateOrigin::CratesIo { repo: None },
); );
for krate in all_crates { for krate in all_crates {
@ -406,7 +406,11 @@ fn parse_crate(crate_str: String) -> (String, CrateOrigin, Option<String>) {
}; };
(a.to_owned(), origin, Some(version.to_string())) (a.to_owned(), origin, Some(version.to_string()))
} else { } else {
(crate_str, CrateOrigin::Unknown, None) let crate_origin = match &*crate_str {
"std" => CrateOrigin::Lang(LangCrateOrigin::Std),
_ => CrateOrigin::CratesIo { repo: None },
};
(crate_str, crate_origin, None)
} }
} }

View file

@ -117,21 +117,22 @@ impl ops::Deref for CrateName {
} }
/// Origin of the crates. It is used in emitting monikers. /// Origin of the crates. It is used in emitting monikers.
#[derive(Debug, Clone)] #[derive(Debug, Clone, PartialEq, Eq, Hash)]
pub enum CrateOrigin { pub enum CrateOrigin {
/// Crates that are from crates.io official registry, /// Crates that are from crates.io official registry,
CratesIo { repo: Option<String> }, CratesIo { repo: Option<String> },
/// Crates that are provided by the language, like std, core, proc-macro, ... /// Crates that are provided by the language, like std, core, proc-macro, ...
Lang, Lang(LangCrateOrigin),
/// Crates that we don't know their origin.
// Ideally this enum should cover all cases, and then we remove this variant.
Unknown,
} }
impl Default for CrateOrigin { #[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
fn default() -> Self { pub enum LangCrateOrigin {
Self::Unknown Alloc,
} Core,
ProcMacro,
Std,
Test,
Other,
} }
#[derive(Debug, Clone, PartialEq, Eq, Hash)] #[derive(Debug, Clone, PartialEq, Eq, Hash)]
@ -585,6 +586,8 @@ impl fmt::Display for CyclicDependenciesError {
#[cfg(test)] #[cfg(test)]
mod tests { mod tests {
use crate::CrateOrigin;
use super::{CfgOptions, CrateGraph, CrateName, Dependency, Edition::Edition2018, Env, FileId}; use super::{CfgOptions, CrateGraph, CrateName, Dependency, Edition::Edition2018, Env, FileId};
#[test] #[test]
@ -600,7 +603,7 @@ mod tests {
Env::default(), Env::default(),
Default::default(), Default::default(),
false, false,
Default::default(), CrateOrigin::CratesIo { repo: None },
); );
let crate2 = graph.add_crate_root( let crate2 = graph.add_crate_root(
FileId(2u32), FileId(2u32),
@ -612,7 +615,7 @@ mod tests {
Env::default(), Env::default(),
Default::default(), Default::default(),
false, false,
Default::default(), CrateOrigin::CratesIo { repo: None },
); );
let crate3 = graph.add_crate_root( let crate3 = graph.add_crate_root(
FileId(3u32), FileId(3u32),
@ -624,7 +627,7 @@ mod tests {
Env::default(), Env::default(),
Default::default(), Default::default(),
false, false,
Default::default(), CrateOrigin::CratesIo { repo: None },
); );
assert!(graph assert!(graph
.add_dep(crate1, Dependency::new(CrateName::new("crate2").unwrap(), crate2)) .add_dep(crate1, Dependency::new(CrateName::new("crate2").unwrap(), crate2))
@ -650,7 +653,7 @@ mod tests {
Env::default(), Env::default(),
Default::default(), Default::default(),
false, false,
Default::default(), CrateOrigin::CratesIo { repo: None },
); );
let crate2 = graph.add_crate_root( let crate2 = graph.add_crate_root(
FileId(2u32), FileId(2u32),
@ -662,7 +665,7 @@ mod tests {
Env::default(), Env::default(),
Default::default(), Default::default(),
false, false,
Default::default(), CrateOrigin::CratesIo { repo: None },
); );
assert!(graph assert!(graph
.add_dep(crate1, Dependency::new(CrateName::new("crate2").unwrap(), crate2)) .add_dep(crate1, Dependency::new(CrateName::new("crate2").unwrap(), crate2))
@ -685,7 +688,7 @@ mod tests {
Env::default(), Env::default(),
Default::default(), Default::default(),
false, false,
Default::default(), CrateOrigin::CratesIo { repo: None },
); );
let crate2 = graph.add_crate_root( let crate2 = graph.add_crate_root(
FileId(2u32), FileId(2u32),
@ -697,7 +700,7 @@ mod tests {
Env::default(), Env::default(),
Default::default(), Default::default(),
false, false,
Default::default(), CrateOrigin::CratesIo { repo: None },
); );
let crate3 = graph.add_crate_root( let crate3 = graph.add_crate_root(
FileId(3u32), FileId(3u32),
@ -709,7 +712,7 @@ mod tests {
Env::default(), Env::default(),
Default::default(), Default::default(),
false, false,
Default::default(), CrateOrigin::CratesIo { repo: None },
); );
assert!(graph assert!(graph
.add_dep(crate1, Dependency::new(CrateName::new("crate2").unwrap(), crate2)) .add_dep(crate1, Dependency::new(CrateName::new("crate2").unwrap(), crate2))
@ -732,7 +735,7 @@ mod tests {
Env::default(), Env::default(),
Default::default(), Default::default(),
false, false,
Default::default(), CrateOrigin::CratesIo { repo: None },
); );
let crate2 = graph.add_crate_root( let crate2 = graph.add_crate_root(
FileId(2u32), FileId(2u32),
@ -744,7 +747,7 @@ mod tests {
Env::default(), Env::default(),
Default::default(), Default::default(),
false, false,
Default::default(), CrateOrigin::CratesIo { repo: None },
); );
assert!(graph assert!(graph
.add_dep( .add_dep(

View file

@ -12,8 +12,8 @@ pub use crate::{
change::Change, change::Change,
input::{ input::{
CrateData, CrateDisplayName, CrateGraph, CrateId, CrateName, CrateOrigin, Dependency, CrateData, CrateDisplayName, CrateGraph, CrateId, CrateName, CrateOrigin, Dependency,
Edition, Env, ProcMacro, ProcMacroExpander, ProcMacroExpansionError, ProcMacroId, Edition, Env, LangCrateOrigin, ProcMacro, ProcMacroExpander, ProcMacroExpansionError,
ProcMacroKind, SourceRoot, SourceRootId, ProcMacroId, ProcMacroKind, SourceRoot, SourceRootId,
}, },
}; };
pub use salsa::{self, Cancelled}; pub use salsa::{self, Cancelled};

View file

@ -148,6 +148,10 @@ impl Crate {
db.crate_graph()[self.id].origin.clone() db.crate_graph()[self.id].origin.clone()
} }
pub fn is_builtin(self, db: &dyn HirDatabase) -> bool {
matches!(self.origin(db), CrateOrigin::Lang(_))
}
pub fn dependencies(self, db: &dyn HirDatabase) -> Vec<CrateDependency> { pub fn dependencies(self, db: &dyn HirDatabase) -> Vec<CrateDependency> {
db.crate_graph()[self.id] db.crate_graph()[self.id]
.dependencies .dependencies
@ -155,7 +159,7 @@ impl Crate {
.map(|dep| { .map(|dep| {
let krate = Crate { id: dep.crate_id }; let krate = Crate { id: dep.crate_id };
let name = dep.as_name(); let name = dep.as_name();
CrateDependency { krate, name } CrateDependency { krate, name, }
}) })
.collect() .collect()
} }
@ -1741,10 +1745,8 @@ impl BuiltinType {
BuiltinType { inner: hir_def::builtin_type::BuiltinType::Str } BuiltinType { inner: hir_def::builtin_type::BuiltinType::Str }
} }
pub fn ty(self, db: &dyn HirDatabase, module: Module) -> Type { pub fn ty(self, db: &dyn HirDatabase) -> Type {
let resolver = module.id.resolver(db.upcast()); Type::new_for_crate(db.crate_graph().iter().next().unwrap(), TyBuilder::builtin(self.inner))
Type::new_with_resolver(db, &resolver, TyBuilder::builtin(self.inner))
.expect("crate not present in resolver")
} }
pub fn name(self) -> Name { pub fn name(self) -> Name {
@ -2619,6 +2621,7 @@ impl Type {
let krate = resolver.krate()?; let krate = resolver.krate()?;
Some(Type::new_with_resolver_inner(db, krate, resolver, ty)) Some(Type::new_with_resolver_inner(db, krate, resolver, ty))
} }
pub(crate) fn new_with_resolver_inner( pub(crate) fn new_with_resolver_inner(
db: &dyn HirDatabase, db: &dyn HirDatabase,
krate: CrateId, krate: CrateId,
@ -2631,6 +2634,10 @@ impl Type {
Type { krate, env: environment, ty } Type { krate, env: environment, ty }
} }
pub(crate) fn new_for_crate(krate: CrateId, ty: Ty) -> Type {
Type { krate, env: Arc::new(TraitEnvironment::empty(krate)), ty }
}
pub fn reference(inner: &Type, m: Mutability) -> Type { pub fn reference(inner: &Type, m: Mutability) -> Type {
inner.derived( inner.derived(
TyKind::Ref( TyKind::Ref(

View file

@ -63,10 +63,7 @@ pub(crate) fn goto_implementation(
Definition::Trait(trait_) => impls_for_trait(&sema, trait_), Definition::Trait(trait_) => impls_for_trait(&sema, trait_),
Definition::Adt(adt) => impls_for_ty(&sema, adt.ty(sema.db)), Definition::Adt(adt) => impls_for_ty(&sema, adt.ty(sema.db)),
Definition::TypeAlias(alias) => impls_for_ty(&sema, alias.ty(sema.db)), Definition::TypeAlias(alias) => impls_for_ty(&sema, alias.ty(sema.db)),
Definition::BuiltinType(builtin) => { Definition::BuiltinType(builtin) => impls_for_ty(&sema, builtin.ty(sema.db)),
let module = sema.to_module_def(position.file_id)?;
impls_for_ty(&sema, builtin.ty(sema.db, module))
}
Definition::Function(f) => { Definition::Function(f) => {
let assoc = f.as_assoc_item(sema.db)?; let assoc = f.as_assoc_item(sema.db)?;
let name = assoc.name(sema.db)?; let name = assoc.name(sema.db)?;

View file

@ -64,7 +64,7 @@ use cfg::CfgOptions;
use ide_db::{ use ide_db::{
base_db::{ base_db::{
salsa::{self, ParallelDatabase}, salsa::{self, ParallelDatabase},
Env, FileLoader, FileSet, SourceDatabase, VfsPath, CrateOrigin, Env, FileLoader, FileSet, SourceDatabase, VfsPath,
}, },
symbol_index, LineIndexDatabase, symbol_index, LineIndexDatabase,
}; };
@ -232,7 +232,7 @@ impl Analysis {
Env::default(), Env::default(),
Default::default(), Default::default(),
false, false,
Default::default(), CrateOrigin::CratesIo { repo: None },
); );
change.change_file(file_id, Some(Arc::new(text))); change.change_file(file_id, Some(Arc::new(text)));
change.set_crate_graph(crate_graph); change.set_crate_graph(crate_graph);

View file

@ -3,7 +3,7 @@
use hir::{db::DefDatabase, AsAssocItem, AssocItemContainer, Crate, Name, Semantics}; use hir::{db::DefDatabase, AsAssocItem, AssocItemContainer, Crate, Name, Semantics};
use ide_db::{ use ide_db::{
base_db::{CrateOrigin, FileId, FileLoader, FilePosition}, base_db::{CrateOrigin, FileId, FileLoader, FilePosition, LangCrateOrigin},
defs::{Definition, IdentClass}, defs::{Definition, IdentClass},
helpers::pick_best_token, helpers::pick_best_token,
RootDatabase, RootDatabase,
@ -151,11 +151,20 @@ pub(crate) fn def_to_moniker(
let name = krate.display_name(db)?.to_string(); let name = krate.display_name(db)?.to_string();
let (repo, version) = match krate.origin(db) { let (repo, version) = match krate.origin(db) {
CrateOrigin::CratesIo { repo } => (repo?, krate.version(db)?), CrateOrigin::CratesIo { repo } => (repo?, krate.version(db)?),
CrateOrigin::Lang => ( CrateOrigin::Lang(lang) => (
"https://github.com/rust-lang/rust/".to_string(), "https://github.com/rust-lang/rust/".to_string(),
"compiler_version".to_string(), format!(
"https://github.com/rust-lang/rust/library/{}",
match lang {
LangCrateOrigin::Alloc => "alloc",
LangCrateOrigin::Core => "core",
LangCrateOrigin::ProcMacro => "proc_macro",
LangCrateOrigin::Std => "std",
LangCrateOrigin::Test => "test",
LangCrateOrigin::Other => "",
}
),
), ),
CrateOrigin::Unknown => return None,
}; };
PackageInformation { name, repo, version } PackageInformation { name, repo, version }
}, },

View file

@ -3,7 +3,6 @@
use hir::{AsAssocItem, HasVisibility, Semantics}; use hir::{AsAssocItem, HasVisibility, Semantics};
use ide_db::{ use ide_db::{
defs::{Definition, IdentClass, NameClass, NameRefClass}, defs::{Definition, IdentClass, NameClass, NameRefClass},
famous_defs::FamousDefs,
RootDatabase, SymbolKind, RootDatabase, SymbolKind,
}; };
use rustc_hash::FxHashMap; use rustc_hash::FxHashMap;
@ -472,14 +471,12 @@ fn highlight_def(
Definition::ToolModule(_) => Highlight::new(HlTag::Symbol(SymbolKind::ToolModule)), Definition::ToolModule(_) => Highlight::new(HlTag::Symbol(SymbolKind::ToolModule)),
}; };
let famous_defs = FamousDefs(sema, krate);
let def_crate = def.module(db).map(hir::Module::krate).or_else(|| match def { let def_crate = def.module(db).map(hir::Module::krate).or_else(|| match def {
Definition::Module(module) => Some(module.krate()), Definition::Module(module) => Some(module.krate()),
_ => None, _ => None,
}); });
let is_from_other_crate = def_crate != krate; let is_from_other_crate = def_crate != krate;
let is_from_builtin_crate = let is_from_builtin_crate = def_crate.map_or(false, |def_crate| def_crate.is_builtin(db));
def_crate.map_or(false, |def_crate| famous_defs.builtin_crates().any(|it| def_crate == it));
let is_builtin_type = matches!(def, Definition::BuiltinType(_)); let is_builtin_type = matches!(def, Definition::BuiltinType(_));
let is_public = def.visibility(db) == Some(hir::Visibility::Public); let is_public = def.visibility(db) == Some(hir::Visibility::Public);
@ -525,10 +522,9 @@ fn highlight_method_call(
h |= HlMod::Trait; h |= HlMod::Trait;
} }
let famous_defs = FamousDefs(sema, krate);
let def_crate = func.module(sema.db).krate(); let def_crate = func.module(sema.db).krate();
let is_from_other_crate = Some(def_crate) != krate; let is_from_other_crate = Some(def_crate) != krate;
let is_from_builtin_crate = famous_defs.builtin_crates().any(|it| def_crate == it); let is_from_builtin_crate = def_crate.is_builtin(sema.db);
let is_public = func.visibility(sema.db) == hir::Visibility::Public; let is_public = func.visibility(sema.db) == hir::Visibility::Public;
if is_from_other_crate { if is_from_other_crate {

View file

@ -43,5 +43,5 @@ pre { color: #DCDCCC; background: #3F3F3F; font-size: 22px; padd
.unresolved_reference { color: #FC5555; text-decoration: wavy underline; } .unresolved_reference { color: #FC5555; text-decoration: wavy underline; }
</style> </style>
<pre><code><span class="keyword">extern</span> <span class="keyword">crate</span> <span class="module crate_root default_library library">std</span><span class="semicolon">;</span> <pre><code><span class="keyword">extern</span> <span class="keyword">crate</span> <span class="module crate_root default_library library">std</span><span class="semicolon">;</span>
<span class="keyword">extern</span> <span class="keyword">crate</span> <span class="module crate_root default_library library">alloc</span> <span class="keyword">as</span> <span class="module crate_root default_library declaration library">abc</span><span class="semicolon">;</span> <span class="keyword">extern</span> <span class="keyword">crate</span> <span class="module crate_root library">alloc</span> <span class="keyword">as</span> <span class="module crate_root declaration library">abc</span><span class="semicolon">;</span>
</code></pre> </code></pre>

View file

@ -600,8 +600,7 @@ fn handle_as_ref_str(
db: &dyn HirDatabase, db: &dyn HirDatabase,
famous_defs: &FamousDefs, famous_defs: &FamousDefs,
) -> Option<ReferenceConversionType> { ) -> Option<ReferenceConversionType> {
let module = famous_defs.1?.root_module(db); let str_type = hir::BuiltinType::str().ty(db);
let str_type = hir::BuiltinType::str().ty(db, module);
ty.impls_trait(db, famous_defs.core_convert_AsRef()?, &[str_type]) ty.impls_trait(db, famous_defs.core_convert_AsRef()?, &[str_type])
.then(|| ReferenceConversionType::AsRefStr) .then(|| ReferenceConversionType::AsRefStr)

View file

@ -159,13 +159,7 @@ fn pattern_path_completion(
hir::PathResolution::Def(hir::ModuleDef::Adt(hir::Adt::Union(u))) => { hir::PathResolution::Def(hir::ModuleDef::Adt(hir::Adt::Union(u))) => {
u.ty(ctx.db) u.ty(ctx.db)
} }
hir::PathResolution::Def(hir::ModuleDef::BuiltinType(ty)) => { hir::PathResolution::Def(hir::ModuleDef::BuiltinType(ty)) => ty.ty(ctx.db),
let module = match ctx.module {
Some(m) => m,
None => return,
};
ty.ty(ctx.db, module)
}
_ => return, _ => return,
}; };

View file

@ -1,7 +1,6 @@
//! Completion of paths, i.e. `some::prefix::$0`. //! Completion of paths, i.e. `some::prefix::$0`.
use hir::{ScopeDef, Trait}; use hir::{ScopeDef, Trait};
use ide_db::famous_defs::FamousDefs;
use rustc_hash::FxHashSet; use rustc_hash::FxHashSet;
use syntax::ast; use syntax::ast;
@ -26,7 +25,7 @@ pub(crate) fn complete_qualified_path(acc: &mut Completions, ctx: &CompletionCon
}; };
let traits_in_scope = |ctx: &CompletionContext| { let traits_in_scope = |ctx: &CompletionContext| {
let mut traits_in_scope = ctx.scope.visible_traits(); let mut traits_in_scope = ctx.scope.visible_traits();
if let Some(drop) = FamousDefs(&ctx.sema, ctx.krate).core_ops_Drop() { if let Some(drop) = ctx.famous_defs().core_ops_Drop() {
traits_in_scope.remove(&drop.into()); traits_in_scope.remove(&drop.into());
} }
traits_in_scope traits_in_scope
@ -133,12 +132,8 @@ pub(crate) fn complete_qualified_path(acc: &mut Completions, ctx: &CompletionCon
ty ty
} }
hir::ModuleDef::BuiltinType(builtin) => { hir::ModuleDef::BuiltinType(builtin) => {
let module = match ctx.module {
Some(it) => it,
None => return,
};
cov_mark::hit!(completes_primitive_assoc_const); cov_mark::hit!(completes_primitive_assoc_const);
builtin.ty(ctx.db, module) builtin.ty(ctx.db)
} }
_ => unreachable!(), _ => unreachable!(),
}; };

View file

@ -1,4 +1,6 @@
//! See [`FamousDefs`]. //! See [`FamousDefs`].
use base_db::{CrateOrigin, LangCrateOrigin, SourceDatabase};
use hir::{Crate, Enum, Macro, Module, ScopeDef, Semantics, Trait}; use hir::{Crate, Enum, Macro, Module, ScopeDef, Semantics, Trait};
use crate::RootDatabase; use crate::RootDatabase;
@ -21,11 +23,23 @@ pub struct FamousDefs<'a, 'b>(pub &'a Semantics<'b, RootDatabase>, pub Option<Cr
#[allow(non_snake_case)] #[allow(non_snake_case)]
impl FamousDefs<'_, '_> { impl FamousDefs<'_, '_> {
pub fn std(&self) -> Option<Crate> { pub fn std(&self) -> Option<Crate> {
self.find_crate("std") self.find_lang_crate(LangCrateOrigin::Std)
} }
pub fn core(&self) -> Option<Crate> { pub fn core(&self) -> Option<Crate> {
self.find_crate("core") self.find_lang_crate(LangCrateOrigin::Core)
}
pub fn alloc(&self) -> Option<Crate> {
self.find_lang_crate(LangCrateOrigin::Alloc)
}
pub fn test(&self) -> Option<Crate> {
self.find_lang_crate(LangCrateOrigin::Test)
}
pub fn proc_macro(&self) -> Option<Crate> {
self.find_lang_crate(LangCrateOrigin::ProcMacro)
} }
pub fn core_cmp_Ord(&self) -> Option<Trait> { pub fn core_cmp_Ord(&self) -> Option<Trait> {
@ -88,18 +102,6 @@ impl FamousDefs<'_, '_> {
self.find_macro("core:macros:builtin:derive") self.find_macro("core:macros:builtin:derive")
} }
pub fn alloc(&self) -> Option<Crate> {
self.find_crate("alloc")
}
pub fn test(&self) -> Option<Crate> {
self.find_crate("test")
}
pub fn proc_macro(&self) -> Option<Crate> {
self.find_crate("proc_macro")
}
pub fn builtin_crates(&self) -> impl Iterator<Item = Crate> { pub fn builtin_crates(&self) -> impl Iterator<Item = Crate> {
IntoIterator::into_iter([ IntoIterator::into_iter([
self.std(), self.std(),
@ -139,11 +141,15 @@ impl FamousDefs<'_, '_> {
} }
} }
fn find_crate(&self, name: &str) -> Option<Crate> { fn find_lang_crate(&self, origin: LangCrateOrigin) -> Option<Crate> {
let krate = self.1?; let krate = self.1?;
let db = self.0.db; let db = self.0.db;
let res = let crate_graph = self.0.db.crate_graph();
krate.dependencies(db).into_iter().find(|dep| dep.name.to_smol_str() == name)?.krate; let res = krate
.dependencies(db)
.into_iter()
.find(|dep| crate_graph[dep.krate.into()].origin == CrateOrigin::Lang(origin))?
.krate;
Some(res) Some(res)
} }
@ -151,8 +157,16 @@ impl FamousDefs<'_, '_> {
let db = self.0.db; let db = self.0.db;
let mut path = path.split(':'); let mut path = path.split(':');
let trait_ = path.next_back()?; let trait_ = path.next_back()?;
let std_crate = path.next()?; let lang_crate = path.next()?;
let std_crate = self.find_crate(std_crate)?; let lang_crate = match lang_crate {
"core" => LangCrateOrigin::Core,
"alloc" => LangCrateOrigin::Alloc,
"test" => LangCrateOrigin::Test,
"proc_macro" => LangCrateOrigin::ProcMacro,
"std" => LangCrateOrigin::Std,
_ => return None,
};
let std_crate = self.find_lang_crate(lang_crate)?;
let mut module = std_crate.root_module(db); let mut module = std_crate.root_module(db);
for segment in path { for segment in path {
module = module.children(db).find_map(|child| { module = module.children(db).find_map(|child| {

View file

@ -734,13 +734,7 @@ fn def_to_ty(sema: &Semantics<RootDatabase>, def: &Definition) -> Option<hir::Ty
match def { match def {
Definition::Adt(adt) => Some(adt.ty(sema.db)), Definition::Adt(adt) => Some(adt.ty(sema.db)),
Definition::TypeAlias(it) => Some(it.ty(sema.db)), Definition::TypeAlias(it) => Some(it.ty(sema.db)),
Definition::BuiltinType(it) => { Definition::BuiltinType(it) => Some(it.ty(sema.db)),
let graph = sema.db.crate_graph();
let krate = graph.iter().next()?;
let root_file = graph[krate].root_file_id;
let module = sema.to_module_def(root_file)?;
Some(it.ty(sema.db, module))
}
Definition::SelfType(it) => Some(it.self_ty(sema.db)), Definition::SelfType(it) => Some(it.self_ty(sema.db)),
_ => None, _ => None,
} }

View file

@ -1261,7 +1261,9 @@ fn rust_project_hello_world_project_model() {
}, },
], ],
proc_macro: [], proc_macro: [],
origin: Lang, origin: Lang(
Alloc,
),
is_proc_macro: false, is_proc_macro: false,
}, },
CrateId( CrateId(
@ -1291,7 +1293,9 @@ fn rust_project_hello_world_project_model() {
}, },
dependencies: [], dependencies: [],
proc_macro: [], proc_macro: [],
origin: Lang, origin: Lang(
Other,
),
is_proc_macro: false, is_proc_macro: false,
}, },
CrateId( CrateId(
@ -1321,7 +1325,9 @@ fn rust_project_hello_world_project_model() {
}, },
dependencies: [], dependencies: [],
proc_macro: [], proc_macro: [],
origin: Lang, origin: Lang(
Other,
),
is_proc_macro: false, is_proc_macro: false,
}, },
CrateId( CrateId(
@ -1361,7 +1367,9 @@ fn rust_project_hello_world_project_model() {
}, },
], ],
proc_macro: [], proc_macro: [],
origin: Lang, origin: Lang(
ProcMacro,
),
is_proc_macro: false, is_proc_macro: false,
}, },
CrateId( CrateId(
@ -1391,7 +1399,9 @@ fn rust_project_hello_world_project_model() {
}, },
dependencies: [], dependencies: [],
proc_macro: [], proc_macro: [],
origin: Lang, origin: Lang(
Core,
),
is_proc_macro: false, is_proc_macro: false,
}, },
CrateId( CrateId(
@ -1490,7 +1500,9 @@ fn rust_project_hello_world_project_model() {
}, },
dependencies: [], dependencies: [],
proc_macro: [], proc_macro: [],
origin: Lang, origin: Lang(
Other,
),
is_proc_macro: false, is_proc_macro: false,
}, },
CrateId( CrateId(
@ -1520,7 +1532,9 @@ fn rust_project_hello_world_project_model() {
}, },
dependencies: [], dependencies: [],
proc_macro: [], proc_macro: [],
origin: Lang, origin: Lang(
Other,
),
is_proc_macro: false, is_proc_macro: false,
}, },
CrateId( CrateId(
@ -1550,7 +1564,9 @@ fn rust_project_hello_world_project_model() {
}, },
dependencies: [], dependencies: [],
proc_macro: [], proc_macro: [],
origin: Lang, origin: Lang(
Other,
),
is_proc_macro: false, is_proc_macro: false,
}, },
CrateId( CrateId(
@ -1580,7 +1596,9 @@ fn rust_project_hello_world_project_model() {
}, },
dependencies: [], dependencies: [],
proc_macro: [], proc_macro: [],
origin: Lang, origin: Lang(
Test,
),
is_proc_macro: false, is_proc_macro: false,
}, },
CrateId( CrateId(
@ -1692,7 +1710,9 @@ fn rust_project_hello_world_project_model() {
}, },
], ],
proc_macro: [], proc_macro: [],
origin: Lang, origin: Lang(
Std,
),
is_proc_macro: false, is_proc_macro: false,
}, },
CrateId( CrateId(
@ -1722,7 +1742,9 @@ fn rust_project_hello_world_project_model() {
}, },
dependencies: [], dependencies: [],
proc_macro: [], proc_macro: [],
origin: Lang, origin: Lang(
Other,
),
is_proc_macro: false, is_proc_macro: false,
}, },
}, },

View file

@ -7,7 +7,7 @@ use std::{collections::VecDeque, fmt, fs, process::Command};
use anyhow::{format_err, Context, Result}; use anyhow::{format_err, Context, Result};
use base_db::{ use base_db::{
CrateDisplayName, CrateGraph, CrateId, CrateName, CrateOrigin, Dependency, Edition, Env, CrateDisplayName, CrateGraph, CrateId, CrateName, CrateOrigin, Dependency, Edition, Env,
FileId, ProcMacro, FileId, LangCrateOrigin, ProcMacro,
}; };
use cfg::{CfgDiff, CfgOptions}; use cfg::{CfgDiff, CfgOptions};
use paths::{AbsPath, AbsPathBuf}; use paths::{AbsPath, AbsPathBuf};
@ -487,7 +487,7 @@ fn project_json_to_crate_graph(
if krate.display_name.is_some() { if krate.display_name.is_some() {
CrateOrigin::CratesIo { repo: krate.repository.clone() } CrateOrigin::CratesIo { repo: krate.repository.clone() }
} else { } else {
CrateOrigin::Unknown CrateOrigin::CratesIo { repo: None }
}, },
), ),
) )
@ -710,7 +710,7 @@ fn detached_files_to_crate_graph(
Env::default(), Env::default(),
Vec::new(), Vec::new(),
false, false,
CrateOrigin::Unknown, CrateOrigin::CratesIo { repo: None },
); );
public_deps.add(detached_file_crate, &mut crate_graph); public_deps.add(detached_file_crate, &mut crate_graph);
@ -908,7 +908,14 @@ fn sysroot_to_crate_graph(
env, env,
proc_macro, proc_macro,
false, false,
CrateOrigin::Lang, CrateOrigin::Lang(match &*sysroot[krate].name {
"alloc" => LangCrateOrigin::Alloc,
"core" => LangCrateOrigin::Core,
"proc_macro" => LangCrateOrigin::ProcMacro,
"std" => LangCrateOrigin::Std,
"test" => LangCrateOrigin::Test,
_ => LangCrateOrigin::Other,
}),
); );
Some((krate, crate_id)) Some((krate, crate_id))
}) })