Skip to content

Commit a694c34

Browse files
committed
Fix tests not using appropriate target data
1 parent 33591cd commit a694c34

File tree

7 files changed

+111
-55
lines changed

7 files changed

+111
-55
lines changed

crates/base-db/src/fixture.rs

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -162,7 +162,7 @@ impl ChangeFixture {
162162
Ok(Vec::new()),
163163
false,
164164
origin,
165-
None,
165+
meta.target_data_layout.as_deref().map(Arc::from),
166166
);
167167
let prev = crates.insert(crate_name.clone(), crate_id);
168168
assert!(prev.is_none());
@@ -212,6 +212,8 @@ impl ChangeFixture {
212212
.unwrap();
213213
}
214214
}
215+
let target_layout =
216+
crate_graph.iter().next().and_then(|it| crate_graph[it].target_layout.clone());
215217

216218
if let Some(mini_core) = mini_core {
217219
let core_file = file_id;
@@ -236,7 +238,7 @@ impl ChangeFixture {
236238
Ok(Vec::new()),
237239
false,
238240
CrateOrigin::Lang(LangCrateOrigin::Core),
239-
None,
241+
target_layout.clone(),
240242
);
241243

242244
for krate in all_crates {
@@ -274,7 +276,7 @@ impl ChangeFixture {
274276
Ok(proc_macro),
275277
true,
276278
CrateOrigin::CratesIo { repo: None, name: None },
277-
None,
279+
target_layout,
278280
);
279281

280282
for krate in all_crates {
@@ -395,6 +397,7 @@ struct FileMeta {
395397
edition: Edition,
396398
env: Env,
397399
introduce_new_source_root: Option<SourceRootKind>,
400+
target_data_layout: Option<String>,
398401
}
399402

400403
fn parse_crate(crate_str: String) -> (String, CrateOrigin, Option<String>) {
@@ -438,6 +441,7 @@ impl From<Fixture> for FileMeta {
438441
"library" => SourceRootKind::Library,
439442
invalid => panic!("invalid source root kind '{}'", invalid),
440443
}),
444+
target_data_layout: f.target_data_layout,
441445
}
442446
}
443447
}

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

Lines changed: 26 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,22 @@ use crate::{test_db::TestDB, Interner, Substitution};
99

1010
use super::layout_of_ty;
1111

