Skip to content
This repository was archived by the owner on May 28, 2025. It is now read-only.

Commit e11c0e3

Browse files
committed
Auto merge of rust-lang#13966 - HKalbasi:layout, r=Veykril
Don't compute layout if `TargetDataLayout` is not available
2 parents 455ef0c + ec65b3b commit e11c0e3

File tree

7 files changed

+40
-43
lines changed

7 files changed

+40
-43
lines changed

crates/base-db/src/fixture.rs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -110,6 +110,7 @@ impl ChangeFixture {
110110
let mut crates = FxHashMap::default();
111111
let mut crate_deps = Vec::new();
112112
let mut default_crate_root: Option<FileId> = None;
113+
let mut default_target_data_layout: Option<String> = None;
113114
let mut default_cfg = CfgOptions::default();
114115

115116
let mut file_set = FileSet::default();
@@ -175,6 +176,7 @@ impl ChangeFixture {
175176
assert!(default_crate_root.is_none());
176177
default_crate_root = Some(file_id);
177178
default_cfg = meta.cfg;
179+
default_target_data_layout = meta.target_data_layout;
178180
}
179181

180182
change.change_file(file_id, Some(Arc::new(text)));
@@ -198,7 +200,7 @@ impl ChangeFixture {
198200
Ok(Vec::new()),
199201
false,
200202
CrateOrigin::CratesIo { repo: None, name: None },
201-
None,
203+
default_target_data_layout.map(|x| x.into()),
202204
);
203205
} else {
204206
for (from, to, prelude) in crate_deps {

crates/hir-def/src/layout.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -90,6 +90,7 @@ impl IntegerExt for Integer {
9090
pub enum LayoutError {
9191
UserError(String),
9292
SizeOverflow,
93+
TargetLayoutNotAvailable,
9394
HasPlaceholder,
9495
NotImplemented,
9596
Unknown,

crates/hir-ty/src/db.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,7 @@ pub trait HirDatabase: DefDatabase + Upcast<dyn DefDatabase> {
6565
fn layout_of_adt(&self, def: AdtId, subst: Substitution) -> Result<Layout, LayoutError>;
6666

6767
#[salsa::invoke(crate::layout::target_data_layout_query)]
68-
fn target_data_layout(&self, krate: CrateId) -> Arc<TargetDataLayout>;
68+
fn target_data_layout(&self, krate: CrateId) -> Option<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

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,5 @@
11
//! Compute the binary representation of a type
22
3-
use std::sync::Arc;
4-
53
use base_db::CrateId;
64
use chalk_ir::{AdtId, TyKind};
75
use hir_def::{
@@ -31,19 +29,19 @@ mod adt;
3129
mod target;
3230

3331
struct LayoutCx<'a> {
34-
db: &'a dyn HirDatabase,
3532
krate: CrateId,
33+
target: &'a TargetDataLayout,
3634
}
3735

38-
impl LayoutCalculator for LayoutCx<'_> {
39-
type TargetDataLayoutRef = Arc<TargetDataLayout>;
36+
impl<'a> LayoutCalculator for LayoutCx<'a> {
37+
type TargetDataLayoutRef = &'a TargetDataLayout;
4038

4139
fn delay_bug(&self, txt: &str) {
4240
never!("{}", txt);
4341
}
4442

45-
fn current_data_layout(&self) -> Arc<TargetDataLayout> {
46-
self.db.target_data_layout(self.krate)
43+
fn current_data_layout(&self) -> &'a TargetDataLayout {
44+
self.target
4745
}
4846
}
4947

@@ -56,7 +54,8 @@ fn scalar(dl: &TargetDataLayout, value: Primitive) -> Layout {
5654
}
5755

5856
pub fn layout_of_ty(db: &dyn HirDatabase, ty: &Ty, krate: CrateId) -> Result<Layout, LayoutError> {
59-
let cx = LayoutCx { db, krate };
57+
let Some(target) = db.target_data_layout(krate) else { return Err(LayoutError::TargetLayoutNotAvailable) };
58+
let cx = LayoutCx { krate, target: &target };
6059
let dl = &*cx.current_data_layout();
6160
Ok(match ty.kind(Interner) {
6261
TyKind::Adt(AdtId(def), subst) => db.layout_of_adt(*def, subst.clone())?,

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

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,9 @@ pub fn layout_of_adt_query(
2323
def: AdtId,
2424
subst: Substitution,
2525
) -> Result<Layout, LayoutError> {
26-
let cx = LayoutCx { db, krate: def.module(db.upcast()).krate() };
26+
let krate = def.module(db.upcast()).krate();
27+
let Some(target) = db.target_data_layout(krate) else { return Err(LayoutError::TargetLayoutNotAvailable) };
28+
let cx = LayoutCx { krate, target: &target };
2729
let dl = cx.current_data_layout();
2830
let handle_variant = |def: VariantId, var: &VariantData| {
2931
var.fields()

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

Lines changed: 7 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -3,34 +3,15 @@
33
use std::sync::Arc;
44

55
use base_db::CrateId;
6-
use hir_def::layout::{Endian, Size, TargetDataLayout};
6+
use hir_def::layout::TargetDataLayout;
77

88
use crate::db::HirDatabase;
99

10-
pub fn target_data_layout_query(db: &dyn HirDatabase, krate: CrateId) -> Arc<TargetDataLayout> {
10+
pub fn target_data_layout_query(
11+
db: &dyn HirDatabase,
12+
krate: CrateId,
13+
) -> Option<Arc<TargetDataLayout>> {
1114
let crate_graph = db.crate_graph();
12-
let target_layout = &crate_graph[krate].target_layout;
13-
let cfg_options = &crate_graph[krate].cfg_options;
14-
Arc::new(
15-
target_layout
16-
.as_ref()
17-
.and_then(|it| TargetDataLayout::parse_from_llvm_datalayout_string(it).ok())
18-
.unwrap_or_else(|| {
19-
let endian = match cfg_options.get_cfg_values("target_endian").next() {
20-
Some(x) if x.as_str() == "big" => Endian::Big,
21-
_ => Endian::Little,
22-
};
23-
let pointer_size = Size::from_bytes(
24-
match cfg_options.get_cfg_values("target_pointer_width").next() {
25-
Some(x) => match x.as_str() {
26-
"16" => 2,
27-
"32" => 4,
28-
_ => 8,
29-
},
30-
_ => 8,
31-
},
32-
);
33-
TargetDataLayout { endian, pointer_size, ..TargetDataLayout::default() }
34-
}),
35-
)
15+
let target_layout = crate_graph[krate].target_layout.as_ref()?;
16+
Some(Arc::new(TargetDataLayout::parse_from_llvm_datalayout_string(&target_layout).ok()?))
3617
}

crates/ide/src/hover/tests.rs

Lines changed: 18 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -527,6 +527,7 @@ fn hover_field_offset() {
527527
// Hovering over the field when instantiating
528528
check(
529529
r#"
530+
//- /main.rs target_data_layout:e-m:e-p270:32:32-p271:32:32-p272:64:64-i64:64-f80:128-n8:16:32:64-S128
530531
struct Foo { fiel$0d_a: u8, field_b: i32, field_c: i16 }
531532
"#,
532533
expect![[r#"
@@ -548,6 +549,7 @@ fn hover_shows_struct_field_info() {
548549
// Hovering over the field when instantiating
549550
check(
550551
r#"
552+
//- /main.rs target_data_layout:e-m:e-p270:32:32-p271:32:32-p272:64:64-i64:64-f80:128-n8:16:32:64-S128
551553
struct Foo { field_a: u32 }
552554
553555
fn main() {
@@ -570,6 +572,7 @@ fn main() {
570572
// Hovering over the field in the definition
571573
check(
572574
r#"
575+
//- /main.rs target_data_layout:e-m:e-p270:32:32-p271:32:32-p272:64:64-i64:64-f80:128-n8:16:32:64-S128
573576
struct Foo { field_a$0: u32 }
574577
575578
fn main() {
@@ -1515,6 +1518,8 @@ fn my() {}
15151518
fn test_hover_struct_doc_comment() {
15161519
check(
15171520
r#"
1521+
//- /main.rs target_data_layout:e-m:e-p270:32:32-p271:32:32-p272:64:64-i64:64-f80:128-n8:16:32:64-S128
1522+
15181523
/// This is an example
15191524
/// multiline doc
15201525
///
@@ -1573,7 +1578,7 @@ fn foo() { let bar = Ba$0r; }
15731578
```
15741579
15751580
```rust
1576-
struct Bar // size = 0, align = 1
1581+
struct Bar
15771582
```
15781583
15791584
---
@@ -1602,7 +1607,7 @@ fn foo() { let bar = Ba$0r; }
16021607
```
16031608
16041609
```rust
1605-
struct Bar // size = 0, align = 1
1610+
struct Bar
16061611
```
16071612
16081613
---
@@ -1630,7 +1635,7 @@ pub struct B$0ar
16301635
```
16311636
16321637
```rust
1633-
pub struct Bar // size = 0, align = 1
1638+
pub struct Bar
16341639
```
16351640
16361641
---
@@ -1657,7 +1662,7 @@ pub struct B$0ar
16571662
```
16581663
16591664
```rust
1660-
pub struct Bar // size = 0, align = 1
1665+
pub struct Bar
16611666
```
16621667
16631668
---
@@ -2959,6 +2964,8 @@ fn main() { let foo_test = name_with_dashes::wrapper::Thing::new$0(); }
29592964
fn hover_field_pat_shorthand_ref_match_ergonomics() {
29602965
check(
29612966
r#"
2967+
//- /main.rs target_data_layout:e-m:e-p270:32:32-p271:32:32-p272:64:64-i64:64-f80:128-n8:16:32:64-S128
2968+
29622969
struct S {
29632970
f: i32,
29642971
}
@@ -4398,6 +4405,7 @@ fn main() {
43984405
fn hover_intra_doc_links() {
43994406
check(
44004407
r#"
4408+
//- /main.rs target_data_layout:e-m:e-p270:32:32-p271:32:32-p272:64:64-i64:64-f80:128-n8:16:32:64-S128
44014409
44024410
pub mod theitem {
44034411
/// This is the item. Cool!
@@ -4539,7 +4547,7 @@ trait A where
45394547
fn string_shadowed_with_inner_items() {
45404548
check(
45414549
r#"
4542-
//- /main.rs crate:main deps:alloc
4550+
//- /main.rs crate:main deps:alloc target_data_layout:e-m:e-p270:32:32-p271:32:32-p272:64:64-i64:64-f80:128-n8:16:32:64-S128
45434551
45444552
/// Custom `String` type.
45454553
struct String;
@@ -5234,7 +5242,7 @@ foo_macro!(
52345242
```
52355243
52365244
```rust
5237-
pub struct Foo // size = 0, align = 1
5245+
pub struct Foo
52385246
```
52395247
52405248
---
@@ -5248,6 +5256,8 @@ foo_macro!(
52485256
fn hover_intra_in_attr() {
52495257
check(
52505258
r#"
5259+
//- /main.rs target_data_layout:e-m:e-p270:32:32-p271:32:32-p272:64:64-i64:64-f80:128-n8:16:32:64-S128
5260+
52515261
#[doc = "Doc comment for [`Foo$0`]"]
52525262
pub struct Foo(i32);
52535263
"#,
@@ -5368,6 +5378,8 @@ enum Enum {
53685378
fn hover_record_variant_field() {
53695379
check(
53705380
r#"
5381+
//- /main.rs target_data_layout:e-m:e-p270:32:32-p271:32:32-p272:64:64-i64:64-f80:128-n8:16:32:64-S128
5382+
53715383
enum Enum {
53725384
RecordV { field$0: u32 }
53735385
}

0 commit comments

Comments
 (0)