Skip to content

Commit

Permalink
Auto merge of rust-lang#14454 - Veykril:crate-origins, r=Veykril
Browse files Browse the repository at this point in the history
internal: Refine CrateOrigin variants
  • Loading branch information
bors committed Mar 31, 2023
2 parents 42d671f + 31db1fc commit 419d59f
Show file tree
Hide file tree
Showing 12 changed files with 343 additions and 235 deletions.
17 changes: 10 additions & 7 deletions crates/base-db/src/fixture.rs
Original file line number Diff line number Diff line change
Expand Up @@ -166,6 +166,7 @@ impl ChangeFixture {
.as_deref()
.map(Arc::from)
.ok_or_else(|| "target_data_layout unset".into()),
None,
);
let prev = crates.insert(crate_name.clone(), crate_id);
assert!(prev.is_none());
Expand Down Expand Up @@ -200,10 +201,11 @@ impl ChangeFixture {
default_cfg,
Env::default(),
false,
CrateOrigin::CratesIo { repo: None, name: None },
CrateOrigin::Local { repo: None, name: None },
default_target_data_layout
.map(|x| x.into())
.ok_or_else(|| "target_data_layout unset".into()),
None,
);
} else {
for (from, to, prelude) in crate_deps {
Expand Down Expand Up @@ -245,6 +247,7 @@ impl ChangeFixture {
false,
CrateOrigin::Lang(LangCrateOrigin::Core),
target_layout.clone(),
None,
);

for krate in all_crates {
Expand Down Expand Up @@ -281,8 +284,9 @@ impl ChangeFixture {
CfgOptions::default(),
Env::default(),
true,
CrateOrigin::CratesIo { repo: None, name: None },
CrateOrigin::Local { repo: None, name: None },
target_layout,
None,
);
proc_macros.insert(proc_macros_crate, Ok(proc_macro));

Expand Down Expand Up @@ -427,18 +431,17 @@ fn parse_crate(crate_str: String) -> (String, CrateOrigin, Option<String>) {
let (version, origin) = match b.split_once(':') {
Some(("CratesIo", data)) => match data.split_once(',') {
Some((version, url)) => {
(version, CrateOrigin::CratesIo { repo: Some(url.to_owned()), name: None })
(version, CrateOrigin::Local { repo: Some(url.to_owned()), name: None })
}
_ => panic!("Bad crates.io parameter: {data}"),
},
_ => panic!("Bad string for crate origin: {b}"),
};
(a.to_owned(), origin, Some(version.to_string()))
} else {
let crate_origin = match &*crate_str {
"std" => CrateOrigin::Lang(LangCrateOrigin::Std),
"core" => CrateOrigin::Lang(LangCrateOrigin::Core),
_ => CrateOrigin::CratesIo { repo: None, name: None },
let crate_origin = match LangCrateOrigin::from(&*crate_str) {
LangCrateOrigin::Other => CrateOrigin::Local { repo: None, name: None },
origin => CrateOrigin::Lang(origin),
};
(crate_str, crate_origin, None)
}
Expand Down
70 changes: 57 additions & 13 deletions crates/base-db/src/input.rs
Original file line number Diff line number Diff line change
Expand Up @@ -135,8 +135,12 @@ impl ops::Deref for CrateName {
/// Origin of the crates. It is used in emitting monikers.
#[derive(Debug, Clone, PartialEq, Eq, Hash)]
pub enum CrateOrigin {
/// Crates that are from crates.io official registry,
CratesIo { repo: Option<String>, name: Option<String> },
/// Crates that are from the rustc workspace
Rustc { name: String },
/// Crates that are workspace members,
Local { repo: Option<String>, name: Option<String> },
/// Crates that are non member libraries.
Library { repo: Option<String>, name: String },
/// Crates that are provided by the language, like std, core, proc-macro, ...
Lang(LangCrateOrigin),
}
Expand Down Expand Up @@ -257,6 +261,32 @@ pub struct ProcMacro {
pub expander: Arc<dyn ProcMacroExpander>,
}

#[derive(Debug, Copy, Clone)]
pub enum ReleaseChannel {
Stable,
Beta,
Nightly,
}

impl ReleaseChannel {
pub fn as_str(self) -> &'static str {
match self {
ReleaseChannel::Stable => "stable",
ReleaseChannel::Beta => "beta",
ReleaseChannel::Nightly => "nightly",
}
}

pub fn from_str(str: &str) -> Option<Self> {
Some(match str {
"stable" => ReleaseChannel::Stable,
"beta" => ReleaseChannel::Beta,
"nightly" => ReleaseChannel::Nightly,
_ => return None,
})
}
}

#[derive(Debug, Clone)]
pub struct CrateData {
pub root_file_id: FileId,
Expand All @@ -271,11 +301,13 @@ pub struct CrateData {
pub display_name: Option<CrateDisplayName>,
pub cfg_options: CfgOptions,
pub potential_cfg_options: CfgOptions,
pub target_layout: TargetLayoutLoadResult,
pub env: Env,
pub dependencies: Vec<Dependency>,
pub origin: CrateOrigin,
pub is_proc_macro: bool,
// FIXME: These things should not be per crate! These are more per workspace crate graph level things
pub target_layout: TargetLayoutLoadResult,
pub channel: Option<ReleaseChannel>,
}

#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash)]
Expand Down Expand Up @@ -329,6 +361,7 @@ impl CrateGraph {
is_proc_macro: bool,
origin: CrateOrigin,
target_layout: Result<Arc<str>, Arc<str>>,
channel: Option<ReleaseChannel>,
) -> CrateId {
let data = CrateData {
root_file_id,
Expand All @@ -342,6 +375,7 @@ impl CrateGraph {
origin,
target_layout,
is_proc_macro,
channel,
};
let crate_id = CrateId(self.arena.len() as u32);
let prev = self.arena.insert(crate_id, data);
Expand Down Expand Up @@ -653,8 +687,9 @@ mod tests {
CfgOptions::default(),
Env::default(),
false,
CrateOrigin::CratesIo { repo: None, name: None },
CrateOrigin::Local { repo: None, name: None },
Err("".into()),
None,
);
let crate2 = graph.add_crate_root(
FileId(2u32),
Expand All @@ -665,8 +700,9 @@ mod tests {
CfgOptions::default(),
Env::default(),
false,
CrateOrigin::CratesIo { repo: None, name: None },
CrateOrigin::Local { repo: None, name: None },
Err("".into()),
None,
);
let crate3 = graph.add_crate_root(
FileId(3u32),
Expand All @@ -677,8 +713,9 @@ mod tests {
CfgOptions::default(),
Env::default(),
false,
CrateOrigin::CratesIo { repo: None, name: None },
CrateOrigin::Local { repo: None, name: None },
Err("".into()),
None,
);
assert!(graph
.add_dep(crate1, Dependency::new(CrateName::new("crate2").unwrap(), crate2))
Expand All @@ -703,8 +740,9 @@ mod tests {
CfgOptions::default(),
Env::default(),
false,
CrateOrigin::CratesIo { repo: None, name: None },
CrateOrigin::Local { repo: None, name: None },
Err("".into()),
None,
);
let crate2 = graph.add_crate_root(
FileId(2u32),
Expand All @@ -715,8 +753,9 @@ mod tests {
CfgOptions::default(),
Env::default(),
false,
CrateOrigin::CratesIo { repo: None, name: None },
CrateOrigin::Local { repo: None, name: None },
Err("".into()),
None,
);
assert!(graph
.add_dep(crate1, Dependency::new(CrateName::new("crate2").unwrap(), crate2))
Expand All @@ -738,8 +777,9 @@ mod tests {
CfgOptions::default(),
Env::default(),
false,
CrateOrigin::CratesIo { repo: None, name: None },
CrateOrigin::Local { repo: None, name: None },
Err("".into()),
None,
);
let crate2 = graph.add_crate_root(
FileId(2u32),
Expand All @@ -750,8 +790,9 @@ mod tests {
CfgOptions::default(),
Env::default(),
false,
CrateOrigin::CratesIo { repo: None, name: None },
CrateOrigin::Local { repo: None, name: None },
Err("".into()),
None,
);
let crate3 = graph.add_crate_root(
FileId(3u32),
Expand All @@ -762,8 +803,9 @@ mod tests {
CfgOptions::default(),
Env::default(),
false,
CrateOrigin::CratesIo { repo: None, name: None },
CrateOrigin::Local { repo: None, name: None },
Err("".into()),
None,
);
assert!(graph
.add_dep(crate1, Dependency::new(CrateName::new("crate2").unwrap(), crate2))
Expand All @@ -785,8 +827,9 @@ mod tests {
CfgOptions::default(),
Env::default(),
false,
CrateOrigin::CratesIo { repo: None, name: None },
CrateOrigin::Local { repo: None, name: None },
Err("".into()),
None,
);
let crate2 = graph.add_crate_root(
FileId(2u32),
Expand All @@ -797,8 +840,9 @@ mod tests {
CfgOptions::default(),
Env::default(),
false,
CrateOrigin::CratesIo { repo: None, name: None },
CrateOrigin::Local { repo: None, name: None },
Err("".into()),
None,
);
assert!(graph
.add_dep(
Expand Down
4 changes: 2 additions & 2 deletions crates/base-db/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,8 @@ pub use crate::{
input::{
CrateData, CrateDisplayName, CrateGraph, CrateId, CrateName, CrateOrigin, Dependency,
Edition, Env, LangCrateOrigin, ProcMacro, ProcMacroExpander, ProcMacroExpansionError,
ProcMacroId, ProcMacroKind, ProcMacroLoadResult, ProcMacroPaths, ProcMacros, SourceRoot,
SourceRootId, TargetLayoutLoadResult,
ProcMacroId, ProcMacroKind, ProcMacroLoadResult, ProcMacroPaths, ProcMacros,
ReleaseChannel, SourceRoot, SourceRootId, TargetLayoutLoadResult,
},
};
pub use salsa::{self, Cancelled};
Expand Down
31 changes: 26 additions & 5 deletions crates/ide/src/doc_links.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ use url::Url;

use hir::{db::HirDatabase, Adt, AsAssocItem, AssocItem, AssocItemContainer, HasAttrs};
use ide_db::{
base_db::{CrateOrigin, LangCrateOrigin, SourceDatabase},
base_db::{CrateOrigin, LangCrateOrigin, ReleaseChannel, SourceDatabase},
defs::{Definition, NameClass, NameRefClass},
helpers::pick_best_token,
RootDatabase,
Expand Down Expand Up @@ -436,8 +436,9 @@ fn get_doc_base_url(db: &RootDatabase, def: Definition) -> Option<Url> {

let krate = def.krate(db)?;
let display_name = krate.display_name(db)?;

let base = match db.crate_graph()[krate.into()].origin {
let crate_data = &db.crate_graph()[krate.into()];
let channel = crate_data.channel.map_or("nightly", ReleaseChannel::as_str);
let base = match &crate_data.origin {
// std and co do not specify `html_root_url` any longer so we gotta handwrite this ourself.
// FIXME: Use the toolchains channel instead of nightly
CrateOrigin::Lang(
Expand All @@ -447,9 +448,14 @@ fn get_doc_base_url(db: &RootDatabase, def: Definition) -> Option<Url> {
| LangCrateOrigin::Std
| LangCrateOrigin::Test),
) => {
format!("https://doc.rust-lang.org/nightly/{origin}")
format!("https://doc.rust-lang.org/{channel}/{origin}")
}
CrateOrigin::Lang(_) => return None,
CrateOrigin::Rustc { name: _ } => {
format!("https://doc.rust-lang.org/{channel}/nightly-rustc/")
}
_ => {
CrateOrigin::Local { repo: _, name: _ } => {
// FIXME: These should not attempt to link to docs.rs!
krate.get_html_root_url(db).or_else(|| {
let version = krate.version(db);
// Fallback to docs.rs. This uses `display_name` and can never be
Expand All @@ -464,6 +470,21 @@ fn get_doc_base_url(db: &RootDatabase, def: Definition) -> Option<Url> {
))
})?
}
CrateOrigin::Library { repo: _, name } => {
krate.get_html_root_url(db).or_else(|| {
let version = krate.version(db);
// Fallback to docs.rs. This uses `display_name` and can never be
// correct, but that's what fallbacks are about.
//
// FIXME: clicking on the link should just open the file in the editor,
// instead of falling back to external urls.
Some(format!(
"https://docs.rs/{krate}/{version}/",
krate = name,
version = version.as_deref().unwrap_or("*")
))
})?
}
};
Url::parse(&base).ok()?.join(&format!("{display_name}/")).ok()
}
Expand Down
3 changes: 2 additions & 1 deletion crates/ide/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -240,8 +240,9 @@ impl Analysis {
cfg_options,
Env::default(),
false,
CrateOrigin::CratesIo { repo: None, name: None },
CrateOrigin::Local { repo: None, name: None },
Err("Analysis::from_single_file has no target layout".into()),
None,
);
change.change_file(file_id, Some(Arc::new(text)));
change.set_crate_graph(crate_graph);
Expand Down
8 changes: 7 additions & 1 deletion crates/ide/src/moniker.rs
Original file line number Diff line number Diff line change
Expand Up @@ -245,11 +245,17 @@ pub(crate) fn def_to_moniker(
kind: if krate == from_crate { MonikerKind::Export } else { MonikerKind::Import },
package_information: {
let (name, repo, version) = match krate.origin(db) {
CrateOrigin::CratesIo { repo, name } => (
CrateOrigin::Library { repo, name } => (name, repo, krate.version(db)),
CrateOrigin::Local { repo, name } => (
name.unwrap_or(krate.display_name(db)?.canonical_name().to_string()),
repo,
krate.version(db),
),
CrateOrigin::Rustc { name } => (
name.clone(),
Some("https://github.com/rust-lang/rust/".to_string()),
Some(format!("https://github.com/rust-lang/rust/compiler/{name}",)),
),
CrateOrigin::Lang(lang) => (
krate.display_name(db)?.canonical_name().to_string(),
Some("https://github.com/rust-lang/rust/".to_string()),
Expand Down
1 change: 1 addition & 0 deletions crates/ide/src/shuffle_crate_graph.rs
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ pub(crate) fn shuffle_crate_graph(db: &mut RootDatabase) {
data.is_proc_macro,
data.origin.clone(),
data.target_layout.clone(),
data.channel,
);
new_proc_macros.insert(new_id, proc_macros[&old_id].clone());
map.insert(old_id, new_id);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,5 +43,5 @@
.unresolved_reference { color: #FC5555; text-decoration: wavy underline; }
</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>
<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>
<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>
</code></pre>
Loading

0 comments on commit 419d59f

Please sign in to comment.