Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
71 changes: 56 additions & 15 deletions crates/base-db/src/input.rs
Original file line number Diff line number Diff line change
Expand Up @@ -298,6 +298,47 @@ impl CrateDisplayName {
CrateDisplayName { crate_name, canonical_name: Symbol::intern(canonical_name) }
}
}
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
pub enum TargetKind {
Bin,
/// Any kind of Cargo lib crate-type (dylib, rlib, proc-macro, ...).
Lib {
/// Is this target a proc-macro
is_proc_macro: bool,
},
Example,
Test,
Bench,
/// Cargo calls this kind `custom-build`
BuildScript,
Other,
}

impl TargetKind {
pub fn is_executable(self) -> bool {
matches!(self, TargetKind::Bin | TargetKind::Example)
}

pub fn is_proc_macro(self) -> bool {
matches!(self, TargetKind::Lib { is_proc_macro: true })
}

/// If this is a valid cargo target, returns the name cargo uses in command line arguments
/// and output, otherwise None.
/// <https://docs.rs/cargo_metadata/latest/cargo_metadata/enum.TargetKind.html>
pub fn as_cargo_target(self) -> Option<&'static str> {
match self {
TargetKind::Bin => Some("bin"),
TargetKind::Lib { is_proc_macro: true } => Some("proc-macro"),
TargetKind::Lib { is_proc_macro: false } => Some("lib"),
TargetKind::Example => Some("example"),
TargetKind::Test => Some("test"),
TargetKind::Bench => Some("bench"),
TargetKind::BuildScript => Some("custom-build"),
TargetKind::Other => None,
}
}
}

