@@ -62,7 +62,7 @@ use hir_ty::{
62
62
consteval:: { try_const_usize, unknown_const_as_generic, ConstEvalError , ConstExt } ,
63
63
diagnostics:: BodyValidationDiagnostic ,
64
64
display:: HexifiedConst ,
65
- layout:: { LayoutError , RustcEnumVariantIdx , TagEncoding } ,
65
+ layout:: { Layout as TyLayout , LayoutError , RustcEnumVariantIdx , TagEncoding } ,
66
66
method_resolution:: { self , TyFingerprint } ,
67
67
mir:: { self , interpret_mir} ,
68
68
primitive:: UintTy ,
@@ -133,11 +133,8 @@ pub use {
133
133
} ,
134
134
hir_ty:: {
135
135
display:: { ClosureStyle , HirDisplay , HirDisplayError , HirWrite } ,
136
- // FIXME: This just needs a HIR wrapper
137
- layout:: Layout ,
138
136
mir:: MirEvalError ,
139
- PointerCast ,
140
- Safety ,
137
+ PointerCast , Safety ,
141
138
} ,
142
139
} ;
143
140
@@ -964,8 +961,8 @@ impl Field {
964
961
Type :: new ( db, var_id, ty)
965
962
}
966
963
967
- pub fn layout ( & self , db : & dyn HirDatabase ) -> Result < Arc < Layout > , LayoutError > {
968
- db. layout_of_ty ( self . ty ( db) . ty . clone ( ) , self . parent . module ( db) . krate ( ) . into ( ) )
964
+ pub fn layout ( & self , db : & dyn HirDatabase ) -> Result < Layout , LayoutError > {
965
+ db. layout_of_ty ( self . ty ( db) . ty . clone ( ) , self . parent . module ( db) . krate ( ) . into ( ) ) . map ( Layout )
969
966
}
970
967
971
968
pub fn parent_def ( & self , _db : & dyn HirDatabase ) -> VariantDef {
@@ -1138,10 +1135,10 @@ impl Enum {
1138
1135
self . variants ( db) . iter ( ) . any ( |v| !matches ! ( v. kind( db) , StructKind :: Unit ) )
1139
1136
}
1140
1137
1141
- pub fn layout ( self , db : & dyn HirDatabase ) -> Result < ( Arc < Layout > , usize ) , LayoutError > {
1138
+ pub fn layout ( self , db : & dyn HirDatabase ) -> Result < ( Layout , usize ) , LayoutError > {
1142
1139
let layout = Adt :: from ( self ) . layout ( db) ?;
1143
1140
let tag_size =
1144
- if let layout:: Variants :: Multiple { tag, tag_encoding, .. } = & layout. variants {
1141
+ if let layout:: Variants :: Multiple { tag, tag_encoding, .. } = & layout. 0 . variants {
1145
1142
match tag_encoding {
1146
1143
TagEncoding :: Direct => {
1147
1144
let target_data_layout = db
@@ -1222,11 +1219,11 @@ impl Variant {
1222
1219
let parent_enum = self . parent_enum ( db) ;
1223
1220
let ( parent_layout, tag_size) = parent_enum. layout ( db) ?;
1224
1221
Ok ( (
1225
- match & parent_layout. variants {
1222
+ match & parent_layout. 0 . variants {
1226
1223
layout:: Variants :: Multiple { variants, .. } => {
1227
- variants[ RustcEnumVariantIdx ( self . id ) ] . clone ( )
1224
+ Layout ( Arc :: new ( variants[ RustcEnumVariantIdx ( self . id ) ] . clone ( ) ) )
1228
1225
}
1229
- _ => ( * parent_layout) . clone ( ) ,
1226
+ _ => parent_layout,
1230
1227
} ,
1231
1228
tag_size,
1232
1229
) )
@@ -1258,11 +1255,11 @@ impl Adt {
1258
1255
} )
1259
1256
}
1260
1257
1261
- pub fn layout ( self , db : & dyn HirDatabase ) -> Result < Arc < Layout > , LayoutError > {
1258
+ pub fn layout ( self , db : & dyn HirDatabase ) -> Result < Layout , LayoutError > {
1262
1259
if db. generic_params ( self . into ( ) ) . iter ( ) . count ( ) != 0 {
1263
1260
return Err ( LayoutError :: HasPlaceholder ) ;
1264
1261
}
1265
- db. layout_of_adt ( self . into ( ) , Substitution :: empty ( Interner ) , self . krate ( db) . id )
1262
+ db. layout_of_adt ( self . into ( ) , Substitution :: empty ( Interner ) , self . krate ( db) . id ) . map ( Layout )
1266
1263
}
1267
1264
1268
1265
/// Turns this ADT into a type. Any type parameters of the ADT will be
@@ -4246,8 +4243,8 @@ impl Type {
4246
4243
. collect ( )
4247
4244
}
4248
4245
4249
- pub fn layout ( & self , db : & dyn HirDatabase ) -> Result < Arc < Layout > , LayoutError > {
4250
- db. layout_of_ty ( self . ty . clone ( ) , self . env . krate )
4246
+ pub fn layout ( & self , db : & dyn HirDatabase ) -> Result < Layout , LayoutError > {
4247
+ db. layout_of_ty ( self . ty . clone ( ) , self . env . krate ) . map ( Layout )
4251
4248
}
4252
4249
}
4253
4250
@@ -4358,6 +4355,35 @@ fn closure_source(db: &dyn HirDatabase, closure: ClosureId) -> Option<ast::Closu
4358
4355
}
4359
4356
}
4360
4357
4358
+ #[ derive( Clone , Debug , Eq , PartialEq ) ]
4359
+ pub struct Layout ( Arc < TyLayout > ) ;
4360
+
4361
+ impl Layout {
4362
+ pub fn size ( & self ) -> u64 {
4363
+ self . 0 . size . bytes ( )
4364
+ }
4365
+
4366
+ pub fn align ( & self ) -> u64 {
4367
+ self . 0 . align . abi . bytes ( )
4368
+ }
4369
+
4370
+ pub fn niches ( & self , db : & dyn HirDatabase , krate : Crate ) -> Option < u128 > {
4371
+ Some ( self . 0 . largest_niche ?. available ( & * db. target_data_layout ( krate. id ) ?) )
4372
+ }
4373
+
4374
+ pub fn field_offset ( & self , idx : usize ) -> Option < u64 > {
4375
+ match self . 0 . fields {
4376
+ layout:: FieldsShape :: Primitive => None ,
4377
+ layout:: FieldsShape :: Union ( _) => Some ( 0 ) ,
4378
+ layout:: FieldsShape :: Array { stride, count } => {
4379
+ let i = u64:: try_from ( idx) . ok ( ) ?;
4380
+ ( i < count) . then_some ( ( stride * i) . bytes ( ) )
4381
+ }
4382
+ layout:: FieldsShape :: Arbitrary { ref offsets, .. } => Some ( offsets. get ( idx) ?. bytes ( ) ) ,
4383
+ }
4384
+ }
4385
+ }
4386
+
4361
4387
#[ derive( Copy , Clone , Debug , Eq , PartialEq ) ]
4362
4388
pub enum BindingMode {
4363
4389
Move ,
0 commit comments