Skip to content

Commit

Permalink
Fully test the alloc crate through alloctests
Browse files Browse the repository at this point in the history
For the tests that make use of internal implementation details, we
include the module to test using #[path] in alloctests now.
  • Loading branch information
bjorn3 committed Feb 6, 2025
1 parent f721090 commit 34a86f7
Show file tree
Hide file tree
Showing 27 changed files with 229 additions and 308 deletions.
2 changes: 0 additions & 2 deletions library/Cargo.lock
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,6 @@ version = "0.0.0"
dependencies = [
"compiler_builtins",
"core",
"rand",
"rand_xorshift",
]

[[package]]
Expand Down
8 changes: 4 additions & 4 deletions library/alloc/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -8,14 +8,14 @@ autotests = false
autobenches = false
edition = "2021"

[lib]
test = false
bench = false

[dependencies]
core = { path = "../core" }
compiler_builtins = { version = "=0.1.145", features = ['rustc-dep-of-std'] }

[dev-dependencies]
rand = { version = "0.8.5", default-features = false, features = ["alloc"] }
rand_xorshift = "0.3.0"

[features]
compiler-builtins-mem = ['compiler_builtins/mem']
compiler-builtins-c = ["compiler_builtins/c"]
Expand Down
18 changes: 3 additions & 15 deletions library/alloc/src/alloc.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,7 @@
#[stable(feature = "alloc_module", since = "1.28.0")]
#[doc(inline)]
pub use core::alloc::*;
#[cfg(not(test))]
use core::hint;
#[cfg(not(test))]
use core::ptr::{self, NonNull};