#[derive(Debug, Copy, Clone, PartialEq, Eq, Hash, PartialOrd, Ord)]
pub enum ReleaseChannel {
Expand Down Expand Up @@ -357,7 +398,7 @@ pub struct CrateData<Id> {
pub origin: CrateOrigin,
/// Extra crate-level attributes, including the surrounding `#![]`.
pub crate_attrs: Box<[Box<str>]>,
pub is_proc_macro: bool,
pub target_kind: TargetKind,
/// The working directory to run proc-macros in invoked in the context of this crate.
/// This is the workspace root of the cargo workspace for workspace members, the crate manifest
/// dir otherwise.
Expand Down Expand Up @@ -537,7 +578,7 @@ impl CrateGraphBuilder {
mut env: Env,
origin: CrateOrigin,
crate_attrs: Vec<String>,
is_proc_macro: bool,
target_kind: TargetKind,
proc_macro_cwd: Arc<AbsPathBuf>,
ws_data: Arc<CrateWorkspaceData>,
) -> CrateBuilderId {
Expand All @@ -557,7 +598,7 @@ impl CrateGraphBuilder {
dependencies: Vec::new(),
origin,
crate_attrs: crate_attrs.into_boxed_slice(),
is_proc_macro,
target_kind,
proc_macro_cwd,
},
extra: ExtraCrateData { version, display_name, potential_cfg_options },
Expand Down Expand Up @@ -655,7 +696,7 @@ impl CrateGraphBuilder {
let crate_data = BuiltCrateData {
dependencies,
edition: krate.basic.edition,
is_proc_macro: krate.basic.is_proc_macro,
target_kind: krate.basic.target_kind,
origin: krate.basic.origin.clone(),
crate_attrs: krate.basic.crate_attrs.clone(),
root_file_id: krate.basic.root_file_id,
Expand Down Expand Up @@ -986,7 +1027,7 @@ mod tests {
use triomphe::Arc;
use vfs::AbsPathBuf;

use crate::{CrateWorkspaceData, DependencyBuilder};
use crate::{CrateWorkspaceData, DependencyBuilder, TargetKind};

use super::{CrateGraphBuilder, CrateName, CrateOrigin, Edition::Edition2018, Env, FileId};

Expand All @@ -1007,7 +1048,7 @@ mod tests {
Env::default(),
CrateOrigin::Local { repo: None, name: None },
Vec::new(),
false,
TargetKind::Lib { is_proc_macro: false },
Arc::new(AbsPathBuf::assert_utf8(std::env::current_dir().unwrap())),
empty_ws_data(),
);
Expand All @@ -1021,7 +1062,7 @@ mod tests {
Env::default(),
CrateOrigin::Local { repo: None, name: None },
Vec::new(),
false,
TargetKind::Lib { is_proc_macro: false },
Arc::new(AbsPathBuf::assert_utf8(std::env::current_dir().unwrap())),
empty_ws_data(),
);
Expand All @@ -1035,7 +1076,7 @@ mod tests {
Env::default(),
CrateOrigin::Local { repo: None, name: None },
Vec::new(),
false,
TargetKind::Lib { is_proc_macro: false },
Arc::new(AbsPathBuf::assert_utf8(std::env::current_dir().unwrap())),
empty_ws_data(),
);
Expand Down Expand Up @@ -1069,7 +1110,7 @@ mod tests {
Env::default(),
CrateOrigin::Local { repo: None, name: None },
Vec::new(),
false,
TargetKind::Lib { is_proc_macro: false },
Arc::new(AbsPathBuf::assert_utf8(std::env::current_dir().unwrap())),
empty_ws_data(),
);
Expand All @@ -1083,7 +1124,7 @@ mod tests {
Env::default(),
CrateOrigin::Local { repo: None, name: None },
Vec::new(),
false,
TargetKind::Lib { is_proc_macro: false },
Arc::new(AbsPathBuf::assert_utf8(std::env::current_dir().unwrap())),
empty_ws_data(),
);
Expand Down Expand Up @@ -1112,7 +1153,7 @@ mod tests {
Env::default(),
CrateOrigin::Local { repo: None, name: None },
Vec::new(),
false,
TargetKind::Lib { is_proc_macro: false },
Arc::new(AbsPathBuf::assert_utf8(std::env::current_dir().unwrap())),
empty_ws_data(),
);
Expand All @@ -1126,7 +1167,7 @@ mod tests {
Env::default(),
CrateOrigin::Local { repo: None, name: None },
Vec::new(),
false,
TargetKind::Lib { is_proc_macro: false },
Arc::new(AbsPathBuf::assert_utf8(std::env::current_dir().unwrap())),
empty_ws_data(),
);
Expand All @@ -1140,7 +1181,7 @@ mod tests {
Env::default(),
CrateOrigin::Local { repo: None, name: None },
Vec::new(),
false,
TargetKind::Lib { is_proc_macro: false },
Arc::new(AbsPathBuf::assert_utf8(std::env::current_dir().unwrap())),
empty_ws_data(),
);
Expand Down Expand Up @@ -1169,7 +1210,7 @@ mod tests {
Env::default(),
CrateOrigin::Local { repo: None, name: None },
Vec::new(),
false,
TargetKind::Lib { is_proc_macro: false },
Arc::new(AbsPathBuf::assert_utf8(std::env::current_dir().unwrap())),
empty_ws_data(),
);
Expand All @@ -1183,7 +1224,7 @@ mod tests {
Env::default(),
CrateOrigin::Local { repo: None, name: None },
Vec::new(),
false,
TargetKind::Lib { is_proc_macro: false },
Arc::new(AbsPathBuf::assert_utf8(std::env::current_dir().unwrap())),
empty_ws_data(),
);
Expand Down
2 changes: 1 addition & 1 deletion crates/base-db/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ pub use crate::{
BuiltCrateData, BuiltDependency, Crate, CrateBuilder, CrateBuilderId, CrateDataBuilder,
CrateDisplayName, CrateGraphBuilder, CrateName, CrateOrigin, CratesIdMap, CratesMap,
DependencyBuilder, Env, ExtraCrateData, LangCrateOrigin, ProcMacroLoadingError,
ProcMacroPaths, ReleaseChannel, SourceRoot, SourceRootId, UniqueCrateData,
ProcMacroPaths, ReleaseChannel, SourceRoot, SourceRootId, TargetKind, UniqueCrateData,
},
};
use dashmap::{DashMap, mapref::entry::Entry};
Expand Down
4 changes: 2 additions & 2 deletions crates/hir-def/src/nameres/collector.rs
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ pub(super) fn collect_defs(
deps.insert(dep.as_name(), dep.clone());
}

let proc_macros = if krate.is_proc_macro {
let proc_macros = if krate.target_kind.is_proc_macro() {
ProcMacros::get_for_crate(db, def_map.krate)
.and_then(|proc_macros| {
proc_macros.list(tree_id.file_id().syntax_context(db, krate.edition))
Expand All @@ -105,7 +105,7 @@ pub(super) fn collect_defs(
skip_attrs: Default::default(),
prev_active_attrs: Default::default(),
unresolved_extern_crates: Default::default(),
is_proc_macro: krate.is_proc_macro,
is_proc_macro: krate.target_kind.is_proc_macro(),
deferred_builtin_derives: Default::default(),
};
if tree_id.is_block() {
Expand Down
4 changes: 2 additions & 2 deletions crates/hir-def/src/nameres/tests/incremental.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use base_db::{
CrateDisplayName, CrateGraphBuilder, CrateName, CrateOrigin, CrateWorkspaceData,
DependencyBuilder, Env, SourceDatabase, all_crates,
DependencyBuilder, Env, SourceDatabase, TargetKind, all_crates,
};
use expect_test::{Expect, expect};
use intern::Symbol;
Expand Down Expand Up @@ -77,7 +77,7 @@ pub const BAZ: u32 = 0;
Env::default(),
CrateOrigin::Local { repo: None, name: Some(Symbol::intern(crate_name)) },
Vec::new(),
false,
TargetKind::Lib { is_proc_macro: false },
Arc::new(
// FIXME: This is less than ideal
TryFrom::try_from(
Expand Down
8 changes: 7 additions & 1 deletion crates/hir/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,9 @@ use std::{
};

use arrayvec::ArrayVec;
use base_db::{CrateDisplayName, CrateOrigin, LangCrateOrigin, SourceDatabase, all_crates};
use base_db::{
CrateDisplayName, CrateOrigin, LangCrateOrigin, SourceDatabase, TargetKind, all_crates,
};
use either::Either;
use hir_def::{
AdtId, AssocItemId, AssocItemLoc, BuiltinDeriveImplId, CallableDefId, ConstId, ConstParamId,
Expand Down Expand Up @@ -326,6 +328,10 @@ impl Crate {
self.id.extra_data(db).display_name.clone()
}

pub fn target_kind(self, db: &dyn HirDatabase) -> TargetKind {
self.id.data(db).target_kind
}

pub fn query_external_importables(
self,
db: &dyn SourceDatabase,
Expand Down
5 changes: 3 additions & 2 deletions crates/ide/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,7 @@ use std::time::Duration;
use cfg::CfgOptions;
use fetch_crates::CrateInfo;
use hir::{ChangeWithProcMacros, EditionedFileId, crate_def_map, sym};
use ide_db::base_db::TargetKind;
use ide_db::{
FxHashMap, FxIndexSet,
base_db::{
Expand Down Expand Up @@ -280,7 +281,7 @@ impl Analysis {
Env::default(),
CrateOrigin::Local { repo: None, name: None },
crate_attrs,
false,
TargetKind::Lib { is_proc_macro: false },
proc_macro_cwd,
Arc::new(CrateWorkspaceData {
target: Err("fixture has no layout".into()),
Expand Down Expand Up @@ -694,7 +695,7 @@ impl Analysis {

/// Returns whether the given crate is a proc macro.
pub fn is_proc_macro_crate(&self, crate_id: Crate) -> Cancellable<bool> {
self.with_db(|db| crate_id.data(db).is_proc_macro)
self.with_db(|db| crate_id.data(db).target_kind.is_proc_macro())
}

/// Returns true if this crate has `no_std` or `no_core` specified.
Expand Down
42 changes: 31 additions & 11 deletions crates/ide/src/moniker.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ use core::fmt;
use hir::{Adt, AsAssocItem, Crate, HirDisplay, MacroKind, Semantics};
use ide_db::{
FilePosition, RootDatabase,
base_db::{CrateOrigin, LangCrateOrigin},
base_db::{CrateOrigin, LangCrateOrigin, TargetKind},
defs::{Definition, IdentClass},
helpers::pick_best_token,
};
Expand Down Expand Up @@ -322,8 +322,28 @@ fn def_to_non_local_moniker(
} else {
match def {
Definition::Module(module) if module.is_crate_root(db) => {
// only include `crate` namespace by itself because we prefer
// `rust-analyzer cargo foo . bar/` over `rust-analyzer cargo foo . crate/bar/`
let krate = module.krate(db);

// There can be only one lib, proc-macro or build script per crates, so we don't need to specify the display name in these cases.
if !matches!(
krate.target_kind(db),
TargetKind::Lib { .. } | TargetKind::BuildScript
) && let Some(display_name) = krate.display_name(db)
{
reverse_description.push(MonikerDescriptor {
name: display_name.to_string(),
desc: MonikerDescriptorKind::Namespace,
});
}

if let Some(target_type) = krate.target_kind(db).as_cargo_target() {
reverse_description.push(MonikerDescriptor {
name: target_type.to_owned(),
desc: MonikerDescriptorKind::Namespace,
});
}

// Fallback in case we didn't get anything: when we have a target_kind of Other and no display name.
if reverse_description.is_empty() {
reverse_description.push(MonikerDescriptor {
name: "crate".to_owned(),
Expand Down Expand Up @@ -475,7 +495,7 @@ pub mod module {
pub fn func() {}
}
"#,
"foo::module::func",
"foo::lib::module::func",
r#"PackageInformation { name: "foo", repo: Some("https://a.b/foo.git"), version: Some("0.1.0") }"#,
MonikerKind::Import,
);
Expand All @@ -491,7 +511,7 @@ pub mod module {
pub fn func$0() {}
}
"#,
"foo::module::func",
"foo::lib::module::func",
r#"PackageInformation { name: "foo", repo: Some("https://a.b/foo.git"), version: Some("0.1.0") }"#,
MonikerKind::Export,
);
Expand All @@ -508,7 +528,7 @@ pub mod module {
}
}
"#,
"foo::module::MyTrait::func",
"foo::lib::module::MyTrait::func",
r#"PackageInformation { name: "foo", repo: Some("https://a.b/foo.git"), version: Some("0.1.0") }"#,
MonikerKind::Export,
);
Expand All @@ -525,7 +545,7 @@ pub mod module {
}
}
"#,
"foo::module::MyTrait::MY_CONST",
"foo::lib::module::MyTrait::MY_CONST",
r#"PackageInformation { name: "foo", repo: Some("https://a.b/foo.git"), version: Some("0.1.0") }"#,
MonikerKind::Export,
);
Expand All @@ -542,7 +562,7 @@ pub mod module {
}
}
"#,
"foo::module::MyTrait::MyType",
"foo::lib::module::MyTrait::MyType",
r#"PackageInformation { name: "foo", repo: Some("https://a.b/foo.git"), version: Some("0.1.0") }"#,
MonikerKind::Export,
);
Expand All @@ -563,7 +583,7 @@ pub mod module {
}
}
"#,
"foo::module::impl::MyStruct::MyTrait::func",
"foo::lib::module::impl::MyStruct::MyTrait::func",
r#"PackageInformation { name: "foo", repo: Some("https://a.b/foo.git"), version: Some("0.1.0") }"#,
MonikerKind::Export,
);
Expand All @@ -583,7 +603,7 @@ pub struct St {
pub a: i32,
}
"#,
"foo::St::a",
"foo::lib::St::a",
r#"PackageInformation { name: "foo", repo: Some("https://a.b/foo.git"), version: Some("0.1.0") }"#,
MonikerKind::Import,
);
Expand All @@ -605,7 +625,7 @@ pub mod module {
}
}
"#,
"foo::module::func",
"foo::lib::module::func",
r#"PackageInformation { name: "foo", repo: Some("https://a.b/foo.git"), version: Some("0.1.0") }"#,
MonikerKind::Export,
);
Expand Down
Loading