Unnamed repository; edit this file 'description' to name the repository.
Diffstat (limited to 'crates/test-utils/src/minicore.rs')
| -rw-r--r-- | crates/test-utils/src/minicore.rs | 77 |
1 files changed, 61 insertions, 16 deletions
diff --git a/crates/test-utils/src/minicore.rs b/crates/test-utils/src/minicore.rs index f125792d12..0257ed9ab4 100644 --- a/crates/test-utils/src/minicore.rs +++ b/crates/test-utils/src/minicore.rs @@ -28,7 +28,7 @@ //! env: option //! eq: sized //! error: fmt -//! fmt: option, result, transmute, coerce_unsized +//! fmt: option, result, transmute, coerce_unsized, copy, clone, derive //! fn: //! from: sized //! future: pin @@ -913,11 +913,13 @@ pub mod fmt { } mod rt { + use super::*; extern "C" { type Opaque; } + #[derive(Copy, Clone)] #[lang = "format_argument"] pub struct Argument<'a> { value: &'a Opaque, @@ -930,8 +932,8 @@ pub mod fmt { unsafe { Argument { formatter: transmute(f), value: transmute(x) } } } - pub fn new_display<'b, T: Display>(x: &'b T) -> Argument<'_> { - Self::new(x, Display::fmt) + pub fn new_display<'b, T: crate::fmt::Display>(x: &'b T) -> Argument<'_> { + Self::new(x, crate::fmt::Display::fmt) } } @@ -968,7 +970,9 @@ pub mod fmt { flags: u32, precision: Count, width: Count, - ) -> Self; + ) -> Self { + Placeholder { position, fill, align, flags, precision, width } + } } #[lang = "format_unsafe_arg"] @@ -977,10 +981,13 @@ pub mod fmt { } impl UnsafeArg { - pub unsafe fn new() -> Self; + pub unsafe fn new() -> Self { + UnsafeArg { _private: () } + } } } + #[derive(Copy, Clone)] #[lang = "format_arguments"] pub struct Arguments<'a> { pieces: &'a [&'static str], @@ -1005,6 +1012,14 @@ pub mod fmt { ) -> Arguments<'a> { Arguments { pieces, fmt: Some(fmt), args } } + + pub const fn as_str(&self) -> Option<&'static str> { + match (self.pieces, self.args) { + ([], []) => Some(""), + ([s], []) => Some(s), + _ => None, + } + } } // region:derive @@ -1154,8 +1169,8 @@ pub mod pin { pointer: P, } impl<P> Pin<P> { - pub fn new(_pointer: P) -> Pin<P> { - loop {} + pub fn new(pointer: P) -> Pin<P> { + Pin { pointer } } } // region:deref @@ -1356,18 +1371,48 @@ pub mod iter { // region:panic mod panic { pub macro panic_2021 { - () => ( - $crate::panicking::panic("explicit panic") - ), - ($($t:tt)+) => ( - $crate::panicking::panic_fmt($crate::const_format_args!($($t)+)) - ), + () => ({ + const fn panic_cold_explicit() -> ! { + $crate::panicking::panic_explicit() + } + panic_cold_explicit(); + }), + // Special-case the single-argument case for const_panic. + ("{}", $arg:expr $(,)?) => ({ + #[rustc_const_panic_str] // enforce a &&str argument in const-check and hook this by const-eval + #[rustc_do_not_const_check] // hooked by const-eval + const fn panic_cold_display<T: $crate::fmt::Display>(arg: &T) -> ! { + $crate::panicking::panic_display(arg) + } + panic_cold_display(&$arg); + }), + ($($t:tt)+) => ({ + // Semicolon to prevent temporaries inside the formatting machinery from + // being considered alive in the caller after the panic_fmt call. + $crate::panicking::panic_fmt($crate::const_format_args!($($t)+)); + }), } } mod panicking { - #[lang = "panic_fmt"] - pub const fn panic_fmt(_fmt: crate::fmt::Arguments<'_>) -> ! { + #[rustc_const_panic_str] // enforce a &&str argument in const-check and hook this by const-eval + pub const fn panic_display<T: crate::fmt::Display>(x: &T) -> ! { + panic_fmt(crate::format_args!("{}", *x)); + } + + // This function is used instead of panic_fmt in const eval. + #[lang = "const_panic_fmt"] + pub const fn const_panic_fmt(fmt: crate::fmt::Arguments<'_>) -> ! { + if let Some(msg) = fmt.as_str() { + // The panic_display function is hooked by const eval. + panic_display(&msg); + } else { + loop {} + } + } + + #[lang = "panic_fmt"] // needed for const-evaluated panics + pub const fn panic_fmt(fmt: crate::fmt::Arguments<'_>) -> ! { loop {} } @@ -1378,6 +1423,7 @@ mod panicking { } // endregion:panic +#[macro_use] mod macros { // region:panic #[macro_export] @@ -1470,7 +1516,6 @@ mod macros { } // endregion:unimplemented - // region:derive pub(crate) mod builtin { #[rustc_builtin_macro] |