From 88e297e47deb63b249b83fb5fe153fed365407d6 Mon Sep 17 00:00:00 2001 From: Lukas Wirth Date: Fri, 20 May 2022 16:42:28 +0200 Subject: [PATCH] minor: Simplify --- crates/base-db/src/fixture.rs | 1 + .../macro_expansion_tests/builtin_derive_macro.rs | 1 + crates/hir-expand/src/builtin_derive_macro.rs | 10 ++++------ crates/ide-db/src/defs.rs | 13 ++++++++++--- crates/ide/src/doc_links.rs | 15 +++------------ crates/ide/src/syntax_highlighting/highlight.rs | 5 +---- 6 files changed, 20 insertions(+), 25 deletions(-) diff --git a/crates/base-db/src/fixture.rs b/crates/base-db/src/fixture.rs index daa0e3f6997..69585b44a1a 100644 --- a/crates/base-db/src/fixture.rs +++ b/crates/base-db/src/fixture.rs @@ -408,6 +408,7 @@ fn parse_crate(crate_str: String) -> (String, CrateOrigin, Option) { } 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) diff --git a/crates/hir-def/src/macro_expansion_tests/builtin_derive_macro.rs b/crates/hir-def/src/macro_expansion_tests/builtin_derive_macro.rs index 8ee33101e4d..6819e9114a0 100644 --- a/crates/hir-def/src/macro_expansion_tests/builtin_derive_macro.rs +++ b/crates/hir-def/src/macro_expansion_tests/builtin_derive_macro.rs @@ -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] diff --git a/crates/hir-expand/src/builtin_derive_macro.rs b/crates/hir-expand/src/builtin_derive_macro.rs index f7d5532df5e..79989bc2e38 100644 --- a/crates/hir-expand/src/builtin_derive_macro.rs +++ b/crates/hir-expand/src/builtin_derive_macro.rs @@ -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() diff --git a/crates/ide-db/src/defs.rs b/crates/ide-db/src/defs.rs index bea6f24523d..540cc70dd52 100644 --- a/crates/ide-db/src/defs.rs +++ b/crates/ide-db/src/defs.rs @@ -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 { + Some(match self { + Definition::Module(m) => m.krate(), + _ => self.module(db)?.krate(), + }) + } + pub fn module(&self, db: &RootDatabase) -> Option { let module = match self { Definition::Macro(it) => it.module(db), diff --git a/crates/ide/src/doc_links.rs b/crates/ide/src/doc_links.rs index 9ad11b7a653..5c6363f3785 100644 --- a/crates/ide/src/doc_links.rs +++ b/crates/ide/src/doc_links.rs @@ -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 { 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 { - 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 { def.canonical_module_path(db).map(|it| { let mut path = String::new(); diff --git a/crates/ide/src/syntax_highlighting/highlight.rs b/crates/ide/src/syntax_highlighting/highlight.rs index e04fd5a7c7f..198e17db535 100644 --- a/crates/ide/src/syntax_highlighting/highlight.rs +++ b/crates/ide/src/syntax_highlighting/highlight.rs @@ -466,10 +466,7 @@ fn highlight_def(sema: &Semantics, 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(_));