extern "Rust" {
Expand Down Expand Up @@ -44,14 +42,10 @@ extern "Rust" {
/// accessed through the [free functions in `alloc`](self#functions).
#[unstable(feature = "allocator_api", issue = "32838")]
#[derive(Copy, Clone, Default, Debug)]
#[cfg(not(test))]
// the compiler needs to know when a Box uses the global allocator vs a custom one
#[lang = "global_alloc_ty"]
pub struct Global;

#[cfg(test)]
pub use std::alloc::Global;

/// Allocates memory with the global allocator.
///
/// This function forwards calls to the [`GlobalAlloc::alloc`] method
Expand Down Expand Up @@ -180,7 +174,6 @@ pub unsafe fn alloc_zeroed(layout: Layout) -> *mut u8 {
}
}

#[cfg(not(test))]
impl Global {
#[inline]
#[cfg_attr(miri, track_caller)] // even without panics, this helps for Miri backtraces
Expand Down Expand Up @@ -246,7 +239,6 @@ impl Global {
}

#[unstable(feature = "allocator_api", issue = "32838")]
#[cfg(not(test))]
unsafe impl Allocator for Global {
#[inline]
#[cfg_attr(miri, track_caller)] // even without panics, this helps for Miri backtraces
Expand Down Expand Up @@ -340,7 +332,7 @@ unsafe impl Allocator for Global {
}

/// The allocator for `Box`.
#[cfg(all(not(no_global_oom_handling), not(test)))]
#[cfg(not(no_global_oom_handling))]
#[lang = "exchange_malloc"]
#[inline]
#[cfg_attr(miri, track_caller)] // even without panics, this helps for Miri backtraces
Expand Down Expand Up @@ -389,7 +381,7 @@ extern "Rust" {
/// [no_std]: https://doc.rust-lang.org/reference/names/preludes.html#the-no_std-attribute
#[stable(feature = "global_alloc", since = "1.28.0")]
#[rustc_const_unstable(feature = "const_alloc_error", issue = "92523")]
#[cfg(all(not(no_global_oom_handling), not(test)))]
#[cfg(not(no_global_oom_handling))]
#[cold]
#[optimize(size)]
pub const fn handle_alloc_error(layout: Layout) -> ! {
Expand All @@ -413,11 +405,7 @@ pub const fn handle_alloc_error(layout: Layout) -> ! {
ct_error(layout)
}

// For alloc test `std::alloc::handle_alloc_error` can be used directly.
#[cfg(all(not(no_global_oom_handling), test))]
pub use std::alloc::handle_alloc_error;

#[cfg(all(not(no_global_oom_handling), not(test)))]
#[cfg(not(no_global_oom_handling))]
#[doc(hidden)]
#[allow(unused_attributes)]
#[unstable(feature = "alloc_internals", issue = "none")]
Expand Down
6 changes: 3 additions & 3 deletions library/alloc/src/borrow.rs
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ where
/// implementing the `Clone` trait. But `Clone` works only for going from `&T`
/// to `T`. The `ToOwned` trait generalizes `Clone` to construct owned data
/// from any borrow of a given type.
#[cfg_attr(not(test), rustc_diagnostic_item = "ToOwned")]
#[rustc_diagnostic_item = "ToOwned"]
#[stable(feature = "rust1", since = "1.0.0")]
pub trait ToOwned {
/// The resulting type after obtaining ownership.
Expand All @@ -54,7 +54,7 @@ pub trait ToOwned {
/// ```
#[stable(feature = "rust1", since = "1.0.0")]
#[must_use = "cloning is often expensive and is not expected to have side effects"]
#[cfg_attr(not(test), rustc_diagnostic_item = "to_owned_method")]
#[rustc_diagnostic_item = "to_owned_method"]
fn to_owned(&self) -> Self::Owned;

/// Uses borrowed data to replace owned data, usually by cloning.
Expand Down Expand Up @@ -175,7 +175,7 @@ where
/// }
/// ```
#[stable(feature = "rust1", since = "1.0.0")]
#[cfg_attr(not(test), rustc_diagnostic_item = "Cow")]
#[rustc_diagnostic_item = "Cow"]
pub enum Cow<'a, B: ?Sized + 'a>
where
B: ToOwned,
Expand Down
20 changes: 0 additions & 20 deletions library/alloc/src/bstr.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,13 +12,10 @@ use core::ops::{
Deref, DerefMut, DerefPure, Index, IndexMut, Range, RangeFrom, RangeFull, RangeInclusive,
RangeTo, RangeToInclusive,
};
#[cfg(not(test))] // https://github.com/rust-lang/rust/issues/135100
use core::str::FromStr;
use core::{fmt, hash};

#[cfg(not(test))] // https://github.com/rust-lang/rust/issues/135100
use crate::borrow::{Cow, ToOwned};
#[cfg(not(test))] // https://github.com/rust-lang/rust/issues/135100
use crate::boxed::Box;
#[cfg(not(no_rc))]
use crate::rc::Rc;
Expand Down Expand Up @@ -181,7 +178,6 @@ impl Default for ByteString {

// Omitted due to inference failures
//
// #[cfg(not(test))] // https://github.com/rust-lang/rust/issues/135100
// #[unstable(feature = "bstr", issue = "134915")]
// impl<'a, const N: usize> From<&'a [u8; N]> for ByteString {
// #[inline]
Expand All @@ -190,7 +186,6 @@ impl Default for ByteString {
// }
// }
//
// #[cfg(not(test))] // https://github.com/rust-lang/rust/issues/135100
// #[unstable(feature = "bstr", issue = "134915")]
// impl<const N: usize> From<[u8; N]> for ByteString {
// #[inline]
Expand All @@ -199,7 +194,6 @@ impl Default for ByteString {
// }
// }
//
// #[cfg(not(test))] // https://github.com/rust-lang/rust/issues/135100
// #[unstable(feature = "bstr", issue = "134915")]
// impl<'a> From<&'a [u8]> for ByteString {
// #[inline]
Expand All @@ -226,7 +220,6 @@ impl From<ByteString> for Vec<u8> {

// Omitted due to inference failures
//
// #[cfg(not(test))] // https://github.com/rust-lang/rust/issues/135100
// #[unstable(feature = "bstr", issue = "134915")]
// impl<'a> From<&'a str> for ByteString {
// #[inline]
Expand All @@ -243,7 +236,6 @@ impl From<ByteString> for Vec<u8> {
// }
// }

#[cfg(not(test))] // https://github.com/rust-lang/rust/issues/135100
#[unstable(feature = "bstr", issue = "134915")]
impl<'a> From<&'a ByteStr> for ByteString {
#[inline]
Expand All @@ -252,7 +244,6 @@ impl<'a> From<&'a ByteStr> for ByteString {
}
}

#[cfg(not(test))] // https://github.com/rust-lang/rust/issues/135100
#[unstable(feature = "bstr", issue = "134915")]
impl<'a> From<ByteString> for Cow<'a, ByteStr> {
#[inline]
Expand All @@ -261,7 +252,6 @@ impl<'a> From<ByteString> for Cow<'a, ByteStr> {
}
}

#[cfg(not(test))] // https://github.com/rust-lang/rust/issues/135100
#[unstable(feature = "bstr", issue = "134915")]
impl<'a> From<&'a ByteString> for Cow<'a, ByteStr> {
#[inline]
Expand Down Expand Up @@ -330,7 +320,6 @@ impl FromIterator<ByteString> for ByteString {
}
}

#[cfg(not(test))] // https://github.com/rust-lang/rust/issues/135100
#[unstable(feature = "bstr", issue = "134915")]
impl FromStr for ByteString {
type Err = core::convert::Infallible;
Expand Down Expand Up @@ -488,7 +477,6 @@ impl PartialEq for ByteString {

macro_rules! impl_partial_eq_ord_cow {
($lhs:ty, $rhs:ty) => {
#[cfg(not(test))] // https://github.com/rust-lang/rust/issues/135100
#[allow(unused_lifetimes)]
#[unstable(feature = "bstr", issue = "134915")]
impl<'a> PartialEq<$rhs> for $lhs {
Expand All @@ -499,7 +487,6 @@ macro_rules! impl_partial_eq_ord_cow {
}
}

#[cfg(not(test))] // https://github.com/rust-lang/rust/issues/135100
#[allow(unused_lifetimes)]
#[unstable(feature = "bstr", issue = "134915")]
impl<'a> PartialEq<$lhs> for $rhs {
Expand All @@ -510,7 +497,6 @@ macro_rules! impl_partial_eq_ord_cow {
}
}

#[cfg(not(test))] // https://github.com/rust-lang/rust/issues/135100
#[allow(unused_lifetimes)]
#[unstable(feature = "bstr", issue = "134915")]
impl<'a> PartialOrd<$rhs> for $lhs {
Expand All @@ -521,7 +507,6 @@ macro_rules! impl_partial_eq_ord_cow {
}
}

#[cfg(not(test))] // https://github.com/rust-lang/rust/issues/135100
#[allow(unused_lifetimes)]
#[unstable(feature = "bstr", issue = "134915")]
impl<'a> PartialOrd<$lhs> for $rhs {
Expand Down Expand Up @@ -572,7 +557,6 @@ impl PartialOrd for ByteString {
}
}

#[cfg(not(test))] // https://github.com/rust-lang/rust/issues/135100
#[unstable(feature = "bstr", issue = "134915")]
impl ToOwned for ByteStr {
type Owned = ByteString;
Expand Down Expand Up @@ -605,7 +589,6 @@ impl<'a> TryFrom<&'a ByteString> for &'a str {

// Additional impls for `ByteStr` that require types from `alloc`:

#[cfg(not(test))] // https://github.com/rust-lang/rust/issues/135100
#[unstable(feature = "bstr", issue = "134915")]
impl Clone for Box<ByteStr> {
#[inline]
Expand All @@ -614,7 +597,6 @@ impl Clone for Box<ByteStr> {
}
}

#[cfg(not(test))] // https://github.com/rust-lang/rust/issues/135100
#[unstable(feature = "bstr", issue = "134915")]
impl<'a> From<&'a ByteStr> for Cow<'a, ByteStr> {
#[inline]
Expand All @@ -623,7 +605,6 @@ impl<'a> From<&'a ByteStr> for Cow<'a, ByteStr> {
}
}

#[cfg(not(test))] // https://github.com/rust-lang/rust/issues/135100
#[unstable(feature = "bstr", issue = "134915")]
impl From<Box<[u8]>> for Box<ByteStr> {
#[inline]
Expand All @@ -633,7 +614,6 @@ impl From<Box<[u8]>> for Box<ByteStr> {
}
}

#[cfg(not(test))] // https://github.com/rust-lang/rust/issues/135100
#[unstable(feature = "bstr", issue = "134915")]
impl From<Box<ByteStr>> for Box<[u8]> {
#[inline]
Expand Down
5 changes: 4 additions & 1 deletion library/alloc/src/collections/binary_heap/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -153,7 +153,9 @@ use core::{fmt, ptr};
use crate::alloc::Global;
use crate::collections::TryReserveError;
use crate::slice;
use crate::vec::{self, AsVecIntoIter, Vec};
#[cfg(not(test))]
use crate::vec::AsVecIntoIter;
use crate::vec::{self, Vec};

/// A priority queue implemented with a binary heap.
///
Expand Down Expand Up @@ -1600,6 +1602,7 @@ unsafe impl<I, A: Allocator> InPlaceIterable for IntoIter<I, A> {
const MERGE_BY: Option<NonZero<usize>> = NonZero::new(1);
}

#[cfg(not(test))]
unsafe impl<I> AsVecIntoIter for IntoIter<I> {
type Item = I;

Expand Down
21 changes: 21 additions & 0 deletions library/alloc/src/collections/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,41 +24,54 @@ pub mod btree_map {
pub mod btree_set {
//! An ordered set based on a B-Tree.
#[stable(feature = "rust1", since = "1.0.0")]
#[cfg(not(test))]
pub use super::btree::set::*;
}

#[cfg(not(test))]
use core::fmt::Display;

#[cfg(not(no_global_oom_handling))]
#[stable(feature = "rust1", since = "1.0.0")]
#[doc(no_inline)]
#[cfg(not(test))]
pub use binary_heap::BinaryHeap;
#[cfg(not(no_global_oom_handling))]
#[stable(feature = "rust1", since = "1.0.0")]
#[doc(no_inline)]
#[cfg(not(test))]
pub use btree_map::BTreeMap;
#[cfg(not(no_global_oom_handling))]
#[stable(feature = "rust1", since = "1.0.0")]
#[doc(no_inline)]
#[cfg(not(test))]
pub use btree_set::BTreeSet;
#[cfg(not(no_global_oom_handling))]
#[stable(feature = "rust1", since = "1.0.0")]
#[doc(no_inline)]
#[cfg(not(test))]
pub use linked_list::LinkedList;
#[cfg(not(no_global_oom_handling))]
#[stable(feature = "rust1", since = "1.0.0")]
#[doc(no_inline)]
#[cfg(not(test))]
pub use vec_deque::VecDeque;

#[cfg(not(test))]
use crate::alloc::{Layout, LayoutError};

/// The error type for `try_reserve` methods.
#[derive(Clone, PartialEq, Eq, Debug)]
#[stable(feature = "try_reserve", since = "1.57.0")]
#[cfg(not(test))]
pub struct TryReserveError {
kind: TryReserveErrorKind,
}

#[cfg(test)]
pub use realalloc::collections::TryReserveError;

#[cfg(not(test))]
impl TryReserveError {
/// Details about the allocation that caused the error
#[inline]
Expand All @@ -80,6 +93,7 @@ impl TryReserveError {
reason = "Uncertain how much info should be exposed",
issue = "48043"
)]
#[cfg(not(test))]
pub enum TryReserveErrorKind {
/// Error due to the computed capacity exceeding the collection's maximum
/// (usually `isize::MAX` bytes).
Expand All @@ -103,11 +117,15 @@ pub enum TryReserveErrorKind {
},
}

#[cfg(test)]
pub use realalloc::collections::TryReserveErrorKind;

#[unstable(
feature = "try_reserve_kind",
reason = "Uncertain how much info should be exposed",
issue = "48043"
)]
#[cfg(not(test))]
impl From<TryReserveErrorKind> for TryReserveError {
#[inline]
fn from(kind: TryReserveErrorKind) -> Self {
Expand All @@ -116,6 +134,7 @@ impl From<TryReserveErrorKind> for TryReserveError {
}

#[unstable(feature = "try_reserve_kind", reason = "new API", issue = "48043")]
#[cfg(not(test))]
impl From<LayoutError> for TryReserveErrorKind {
/// Always evaluates to [`TryReserveErrorKind::CapacityOverflow`].
#[inline]
Expand All @@ -125,6 +144,7 @@ impl From<LayoutError> for TryReserveErrorKind {
}

#[stable(feature = "try_reserve", since = "1.57.0")]
#[cfg(not(test))]
impl Display for TryReserveError {
fn fmt(
&self,
Expand Down Expand Up @@ -152,4 +172,5 @@ trait SpecExtend<I: IntoIterator> {
}

#[stable(feature = "try_reserve", since = "1.57.0")]
#[cfg(not(test))]
impl core::error::Error for TryReserveError {}
Loading

0 comments on commit 34a86f7

Please sign in to comment.