minor: Simplify

This commit is contained in:
Lukas Wirth 2022-05-20 16:42:28 +02:00
parent f5229cebb2
commit 88e297e47d
6 changed files with 20 additions and 25 deletions

View File

@ -408,6 +408,7 @@ fn parse_crate(crate_str: String) -> (String, CrateOrigin, Option<String>) {
} else {
let crate_origin = match &*crate_str {
"std" => CrateOrigin::Lang(LangCrateOrigin::Std),
"core" => CrateOrigin::Lang(LangCrateOrigin::Core),
_ => CrateOrigin::CratesIo { repo: None },
};
(crate_str, crate_origin, None)

View File

@ -25,6 +25,7 @@ fn test_copy_expand_in_core() {
cov_mark::check!(test_copy_expand_in_core);
check(
r#"
//- /lib.rs crate:core
#[rustc_builtin_macro]
macro derive {}
#[rustc_builtin_macro]

View File

@ -1,5 +1,6 @@
//! Builtin derives.
use base_db::{CrateOrigin, LangCrateOrigin};
use tracing::debug;
use syntax::{
@ -160,14 +161,11 @@ fn find_builtin_crate(db: &dyn AstDatabase, id: MacroCallId) -> tt::TokenTree {
let cg = db.crate_graph();
let krate = db.lookup_intern_macro_call(id).krate;
// XXX
// All crates except core itself should have a dependency on core,
// We detect `core` by seeing whether it doesn't have such a dependency.
let tt = if cg[krate].dependencies.iter().any(|dep| &*dep.name == "core") {
quote! { core }
} else {
let tt = if matches!(cg[krate].origin, CrateOrigin::Lang(LangCrateOrigin::Core)) {
cov_mark::hit!(test_copy_expand_in_core);
quote! { crate }
} else {
quote! { core }
};
tt.token_trees[0].clone()

View File

@ -7,9 +7,9 @@
use arrayvec::ArrayVec;
use hir::{
Adt, AsAssocItem, AssocItem, BuiltinAttr, BuiltinType, Const, Field, Function, GenericParam,
HasVisibility, Impl, ItemInNs, Label, Local, Macro, Module, ModuleDef, Name, PathResolution,
Semantics, Static, ToolModule, Trait, TypeAlias, Variant, Visibility,
Adt, AsAssocItem, AssocItem, BuiltinAttr, BuiltinType, Const, Crate, Field, Function,
GenericParam, HasVisibility, Impl, ItemInNs, Label, Local, Macro, Module, ModuleDef, Name,
PathResolution, Semantics, Static, ToolModule, Trait, TypeAlias, Variant, Visibility,
};
use stdx::impl_from;
use syntax::{
@ -46,6 +46,13 @@ impl Definition {
self.module(db).map(|it| it.path_to_root(db).into_iter().rev())
}
pub fn krate(&self, db: &RootDatabase) -> Option<Crate> {
Some(match self {
Definition::Module(m) => m.krate(),
_ => self.module(db)?.krate(),
})
}
pub fn module(&self, db: &RootDatabase) -> Option<Module> {
let module = match self {
Definition::Macro(it) => it.module(db),

View File

@ -293,7 +293,7 @@ fn broken_link_clone_cb<'a>(link: BrokenLink<'a>) -> Option<(CowStr<'a>, CowStr<
fn get_doc_link(db: &RootDatabase, def: Definition) -> Option<String> {
let (target, file, frag) = filename_and_frag_for_def(db, def)?;
let krate = crate_of_def(db, target)?;
let krate = target.krate(db)?;
let mut url = get_doc_base_url(db, krate)?;
if let Some(path) = mod_path_of_def(db, target) {
@ -315,7 +315,7 @@ fn rewrite_intra_doc_link(
let (link, ns) = parse_intra_doc_link(target);
let resolved = resolve_doc_path_for_def(db, def, link, ns)?;
let krate = crate_of_def(db, resolved)?;
let krate = resolved.krate(db)?;
let mut url = get_doc_base_url(db, krate)?;
let (_, file, frag) = filename_and_frag_for_def(db, resolved)?;
@ -335,7 +335,7 @@ fn rewrite_url_link(db: &RootDatabase, def: Definition, target: &str) -> Option<
return None;
}
let krate = crate_of_def(db, def)?;
let krate = def.krate(db)?;
let mut url = get_doc_base_url(db, krate)?;
let (def, file, frag) = filename_and_frag_for_def(db, def)?;
@ -348,15 +348,6 @@ fn rewrite_url_link(db: &RootDatabase, def: Definition, target: &str) -> Option<
url.join(target).ok().map(Into::into)
}
fn crate_of_def(db: &RootDatabase, def: Definition) -> Option<Crate> {
let krate = match def {
// Definition::module gives back the parent module, we don't want that as it fails for root modules
Definition::Module(module) => module.krate(),
def => def.module(db)?.krate(),
};
Some(krate)
}
fn mod_path_of_def(db: &RootDatabase, def: Definition) -> Option<String> {
def.canonical_module_path(db).map(|it| {
let mut path = String::new();

View File

@ -466,10 +466,7 @@ fn highlight_def(sema: &Semantics<RootDatabase>, krate: hir::Crate, def: Definit
Definition::ToolModule(_) => Highlight::new(HlTag::Symbol(SymbolKind::ToolModule)),
};
let def_crate = def.module(db).map(hir::Module::krate).or_else(|| match def {
Definition::Module(module) => Some(module.krate()),
_ => None,
});
let def_crate = def.krate(db);
let is_from_other_crate = def_crate != Some(krate);
let is_from_builtin_crate = def_crate.map_or(false, |def_crate| def_crate.is_builtin(db));
let is_builtin_type = matches!(def, Definition::BuiltinType(_));