Skip to content

Commit 2786acc

Browse files
Enforce Tuple trait on Fn traits
1 parent 6b8d9dd commit 2786acc

File tree

4 files changed

+419
-0
lines changed

4 files changed

+419
-0
lines changed

library/alloc/src/boxed.rs

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -158,6 +158,8 @@ use core::hash::{Hash, Hasher};
158158
#[cfg(not(no_global_oom_handling))]
159159
use core::iter::FromIterator;
160160
use core::iter::{FusedIterator, Iterator};
161+
#[cfg(not(bootstrap))]
162+
use core::marker::Tuple;
161163
use core::marker::{Destruct, Unpin, Unsize};
162164
use core::mem;
163165
use core::ops::{
@@ -1979,6 +1981,7 @@ impl<I: ExactSizeIterator + ?Sized, A: Allocator> ExactSizeIterator for Box<I, A
19791981
#[stable(feature = "fused", since = "1.26.0")]
19801982
impl<I: FusedIterator + ?Sized, A: Allocator> FusedIterator for Box<I, A> {}
19811983

1984+
#[cfg(bootstrap)]
19821985
#[stable(feature = "boxed_closure_impls", since = "1.35.0")]
19831986
impl<Args, F: FnOnce<Args> + ?Sized, A: Allocator> FnOnce<Args> for Box<F, A> {
19841987
type Output = <F as FnOnce<Args>>::Output;
@@ -1988,20 +1991,48 @@ impl<Args, F: FnOnce<Args> + ?Sized, A: Allocator> FnOnce<Args> for Box<F, A> {
19881991
}
19891992
}
19901993

1994+
#[cfg(not(bootstrap))]
1995+
#[stable(feature = "boxed_closure_impls", since = "1.35.0")]
1996+
impl<Args: Tuple, F: FnOnce<Args> + ?Sized, A: Allocator> FnOnce<Args> for Box<F, A> {
1997+
type Output = <F as FnOnce<Args>>::Output;
1998+
1999+
extern "rust-call" fn call_once(self, args: Args) -> Self::Output {
2000+
<F as FnOnce<Args>>::call_once(*self, args)
2001+
}
2002+
}
2003+
2004+
#[cfg(bootstrap)]
19912005
#[stable(feature = "boxed_closure_impls", since = "1.35.0")]
19922006
impl<Args, F: FnMut<Args> + ?Sized, A: Allocator> FnMut<Args> for Box<F, A> {
19932007
extern "rust-call" fn call_mut(&mut self, args: Args) -> Self::Output {
19942008
<F as FnMut<Args>>::call_mut(self, args)
19952009
}
19962010
}
19972011

2012+
#[cfg(not(bootstrap))]
2013+
#[stable(feature = "boxed_closure_impls", since = "1.35.0")]
2014+
impl<Args: Tuple, F: FnMut<Args> + ?Sized, A: Allocator> FnMut<Args> for Box<F, A> {
2015+
extern "rust-call" fn call_mut(&mut self, args: Args) -> Self::Output {
2016+
<F as FnMut<Args>>::call_mut(self, args)
2017+
}
2018+
}
2019+
2020+
#[cfg(bootstrap)]
19982021
#[stable(feature = "boxed_closure_impls", since = "1.35.0")]
19992022
impl<Args, F: Fn<Args> + ?Sized, A: Allocator> Fn<Args> for Box<F, A> {
20002023
extern "rust-call" fn call(&self, args: Args) -> Self::Output {
20012024
<F as Fn<Args>>::call(self, args)
20022025
}
20032026
}
20042027

2028+
#[cfg(not(bootstrap))]
2029+
#[stable(feature = "boxed_closure_impls", since = "1.35.0")]
2030+
impl<Args: Tuple, F: Fn<Args> + ?Sized, A: Allocator> Fn<Args> for Box<F, A> {
2031+
extern "rust-call" fn call(&self, args: Args) -> Self::Output {
2032+
<F as Fn<Args>>::call(self, args)
2033+
}
2034+
}
2035+
20052036
#[unstable(feature = "coerce_unsized", issue = "27732")]
20062037
impl<T: ?Sized + Unsize<U>, U: ?Sized, A: Allocator> CoerceUnsized<Box<U, A>> for Box<T, A> {}
20072038

library/alloc/src/lib.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -150,6 +150,7 @@
150150
#![feature(trusted_len)]
151151
#![feature(trusted_random_access)]
152152
#![feature(try_trait_v2)]
153+
#![cfg_attr(not(bootstrap), feature(tuple_trait))]
153154
#![feature(unchecked_math)]
154155
#![feature(unicode_internals)]
155156
#![feature(unsize)]

library/core/src/intrinsics.rs

Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,8 @@
5555
#![allow(missing_docs)]
5656

5757
use crate::marker::DiscriminantKind;
58+
#[cfg(not(bootstrap))]
59+
use crate::marker::Tuple;
5860
use crate::mem;
5961

6062
// These imports are used for simplifying intra-doc links
@@ -2169,11 +2171,75 @@ extern "rust-intrinsic" {
21692171
/// `unreachable_unchecked` is actually being reached. The bug is in *crate A*,
21702172
/// which violates the principle that a `const fn` must behave the same at
21712173
/// compile-time and at run-time. The unsafe code in crate B is fine.
2174+
#[cfg(bootstrap)]
21722175
#[rustc_const_unstable(feature = "const_eval_select", issue = "none")]
21732176
pub fn const_eval_select<ARG, F, G, RET>(arg: ARG, called_in_const: F, called_at_rt: G) -> RET
21742177
where
21752178
G: FnOnce<ARG, Output = RET>,
21762179
F: FnOnce<ARG, Output = RET>;
2180+
2181+
/// Selects which function to call depending on the context.
2182+
///
2183+
/// If this function is evaluated at compile-time, then a call to this
2184+
/// intrinsic will be replaced with a call to `called_in_const`. It gets
2185+
/// replaced with a call to `called_at_rt` otherwise.
2186+
///
2187+
/// # Type Requirements
2188+
///
2189+
/// The two functions must be both function items. They cannot be function
2190+
/// pointers or closures. The first function must be a `const fn`.
2191+
///
2192+
/// `arg` will be the tupled arguments that will be passed to either one of
2193+
/// the two functions, therefore, both functions must accept the same type of
2194+
/// arguments. Both functions must return RET.
2195+
///
2196+
/// # Safety
2197+
///
2198+
/// The two functions must behave observably equivalent. Safe code in other
2199+
/// crates may assume that calling a `const fn` at compile-time and at run-time
2200+
/// produces the same result. A function that produces a different result when
2201+
/// evaluated at run-time, or has any other observable side-effects, is
2202+
/// *unsound*.
2203+
///
2204+
/// Here is an example of how this could cause a problem:
2205+
/// ```no_run
2206+
/// #![feature(const_eval_select)]
2207+
/// #![feature(core_intrinsics)]
2208+
/// use std::hint::unreachable_unchecked;
2209+
/// use std::intrinsics::const_eval_select;
2210+
///
2211+
/// // Crate A
2212+
/// pub const fn inconsistent() -> i32 {
2213+
/// fn runtime() -> i32 { 1 }
2214+
/// const fn compiletime() -> i32 { 2 }
2215+
///
2216+
/// unsafe {
2217+
// // ⚠ This code violates the required equivalence of `compiletime`
2218+
/// // and `runtime`.
2219+
/// const_eval_select((), compiletime, runtime)
2220+
/// }
2221+
/// }
2222+
///
2223+
/// // Crate B
2224+
/// const X: i32 = inconsistent();
2225+
/// let x = inconsistent();
2226+
/// if x != X { unsafe { unreachable_unchecked(); }}
2227+
/// ```
2228+
///
2229+
/// This code causes Undefined Behavior when being run, since the
2230+
/// `unreachable_unchecked` is actually being reached. The bug is in *crate A*,
2231+
/// which violates the principle that a `const fn` must behave the same at
2232+
/// compile-time and at run-time. The unsafe code in crate B is fine.
2233+
#[cfg(not(bootstrap))]
2234+
#[rustc_const_unstable(feature = "const_eval_select", issue = "none")]
2235+
pub fn const_eval_select<ARG: Tuple, F, G, RET>(
2236+
arg: ARG,
2237+
called_in_const: F,
2238+
called_at_rt: G,
2239+
) -> RET
2240+
where
2241+
G: FnOnce<ARG, Output = RET>,
2242+
F: FnOnce<ARG, Output = RET>;
21772243
}
21782244

21792245
// Some functions are defined here because they accidentally got made

0 commit comments

Comments
 (0)