Skip to content

Commit 25d1267

Browse files
committed
Auto merge of #16586 - Veykril:crate-graph-side-table, r=Veykril
fix: Remove cargo knowledge from `CrateData` Fixes rust-lang/rust-analyzer#16170, Fixes rust-lang/rust-analyzer#15656
2 parents bb0f93a + ead3691 commit 25d1267

34 files changed

+462
-739
lines changed

crates/base-db/src/input.rs

+38-191
Large diffs are not rendered by default.

crates/base-db/src/lib.rs

+14
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,20 @@ pub trait SourceDatabase: FileLoader + std::fmt::Debug {
6262
/// The crate graph.
6363
#[salsa::input]
6464
fn crate_graph(&self) -> Arc<CrateGraph>;
65+
66+
// FIXME: Consider removing this, making HirDatabase::target_data_layout an input query
67+
#[salsa::input]
68+
fn data_layout(&self, krate: CrateId) -> TargetLayoutLoadResult;
69+
70+
#[salsa::input]
71+
fn toolchain(&self, krate: CrateId) -> Option<Version>;
72+
73+
#[salsa::transparent]
74+
fn toolchain_channel(&self, krate: CrateId) -> Option<ReleaseChannel>;
75+
}
76+
77+
fn toolchain_channel(db: &dyn SourceDatabase, krate: CrateId) -> Option<ReleaseChannel> {
78+
db.toolchain(krate).as_ref().and_then(|v| ReleaseChannel::from_str(&v.pre))
6579
}
6680

