Skip to content

Commit 53732d5

Browse files
committed
Auto merge of rust-lang#149109 - scottmcm:align-always-alignment, r=tgross35
See if this is the time we can remove `layout::size_align` This was a bad idea before, but now that `size_of` and `align_of` work completely differently than when removing it was first tried in 2020, maybe it makes sense now. (Or maybe I'll just add another attempt to the list in the comments...) r? ghost
2 parents 7281a3b + 30f4a2a commit 53732d5

File tree

3 files changed

+16
-25
lines changed

3 files changed

+16
-25
lines changed

library/core/src/alloc/layout.rs

Lines changed: 1 addition & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -10,16 +10,6 @@ use crate::mem::SizedTypeProperties;
1010
use crate::ptr::{Alignment, NonNull};
1111
use crate::{assert_unsafe_precondition, fmt, mem};
1212

13-
// While this function is used in one place and its implementation
14-
// could be inlined, the previous attempts to do so made rustc
15-
// slower:
16-
//
17-
// * https://github.com/rust-lang/rust/pull/72189
18-
// * https://github.com/rust-lang/rust/pull/79827
19-
const fn size_align<T>() -> (usize, usize) {
20-
(size_of::<T>(), align_of::<T>())
21-
}
22-
2313
/// Layout of a block of memory.
2414
///
2515
/// An instance of `Layout` describes a particular layout of memory.
@@ -168,11 +158,7 @@ impl Layout {
168158
#[must_use]
169159
#[inline]
170160
pub const fn new<T>() -> Self {
171-
let (size, align) = size_align::<T>();
172-
// SAFETY: if the type is instantiated, rustc already ensures that its
173-
// layout is valid. Use the unchecked constructor to avoid inserting a
174-
// panicking codepath that needs to be optimized out.
175-
unsafe { Layout::from_size_align_unchecked(size, align) }
161+
<T as SizedTypeProperties>::LAYOUT
176162
}
177163

178164
/// Produces layout describing a record that could be used to

library/core/src/mem/mod.rs

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1283,7 +1283,12 @@ pub trait SizedTypeProperties: Sized {
12831283

12841284
#[doc(hidden)]
12851285
#[unstable(feature = "sized_type_properties", issue = "none")]
1286-
const LAYOUT: Layout = Layout::new::<Self>();
1286+
const LAYOUT: Layout = {
1287+
// SAFETY: if the type is instantiated, rustc already ensures that its
1288+
// layout is valid. Use the unchecked constructor to avoid inserting a
1289+
// panicking codepath that needs to be optimized out.
1290+
unsafe { Layout::from_size_align_unchecked(Self::SIZE, Self::ALIGN) }
1291+
};
12871292

12881293
/// The largest safe length for a `[Self]`.
12891294
///

tests/ui/thir-print/offset_of.stdout

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,7 @@ body:
6868
)
6969
else_block: None
7070
lint_level: Explicit(HirId(DefId(offset_of::concrete).10))
71-
span: $DIR/offset_of.rs:37:5: 1430:57 (#0)
71+
span: $DIR/offset_of.rs:37:5: 1435:57 (#0)
7272
}
7373
}
7474
Stmt {
@@ -117,7 +117,7 @@ body:
117117
)
118118
else_block: None
119119
lint_level: Explicit(HirId(DefId(offset_of::concrete).20))
120-
span: $DIR/offset_of.rs:38:5: 1430:57 (#0)
120+
span: $DIR/offset_of.rs:38:5: 1435:57 (#0)
121121
}
122122
}
123123
Stmt {
@@ -166,7 +166,7 @@ body:
166166
)
167167
else_block: None
168168
lint_level: Explicit(HirId(DefId(offset_of::concrete).30))
169-
span: $DIR/offset_of.rs:39:5: 1430:57 (#0)
169+
span: $DIR/offset_of.rs:39:5: 1435:57 (#0)
170170
}
171171
}
172172
Stmt {
@@ -215,7 +215,7 @@ body:
215215
)
216216
else_block: None
217217
lint_level: Explicit(HirId(DefId(offset_of::concrete).40))
218-
span: $DIR/offset_of.rs:40:5: 1430:57 (#0)
218+
span: $DIR/offset_of.rs:40:5: 1435:57 (#0)
219219
}
220220
}
221221
Stmt {
@@ -264,7 +264,7 @@ body:
264264
)
265265
else_block: None
266266
lint_level: Explicit(HirId(DefId(offset_of::concrete).50))
267-
span: $DIR/offset_of.rs:41:5: 1430:57 (#0)
267+
span: $DIR/offset_of.rs:41:5: 1435:57 (#0)
268268
}
269269
}
270270
]
@@ -864,7 +864,7 @@ body:
864864
)
865865
else_block: None
866866
lint_level: Explicit(HirId(DefId(offset_of::generic).12))
867-
span: $DIR/offset_of.rs:45:5: 1430:57 (#0)
867+
span: $DIR/offset_of.rs:45:5: 1435:57 (#0)
868868
}
869869
}
870870
Stmt {
@@ -913,7 +913,7 @@ body:
913913
)
914914
else_block: None
915915
lint_level: Explicit(HirId(DefId(offset_of::generic).24))
916-
span: $DIR/offset_of.rs:46:5: 1430:57 (#0)
916+
span: $DIR/offset_of.rs:46:5: 1435:57 (#0)
917917
}
918918
}
919919
Stmt {
@@ -962,7 +962,7 @@ body:
962962
)
963963
else_block: None
964964
lint_level: Explicit(HirId(DefId(offset_of::generic).36))
965-
span: $DIR/offset_of.rs:47:5: 1430:57 (#0)
965+
span: $DIR/offset_of.rs:47:5: 1435:57 (#0)
966966
}
967967
}
968968
Stmt {
@@ -1011,7 +1011,7 @@ body:
10111011
)
10121012
else_block: None
10131013
lint_level: Explicit(HirId(DefId(offset_of::generic).48))
1014-
span: $DIR/offset_of.rs:48:5: 1430:57 (#0)
1014+
span: $DIR/offset_of.rs:48:5: 1435:57 (#0)
10151015
}
10161016
}
10171017
]

0 commit comments

Comments
 (0)