Skip to content

Commit

Permalink
Split part of adt_const_params into unsized_const_params
Browse files Browse the repository at this point in the history
  • Loading branch information
BoxyUwU committed Jul 17, 2024
1 parent 857ed93 commit be0c06b
Show file tree
Hide file tree
Showing 4 changed files with 63 additions and 19 deletions.
1 change: 1 addition & 0 deletions core/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -248,6 +248,7 @@
#![feature(transparent_unions)]
#![feature(try_blocks)]
#![feature(unboxed_closures)]
#![feature(unsized_const_params)]
#![feature(unsized_fn_params)]
#![feature(with_negative_coherence)]
// tidy-alphabetical-end
Expand Down
61 changes: 47 additions & 14 deletions core/src/marker.rs
Original file line number Diff line number Diff line change
Expand Up @@ -975,41 +975,74 @@ pub trait PointerLike {}
/// that all fields are also `ConstParamTy`, which implies that recursively, all fields
/// are `StructuralPartialEq`.
#[lang = "const_param_ty"]
#[unstable(feature = "adt_const_params", issue = "95174")]
#[unstable(feature = "unsized_const_params", issue = "95174")]
#[diagnostic::on_unimplemented(message = "`{Self}` can't be used as a const parameter type")]
#[allow(multiple_supertrait_upcastable)]
pub trait ConstParamTy: StructuralPartialEq + Eq {}
// We name this differently than the derive macro so that the `adt_const_params` can
// be used independently of `unsized_const_params` without requiring a full path
// to the derive macro every time it is used. This should be renamed on stabilization.
pub trait ConstParamTy_: UnsizedConstParamTy + StructuralPartialEq + Eq {}

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

#[cfg_attr(not(bootstrap), lang = "unsized_const_param_ty")]
#[unstable(feature = "unsized_const_params", issue = "95174")]
#[diagnostic::on_unimplemented(message = "`{Self}` can't be used as a const parameter type")]
/// A marker for types which can be used as types of `const` generic parameters.
///
/// Equivalent to [`ConstParamTy_`] except that this is used by
/// the `unsized_const_params` to allow for fake unstable impls.
pub trait UnsizedConstParamTy: StructuralPartialEq + Eq {}

/// Derive macro generating an impl of the trait `ConstParamTy`.
#[cfg(not(bootstrap))]
#[cfg_attr(not(bootstrap), rustc_builtin_macro)]
#[cfg_attr(not(bootstrap), allow_internal_unstable(unsized_const_params))]
#[cfg_attr(not(bootstrap), unstable(feature = "unsized_const_params", issue = "95174"))]
pub macro UnsizedConstParamTy($item:item) {
/* compiler built-in */
}

// FIXME(adt_const_params): handle `ty::FnDef`/`ty::Closure`
marker_impls! {
#[unstable(feature = "adt_const_params", issue = "95174")]
ConstParamTy for
ConstParamTy_ for
usize, u8, u16, u32, u64, u128,
isize, i8, i16, i32, i64, i128,
bool,
char,
(),
{T: ConstParamTy, const N: usize} [T; N],
{T: ConstParamTy_, const N: usize} [T; N],
}
#[cfg(bootstrap)]
marker_impls! {
#[unstable(feature = "adt_const_params", issue = "95174")]
ConstParamTy_ for
str,
{T: ConstParamTy_} [T],
{T: ConstParamTy_ + ?Sized} &T,
}

#[unstable(feature = "adt_const_params", issue = "95174")]
#[rustc_reservation_impl = "types that are not `Sized` are not supported as the type of a const generic parameter"]
impl<T> ConstParamTy for [T] {}

#[unstable(feature = "adt_const_params", issue = "95174")]
#[rustc_reservation_impl = "types that are not `Sized` are not supported as the type of a const generic parameter"]
impl ConstParamTy for str {}
marker_impls! {
#[unstable(feature = "unsized_const_params", issue = "95174")]
UnsizedConstParamTy for
usize, u8, u16, u32, u64, u128,
isize, i8, i16, i32, i64, i128,
bool,
char,
(),
{T: UnsizedConstParamTy, const N: usize} [T; N],

#[unstable(feature = "adt_const_params", issue = "95174")]
#[rustc_reservation_impl = "references are not supported as the type of a const generic parameter"]
impl<T: ?Sized> ConstParamTy for &T {}
str,
{T: UnsizedConstParamTy} [T],
{T: UnsizedConstParamTy + ?Sized} &T,
}

/// A common trait implemented by all function pointers.
#[unstable(
Expand Down
6 changes: 4 additions & 2 deletions core/src/mem/transmutability.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use crate::marker::ConstParamTy;
use crate::marker::{ConstParamTy_, UnsizedConstParamTy};

/// Are values of a type transmutable into values of another type?
///
Expand Down Expand Up @@ -39,7 +39,9 @@ pub struct Assume {
}

#[unstable(feature = "transmutability", issue = "99571")]
impl ConstParamTy for Assume {}
impl ConstParamTy_ for Assume {}
#[unstable(feature = "transmutability", issue = "99571")]
impl UnsizedConstParamTy for Assume {}

impl Assume {
/// Do not assume that *you* have ensured any safety properties are met.
Expand Down
14 changes: 11 additions & 3 deletions core/src/tuple.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
// See core/src/primitive_docs.rs for documentation.

use crate::cmp::Ordering::{self, *};
use crate::marker::ConstParamTy;
use crate::marker::ConstParamTy_;
use crate::marker::StructuralPartialEq;
use crate::marker::UnsizedConstParamTy;

// Recursive macro for implementing n-ary tuple functions and operations
//
Expand Down Expand Up @@ -49,8 +50,15 @@ macro_rules! tuple_impls {

maybe_tuple_doc! {
$($T)+ @
#[unstable(feature = "structural_match", issue = "31434")]
impl<$($T: ConstParamTy),+> ConstParamTy for ($($T,)+)
#[unstable(feature = "adt_const_params", issue = "95174")]
impl<$($T: ConstParamTy_),+> ConstParamTy_ for ($($T,)+)
{}
}

maybe_tuple_doc! {
$($T)+ @
#[unstable(feature = "unsized_const_params", issue = "95174")]
impl<$($T: UnsizedConstParamTy),+> UnsizedConstParamTy for ($($T,)+)
{}
}

Expand Down

0 comments on commit be0c06b

Please sign in to comment.