Skip to content

Commit 33591cd

Browse files
committed
Calculate the TargetDataLayout correctly for the selected target
1 parent 9ed1829 commit 33591cd

File tree

15 files changed

+246
-61
lines changed

15 files changed

+246
-61
lines changed

crates/base-db/src/fixture.rs

+4
Original file line numberDiff line numberDiff line change
@@ -162,6 +162,7 @@ impl ChangeFixture {
162162
Ok(Vec::new()),
163163
false,
164164
origin,
165+
None,
165166
);
166167
let prev = crates.insert(crate_name.clone(), crate_id);
167168
assert!(prev.is_none());
@@ -197,6 +198,7 @@ impl ChangeFixture {
197198
Ok(Vec::new()),
198199
false,
199200
CrateOrigin::CratesIo { repo: None, name: None },
201+
None,
200202
);
201203
} else {
202204
for (from, to, prelude) in crate_deps {
@@ -234,6 +236,7 @@ impl ChangeFixture {
234236
Ok(Vec::new()),
235237
false,
236238
CrateOrigin::Lang(LangCrateOrigin::Core),
239+
None,
237240
);
238241

239242
for krate in all_crates {
@@ -271,6 +274,7 @@ impl ChangeFixture {
271274
Ok(proc_macro),
272275
true,
273276
CrateOrigin::CratesIo { repo: None, name: None },
277+
None,
274278
);
275279

276280
for krate in all_crates {

crates/base-db/src/input.rs

+13
Original file line numberDiff line numberDiff line change
@@ -270,6 +270,7 @@ pub struct CrateData {
270270
pub display_name: Option<CrateDisplayName>,
271271
pub cfg_options: CfgOptions,
272272
pub potential_cfg_options: CfgOptions,
273+
pub target_layout: Option<Arc<str>>,
273274
pub env: Env,
274275
pub dependencies: Vec<Dependency>,
275276
pub proc_macro: ProcMacroLoadResult,
@@ -328,6 +329,7 @@ impl CrateGraph {
328329
proc_macro: ProcMacroLoadResult,
329330
is_proc_macro: bool,
330331
origin: CrateOrigin,
332+
target_layout: Option<Arc<str>>,
331333
) -> CrateId {
332334
let data = CrateData {
333335
root_file_id,
@@ -340,6 +342,7 @@ impl CrateGraph {
340342
proc_macro,
341343
dependencies: Vec::new(),
342344
origin,
345+
target_layout,
343346
is_proc_macro,
344347
};
345348
let crate_id = CrateId(self.arena.len() as u32);
@@ -649,6 +652,7 @@ mod tests {
649652
Ok(Vec::new()),
650653
false,
651654
CrateOrigin::CratesIo { repo: None, name: None },
655+
None,
652656
);
653657
let crate2 = graph.add_crate_root(
654658
FileId(2u32),
@@ -661,6 +665,7 @@ mod tests {
661665
Ok(Vec::new()),
662666
false,
663667
CrateOrigin::CratesIo { repo: None, name: None },
668+
None,
664669
);
665670
let crate3 = graph.add_crate_root(
666671
FileId(3u32),
@@ -673,6 +678,7 @@ mod tests {
673678
Ok(Vec::new()),
674679
false,
675680
CrateOrigin::CratesIo { repo: None, name: None },
681+
None,
676682
);
677683
assert!(graph
678684
.add_dep(crate1, Dependency::new(CrateName::new("crate2").unwrap(), crate2))
@@ -699,6 +705,7 @@ mod tests {
699705
Ok(Vec::new()),
700706
false,
701707
CrateOrigin::CratesIo { repo: None, name: None },
708+
None,
702709
);
703710
let crate2 = graph.add_crate_root(
704711
FileId(2u32),
@@ -711,6 +718,7 @@ mod tests {
711718
Ok(Vec::new()),
712719
false,
713720
CrateOrigin::CratesIo { repo: None, name: None },
721+
None,
714722
);
715723
assert!(graph
716724
.add_dep(crate1, Dependency::new(CrateName::new("crate2").unwrap(), crate2))
@@ -734,6 +742,7 @@ mod tests {
734742
Ok(Vec::new()),
735743
false,
736744
CrateOrigin::CratesIo { repo: None, name: None },
745+
None,
737746
);
738747
let crate2 = graph.add_crate_root(
739748
FileId(2u32),
@@ -746,6 +755,7 @@ mod tests {
746755
Ok(Vec::new()),
747756
false,
748757
CrateOrigin::CratesIo { repo: None, name: None },
758+
None,
749759
);
750760
let crate3 = graph.add_crate_root(
751761
FileId(3u32),
@@ -758,6 +768,7 @@ mod tests {
758768
Ok(Vec::new()),
759769
false,
760770
CrateOrigin::CratesIo { repo: None, name: None },
771+
None,
761772
);
762773
assert!(graph
763774
.add_dep(crate1, Dependency::new(CrateName::new("crate2").unwrap(), crate2))
@@ -781,6 +792,7 @@ mod tests {
781792
Ok(Vec::new()),
782793
false,
783794
CrateOrigin::CratesIo { repo: None, name: None },
795+
None,
784796
);
785797
let crate2 = graph.add_crate_root(
786798
FileId(2u32),
@@ -793,6 +805,7 @@ mod tests {
793805
Ok(Vec::new()),
794806
false,
795807
CrateOrigin::CratesIo { repo: None, name: None },
808+
None,
796809
);
797810
assert!(graph
798811
.add_dep(

crates/hir-def/src/layout.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ use la_arena::{Idx, RawIdx};
66
pub use rustc_abi::{
77
Abi, AbiAndPrefAlign, AddressSpace, Align, Endian, FieldsShape, Integer, IntegerType,
88
LayoutCalculator, Niche, Primitive, ReprFlags, ReprOptions, Scalar, Size, StructKind,
9-
TargetDataLayout, WrappingRange,
9+
TargetDataLayout, TargetDataLayoutErrors, WrappingRange,
1010
};
1111

1212
use crate::LocalEnumVariantId;

crates/hir-ty/src/db.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -64,8 +64,8 @@ pub trait HirDatabase: DefDatabase + Upcast<dyn DefDatabase> {
6464
#[salsa::cycle(crate::layout::layout_of_adt_recover)]
6565
fn layout_of_adt(&self, def: AdtId, subst: Substitution) -> Result<Layout, LayoutError>;
6666

67-
#[salsa::invoke(crate::layout::current_target_data_layout_query)]
68-
fn current_target_data_layout(&self) -> Arc<TargetDataLayout>;
67+
#[salsa::invoke(crate::layout::target_data_layout_query)]
68+
fn target_data_layout(&self, krate: CrateId) -> Arc<TargetDataLayout>;
6969

7070
#[salsa::invoke(crate::lower::callable_item_sig)]
7171
fn callable_item_signature(&self, def: CallableDefId) -> PolyFnSig;

crates/hir-ty/src/layout.rs

+12-10
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
33
use std::sync::Arc;
44

5+
use base_db::CrateId;
56
use chalk_ir::{AdtId, TyKind};
67
use hir_def::{
78
layout::{
@@ -17,7 +18,7 @@ use crate::{db::HirDatabase, Interner, Substitution, Ty};
1718
use self::adt::struct_variant_idx;
1819
pub use self::{
1920
adt::{layout_of_adt_query, layout_of_adt_recover},
20-
target::current_target_data_layout_query,
21+
target::target_data_layout_query,
2122
};
2223

2324
macro_rules! user_error {
@@ -31,6 +32,7 @@ mod target;
3132

3233
struct LayoutCx<'a> {
3334
db: &'a dyn HirDatabase,
35+
krate: CrateId,
3436
}
3537

3638
impl LayoutCalculator for LayoutCx<'_> {
@@ -41,7 +43,7 @@ impl LayoutCalculator for LayoutCx<'_> {
4143
}
4244

4345
fn current_data_layout(&self) -> Arc<TargetDataLayout> {
44-
self.db.current_target_data_layout()
46+
self.db.target_data_layout(self.krate)
4547
}
4648
}
4749

@@ -53,9 +55,9 @@ fn scalar(dl: &TargetDataLayout, value: Primitive) -> Layout {
5355
Layout::scalar(dl, scalar_unit(dl, value))
5456
}
5557

56-
pub fn layout_of_ty(db: &dyn HirDatabase, ty: &Ty) -> Result<Layout, LayoutError> {
57-
let dl = &*db.current_target_data_layout();
58-
let cx = LayoutCx { db };
58+
pub fn layout_of_ty(db: &dyn HirDatabase, ty: &Ty, krate: CrateId) -> Result<Layout, LayoutError> {
59+
let cx = LayoutCx { db, krate };
60+
let dl = &*cx.current_data_layout();
5961
Ok(match ty.kind(Interner) {
6062
TyKind::Adt(AdtId(def), subst) => db.layout_of_adt(*def, subst.clone())?,
6163
TyKind::Scalar(s) => match s {
@@ -84,7 +86,7 @@ pub fn layout_of_ty(db: &dyn HirDatabase, ty: &Ty) -> Result<Layout, LayoutError
8486
chalk_ir::IntTy::I64 => Integer::I64,
8587
chalk_ir::IntTy::I128 => Integer::I128,
8688
},
87-
false,
89+
true,
8890
),
8991
),
9092
chalk_ir::Scalar::Uint(i) => scalar(
@@ -98,7 +100,7 @@ pub fn layout_of_ty(db: &dyn HirDatabase, ty: &Ty) -> Result<Layout, LayoutError
98100
chalk_ir::UintTy::U64 => Integer::I64,
99101
chalk_ir::UintTy::U128 => Integer::I128,
100102
},
101-
true,
103+
false,
102104
),
103105
),
104106
chalk_ir::Scalar::Float(f) => scalar(
@@ -114,7 +116,7 @@ pub fn layout_of_ty(db: &dyn HirDatabase, ty: &Ty) -> Result<Layout, LayoutError
114116

115117
let fields = tys
116118
.iter(Interner)
117-
.map(|k| layout_of_ty(db, k.assert_ty_ref(Interner)))
119+
.map(|k| layout_of_ty(db, k.assert_ty_ref(Interner), krate))
118120
.collect::<Result<Vec<_>, _>>()?;
119121
let fields = fields.iter().collect::<Vec<_>>();
120122
let fields = fields.iter().collect::<Vec<_>>();
@@ -132,7 +134,7 @@ pub fn layout_of_ty(db: &dyn HirDatabase, ty: &Ty) -> Result<Layout, LayoutError
132134
},
133135
_ => return Err(LayoutError::HasPlaceholder),
134136
};
135-
let element = layout_of_ty(db, element)?;
137+
let element = layout_of_ty(db, element, krate)?;
136138
let size = element.size.checked_mul(count, dl).ok_or(LayoutError::SizeOverflow)?;
137139

138140
let abi = if count != 0 && matches!(element.abi, Abi::Uninhabited) {
@@ -153,7 +155,7 @@ pub fn layout_of_ty(db: &dyn HirDatabase, ty: &Ty) -> Result<Layout, LayoutError
153155
}
154156
}
155157
TyKind::Slice(element) => {
156-
let element = layout_of_ty(db, element)?;
158+
let element = layout_of_ty(db, element, krate)?;
157159
Layout {
158160
variants: Variants::Single { index: struct_variant_idx() },
159161
fields: FieldsShape::Array { stride: element.size, count: 0 },

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

+4-4
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ use std::ops::Bound;
55
use hir_def::{
66
adt::VariantData,
77
layout::{Integer, IntegerExt, Layout, LayoutCalculator, LayoutError, RustcEnumVariantIdx},
8-
AdtId, EnumVariantId, LocalEnumVariantId, VariantId,
8+
AdtId, EnumVariantId, HasModule, LocalEnumVariantId, VariantId,
99
};
1010
use la_arena::RawIdx;
1111
use smallvec::SmallVec;
@@ -23,12 +23,12 @@ pub fn layout_of_adt_query(
2323
def: AdtId,
2424
subst: Substitution,
2525
) -> Result<Layout, LayoutError> {
26-
let dl = db.current_target_data_layout();
27-
let cx = LayoutCx { db };
26+
let cx = LayoutCx { db, krate: def.module(db.upcast()).krate() };
27+
let dl = cx.current_data_layout();
2828
let handle_variant = |def: VariantId, var: &VariantData| {
2929
var.fields()
3030
.iter()
31-
.map(|(fd, _)| layout_of_ty(db, &field_ty(db, def, fd, &subst)))
31+
.map(|(fd, _)| layout_of_ty(db, &field_ty(db, def, fd, &subst), cx.krate))
3232
.collect::<Result<Vec<_>, _>>()
3333
};
3434
let (variants, is_enum, is_union, repr) = match def {

0 commit comments

Comments
 (0)