Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
41 commits
Select commit Hold shift + click to select a range
a5a60d7
Add `size_of`, `size_of_val`, `align_of`, and `align_of_val` to the p…
joshtriplett Mar 28, 2024
b4834a1
rewrite resolve-rename in rmake
Oneirical May 28, 2024
d553d5b
rewrite incr-prev-body-beyond-eof in rmake
Oneirical May 28, 2024
8530285
rustc_span: Inline some hot functions
petrochenkov Jun 2, 2024
07dc3eb
Allow static mut definitions with #[linkage]
bjorn3 Jun 3, 2024
8f08625
Remove a bunch of redundant args from report_method_error
compiler-errors Jun 3, 2024
e9957b9
Stop passing empty args to check_expr_path
compiler-errors Jun 3, 2024
8f677e8
bootstrap: implement new feature `bootstrap-self-test`
onur-ozkan May 19, 2024
2bad3d1
Make deleting on LinkedList aware of the allocator
xTachyon Jun 4, 2024
59e2074
rewrite suspicious-library in rmake
Oneirical May 28, 2024
5d26f58
Closures are recursively reachable
tmiasko Jun 4, 2024
ac96fa4
Use inline const instead of unsafe to construct arrays in `MaybeUnini…
kpreid May 13, 2024
ec8fa17
Use inline const instead of unsafe to implement `MaybeUninit::uninit_…
kpreid May 13, 2024
2a21937
Add a co-maintainer for the two ARMv4T targets
Lokathor Jun 4, 2024
e1ae0fa
`UNICODE_ARRAY` and `ASCII_ARRAY` fixes.
nnethercote May 29, 2024
d1215da
Don't use the word "parse" for lexing operations.
nnethercote May 30, 2024
f657624
Inline and remove `error_malformed_cfg_attr_missing`.
nnethercote May 31, 2024
769ca3f
Rename `maybe_file_to_stream` as `maybe_source_file_to_stream`.
nnethercote May 31, 2024
3c321b9
Remove `stream_to_parser`.
nnethercote May 31, 2024
af13b48
Improve `panictry_buffer!`.
nnethercote May 31, 2024
29e6e28
Remove low-value comments.
nnethercote May 31, 2024
191b76e
Rename `maybe_source_file_to_parser` as `maybe_new_parser_from_source…
nnethercote May 31, 2024
8964106
Use `source_str_to_stream` in a test file.
nnethercote May 31, 2024
25972ae
Inline and remove `parse_crate{,_attrs}_from_{file,source_str}`.
nnethercote May 31, 2024
ab192a0
Reorder `source_str_to_stream` arguments.
nnethercote May 31, 2024
264dbe4
Inline and remove `source_file_to_stream`.
nnethercote May 31, 2024
b903733
Make top-level `rustc_parse` functions fallible.
nnethercote May 31, 2024
2d4e7df
rustfmt: Remove an unnecessary `catch_unwind` use.
nnethercote Jun 3, 2024
dd6bca5
Add another test for hidden types capturing lifetimes that outlive bu…
compiler-errors Jun 5, 2024
9ccc7b7
Rollup merge of #123168 - joshtriplett:size-of-prelude, r=Amanieu
workingjubilee Jun 5, 2024
0f86182
Rollup merge of #125273 - onur-ozkan:bootstrap-self-test, r=albertlar…
workingjubilee Jun 5, 2024
0f2b34a
Rollup merge of #125683 - Oneirical:patience-testing-test, r=jieyouxu
workingjubilee Jun 5, 2024
05b4674
Rollup merge of #125815 - nnethercote:rustc_parse-top-level-cleanups,…
workingjubilee Jun 5, 2024
0096138
Rollup merge of #125903 - petrochenkov:upctxt3, r=nnethercote
workingjubilee Jun 5, 2024
78d9a7e
Rollup merge of #125906 - compiler-errors:simplify-method-error-args,…
workingjubilee Jun 5, 2024
2b89c1b
Rollup merge of #125920 - bjorn3:allow_static_mut_linkage_def, r=Urgau
workingjubilee Jun 5, 2024
448159c
Rollup merge of #125982 - xTachyon:fix-linked-list, r=jhpratt
workingjubilee Jun 5, 2024
6b6b698
Rollup merge of #125995 - kpreid:const-uninit-stable, r=Nilstrieb
workingjubilee Jun 5, 2024
eb2819e
Rollup merge of #125996 - tmiasko:closure-recursively-reachable, r=ol…
workingjubilee Jun 5, 2024
669935e
Rollup merge of #126003 - Lokathor:new-co-maintainer, r=compiler-errors
workingjubilee Jun 5, 2024
f12fe3a
Rollup merge of #126004 - compiler-errors:captures-soundness-test, r=…
workingjubilee Jun 5, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Next Next commit
Add size_of, size_of_val, align_of, and align_of_val to the p…
…relude

Many, many projects use `size_of` to get the size of a type. However,
it's also often equally easy to hardcode a size (e.g. `8` instead of
`size_of::<u64>()`). Minimizing friction in the use of `size_of` helps
ensure that people use it and make code more self-documenting.

The name `size_of` is unambiguous: the name alone, without any prefix or
path, is self-explanatory and unmistakeable for any other functionality.
Adding it to the prelude cannot produce any name conflicts, as any local
definition will silently shadow the one from the prelude. Thus, we don't
need to wait for a new edition prelude to add it.

Add `size_of_val`, `align_of`, and `align_of_val` as well, with similar
justification: widely useful, self-explanatory, unmistakeable for
anything else, won't produce conflicts.
  • Loading branch information
joshtriplett committed May 13, 2024
commit a5a60d75a87881f79deea115d7cd002ce3373fd5
2 changes: 1 addition & 1 deletion compiler/rustc_middle/src/query/erase.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ use crate::traits;
use crate::ty::adjustment::CoerceUnsizedInfo;
use crate::ty::{self, Ty};
use std::intrinsics::transmute_unchecked;
use std::mem::{size_of, MaybeUninit};
use std::mem::MaybeUninit;

#[derive(Copy, Clone)]
pub struct Erased<T: Copy> {
Expand Down
2 changes: 0 additions & 2 deletions library/alloc/src/rc.rs
Original file line number Diff line number Diff line change
Expand Up @@ -257,8 +257,6 @@ use core::intrinsics::abort;
#[cfg(not(no_global_oom_handling))]
use core::iter;
use core::marker::{PhantomData, Unsize};
#[cfg(not(no_global_oom_handling))]
use core::mem::size_of_val;
use core::mem::{self, align_of_val_raw, forget, ManuallyDrop};
use core::ops::{CoerceUnsized, Deref, DerefMut, DerefPure, DispatchFromDyn, Receiver};
use core::panic::{RefUnwindSafe, UnwindSafe};
Expand Down
2 changes: 0 additions & 2 deletions library/alloc/src/sync.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,6 @@ use core::intrinsics::abort;
#[cfg(not(no_global_oom_handling))]
use core::iter;
use core::marker::{PhantomData, Unsize};
#[cfg(not(no_global_oom_handling))]
use core::mem::size_of_val;
use core::mem::{self, align_of_val_raw};
use core::ops::{CoerceUnsized, Deref, DerefPure, DispatchFromDyn, Receiver};
use core::panic::{RefUnwindSafe, UnwindSafe};
Expand Down
2 changes: 1 addition & 1 deletion library/core/src/cell.rs
Original file line number Diff line number Diff line change
Expand Up @@ -238,7 +238,7 @@
use crate::cmp::Ordering;
use crate::fmt::{self, Debug, Display};
use crate::marker::{PhantomData, Unsize};
use crate::mem::{self, size_of};
use crate::mem;
use crate::ops::{CoerceUnsized, Deref, DerefMut, DerefPure, DispatchFromDyn};
use crate::ptr::{self, NonNull};

Expand Down
1 change: 0 additions & 1 deletion library/core/src/intrinsics.rs
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,6 @@

use crate::marker::DiscriminantKind;
use crate::marker::Tuple;
use crate::mem::align_of;
use crate::ptr;
use crate::ub_checks;

Expand Down
3 changes: 3 additions & 0 deletions library/core/src/prelude/common.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,9 @@ pub use crate::ops::{Drop, Fn, FnMut, FnOnce};
#[stable(feature = "core_prelude", since = "1.4.0")]
#[doc(no_inline)]
pub use crate::mem::drop;
#[stable(feature = "size_of_prelude", since = "CURRENT_RUSTC_VERSION")]
#[doc(no_inline)]
pub use crate::mem::{align_of, align_of_val, size_of, size_of_val};

// Re-exported types and traits
#[stable(feature = "core_prelude", since = "1.4.0")]
Expand Down
2 changes: 1 addition & 1 deletion library/core/src/ptr/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -392,7 +392,7 @@ use crate::intrinsics;
use crate::marker::FnPtr;
use crate::ub_checks;

use crate::mem::{self, align_of, size_of, MaybeUninit};
use crate::mem::{self, MaybeUninit};

mod alignment;
#[unstable(feature = "ptr_alignment_type", issue = "102070")]
Expand Down
1 change: 0 additions & 1 deletion library/core/src/slice/raw.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
//! Free functions to create `&[T]` and `&mut [T]`.

use crate::array;
use crate::mem::{align_of, size_of};
use crate::ops::Range;
use crate::ptr;
use crate::ub_checks;
Expand Down
3 changes: 0 additions & 3 deletions library/core/src/sync/atomic.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1296,7 +1296,6 @@ impl<T> AtomicPtr<T> {
#[cfg(target_has_atomic_equal_alignment = "ptr")]
#[unstable(feature = "atomic_from_mut", issue = "76314")]
pub fn from_mut(v: &mut *mut T) -> &mut Self {
use crate::mem::align_of;
let [] = [(); align_of::<AtomicPtr<()>>() - align_of::<*mut ()>()];
// SAFETY:
// - the mutable reference guarantees unique ownership.
Expand Down Expand Up @@ -2286,7 +2285,6 @@ macro_rules! atomic_int {
#[$cfg_align]
#[unstable(feature = "atomic_from_mut", issue = "76314")]
pub fn from_mut(v: &mut $int_type) -> &mut Self {
use crate::mem::align_of;
let [] = [(); align_of::<Self>() - align_of::<$int_type>()];
// SAFETY:
// - the mutable reference guarantees unique ownership.
Expand Down Expand Up @@ -2354,7 +2352,6 @@ macro_rules! atomic_int {
#[$cfg_align]
#[unstable(feature = "atomic_from_mut", issue = "76314")]
pub fn from_mut_slice(v: &mut [$int_type]) -> &mut [Self] {
use crate::mem::align_of;
let [] = [(); align_of::<Self>() - align_of::<$int_type>()];
// SAFETY:
// - the mutable reference guarantees unique ownership.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,7 @@ where
fn cast<U>(self) -> Self::CastPtr<U> {
// SimdElement currently requires zero-sized metadata, so this should never fail.
// If this ever changes, `simd_cast_ptr` should produce a post-mono error.
use core::{mem::size_of, ptr::Pointee};
use core::ptr::Pointee;
assert_eq!(size_of::<<T as Pointee>::Metadata>(), 0);
assert_eq!(size_of::<<U as Pointee>::Metadata>(), 0);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@ where
fn cast<U>(self) -> Self::CastPtr<U> {
// SimdElement currently requires zero-sized metadata, so this should never fail.
// If this ever changes, `simd_cast_ptr` should produce a post-mono error.
use core::{mem::size_of, ptr::Pointee};
use core::ptr::Pointee;
assert_eq!(size_of::<<T as Pointee>::Metadata>(), 0);
assert_eq!(size_of::<<U as Pointee>::Metadata>(), 0);

Expand Down
1 change: 0 additions & 1 deletion library/proc_macro/src/bridge/fxhash.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@
use std::collections::HashMap;
use std::hash::BuildHasherDefault;
use std::hash::Hasher;
use std::mem::size_of;
use std::ops::BitXor;

/// Type alias for a hashmap using the `fx` hash algorithm.
Expand Down
1 change: 0 additions & 1 deletion library/std/src/io/error/repr_bitpacked.rs
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,6 @@

use super::{Custom, ErrorData, ErrorKind, RawOsError, SimpleMessage};
use core::marker::PhantomData;
use core::mem::{align_of, size_of};
use core::ptr::{self, NonNull};

// The 2 least-significant bits are used as tag.
Expand Down
2 changes: 1 addition & 1 deletion library/std/src/os/unix/net/ancillary.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
use super::{sockaddr_un, SocketAddr};
use crate::io::{self, IoSlice, IoSliceMut};
use crate::marker::PhantomData;
use crate::mem::{size_of, zeroed};
use crate::mem::zeroed;
use crate::os::unix::io::RawFd;
use crate::path::Path;
use crate::ptr::{eq, read_unaligned};
Expand Down
3 changes: 3 additions & 0 deletions library/std/src/prelude/common.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,9 @@ pub use crate::ops::{Drop, Fn, FnMut, FnOnce};
#[stable(feature = "rust1", since = "1.0.0")]
#[doc(no_inline)]
pub use crate::mem::drop;
#[stable(feature = "size_of_prelude", since = "CURRENT_RUSTC_VERSION")]
#[doc(no_inline)]
pub use crate::mem::{align_of, align_of_val, size_of, size_of_val};

// Re-exported types and traits
#[stable(feature = "rust1", since = "1.0.0")]
Expand Down
4 changes: 4 additions & 0 deletions library/std/src/prelude/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,10 @@
//! operations for both destructors and overloading `()`.
//! * <code>[std::mem]::[drop]</code>, a convenience function for explicitly
//! dropping a value.
//! * <code>[std::mem]::{[size_of], [size_of_val]}</code>, to get the size of
//! a type or value.
//! * <code>[std::mem]::{[align_of], [align_of_val]}</code>, to get the
//! alignment of a type or value.
//! * <code>[std::boxed]::[Box]</code>, a way to allocate values on the heap.
//! * <code>[std::borrow]::[ToOwned]</code>, the conversion trait that defines
//! [`to_owned`], the generic method for creating an owned type from a
Expand Down
4 changes: 2 additions & 2 deletions tests/ui/resolve/filter-intrinsics.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
fn main() {
// Should suggest only `std::mem::size_of`
let _ = size_of::<usize>();
// Should suggest only `std::mem::transmute`
let _ = transmute::<usize>();
//~^ ERROR cannot find

// Should suggest `std::intrinsics::fabsf64`,
Expand Down
8 changes: 4 additions & 4 deletions tests/ui/resolve/filter-intrinsics.stderr
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
error[E0425]: cannot find function `size_of` in this scope
error[E0425]: cannot find function `transmute` in this scope
--> $DIR/filter-intrinsics.rs:3:13
|
LL | let _ = size_of::<usize>();
| ^^^^^^^ not found in this scope
LL | let _ = transmute::<usize>();
| ^^^^^^^^^ not found in this scope
|
help: consider importing this function
|
LL + use std::mem::size_of;
LL + use std::mem::transmute;
|

error[E0425]: cannot find function `fabsf64` in this scope
Expand Down