Auto merge of #13966 - HKalbasi:layout, r=Veykril

Don't compute layout if `TargetDataLayout` is not available
This commit is contained in:
bors 2023-01-17 09:39:32 +00:00
commit e11c0e3e85
7 changed files with 40 additions and 43 deletions

View File

@ -110,6 +110,7 @@ impl ChangeFixture {
let mut crates = FxHashMap::default();
let mut crate_deps = Vec::new();
let mut default_crate_root: Option<FileId> = None;
let mut default_target_data_layout: Option<String> = None;
let mut default_cfg = CfgOptions::default();
let mut file_set = FileSet::default();
@ -175,6 +176,7 @@ impl ChangeFixture {
assert!(default_crate_root.is_none());
default_crate_root = Some(file_id);
default_cfg = meta.cfg;
default_target_data_layout = meta.target_data_layout;
}
change.change_file(file_id, Some(Arc::new(text)));
@ -198,7 +200,7 @@ impl ChangeFixture {
Ok(Vec::new()),
false,
CrateOrigin::CratesIo { repo: None, name: None },
None,
default_target_data_layout.map(|x| x.into()),
);
} else {
for (from, to, prelude) in crate_deps {

View File

@ -90,6 +90,7 @@ impl IntegerExt for Integer {
pub enum LayoutError {
UserError(String),
SizeOverflow,
TargetLayoutNotAvailable,
HasPlaceholder,
NotImplemented,
Unknown,

View File

@ -65,7 +65,7 @@ pub trait HirDatabase: DefDatabase + Upcast<dyn DefDatabase> {
fn layout_of_adt(&self, def: AdtId, subst: Substitution) -> Result<Layout, LayoutError>;
#[salsa::invoke(crate::layout::target_data_layout_query)]
fn target_data_layout(&self, krate: CrateId) -> Arc<TargetDataLayout>;
fn target_data_layout(&self, krate: CrateId) -> Option<Arc<TargetDataLayout>>;
#[salsa::invoke(crate::lower::callable_item_sig)]
fn callable_item_signature(&self, def: CallableDefId) -> PolyFnSig;

View File

@ -1,7 +1,5 @@
//! Compute the binary representation of a type
use std::sync::Arc;
use base_db::CrateId;
use chalk_ir::{AdtId, TyKind};
use hir_def::{
@ -31,19 +29,19 @@ mod adt;
mod target;
struct LayoutCx<'a> {
db: &'a dyn HirDatabase,
krate: CrateId,
target: &'a TargetDataLayout,
}
impl LayoutCalculator for LayoutCx<'_> {
type TargetDataLayoutRef = Arc<TargetDataLayout>;
impl<'a> LayoutCalculator for LayoutCx<'a> {
type TargetDataLayoutRef = &'a TargetDataLayout;
fn delay_bug(&self, txt: &str) {
never!("{}", txt);
}
fn current_data_layout(&self) -> Arc<TargetDataLayout> {
self.db.target_data_layout(self.krate)
fn current_data_layout(&self) -> &'a TargetDataLayout {
self.target
}
}
@ -56,7 +54,8 @@ fn scalar(dl: &TargetDataLayout, value: Primitive) -> Layout {
}
pub fn layout_of_ty(db: &dyn HirDatabase, ty: &Ty, krate: CrateId) -> Result<Layout, LayoutError> {
let cx = LayoutCx { db, krate };
let Some(target) = db.target_data_layout(krate) else { return Err(LayoutError::TargetLayoutNotAvailable) };
let cx = LayoutCx { krate, target: &target };
let dl = &*cx.current_data_layout();
Ok(match ty.kind(Interner) {
TyKind::Adt(AdtId(def), subst) => db.layout_of_adt(*def, subst.clone())?,

View File

@ -23,7 +23,9 @@ pub fn layout_of_adt_query(
def: AdtId,
subst: Substitution,
) -> Result<Layout, LayoutError> {
let cx = LayoutCx { db, krate: def.module(db.upcast()).krate() };
let krate = def.module(db.upcast()).krate();
let Some(target) = db.target_data_layout(krate) else { return Err(LayoutError::TargetLayoutNotAvailable) };
let cx = LayoutCx { krate, target: &target };
let dl = cx.current_data_layout();
let handle_variant = |def: VariantId, var: &VariantData| {
var.fields()

View File

@ -3,34 +3,15 @@
use std::sync::Arc;
use base_db::CrateId;
use hir_def::layout::{Endian, Size, TargetDataLayout};
use hir_def::layout::TargetDataLayout;
use crate::db::HirDatabase;
pub fn target_data_layout_query(db: &dyn HirDatabase, krate: CrateId) -> Arc<TargetDataLayout> {
pub fn target_data_layout_query(
db: &dyn HirDatabase,
krate: CrateId,
) -> Option<Arc<TargetDataLayout>> {
let crate_graph = db.crate_graph();
let target_layout = &crate_graph[krate].target_layout;
let cfg_options = &crate_graph[krate].cfg_options;
Arc::new(
target_layout
.as_ref()
.and_then(|it| TargetDataLayout::parse_from_llvm_datalayout_string(it).ok())
.unwrap_or_else(|| {
let endian = match cfg_options.get_cfg_values("target_endian").next() {
Some(x) if x.as_str() == "big" => Endian::Big,
_ => Endian::Little,
};
let pointer_size = Size::from_bytes(
match cfg_options.get_cfg_values("target_pointer_width").next() {
Some(x) => match x.as_str() {
"16" => 2,
"32" => 4,
_ => 8,
},
_ => 8,
},
);
TargetDataLayout { endian, pointer_size, ..TargetDataLayout::default() }
}),
)
let target_layout = crate_graph[krate].target_layout.as_ref()?;
Some(Arc::new(TargetDataLayout::parse_from_llvm_datalayout_string(&target_layout).ok()?))
}

View File

@ -527,6 +527,7 @@ fn hover_field_offset() {
// Hovering over the field when instantiating
check(
r#"
//- /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
struct Foo { fiel$0d_a: u8, field_b: i32, field_c: i16 }
"#,
expect![[r#"
@ -548,6 +549,7 @@ fn hover_shows_struct_field_info() {
// Hovering over the field when instantiating
check(
r#"
//- /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
struct Foo { field_a: u32 }
fn main() {
@ -570,6 +572,7 @@ fn main() {
// Hovering over the field in the definition
check(
r#"
//- /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
struct Foo { field_a$0: u32 }
fn main() {
@ -1515,6 +1518,8 @@ fn my() {}
fn test_hover_struct_doc_comment() {
check(
r#"
//- /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
/// This is an example
/// multiline doc
///
@ -1573,7 +1578,7 @@ fn foo() { let bar = Ba$0r; }
```
```rust
struct Bar // size = 0, align = 1
struct Bar
```
---
@ -1602,7 +1607,7 @@ fn foo() { let bar = Ba$0r; }
```
```rust
struct Bar // size = 0, align = 1
struct Bar
```
---
@ -1630,7 +1635,7 @@ pub struct B$0ar
```
```rust
pub struct Bar // size = 0, align = 1
pub struct Bar
```
---
@ -1657,7 +1662,7 @@ pub struct B$0ar
```
```rust
pub struct Bar // size = 0, align = 1
pub struct Bar
```
---
@ -2959,6 +2964,8 @@ fn main() { let foo_test = name_with_dashes::wrapper::Thing::new$0(); }
fn hover_field_pat_shorthand_ref_match_ergonomics() {
check(
r#"
//- /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
struct S {
f: i32,
}
@ -4398,6 +4405,7 @@ fn main() {
fn hover_intra_doc_links() {
check(
r#"
//- /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
pub mod theitem {
/// This is the item. Cool!
@ -4539,7 +4547,7 @@ trait A where
fn string_shadowed_with_inner_items() {
check(
r#"
//- /main.rs crate:main deps:alloc
//- /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
/// Custom `String` type.
struct String;
@ -5234,7 +5242,7 @@ foo_macro!(
```
```rust
pub struct Foo // size = 0, align = 1
pub struct Foo
```
---
@ -5248,6 +5256,8 @@ foo_macro!(
fn hover_intra_in_attr() {
check(
r#"
//- /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
#[doc = "Doc comment for [`Foo$0`]"]
pub struct Foo(i32);
"#,
@ -5368,6 +5378,8 @@ enum Enum {
fn hover_record_variant_field() {
check(
r#"
//- /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
enum Enum {
RecordV { field$0: u32 }
}