Skip to content

Commit 2fff48d

Browse files
committed
Auto merge of rust-lang#127722 - BoxyUwU:new_adt_const_params_limitations, r=compiler-errors
Forbid borrows and unsized types from being used as the type of a const generic under `adt_const_params` Fixes rust-lang#112219 Fixes rust-lang#112124 Fixes rust-lang#112125 ### Motivation Currently the `adt_const_params` feature allows writing `Foo<const N: [u8]>` this is entirely useless as it is not possible to write an expression which evaluates to a type that is not `Sized`. In order to actually use unsized types in const generics they are typically written as `const N: &[u8]` which *is* possible to provide a value of. Unfortunately allowing the types of const parameters to contain references is non trivial (rust-lang#120961) as it introduces a number of difficult questions about how equality of references in the type system should behave. References in the types of const generics is largely only useful for using unsized types in const generics. This PR introduces a new feature gate `unsized_const_parameters` and moves support for `const N: [u8]` and `const N: &...` from `adt_const_params` into it. The goal here hopefully is to experiment with allowing `const N: [u8]` to work without references and then eventually completely forbid references in const generics. Splitting this out into a new feature gate means that stabilization of `adt_const_params` does not have to resolve rust-lang#120961 which is the only remaining "big" blocker for the feature. Remaining issues after this are a few ICEs and naming bikeshed for `ConstParamTy`. ### Implementation The implementation is slightly subtle here as we would like to ensure that a stabilization of `adt_const_params` is forwards compatible with any outcome of `unsized_const_parameters`. This is inherently tricky as we do not support unstable trait implementations and we determine whether a type is valid as the type of a const parameter via a trait bound. There are a few constraints here: - We would like to *allow for the possibility* of adding a `Sized` supertrait to `ConstParamTy` in the event that we wind up opting to not support unsized types and instead requiring people to write the 'sized version', e.g. `const N: [u8; M]` instead of `const N: [u8]`. - Crates should be able to enable `unsized_const_parameters` and write trait implementations of `ConstParamTy` for `!Sized` types without downstream crates that only enable `adt_const_params` being able to observe this (required for std to be able to `impl<T> ConstParamTy for [T]` Ultimately the way this is accomplished is via having two traits (sad), `ConstParamTy` and `UnsizedConstParamTy`. Depending on whether `unsized_const_parameters` is enabled or not we change which trait is used to check whether a type is allowed to be a const parameter. Long term (when stabilizing `UnsizedConstParamTy`) it should be possible to completely merge these traits (and derive macros), only having a single `trait ConstParamTy` and `macro ConstParamTy`. Under `adt_const_params` it is now illegal to directly refer to `ConstParamTy` it is only used as an internal impl detail by `derive(ConstParamTy)` and checking const parameters are well formed. This is necessary in order to ensure forwards compatibility with all possible future directions for `feature(unsized_const_parameters)`. Generally the intuition here should be that `ConstParamTy` is the stable trait that everything uses, and `UnsizedConstParamTy` is that plus unstable implementations (well, I suppose `ConstParamTy` isn't stable yet :P).
2 parents d7770e9 + be0c06b commit 2fff48d

File tree

4 files changed

+65
-12
lines changed

4 files changed

+65
-12
lines changed

core/src/lib.rs

+1
Original file line numberDiff line numberDiff line change
@@ -248,6 +248,7 @@
248248
#![feature(transparent_unions)]
249249
#![feature(try_blocks)]
250250
#![feature(unboxed_closures)]
251+
#![feature(unsized_const_params)]
251252
#![feature(unsized_fn_params)]
252253
#![feature(with_negative_coherence)]
253254
// tidy-alphabetical-end

core/src/marker.rs

+49-7
Original file line numberDiff line numberDiff line change
@@ -975,31 +975,73 @@ pub trait PointerLike {}
975975
/// that all fields are also `ConstParamTy`, which implies that recursively, all fields
976976
/// are `StructuralPartialEq`.
977977
#[lang = "const_param_ty"]
978-
#[unstable(feature = "adt_const_params", issue = "95174")]
978+
#[unstable(feature = "unsized_const_params", issue = "95174")]
979979
#[diagnostic::on_unimplemented(message = "`{Self}` can't be used as a const parameter type")]
980980
#[allow(multiple_supertrait_upcastable)]
981-
pub trait ConstParamTy: StructuralPartialEq + Eq {}
981+
// We name this differently than the derive macro so that the `adt_const_params` can
982+
// be used independently of `unsized_const_params` without requiring a full path
983+
// to the derive macro every time it is used. This should be renamed on stabilization.
984+
pub trait ConstParamTy_: UnsizedConstParamTy + StructuralPartialEq + Eq {}
982985

983986
/// Derive macro generating an impl of the trait `ConstParamTy`.
984987
#[rustc_builtin_macro]
988+
#[allow_internal_unstable(unsized_const_params)]
985989
#[unstable(feature = "adt_const_params", issue = "95174")]
986990
pub macro ConstParamTy($item:item) {
987991
/* compiler built-in */
988992
}
989993

