Skip to content

Commit 419d59f

Browse files
committed
Auto merge of rust-lang#14454 - Veykril:crate-origins, r=Veykril
internal: Refine CrateOrigin variants
2 parents 42d671f + 31db1fc commit 419d59f

File tree

12 files changed

+343
-235
lines changed

12 files changed

+343
-235
lines changed

crates/base-db/src/fixture.rs

+10-7
Original file line numberDiff line numberDiff line change
@@ -166,6 +166,7 @@ impl ChangeFixture {
166166
.as_deref()
167167
.map(Arc::from)
168168
.ok_or_else(|| "target_data_layout unset".into()),
169+
None,
169170
);
170171
let prev = crates.insert(crate_name.clone(), crate_id);
171172
assert!(prev.is_none());
@@ -200,10 +201,11 @@ impl ChangeFixture {
200201
default_cfg,
201202
Env::default(),
202203
false,
203-
CrateOrigin::CratesIo { repo: None, name: None },
204+
CrateOrigin::Local { repo: None, name: None },
204205
default_target_data_layout
205206
.map(|x| x.into())
206207
.ok_or_else(|| "target_data_layout unset".into()),
208+
None,
207209
);
208210
} else {
209211
for (from, to, prelude) in crate_deps {
@@ -245,6 +247,7 @@ impl ChangeFixture {
245247
false,
246248
CrateOrigin::Lang(LangCrateOrigin::Core),
247249
target_layout.clone(),
250+
None,
248251
);
249252

250253
for krate in all_crates {
@@ -281,8 +284,9 @@ impl ChangeFixture {
281284
CfgOptions::default(),
282285
Env::default(),
283286
true,
284-
CrateOrigin::CratesIo { repo: None, name: None },
287+
CrateOrigin::Local { repo: None, name: None },
285288
target_layout,
289+
None,
286290
);
287291
proc_macros.insert(proc_macros_crate, Ok(proc_macro));
288292

@@ -427,18 +431,17 @@ fn parse_crate(crate_str: String) -> (String, CrateOrigin, Option<String>) {
427431
let (version, origin) = match b.split_once(':') {
428432
Some(("CratesIo", data)) => match data.split_once(',') {
429433
Some((version, url)) => {
430-
(version, CrateOrigin::CratesIo { repo: Some(url.to_owned()), name: None })
434+
(version, CrateOrigin::Local { repo: Some(url.to_owned()), name: None })
431435
}
432436
_ => panic!("Bad crates.io parameter: {data}"),
433437
},
434438
_ => panic!("Bad string for crate origin: {b}"),
435439
};
436440
(a.to_owned(), origin, Some(version.to_string()))
437441
} else {
438-
let crate_origin = match &*crate_str {
439-
"std" => CrateOrigin::Lang(LangCrateOrigin::Std),
440-
"core" => CrateOrigin::Lang(LangCrateOrigin::Core),
441-
_ => CrateOrigin::CratesIo { repo: None, name: None },
442+
let crate_origin = match LangCrateOrigin::from(&*crate_str) {
443+
LangCrateOrigin::Other => CrateOrigin::Local { repo: None, name: None },
444+
origin => CrateOrigin::Lang(origin),
442445
};
443446
(crate_str, crate_origin, None)
444447
}

crates/base-db/src/input.rs

+57-13
Original file line numberDiff line numberDiff line change
@@ -135,8 +135,12 @@ impl ops::Deref for CrateName {
135135
/// Origin of the crates. It is used in emitting monikers.
136136
#[derive(Debug, Clone, PartialEq, Eq, Hash)]
137137
pub enum CrateOrigin {
138-
/// Crates that are from crates.io official registry,
139-
CratesIo { repo: Option<String>, name: Option<String> },
138+
/// Crates that are from the rustc workspace
139+
Rustc { name: String },
140+
/// Crates that are workspace members,
141+
Local { repo: Option<String>, name: Option<String> },
142+
/// Crates that are non member libraries.
143+
Library { repo: Option<String>, name: String },
140144
/// Crates that are provided by the language, like std, core, proc-macro, ...
141145
Lang(LangCrateOrigin),
142146
}
@@ -257,6 +261,32 @@ pub struct ProcMacro {
257261
pub expander: Arc<dyn ProcMacroExpander>,
258262
}
259263

264+
#[derive(Debug, Copy, Clone)]
265+
pub enum ReleaseChannel {
266+
Stable,
267+
Beta,
268+
Nightly,
269+
}
270+
271+
impl ReleaseChannel {
272+
pub fn as_str(self) -> &'static str {
273+
match self {
274+
ReleaseChannel::Stable => "stable",
275+
ReleaseChannel::Beta => "beta",
276+
ReleaseChannel::Nightly => "nightly",
277+
}
278+
}
279+
280+
pub fn from_str(str: &str) -> Option<Self> {
281+
Some(match str {
282+
"stable" => ReleaseChannel::Stable,
283+
"beta" => ReleaseChannel::Beta,
284+
"nightly" => ReleaseChannel::Nightly,
285+
_ => return None,
286+
})
287+
}
288+
}
289+
260290
#[derive(Debug, Clone)]
261291
pub struct CrateData {
262292
pub root_file_id: FileId,
@@ -271,11 +301,13 @@ pub struct CrateData {
271301
pub display_name: Option<CrateDisplayName>,
272302
pub cfg_options: CfgOptions,
273303
pub potential_cfg_options: CfgOptions,
274-
pub target_layout: TargetLayoutLoadResult,
275304
pub env: Env,
276305
pub dependencies: Vec<Dependency>,
277306
pub origin: CrateOrigin,
278307
pub is_proc_macro: bool,
308+
// FIXME: These things should not be per crate! These are more per workspace crate graph level things
309+
pub target_layout: TargetLayoutLoadResult,
310+
pub channel: Option<ReleaseChannel>,
279311
}
280312

281313
#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash)]
@@ -329,6 +361,7 @@ impl CrateGraph {
329361
is_proc_macro: bool,
330362
origin: CrateOrigin,
331363
target_layout: Result<Arc<str>, Arc<str>>,
364+
channel: Option<ReleaseChannel>,
332365
) -> CrateId {
333366
let data = CrateData {
334367
root_file_id,
@@ -342,6 +375,7 @@ impl CrateGraph {
342375
origin,
343376
target_layout,
344377
is_proc_macro,
378+
channel,
345379
};
346380
let crate_id = CrateId(self.arena.len() as u32);
347381
let prev = self.arena.insert(crate_id, data);
@@ -653,8 +687,9 @@ mod tests {
653687
CfgOptions::default(),
654688
Env::default(),
655689
false,
656-
CrateOrigin::CratesIo { repo: None, name: None },
690+
CrateOrigin::Local { repo: None, name: None },
657691
Err("".into()),
692+
None,
658693
);
659694
let crate2 = graph.add_crate_root(
660695
FileId(2u32),
@@ -665,8 +700,9 @@ mod tests {
665700
CfgOptions::default(),
666701
Env::default(),
667702
false,
668-
CrateOrigin::CratesIo { repo: None, name: None },
703+
CrateOrigin::Local { repo: None, name: None },
669704
Err("".into()),
705+
None,
670706
);
671707
let crate3 = graph.add_crate_root(
672708
FileId(3u32),
@@ -677,8 +713,9 @@ mod tests {
677713
CfgOptions::default(),
678714
Env::default(),
679715
false,
680-
CrateOrigin::CratesIo { repo: None, name: None },
716+
CrateOrigin::Local { repo: None, name: None },
681717
Err("".into()),
718+
None,
682719
);
683720
assert!(graph
684721
.add_dep(crate1, Dependency::new(CrateName::new("crate2").unwrap(), crate2))
@@ -703,8 +740,9 @@ mod tests {
703740
CfgOptions::default(),
704741
Env::default(),
705742
false,
706-
CrateOrigin::CratesIo { repo: None, name: None },
743+
CrateOrigin::Local { repo: None, name: None },
707744
Err("".into()),
745+
None,
708746
);
709747
let crate2 = graph.add_crate_root(
710748
FileId(2u32),
@@ -715,8 +753,9 @@ mod tests {
715753
CfgOptions::default(),
716754
Env::default(),
717755
false,
718-
CrateOrigin::CratesIo { repo: None, name: None },
756+
CrateOrigin::Local { repo: None, name: None },
719757
Err("".into()),
758+
None,
720759
);
721760
assert!(graph
722761
.add_dep(crate1, Dependency::new(CrateName::new("crate2").unwrap(), crate2))
@@ -738,8 +777,9 @@ mod tests {
738777
CfgOptions::default(),
739778
Env::default(),
740779
false,
741-
CrateOrigin::CratesIo { repo: None, name: None },
780+
CrateOrigin::Local { repo: None, name: None },
742781
Err("".into()),
782+
None,
743783
);
744784
let crate2 = graph.add_crate_root(
745785
FileId(2u32),
@@ -750,8 +790,9 @@ mod tests {
750790
CfgOptions::default(),
751791
Env::default(),
752792
false,
753-
CrateOrigin::CratesIo { repo: None, name: None },
793+
CrateOrigin::Local { repo: None, name: None },
754794
Err("".into()),
795+
None,
755796
);
756797
let crate3 = graph.add_crate_root(
757798
FileId(3u32),
@@ -762,8 +803,9 @@ mod tests {
762803
CfgOptions::default(),
763804
Env::default(),
764805
false,
765-
CrateOrigin::CratesIo { repo: None, name: None },
806+
CrateOrigin::Local { repo: None, name: None },
766807
Err("".into()),
808+
None,
767809
);
768810
assert!(graph
769811
.add_dep(crate1, Dependency::new(CrateName::new("crate2").unwrap(), crate2))
@@ -785,8 +827,9 @@ mod tests {
785827
CfgOptions::default(),
786828
Env::default(),
787829
false,
788-
CrateOrigin::CratesIo { repo: None, name: None },
830+
CrateOrigin::Local { repo: None, name: None },
789831
Err("".into()),
832+
None,
790833
);
791834
let crate2 = graph.add_crate_root(
792835
FileId(2u32),
@@ -797,8 +840,9 @@ mod tests {
797840
CfgOptions::default(),
798841
Env::default(),
799842
false,
800-
CrateOrigin::CratesIo { repo: None, name: None },
843+
CrateOrigin::Local { repo: None, name: None },
801844
Err("".into()),
845+
None,
802846
);
803847
assert!(graph
804848
.add_dep(

crates/base-db/src/lib.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,8 @@ pub use crate::{
1616
input::{
1717
CrateData, CrateDisplayName, CrateGraph, CrateId, CrateName, CrateOrigin, Dependency,
1818
Edition, Env, LangCrateOrigin, ProcMacro, ProcMacroExpander, ProcMacroExpansionError,
19-
ProcMacroId, ProcMacroKind, ProcMacroLoadResult, ProcMacroPaths, ProcMacros, SourceRoot,
20-
SourceRootId, TargetLayoutLoadResult,
19+
ProcMacroId, ProcMacroKind, ProcMacroLoadResult, ProcMacroPaths, ProcMacros,
20+
ReleaseChannel, SourceRoot, SourceRootId, TargetLayoutLoadResult,
2121
},
2222
};
2323
pub use salsa::{self, Cancelled};

crates/ide/src/doc_links.rs

+26-5
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ use url::Url;
1212

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

437437
let krate = def.krate(db)?;
438438
let display_name = krate.display_name(db)?;
439-
440-
let base = match db.crate_graph()[krate.into()].origin {
439+
let crate_data = &db.crate_graph()[krate.into()];
440+
let channel = crate_data.channel.map_or("nightly", ReleaseChannel::as_str);
441+
let base = match &crate_data.origin {
441442
// std and co do not specify `html_root_url` any longer so we gotta handwrite this ourself.
442443
// FIXME: Use the toolchains channel instead of nightly
443444
CrateOrigin::Lang(
@@ -447,9 +448,14 @@ fn get_doc_base_url(db: &RootDatabase, def: Definition) -> Option<Url> {
447448
| LangCrateOrigin::Std
448449
| LangCrateOrigin::Test),
449450
) => {
450-
format!("https://doc.rust-lang.org/nightly/{origin}")
451+
format!("https://doc.rust-lang.org/{channel}/{origin}")
452+
}
453+
CrateOrigin::Lang(_) => return None,
454+
CrateOrigin::Rustc { name: _ } => {
455+
format!("https://doc.rust-lang.org/{channel}/nightly-rustc/")
451456
}
452-
_ => {
457+
CrateOrigin::Local { repo: _, name: _ } => {
458+
// FIXME: These should not attempt to link to docs.rs!
453459
krate.get_html_root_url(db).or_else(|| {
454460
let version = krate.version(db);
455461
// Fallback to docs.rs. This uses `display_name` and can never be
@@ -464,6 +470,21 @@ fn get_doc_base_url(db: &RootDatabase, def: Definition) -> Option<Url> {
464470
))
465471
})?
466472
}
473+
CrateOrigin::Library { repo: _, name } => {
474+
krate.get_html_root_url(db).or_else(|| {
475+
let version = krate.version(db);
476+
// Fallback to docs.rs. This uses `display_name` and can never be
477+
// correct, but that's what fallbacks are about.
478+
//
479+
// FIXME: clicking on the link should just open the file in the editor,
480+
// instead of falling back to external urls.
481+
Some(format!(
482+
"https://docs.rs/{krate}/{version}/",
483+
krate = name,
484+
version = version.as_deref().unwrap_or("*")
485+
))
486+
})?
487+
}
467488
};
468489
Url::parse(&base).ok()?.join(&format!("{display_name}/")).ok()
469490
}

crates/ide/src/lib.rs

+2-1
Original file line numberDiff line numberDiff line change
@@ -240,8 +240,9 @@ impl Analysis {
240240
cfg_options,
241241
Env::default(),
242242
false,
243-
CrateOrigin::CratesIo { repo: None, name: None },
243+
CrateOrigin::Local { repo: None, name: None },
244244
Err("Analysis::from_single_file has no target layout".into()),
245+
None,
245246
);
246247
change.change_file(file_id, Some(Arc::new(text)));
247248
change.set_crate_graph(crate_graph);

crates/ide/src/moniker.rs

+7-1
Original file line numberDiff line numberDiff line change
@@ -245,11 +245,17 @@ pub(crate) fn def_to_moniker(
245245
kind: if krate == from_crate { MonikerKind::Export } else { MonikerKind::Import },
246246
package_information: {
247247
let (name, repo, version) = match krate.origin(db) {
248-
CrateOrigin::CratesIo { repo, name } => (
248+
CrateOrigin::Library { repo, name } => (name, repo, krate.version(db)),
249+
CrateOrigin::Local { repo, name } => (
249250
name.unwrap_or(krate.display_name(db)?.canonical_name().to_string()),
250251
repo,
251252
krate.version(db),
252253
),
254+
CrateOrigin::Rustc { name } => (
255+
name.clone(),
256+
Some("https://github.com/rust-lang/rust/".to_string()),
257+
Some(format!("https://github.com/rust-lang/rust/compiler/{name}",)),
258+
),
253259
CrateOrigin::Lang(lang) => (
254260
krate.display_name(db)?.canonical_name().to_string(),
255261
Some("https://github.com/rust-lang/rust/".to_string()),

crates/ide/src/shuffle_crate_graph.rs

+1
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@ pub(crate) fn shuffle_crate_graph(db: &mut RootDatabase) {
4040
data.is_proc_macro,
4141
data.origin.clone(),
4242
data.target_layout.clone(),
43+
data.channel,
4344
);
4445
new_proc_macros.insert(new_id, proc_macros[&old_id].clone());
4546
map.insert(old_id, new_id);

crates/ide/src/syntax_highlighting/test_data/highlight_extern_crate.html

+1-1
Original file line numberDiff line numberDiff line change
@@ -43,5 +43,5 @@
4343
.unresolved_reference { color: #FC5555; text-decoration: wavy underline; }
4444
</style>
4545
<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>
46-
<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>
46+
<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>
4747
</code></pre>

0 commit comments

Comments
 (0)