6781
fn parse(db: &dyn SourceDatabase, file_id: FileId) -> Parse<ast::SourceFile> {

crates/hir-expand/src/change.rs

+33-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,10 @@
11
//! Defines a unit of change that can applied to the database to get the next
22
//! state. Changes are transactional.
3-
use base_db::{salsa::Durability, CrateGraph, FileChange, SourceDatabaseExt, SourceRoot};
3+
use base_db::{
4+
salsa::Durability, CrateGraph, CrateId, FileChange, SourceDatabaseExt, SourceRoot,
5+
TargetLayoutLoadResult, Version,
6+
};
7+
use la_arena::RawIdx;
48
use span::FileId;
59
use triomphe::Arc;
610

@@ -10,6 +14,8 @@ use crate::{db::ExpandDatabase, proc_macro::ProcMacros};
1014
pub struct Change {
1115
pub source_change: FileChange,
1216
pub proc_macros: Option<ProcMacros>,
17+
pub toolchains: Option<Vec<Option<Version>>>,
18+
pub target_data_layouts: Option<Vec<TargetLayoutLoadResult>>,
1319
}
1420

1521
impl Change {
@@ -22,6 +28,24 @@ impl Change {
2228
if let Some(proc_macros) = self.proc_macros {
2329
db.set_proc_macros_with_durability(Arc::new(proc_macros), Durability::HIGH);
2430
}
31+
if let Some(target_data_layouts) = self.target_data_layouts {
32+
for (id, val) in target_data_layouts.into_iter().enumerate() {
33+
db.set_data_layout_with_durability(
34+
CrateId::from_raw(RawIdx::from(id as u32)),
35+
val,
36+
Durability::HIGH,
37+
);
38+
}
39+
}
40+
if let Some(toolchains) = self.toolchains {
41+
for (id, val) in toolchains.into_iter().enumerate() {
42+
db.set_toolchain_with_durability(
43+
CrateId::from_raw(RawIdx::from(id as u32)),
44+
val,
45+
Durability::HIGH,
46+
);
47+
}
48+
}
2549
}
2650

2751
pub fn change_file(&mut self, file_id: FileId, new_text: Option<Arc<str>>) {
@@ -36,6 +60,14 @@ impl Change {
3660
self.proc_macros = Some(proc_macros);
3761
}
3862

63+
pub fn set_toolchains(&mut self, toolchains: Vec<Option<Version>>) {
64+
self.toolchains = Some(toolchains);
65+
}
66+
67+
pub fn set_target_data_layouts(&mut self, target_data_layouts: Vec<TargetLayoutLoadResult>) {
68+
self.target_data_layouts = Some(target_data_layouts);
69+
}
70+
3971
pub fn set_roots(&mut self, roots: Vec<SourceRoot>) {
4072
self.source_change.set_roots(roots)
4173
}

crates/hir-expand/src/declarative.rs

+3-3
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ impl DeclarativeMacroExpander {
3131
call_id: MacroCallId,
3232
) -> ExpandResult<tt::Subtree> {
3333
let loc = db.lookup_intern_macro_call(call_id);
34-
let toolchain = &db.crate_graph()[loc.def.krate].toolchain;
34+
let toolchain = db.toolchain(loc.def.krate);
3535
let new_meta_vars = toolchain.as_ref().map_or(false, |version| {
3636
REQUIREMENT.get_or_init(|| VersionReq::parse(">=1.76").unwrap()).matches(
3737
&base_db::Version {
@@ -67,7 +67,7 @@ impl DeclarativeMacroExpander {
6767
krate: CrateId,
6868
call_site: Span,
6969
) -> ExpandResult<tt::Subtree> {
70-
let toolchain = &db.crate_graph()[krate].toolchain;
70+
let toolchain = db.toolchain(krate);
7171
let new_meta_vars = toolchain.as_ref().map_or(false, |version| {
7272
REQUIREMENT.get_or_init(|| VersionReq::parse(">=1.76").unwrap()).matches(
7373
&base_db::Version {
@@ -119,7 +119,7 @@ impl DeclarativeMacroExpander {
119119
_ => None,
120120
}
121121
};
122-
let toolchain = crate_data.toolchain.as_ref();
122+
let toolchain = db.toolchain(def_crate);
123123
let new_meta_vars = toolchain.as_ref().map_or(false, |version| {
124124
REQUIREMENT.get_or_init(|| VersionReq::parse(">=1.76").unwrap()).matches(
125125
&base_db::Version {

crates/hir-ty/src/layout/target.rs

+3-5
Original file line numberDiff line numberDiff line change
@@ -11,10 +11,8 @@ pub fn target_data_layout_query(
1111
db: &dyn HirDatabase,
1212
krate: CrateId,
1313
) -> Result<Arc<TargetDataLayout>, Arc<str>> {
14-
let crate_graph = db.crate_graph();
15-
let res = crate_graph[krate].target_layout.as_deref();
16-
match res {
17-
Ok(it) => match TargetDataLayout::parse_from_llvm_datalayout_string(it) {
14+
match db.data_layout(krate) {
15+
Ok(it) => match TargetDataLayout::parse_from_llvm_datalayout_string(&it) {
1816
Ok(it) => Ok(Arc::new(it)),
1917
Err(e) => {
2018
Err(match e {
@@ -44,6 +42,6 @@ pub fn target_data_layout_query(
4442
}.into())
4543
}
4644
},
47-
Err(e) => Err(Arc::from(&**e)),
45+
Err(e) => Err(e),
4846
}
4947
}

crates/hir-ty/src/layout/tests.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ fn current_machine_data_layout() -> String {
2727
fn eval_goal(ra_fixture: &str, minicore: &str) -> Result<Arc<Layout>, LayoutError> {
2828
let target_data_layout = current_machine_data_layout();
2929
let ra_fixture = format!(
30-
"{minicore}//- /main.rs crate:test target_data_layout:{target_data_layout}\n{ra_fixture}",
30+
"//- target_data_layout: {target_data_layout}\n{minicore}//- /main.rs crate:test\n{ra_fixture}",
3131
);
3232

3333
let (db, file_ids) = TestDB::with_many_files(&ra_fixture);
@@ -76,7 +76,7 @@ fn eval_goal(ra_fixture: &str, minicore: &str) -> Result<Arc<Layout>, LayoutErro
7676
fn eval_expr(ra_fixture: &str, minicore: &str) -> Result<Arc<Layout>, LayoutError> {
7777
let target_data_layout = current_machine_data_layout();
7878
let ra_fixture = format!(
79-
"{minicore}//- /main.rs crate:test target_data_layout:{target_data_layout}\nfn main(){{let goal = {{{ra_fixture}}};}}",
79+
"//- target_data_layout: {target_data_layout}\n{minicore}//- /main.rs crate:test\nfn main(){{let goal = {{{ra_fixture}}};}}",
8080
);
8181

8282
let (db, file_id) = TestDB::with_single_file(&ra_fixture);

crates/ide-completion/src/context.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -717,7 +717,7 @@ impl<'a> CompletionContext<'a> {
717717
let krate = scope.krate();
718718
let module = scope.module();
719719

720-
let toolchain = db.crate_graph()[krate.into()].channel();
720+
let toolchain = db.toolchain_channel(krate.into());
721721
// `toolchain == None` means we're in some detached files. Since we have no information on
722722
// the toolchain being used, let's just allow unstable items to be listed.
723723
let is_nightly = matches!(toolchain, Some(base_db::ReleaseChannel::Nightly) | None);

crates/ide-db/src/rename.rs

+2-3
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,6 @@ impl Definition {
7171
&self,
7272
sema: &Semantics<'_, RootDatabase>,
7373
new_name: &str,
74-
rename_external: bool,
7574
) -> Result<SourceChange> {
7675
// self.krate() returns None if
7776
// self is a built-in attr, built-in type or tool module.
@@ -80,8 +79,8 @@ impl Definition {
8079
if let Some(krate) = self.krate(sema.db) {
8180
// Can we not rename non-local items?
8281
// Then bail if non-local
83-
if !rename_external && !krate.origin(sema.db).is_local() {
84-
bail!("Cannot rename a non-local definition as the config for it is disabled")
82+
if !krate.origin(sema.db).is_local() {
83+
bail!("Cannot rename a non-local definition")
8584
}
8685
}
8786

crates/ide-diagnostics/src/handlers/incorrect_case.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ fn fixes(ctx: &DiagnosticsContext<'_>, d: &hir::IncorrectCase) -> Option<Vec<Ass
4343
let label = format!("Rename to {}", d.suggested_text);
4444
let mut res = unresolved_fix("change_case", &label, frange.range);
4545
if ctx.resolve.should_resolve(&res.id) {
46-
let source_change = def.rename(&ctx.sema, &d.suggested_text, true);
46+
let source_change = def.rename(&ctx.sema, &d.suggested_text);
4747
res.source_change = Some(source_change.ok().unwrap_or_default());
4848
}
4949

crates/ide/src/doc_links.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -501,7 +501,7 @@ fn get_doc_base_urls(
501501
let Some(krate) = def.krate(db) else { return Default::default() };
502502
let Some(display_name) = krate.display_name(db) else { return Default::default() };
503503
let crate_data = &db.crate_graph()[krate.into()];
504-
let channel = crate_data.channel().unwrap_or(ReleaseChannel::Nightly).as_str();
504+
let channel = db.toolchain_channel(krate.into()).unwrap_or(ReleaseChannel::Nightly).as_str();
505505

506506
let (web_base, local_base) = match &crate_data.origin {
507507
// std and co do not specify `html_root_url` any longer so we gotta handwrite this ourself.

crates/ide/src/lib.rs

+3-4
Original file line numberDiff line numberDiff line change
@@ -253,11 +253,11 @@ impl Analysis {
253253
Env::default(),
254254
false,
255255
CrateOrigin::Local { repo: None, name: None },
256-
Err("Analysis::from_single_file has no target layout".into()),
257-
None,
258256
);
259257
change.change_file(file_id, Some(Arc::from(text)));
260258
change.set_crate_graph(crate_graph);
259+
change.set_target_data_layouts(vec![Err("fixture has no layout".into())]);
260+
change.set_toolchains(vec![None]);
261261
host.apply_change(change);
262262
(host.analysis(), file_id)
263263
}
@@ -675,9 +675,8 @@ impl Analysis {
675675
&self,
676676
position: FilePosition,
677677
new_name: &str,
678-
rename_external: bool,
679678
) -> Cancellable<Result<SourceChange, RenameError>> {
680-
self.with_db(|db| rename::rename(db, position, new_name, rename_external))
679+
self.with_db(|db| rename::rename(db, position, new_name))
681680
}
682681

683682
pub fn prepare_rename(

crates/ide/src/rename.rs

+10-31
Original file line numberDiff line numberDiff line change
@@ -84,7 +84,6 @@ pub(crate) fn rename(
8484
db: &RootDatabase,
8585
position: FilePosition,
8686
new_name: &str,
87-
rename_external: bool,
8887
) -> RenameResult<SourceChange> {
8988
let sema = Semantics::new(db);
9089
let source_file = sema.parse(position.file_id);
@@ -104,7 +103,7 @@ pub(crate) fn rename(
104103
return rename_to_self(&sema, local);
105104
}
106105
}
107-
def.rename(&sema, new_name, rename_external)
106+
def.rename(&sema, new_name)
108107
})
109108
.collect();
110109

@@ -123,9 +122,9 @@ pub(crate) fn will_rename_file(
123122
let module = sema.to_module_def(file_id)?;
124123
let def = Definition::Module(module);
125124
let mut change = if is_raw_identifier(new_name_stem) {
126-
def.rename(&sema, &SmolStr::from_iter(["r#", new_name_stem]), true).ok()?
125+
def.rename(&sema, &SmolStr::from_iter(["r#", new_name_stem])).ok()?
127126
} else {
128-
def.rename(&sema, new_name_stem, true).ok()?
127+
def.rename(&sema, new_name_stem).ok()?
129128
};
130129
change.file_system_edits.clear();
131130
Some(change)
@@ -377,16 +376,11 @@ mod tests {
377376
use super::{RangeInfo, RenameError};
378377

379378
fn check(new_name: &str, ra_fixture_before: &str, ra_fixture_after: &str) {
380-
check_with_rename_config(new_name, ra_fixture_before, ra_fixture_after, true);
379+
check_with_rename_config(new_name, ra_fixture_before, ra_fixture_after);
381380
}
382381

383382
#[track_caller]
384-
fn check_with_rename_config(
385-
new_name: &str,
386-
ra_fixture_before: &str,
387-
ra_fixture_after: &str,
388-
rename_external: bool,
389-
) {
383+
fn check_with_rename_config(new_name: &str, ra_fixture_before: &str, ra_fixture_after: &str) {
390384
let ra_fixture_after = &trim_indent(ra_fixture_after);
391385
let (analysis, position) = fixture::position(ra_fixture_before);
392386
if !ra_fixture_after.starts_with("error: ") {
@@ -395,7 +389,7 @@ mod tests {
395389
}
396390
}
397391
let rename_result = analysis
398-
.rename(position, new_name, rename_external)
392+
.rename(position, new_name)
399393
.unwrap_or_else(|err| panic!("Rename to '{new_name}' was cancelled: {err}"));
400394
match rename_result {
401395
Ok(source_change) => {
@@ -426,10 +420,8 @@ mod tests {
426420

427421
fn check_expect(new_name: &str, ra_fixture: &str, expect: Expect) {
428422
let (analysis, position) = fixture::position(ra_fixture);
429-
let source_change = analysis
430-
.rename(position, new_name, true)
431-
.unwrap()
432-
.expect("Expect returned a RenameError");
423+
let source_change =
424+
analysis.rename(position, new_name).unwrap().expect("Expect returned a RenameError");
433425
expect.assert_eq(&filter_expect(source_change))
434426
}
435427

@@ -2636,19 +2628,7 @@ pub struct S;
26362628
//- /main.rs crate:main deps:lib new_source_root:local
26372629
use lib::S$0;
26382630
"#,
2639-
"error: Cannot rename a non-local definition as the config for it is disabled",
2640-
false,
2641-
);
2642-
2643-
check(
2644-
"Baz",
2645-
r#"
2646-
//- /lib.rs crate:lib new_source_root:library
2647-
pub struct S;
2648-
//- /main.rs crate:main deps:lib new_source_root:local
2649-
use lib::S$0;
2650-
"#,
2651-
"use lib::Baz;\n",
2631+
"error: Cannot rename a non-local definition",
26522632
);
26532633
}
26542634

@@ -2663,8 +2643,7 @@ use core::hash::Hash;
26632643
#[derive(H$0ash)]
26642644
struct A;
26652645
"#,
2666-
"error: Cannot rename a non-local definition as the config for it is disabled",
2667-
false,
2646+
"error: Cannot rename a non-local definition",
26682647
);
26692648
}
26702649

crates/ide/src/shuffle_crate_graph.rs

-2
Original file line numberDiff line numberDiff line change
@@ -39,8 +39,6 @@ pub(crate) fn shuffle_crate_graph(db: &mut RootDatabase) {
3939
data.env.clone(),
4040
data.is_proc_macro,
4141
data.origin.clone(),
42-
data.target_layout.clone(),
43-
data.toolchain.clone(),
4442
);
4543
new_proc_macros.insert(new_id, proc_macros[&old_id].clone());
4644
map.insert(old_id, new_id);

crates/ide/src/status.rs

-8
Original file line numberDiff line numberDiff line change
@@ -72,8 +72,6 @@ pub(crate) fn status(db: &RootDatabase, file_id: Option<FileId>) -> String {
7272
dependencies,
7373
origin,
7474
is_proc_macro,
75-
target_layout,
76-
toolchain,
7775
} = &crate_graph[crate_id];
7876
format_to!(
7977
buf,
@@ -91,12 +89,6 @@ pub(crate) fn status(db: &RootDatabase, file_id: Option<FileId>) -> String {
9189
format_to!(buf, " Env: {:?}\n", env);
9290
format_to!(buf, " Origin: {:?}\n", origin);
9391
format_to!(buf, " Is a proc macro crate: {}\n", is_proc_macro);
94-
format_to!(buf, " Workspace Target Layout: {:?}\n", target_layout);
95-
format_to!(
96-
buf,
97-
" Workspace Toolchain: {}\n",
98-
toolchain.as_ref().map_or_else(|| "n/a".into(), |v| v.to_string())
99-
);
10092
let deps = dependencies
10193
.iter()
10294
.map(|dep| format!("{}={}", dep.name, dep.crate_id.into_raw()))

0 commit comments

Comments
 (0)