994+
#[cfg_attr(not(bootstrap), lang = "unsized_const_param_ty")]
995+
#[unstable(feature = "unsized_const_params", issue = "95174")]
996+
#[diagnostic::on_unimplemented(message = "`{Self}` can't be used as a const parameter type")]
997+
/// A marker for types which can be used as types of `const` generic parameters.
998+
///
999+
/// Equivalent to [`ConstParamTy_`] except that this is used by
1000+
/// the `unsized_const_params` to allow for fake unstable impls.
1001+
pub trait UnsizedConstParamTy: StructuralPartialEq + Eq {}
1002+
1003+
/// Derive macro generating an impl of the trait `ConstParamTy`.
1004+
#[cfg(not(bootstrap))]
1005+
#[cfg_attr(not(bootstrap), rustc_builtin_macro)]
1006+
#[cfg_attr(not(bootstrap), allow_internal_unstable(unsized_const_params))]
1007+
#[cfg_attr(not(bootstrap), unstable(feature = "unsized_const_params", issue = "95174"))]
1008+
pub macro UnsizedConstParamTy($item:item) {
1009+
/* compiler built-in */
1010+
}
1011+
9901012
// FIXME(adt_const_params): handle `ty::FnDef`/`ty::Closure`
9911013
marker_impls! {
9921014
#[unstable(feature = "adt_const_params", issue = "95174")]
993-
ConstParamTy for
1015+
ConstParamTy_ for
1016+
usize, u8, u16, u32, u64, u128,
1017+
isize, i8, i16, i32, i64, i128,
1018+
bool,
1019+
char,
1020+
(),
1021+
{T: ConstParamTy_, const N: usize} [T; N],
1022+
}
1023+
#[cfg(bootstrap)]
1024+
marker_impls! {
1025+
#[unstable(feature = "adt_const_params", issue = "95174")]
1026+
ConstParamTy_ for
1027+
str,
1028+
{T: ConstParamTy_} [T],
1029+
{T: ConstParamTy_ + ?Sized} &T,
1030+
}
1031+
1032+
marker_impls! {
1033+
#[unstable(feature = "unsized_const_params", issue = "95174")]
1034+
UnsizedConstParamTy for
9941035
usize, u8, u16, u32, u64, u128,
9951036
isize, i8, i16, i32, i64, i128,
9961037
bool,
9971038
char,
998-
str /* Technically requires `[u8]: ConstParamTy` */,
9991039
(),
1000-
{T: ConstParamTy, const N: usize} [T; N],
1001-
{T: ConstParamTy} [T],
1002-
{T: ?Sized + ConstParamTy} &T,
1040+
{T: UnsizedConstParamTy, const N: usize} [T; N],
1041+
1042+
str,
1043+
{T: UnsizedConstParamTy} [T],
1044+
{T: UnsizedConstParamTy + ?Sized} &T,
10031045
}
10041046

10051047
/// A common trait implemented by all function pointers.

core/src/mem/transmutability.rs

+4-2
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
use crate::marker::ConstParamTy;
1+
use crate::marker::{ConstParamTy_, UnsizedConstParamTy};
22

33
/// Are values of a type transmutable into values of another type?
44
///
@@ -39,7 +39,9 @@ pub struct Assume {
3939
}
4040

4141
#[unstable(feature = "transmutability", issue = "99571")]
42-
impl ConstParamTy for Assume {}
42+
impl ConstParamTy_ for Assume {}
43+
#[unstable(feature = "transmutability", issue = "99571")]
44+
impl UnsizedConstParamTy for Assume {}
4345

4446
impl Assume {
4547
/// Do not assume that *you* have ensured any safety properties are met.

core/src/tuple.rs

+11-3
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,9 @@
11
// See core/src/primitive_docs.rs for documentation.
22

33
use crate::cmp::Ordering::{self, *};
4-
use crate::marker::ConstParamTy;
4+
use crate::marker::ConstParamTy_;
55
use crate::marker::StructuralPartialEq;
6+
use crate::marker::UnsizedConstParamTy;
67

78
// Recursive macro for implementing n-ary tuple functions and operations
89
//
@@ -49,8 +50,15 @@ macro_rules! tuple_impls {
4950

5051
maybe_tuple_doc! {
5152
$($T)+ @
52-
#[unstable(feature = "structural_match", issue = "31434")]
53-
impl<$($T: ConstParamTy),+> ConstParamTy for ($($T,)+)
53+
#[unstable(feature = "adt_const_params", issue = "95174")]
54+
impl<$($T: ConstParamTy_),+> ConstParamTy_ for ($($T,)+)
55+
{}
56+
}
57+
58+
maybe_tuple_doc! {
59+
$($T)+ @
60+
#[unstable(feature = "unsized_const_params", issue = "95174")]
61+
impl<$($T: UnsizedConstParamTy),+> UnsizedConstParamTy for ($($T,)+)
5462
{}
5563
}
5664

0 commit comments

Comments
 (0)