Skip to content

Commit a02cdb0

Browse files
committed
Auto merge of #99943 - compiler-errors:tuple-trait, r=jackh726
Implement `std::marker::Tuple`, use it in `extern "rust-call"` and `Fn`-family traits Implements rust-lang/compiler-team#537 I made a few opinionated decisions in this implementation, specifically: 1. Enforcing `extern "rust-call"` on fn items during wfcheck, 2. Enforcing this for all functions (not just ones that have bodies), 3. Gating this `Tuple` marker trait behind its own feature, instead of grouping it into (e.g.) `unboxed_closures`. Still needing to be done: 1. Enforce that `extern "rust-call"` `fn`-ptrs are well-formed only if they have 1/2 args and the second one implements `Tuple`. (Doing this would fix ICE in #66696.) 2. Deny all explicit/user `impl`s of the `Tuple` trait, kinda like `Sized`. 3. Fixing `Tuple` trait built-in impl for chalk, so that chalkification tests are un-broken. Open questions: 1. Does this need t-lang or t-libs signoff? Fixes #99820
2 parents 8587fd8 + ba10458 commit a02cdb0

File tree

5 files changed

+449
-3
lines changed

5 files changed

+449
-3
lines changed

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

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)]

core/src/const_closure.rs

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
11
use crate::marker::Destruct;
2+
#[cfg(not(bootstrap))]
3+
use crate::marker::Tuple;
24

35
/// Struct representing a closure with mutably borrowed data.
46
///
@@ -44,6 +46,7 @@ impl<'a, CapturedData: ?Sized, Function> ConstFnMutClosure<&'a mut CapturedData,
4446

4547
macro_rules! impl_fn_mut_tuple {
4648
($($var:ident)*) => {
49+
#[cfg(bootstrap)]
4750
#[allow(unused_parens)]
4851
impl<'a, $($var,)* ClosureArguments, Function, ClosureReturnValue> const
4952
FnOnce<ClosureArguments> for ConstFnMutClosure<($(&'a mut $var),*), Function>
@@ -56,6 +59,7 @@ macro_rules! impl_fn_mut_tuple {
5659
self.call_mut(args)
5760
}
5861
}
62+
#[cfg(bootstrap)]
5963
#[allow(unused_parens)]
6064
impl<'a, $($var,)* ClosureArguments, Function, ClosureReturnValue> const
6165
FnMut<ClosureArguments> for ConstFnMutClosure<($(&'a mut $var),*), Function>
@@ -68,6 +72,32 @@ macro_rules! impl_fn_mut_tuple {
6872
(self.func)(($($var),*), args)
6973
}
7074
}
75+
#[cfg(not(bootstrap))]
76+
#[allow(unused_parens)]
77+
impl<'a, $($var,)* ClosureArguments: Tuple, Function, ClosureReturnValue> const
78+
FnOnce<ClosureArguments> for ConstFnMutClosure<($(&'a mut $var),*), Function>
79+
where
80+
Function: ~const Fn(($(&mut $var),*), ClosureArguments) -> ClosureReturnValue+ ~const Destruct,
81+
{
82+
type Output = ClosureReturnValue;
83+
84+
extern "rust-call" fn call_once(mut self, args: ClosureArguments) -> Self::Output {
85+
self.call_mut(args)
86+
}
87+
}
88+
#[cfg(not(bootstrap))]
89+
#[allow(unused_parens)]
90+
impl<'a, $($var,)* ClosureArguments: Tuple, Function, ClosureReturnValue> const
91+
FnMut<ClosureArguments> for ConstFnMutClosure<($(&'a mut $var),*), Function>
92+
where
93+
Function: ~const Fn(($(&mut $var),*), ClosureArguments)-> ClosureReturnValue,
94+
{
95+
extern "rust-call" fn call_mut(&mut self, args: ClosureArguments) -> Self::Output {
96+
#[allow(non_snake_case)]
97+
let ($($var),*) = &mut self.data;
98+
(self.func)(($($var),*), args)
99+
}
100+
}
71101
};
72102
}
73103
impl_fn_mut_tuple!(A);

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)