12-
fn eval_goal(ra_fixture: &str) -> Result<Layout, LayoutError> {
13-
let (db, file_id) = TestDB::with_single_file(ra_fixture);
12+
fn eval_goal(ra_fixture: &str, minicore: &str) -> Result<Layout, LayoutError> {
13+
// using unstable cargo features failed, fall back to using plain rustc
14+
let mut cmd = std::process::Command::new("rustc");
15+
cmd.args(&["-Z", "unstable-options", "--print", "target-spec-json"])
16+
.env("RUSTC_BOOTSTRAP", "1");
17+
let output = cmd.output().unwrap();
18+
assert!(output.status.success(), "{}", output.status);
19+
let stdout = String::from_utf8(output.stdout).unwrap();
20+
let target_data_layout =
21+
stdout.split_once(r#""data-layout": ""#).unwrap().1.split_once('"').unwrap().0.to_owned();
22+
23+
let ra_fixture = format!(
24+
"{minicore}//- /main.rs crate:test target_data_layout:{target_data_layout}\n{ra_fixture}",
25+
);
26+
27+
let (db, file_id) = TestDB::with_single_file(&ra_fixture);
1428
let module_id = db.module_for_file(file_id);
1529
let def_map = module_id.def_map(&db);
1630
let scope = &def_map[module_id.local_id].scope;
@@ -20,15 +34,11 @@ fn eval_goal(ra_fixture: &str) -> Result<Layout, LayoutError> {
2034
.find_map(|x| match x {
2135
hir_def::ModuleDefId::AdtId(x) => {
2236
let name = match x {
23-
hir_def::AdtId::StructId(x) => db.struct_data(x).name.to_string(),
24-
hir_def::AdtId::UnionId(x) => db.union_data(x).name.to_string(),
25-
hir_def::AdtId::EnumId(x) => db.enum_data(x).name.to_string(),
37+
hir_def::AdtId::StructId(x) => db.struct_data(x).name.to_smol_str(),
38+
hir_def::AdtId::UnionId(x) => db.union_data(x).name.to_smol_str(),
39+
hir_def::AdtId::EnumId(x) => db.enum_data(x).name.to_smol_str(),
2640
};
27-
if name == "Goal" {
28-
Some(x)
29-
} else {
30-
None
31-
}
41+
(name == "Goal").then(|| x)
3242
}
3343
_ => None,
3444
})
@@ -38,15 +48,15 @@ fn eval_goal(ra_fixture: &str) -> Result<Layout, LayoutError> {
3848
}
3949

4050
#[track_caller]
41-
fn check_size_and_align(ra_fixture: &str, size: u64, align: u64) {
42-
let l = eval_goal(ra_fixture).unwrap();
51+
fn check_size_and_align(ra_fixture: &str, minicore: &str, size: u64, align: u64) {
52+
let l = eval_goal(ra_fixture, minicore).unwrap();
4353
assert_eq!(l.size.bytes(), size);
4454
assert_eq!(l.align.abi.bytes(), align);
4555
}
4656

4757
#[track_caller]
4858
fn check_fail(ra_fixture: &str, e: LayoutError) {
49-
let r = eval_goal(ra_fixture);
59+
let r = eval_goal(ra_fixture, "");
5060
assert_eq!(r, Err(e));
5161
}
5262

@@ -56,7 +66,8 @@ macro_rules! size_and_align {
5666
#[allow(dead_code)]
5767
$($t)*
5868
check_size_and_align(
59-
&format!("//- minicore: {}\n{}", stringify!($($x),*), stringify!($($t)*)),
69+
stringify!($($t)*),
70+
&format!("//- minicore: {}\n", stringify!($($x),*)),
6071
::std::mem::size_of::<Goal>() as u64,
6172
::std::mem::align_of::<Goal>() as u64,
6273
);
@@ -68,6 +79,7 @@ macro_rules! size_and_align {
6879
$($t)*
6980
check_size_and_align(
7081
stringify!($($t)*),
82+
"",
7183
::std::mem::size_of::<Goal>() as u64,
7284
::std::mem::align_of::<Goal>() as u64,
7385
);

crates/project-model/src/lib.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ mod sysroot;
2525
mod workspace;
2626
mod rustc_cfg;
2727
mod build_scripts;
28+
mod target_data_layout;
2829

2930
#[cfg(test)]
3031
mod tests;
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
//! Runs `rustc --print target-spec-json` to get the target_data_layout.
2+
use std::process::Command;
3+
4+
use rustc_hash::FxHashMap;
5+
6+
use crate::{utf8_stdout, ManifestPath};
7+
8+
pub(super) fn get(
9+
cargo_toml: Option<&ManifestPath>,
10+
target: Option<&str>,
11+
extra_env: &FxHashMap<String, String>,
12+
) -> Option<String> {
13+
let output = (|| {
14+
if let Some(cargo_toml) = cargo_toml {
15+
let mut cmd = Command::new(toolchain::rustc());
16+
cmd.envs(extra_env);
17+
cmd.current_dir(cargo_toml.parent())
18+
.args(&["-Z", "unstable-options", "rustc", "--print", "target-spec-json"])
19+
.env("RUSTC_BOOTSTRAP", "1");
20+
if let Some(target) = target {
21+
cmd.args(&["--target", target]);
22+
}
23+
match utf8_stdout(cmd) {
24+
Ok(it) => return Ok(it),
25+
Err(e) => tracing::debug!("{e:?}: falling back to querying rustc for cfgs"),
26+
}
27+
}
28+
// using unstable cargo features failed, fall back to using plain rustc
29+
let mut cmd = Command::new(toolchain::rustc());
30+
cmd.envs(extra_env)
31+
.args(&["-Z", "unstable-options", "rustc", "--print", "target-spec-json"])
32+
.env("RUSTC_BOOTSTRAP", "1");
33+
if let Some(target) = target {
34+
cmd.args(&["--target", target]);
35+
}
36+
utf8_stdout(cmd)
37+
})()
38+
.ok()?;
39+
Some(output.split_once(r#""data-layout": ""#)?.1.split_once('"')?.0.to_owned())
40+
}

crates/project-model/src/tests.rs

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -151,6 +151,7 @@ fn cargo_hello_world_project_model_with_wildcard_overrides() {
151151
"debug_assertions",
152152
],
153153
),
154+
target_layout: None,
154155
env: Env {
155156
entries: {
156157
"CARGO_PKG_LICENSE": "",
@@ -220,6 +221,7 @@ fn cargo_hello_world_project_model_with_wildcard_overrides() {
220221
"debug_assertions",
221222
],
222223
),
224+
target_layout: None,
223225
env: Env {
224226
entries: {
225227
"CARGO_PKG_LICENSE": "",
@@ -298,6 +300,7 @@ fn cargo_hello_world_project_model_with_wildcard_overrides() {
298300
"debug_assertions",
299301
],
300302
),
303+
target_layout: None,
301304
env: Env {
302305
entries: {
303306
"CARGO_PKG_LICENSE": "",
@@ -376,6 +379,7 @@ fn cargo_hello_world_project_model_with_wildcard_overrides() {
376379
"debug_assertions",
377380
],
378381
),
382+
target_layout: None,
379383
env: Env {
380384
entries: {
381385
"CARGO_PKG_LICENSE": "",
@@ -463,6 +467,7 @@ fn cargo_hello_world_project_model_with_wildcard_overrides() {
463467
"feature=use_std",
464468
],
465469
),
470+
target_layout: None,
466471
env: Env {
467472
entries: {
468473
"CARGO_PKG_LICENSE": "",
@@ -548,6 +553,7 @@ fn cargo_hello_world_project_model_with_selective_overrides() {
548553
"test",
549554
],
550555
),
556+
target_layout: None,
551557
env: Env {
552558
entries: {
553559
"CARGO_PKG_LICENSE": "",
@@ -619,6 +625,7 @@ fn cargo_hello_world_project_model_with_selective_overrides() {
619625
"test",
620626
],
621627
),
628+
target_layout: None,
622629
env: Env {
623630
entries: {
624631
"CARGO_PKG_LICENSE": "",
@@ -699,6 +706,7 @@ fn cargo_hello_world_project_model_with_selective_overrides() {
699706
"test",
700707
],
701708
),
709+
target_layout: None,
702710
env: Env {
703711
entries: {
704712
"CARGO_PKG_LICENSE": "",
@@ -779,6 +787,7 @@ fn cargo_hello_world_project_model_with_selective_overrides() {
779787
"test",
780788
],
781789
),
790+
target_layout: None,
782791
env: Env {
783792
entries: {
784793
"CARGO_PKG_LICENSE": "",
@@ -866,6 +875,7 @@ fn cargo_hello_world_project_model_with_selective_overrides() {
866875
"feature=use_std",
867876
],
868877
),
878+
target_layout: None,
869879
env: Env {
870880
entries: {
871881
"CARGO_PKG_LICENSE": "",
@@ -942,6 +952,7 @@ fn cargo_hello_world_project_model() {
942952
"test",
943953
],
944954
),
955+
target_layout: None,
945956
env: Env {
946957
entries: {
947958
"CARGO_PKG_LICENSE": "",
@@ -1013,6 +1024,7 @@ fn cargo_hello_world_project_model() {
10131024
"test",
10141025
],
10151026
),
1027+
target_layout: None,
10161028
env: Env {
10171029
entries: {
10181030
"CARGO_PKG_LICENSE": "",
@@ -1093,6 +1105,7 @@ fn cargo_hello_world_project_model() {
10931105
"test",
10941106
],
10951107
),
1108+
target_layout: None,
10961109
env: Env {
10971110
entries: {
10981111
"CARGO_PKG_LICENSE": "",
@@ -1173,6 +1186,7 @@ fn cargo_hello_world_project_model() {
11731186
"test",
11741187
],
11751188
),
1189+
target_layout: None,
11761190
env: Env {
11771191
entries: {
11781192
"CARGO_PKG_LICENSE": "",
@@ -1260,6 +1274,7 @@ fn cargo_hello_world_project_model() {
12601274
"feature=use_std",
12611275
],
12621276
),
1277+
target_layout: None,
12631278
env: Env {
12641279
entries: {
12651280
"CARGO_PKG_LICENSE": "",
@@ -1328,6 +1343,7 @@ fn rust_project_hello_world_project_model() {
13281343
potential_cfg_options: CfgOptions(
13291344
[],
13301345
),
1346+
target_layout: None,
13311347
env: Env {
13321348
entries: {},
13331349
},
@@ -1372,6 +1388,7 @@ fn rust_project_hello_world_project_model() {
13721388
potential_cfg_options: CfgOptions(
13731389
[],
13741390
),
1391+
target_layout: None,
13751392
env: Env {
13761393
entries: {},
13771394
},
@@ -1406,6 +1423,7 @@ fn rust_project_hello_world_project_model() {
14061423
potential_cfg_options: CfgOptions(
14071424
[],
14081425
),
1426+
target_layout: None,
14091427
env: Env {
14101428
entries: {},
14111429
},
@@ -1440,6 +1458,7 @@ fn rust_project_hello_world_project_model() {
14401458
potential_cfg_options: CfgOptions(
14411459
[],
14421460
),
1461+
target_layout: None,
14431462
env: Env {
14441463
entries: {},
14451464
},
@@ -1474,6 +1493,7 @@ fn rust_project_hello_world_project_model() {
14741493
potential_cfg_options: CfgOptions(
14751494
[],
14761495
),
1496+
target_layout: None,
14771497
env: Env {
14781498
entries: {},
14791499
},
@@ -1518,6 +1538,7 @@ fn rust_project_hello_world_project_model() {
15181538
potential_cfg_options: CfgOptions(
15191539
[],
15201540
),
1541+
target_layout: None,
15211542
env: Env {
15221543
entries: {},
15231544
},
@@ -1552,6 +1573,7 @@ fn rust_project_hello_world_project_model() {
15521573
potential_cfg_options: CfgOptions(
15531574
[],
15541575
),
1576+
target_layout: None,
15551577
env: Env {
15561578
entries: {},
15571579
},
@@ -1659,6 +1681,7 @@ fn rust_project_hello_world_project_model() {
16591681
potential_cfg_options: CfgOptions(
16601682
[],
16611683
),
1684+
target_layout: None,
16621685
env: Env {
16631686
entries: {},
16641687
},
@@ -1693,6 +1716,7 @@ fn rust_project_hello_world_project_model() {
16931716
potential_cfg_options: CfgOptions(
16941717
[],
16951718
),
1719+
target_layout: None,
16961720
env: Env {
16971721
entries: {},
16981722
},
@@ -1727,6 +1751,7 @@ fn rust_project_hello_world_project_model() {
17271751
potential_cfg_options: CfgOptions(
17281752
[],
17291753
),
1754+
target_layout: None,
17301755
env: Env {
17311756
entries: {},
17321757
},
@@ -1761,6 +1786,7 @@ fn rust_project_hello_world_project_model() {
17611786
potential_cfg_options: CfgOptions(
17621787
[],
17631788
),
1789+
target_layout: None,
17641790
env: Env {
17651791
entries: {},
17661792
},

0 commit comments

Comments
 (0)