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 { } else {
let crate_origin = match &*crate_str { let crate_origin = match &*crate_str {
"std" => CrateOrigin::Lang(LangCrateOrigin::Std), "std" => CrateOrigin::Lang(LangCrateOrigin::Std),
"core" => CrateOrigin::Lang(LangCrateOrigin::Core),
_ => CrateOrigin::CratesIo { repo: None }, _ => CrateOrigin::CratesIo { repo: None },
}; };
(crate_str, crate_origin, 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); cov_mark::check!(test_copy_expand_in_core);
check( check(
r#" r#"
//- /lib.rs crate:core
#[rustc_builtin_macro] #[rustc_builtin_macro]
macro derive {} macro derive {}
#[rustc_builtin_macro] #[rustc_builtin_macro]

View file

@ -1,5 +1,6 @@
//! Builtin derives. //! Builtin derives.
use base_db::{CrateOrigin, LangCrateOrigin};
use tracing::debug; use tracing::debug;
use syntax::{ use syntax::{
@ -160,14 +161,11 @@ fn find_builtin_crate(db: &dyn AstDatabase, id: MacroCallId) -> tt::TokenTree {
let cg = db.crate_graph(); let cg = db.crate_graph();
let krate = db.lookup_intern_macro_call(id).krate; let krate = db.lookup_intern_macro_call(id).krate;
// XXX let tt = if matches!(cg[krate].origin, CrateOrigin::Lang(LangCrateOrigin::Core)) {
// 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 {
cov_mark::hit!(test_copy_expand_in_core); cov_mark::hit!(test_copy_expand_in_core);
quote! { crate } quote! { crate }
} else {
quote! { core }
}; };
tt.token_trees[0].clone() tt.token_trees[0].clone()

View file

@ -7,9 +7,9 @@
use arrayvec::ArrayVec; use arrayvec::ArrayVec;
use hir::{ use hir::{
Adt, AsAssocItem, AssocItem, BuiltinAttr, BuiltinType, Const, Field, Function, GenericParam, Adt, AsAssocItem, AssocItem, BuiltinAttr, BuiltinType, Const, Crate, Field, Function,
HasVisibility, Impl, ItemInNs, Label, Local, Macro, Module, ModuleDef, Name, PathResolution, GenericParam, HasVisibility, Impl, ItemInNs, Label, Local, Macro, Module, ModuleDef, Name,
Semantics, Static, ToolModule, Trait, TypeAlias, Variant, Visibility, PathResolution, Semantics, Static, ToolModule, Trait, TypeAlias, Variant, Visibility,
}; };
use stdx::impl_from; use stdx::impl_from;
use syntax::{ use syntax::{
@ -46,6 +46,13 @@ impl Definition {
self.module(db).map(|it| it.path_to_root(db).into_iter().rev()) 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> { pub fn module(&self, db: &RootDatabase) -> Option<Module> {
let module = match self { let module = match self {
Definition::Macro(it) => it.module(db), 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> { fn get_doc_link(db: &RootDatabase, def: Definition) -> Option<String> {
let (target, file, frag) = filename_and_frag_for_def(db, def)?; 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)?; let mut url = get_doc_base_url(db, krate)?;
if let Some(path) = mod_path_of_def(db, target) { 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 (link, ns) = parse_intra_doc_link(target);
let resolved = resolve_doc_path_for_def(db, def, link, ns)?; 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 mut url = get_doc_base_url(db, krate)?;
let (_, file, frag) = filename_and_frag_for_def(db, resolved)?; 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; return None;
} }
let krate = crate_of_def(db, def)?; let krate = def.krate(db)?;
let mut url = get_doc_base_url(db, krate)?; let mut url = get_doc_base_url(db, krate)?;
let (def, file, frag) = filename_and_frag_for_def(db, def)?; 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) 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> { fn mod_path_of_def(db: &RootDatabase, def: Definition) -> Option<String> {
def.canonical_module_path(db).map(|it| { def.canonical_module_path(db).map(|it| {
let mut path = String::new(); 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)), Definition::ToolModule(_) => Highlight::new(HlTag::Symbol(SymbolKind::ToolModule)),
}; };
let def_crate = def.module(db).map(hir::Module::krate).or_else(|| match def { let def_crate = def.krate(db);
Definition::Module(module) => Some(module.krate()),
_ => None,
});
let is_from_other_crate = def_crate != Some(krate); 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_from_builtin_crate = def_crate.map_or(false, |def_crate| def_crate.is_builtin(db));
let is_builtin_type = matches!(def, Definition::BuiltinType(_)); let is_builtin_type = matches!(def, Definition::BuiltinType(_));