Skip to content

Commit 56fb0ca

Browse files
committed
Auto merge of #13984 - Veykril:target-data-layout, r=Veykril
fix: Fix target-data-layout fetching incorrectly passing 'rustc' to rustc
2 parents bbb730a + 384fa4b commit 56fb0ca

File tree

8 files changed

+144
-63
lines changed

8 files changed

+144
-63
lines changed

crates/base-db/src/fixture.rs

+11-4
Original file line numberDiff line numberDiff line change
@@ -163,7 +163,10 @@ impl ChangeFixture {
163163
Ok(Vec::new()),
164164
false,
165165
origin,
166-
meta.target_data_layout.as_deref().map(Arc::from),
166+
meta.target_data_layout
167+
.as_deref()
168+
.map(Arc::from)
169+
.ok_or_else(|| "target_data_layout unset".into()),
167170
);
168171
let prev = crates.insert(crate_name.clone(), crate_id);
169172
assert!(prev.is_none());
@@ -200,7 +203,9 @@ impl ChangeFixture {
200203
Ok(Vec::new()),
201204
false,
202205
CrateOrigin::CratesIo { repo: None, name: None },
203-
default_target_data_layout.map(|x| x.into()),
206+
default_target_data_layout
207+
.map(|x| x.into())
208+
.ok_or_else(|| "target_data_layout unset".into()),
204209
);
205210
} else {
206211
for (from, to, prelude) in crate_deps {
@@ -214,8 +219,10 @@ impl ChangeFixture {
214219
.unwrap();
215220
}
216221
}
217-
let target_layout =
218-
crate_graph.iter().next().and_then(|it| crate_graph[it].target_layout.clone());
222+
let target_layout = crate_graph.iter().next().map_or_else(
223+
|| Err("target_data_layout unset".into()),
224+
|it| crate_graph[it].target_layout.clone(),
225+
);
219226

220227
if let Some(mini_core) = mini_core {
221228
let core_file = file_id;

crates/base-db/src/input.rs

+13-12
Original file line numberDiff line numberDiff line change
@@ -243,6 +243,7 @@ pub enum ProcMacroExpansionError {
243243
}
244244

245245
pub type ProcMacroLoadResult = Result<Vec<ProcMacro>, String>;
246+
pub type TargetLayoutLoadResult = Result<Arc<str>, Arc<str>>;
246247

247248
#[derive(Debug, Clone)]
248249
pub struct ProcMacro {
@@ -265,7 +266,7 @@ pub struct CrateData {
265266
pub display_name: Option<CrateDisplayName>,
266267
pub cfg_options: CfgOptions,
267268
pub potential_cfg_options: CfgOptions,
268-
pub target_layout: Option<Arc<str>>,
269+
pub target_layout: TargetLayoutLoadResult,
269270
pub env: Env,
270271
pub dependencies: Vec<Dependency>,
271272
pub proc_macro: ProcMacroLoadResult,
@@ -324,7 +325,7 @@ impl CrateGraph {
324325
proc_macro: ProcMacroLoadResult,
325326
is_proc_macro: bool,
326327
origin: CrateOrigin,
327-
target_layout: Option<Arc<str>>,
328+
target_layout: Result<Arc<str>, Arc<str>>,
328329
) -> CrateId {
329330
let data = CrateData {
330331
root_file_id,
@@ -647,7 +648,7 @@ mod tests {
647648
Ok(Vec::new()),
648649
false,
649650
CrateOrigin::CratesIo { repo: None, name: None },
650-
None,
651+
Err("".into()),
651652
);
652653
let crate2 = graph.add_crate_root(
653654
FileId(2u32),
@@ -660,7 +661,7 @@ mod tests {
660661
Ok(Vec::new()),
661662
false,
662663
CrateOrigin::CratesIo { repo: None, name: None },
663-
None,
664+
Err("".into()),
664665
);
665666
let crate3 = graph.add_crate_root(
666667
FileId(3u32),
@@ -673,7 +674,7 @@ mod tests {
673674
Ok(Vec::new()),
674675
false,
675676
CrateOrigin::CratesIo { repo: None, name: None },
676-
None,
677+
Err("".into()),
677678
);
678679
assert!(graph
679680
.add_dep(crate1, Dependency::new(CrateName::new("crate2").unwrap(), crate2))
@@ -700,7 +701,7 @@ mod tests {
700701
Ok(Vec::new()),
701702
false,
702703
CrateOrigin::CratesIo { repo: None, name: None },
703-
None,
704+
Err("".into()),
704705
);
705706
let crate2 = graph.add_crate_root(
706707
FileId(2u32),
@@ -713,7 +714,7 @@ mod tests {
713714
Ok(Vec::new()),
714715
false,
715716
CrateOrigin::CratesIo { repo: None, name: None },
716-
None,
717+
Err("".into()),
717718
);
718719
assert!(graph
719720
.add_dep(crate1, Dependency::new(CrateName::new("crate2").unwrap(), crate2))
@@ -737,7 +738,7 @@ mod tests {
737738
Ok(Vec::new()),
738739
false,
739740
CrateOrigin::CratesIo { repo: None, name: None },
740-
None,
741+
Err("".into()),
741742
);
742743
let crate2 = graph.add_crate_root(
743744
FileId(2u32),
@@ -750,7 +751,7 @@ mod tests {
750751
Ok(Vec::new()),
751752
false,
752753
CrateOrigin::CratesIo { repo: None, name: None },
753-
None,
754+
Err("".into()),
754755
);
755756
let crate3 = graph.add_crate_root(
756757
FileId(3u32),
@@ -763,7 +764,7 @@ mod tests {
763764
Ok(Vec::new()),
764765
false,
765766
CrateOrigin::CratesIo { repo: None, name: None },
766-
None,
767+
Err("".into()),
767768
);
768769
assert!(graph
769770
.add_dep(crate1, Dependency::new(CrateName::new("crate2").unwrap(), crate2))
@@ -787,7 +788,7 @@ mod tests {
787788
Ok(Vec::new()),
788789
false,
789790
CrateOrigin::CratesIo { repo: None, name: None },
790-
None,
791+
Err("".into()),
791792
);
792793
let crate2 = graph.add_crate_root(
793794
FileId(2u32),
@@ -800,7 +801,7 @@ mod tests {
800801
Ok(Vec::new()),
801802
false,
802803
CrateOrigin::CratesIo { repo: None, name: None },
803-
None,
804+
Err("".into()),
804805
);
805806
assert!(graph
806807
.add_dep(

crates/base-db/src/lib.rs

+1
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ pub use crate::{
1717
CrateData, CrateDisplayName, CrateGraph, CrateId, CrateName, CrateOrigin, Dependency,
1818
Edition, Env, LangCrateOrigin, ProcMacro, ProcMacroExpander, ProcMacroExpansionError,
1919
ProcMacroId, ProcMacroKind, ProcMacroLoadResult, SourceRoot, SourceRootId,
20+
TargetLayoutLoadResult,
2021
},
2122
};
2223
pub use salsa::{self, Cancelled};

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

+9-2
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,13 @@ pub fn target_data_layout_query(
1212
krate: CrateId,
1313
) -> Option<Arc<TargetDataLayout>> {
1414
let crate_graph = db.crate_graph();
15-
let target_layout = crate_graph[krate].target_layout.as_ref()?;
16-
Some(Arc::new(TargetDataLayout::parse_from_llvm_datalayout_string(&target_layout).ok()?))
15+
let target_layout = crate_graph[krate].target_layout.as_ref().ok()?;
16+
let res = TargetDataLayout::parse_from_llvm_datalayout_string(&target_layout);
17+
if let Err(_e) = &res {
18+
// FIXME: Print the error here once it implements debug/display
19+
// also logging here is somewhat wrong, but unfortunately this is the earliest place we can
20+
// parse that doesn't impose a dependency to the rust-abi crate for project-model
21+
tracing::error!("Failed to parse target data layout for {krate:?}");
22+
}
23+
res.ok().map(Arc::new)
1724
}

crates/ide/src/lib.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -237,7 +237,7 @@ impl Analysis {
237237
Ok(Vec::new()),
238238
false,
239239
CrateOrigin::CratesIo { repo: None, name: None },
240-
None,
240+
Err("Analysis::from_single_file has no target layout".into()),
241241
);
242242
change.change_file(file_id, Some(Arc::new(text)));
243243
change.set_crate_graph(crate_graph);

crates/project-model/src/target_data_layout.rs

+6-5
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
//! Runs `rustc --print target-spec-json` to get the target_data_layout.
22
use std::process::Command;
33

4+
use anyhow::Result;
45
use rustc_hash::FxHashMap;
56

67
use crate::{utf8_stdout, ManifestPath};
@@ -9,7 +10,7 @@ pub(super) fn get(
910
cargo_toml: Option<&ManifestPath>,
1011
target: Option<&str>,
1112
extra_env: &FxHashMap<String, String>,
12-
) -> Option<String> {
13+
) -> Result<String> {
1314
let output = (|| {
1415
if let Some(cargo_toml) = cargo_toml {
1516
let mut cmd = Command::new(toolchain::rustc());
@@ -28,13 +29,13 @@ pub(super) fn get(
2829
// using unstable cargo features failed, fall back to using plain rustc
2930
let mut cmd = Command::new(toolchain::rustc());
3031
cmd.envs(extra_env)
31-
.args(["-Z", "unstable-options", "rustc", "--print", "target-spec-json"])
32+
.args(["-Z", "unstable-options", "--print", "target-spec-json"])
3233
.env("RUSTC_BOOTSTRAP", "1");
3334
if let Some(target) = target {
3435
cmd.args(["--target", target]);
3536
}
3637
utf8_stdout(cmd)
37-
})()
38-
.ok()?;
39-
Some(output.split_once(r#""data-layout": ""#)?.1.split_once('"')?.0.to_owned())
38+
})()?;
39+
(|| Some(output.split_once(r#""data-layout": ""#)?.1.split_once('"')?.0.to_owned()))()
40+
.ok_or_else(|| anyhow::format_err!("could not fetch target-spec-json from command output"))
4041
}

0 commit comments

Comments
 (0)