Skip to content

Commit 3906645

Browse files
Rollup merge of #115087 - Nilstrieb:sizeassert, r=fee1-dead
Add disclaimer on size assertion macro Sometimes people are inspired by rustc to add size assertions to their code and copy the macro. This is bad because it causes hard build errors. rustc happens to be special where it makes this okay. For example, see #115028 (not sure whether they were directly inspired by this function), but I think I've also seen other cases.
2 parents 0e84d42 + d16e9c3 commit 3906645

File tree

1 file changed

+12
-0
lines changed
  • compiler/rustc_index/src

1 file changed

+12
-0
lines changed

compiler/rustc_index/src/lib.rs

+12
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,18 @@ pub use {idx::Idx, slice::IndexSlice, vec::IndexVec};
2929
pub use rustc_macros::newtype_index;
3030

3131
/// Type size assertion. The first argument is a type and the second argument is its expected size.
32+
///
33+
/// <div class="warning">
34+
///
35+
/// Emitting hard errors from size assertions like this is generally not
36+
/// recommended, especially in libraries, because they can cause build failures if the layout
37+
/// algorithm or dependencies change. Here in rustc we control the toolchain and layout algorithm,
38+
/// so the former is not a problem. For the latter we have a lockfile as rustc is an application and
39+
/// precompiled library.
40+
///
41+
/// Short version: Don't copy this macro into your own code. Use a `#[test]` instead.
42+
///
43+
/// </div>
3244
#[macro_export]
3345
macro_rules! static_assert_size {
3446
($ty:ty, $size:expr) => {

0 commit comments

Comments
 (0)