Auto merge of #134628 - estebank:const-default, r=oli-obk
Make `Default` const and add some `const Default` impls Full list of `impl const Default` types: - () - bool - char - std::ascii::Char - usize - u8 - u16 - u32 - u64 - u128 - i8 - i16 - i32 - i64 - i128 - f16 - f32 - f64 - f128 - std::marker::PhantomData<T> - Option<T> - std::iter::Empty<T> - std::ptr::Alignment - &[T] - &mut [T] - &str - &mut str - String - Vec<T>
This commit is contained in:
commit
f838cbc06d
28 changed files with 192 additions and 160 deletions
|
|
@ -107,8 +107,10 @@
|
|||
#![feature(char_max_len)]
|
||||
#![feature(clone_to_uninit)]
|
||||
#![feature(coerce_unsized)]
|
||||
#![feature(const_default)]
|
||||
#![feature(const_eval_select)]
|
||||
#![feature(const_heap)]
|
||||
#![feature(const_trait_impl)]
|
||||
#![feature(core_intrinsics)]
|
||||
#![feature(deprecated_suggestion)]
|
||||
#![feature(deref_pure_trait)]
|
||||
|
|
|
|||
|
|
@ -2611,7 +2611,8 @@ impl_eq! { Cow<'a, str>, &'b str }
|
|||
impl_eq! { Cow<'a, str>, String }
|
||||
|
||||
#[stable(feature = "rust1", since = "1.0.0")]
|
||||
impl Default for String {
|
||||
#[rustc_const_unstable(feature = "const_default", issue = "67792")]
|
||||
impl const Default for String {
|
||||
/// Creates an empty `String`.
|
||||
#[inline]
|
||||
fn default() -> String {
|
||||
|
|
|
|||
|
|
@ -3895,7 +3895,8 @@ unsafe impl<#[may_dangle] T, A: Allocator> Drop for Vec<T, A> {
|
|||
}
|
||||
|
||||
#[stable(feature = "rust1", since = "1.0.0")]
|
||||
impl<T> Default for Vec<T> {
|
||||
#[rustc_const_unstable(feature = "const_default", issue = "67792")]
|
||||
impl<T> const Default for Vec<T> {
|
||||
/// Creates an empty `Vec<T>`.
|
||||
///
|
||||
/// The vector will not allocate until elements are pushed onto it.
|
||||
|
|
|
|||
|
|
@ -333,7 +333,8 @@ impl<T: Copy> Clone for Cell<T> {
|
|||
}
|
||||
|
||||
#[stable(feature = "rust1", since = "1.0.0")]
|
||||
impl<T: Default> Default for Cell<T> {
|
||||
#[rustc_const_unstable(feature = "const_default", issue = "67792")]
|
||||
impl<T: ~const Default> const Default for Cell<T> {
|
||||
/// Creates a `Cell<T>`, with the `Default` value for T.
|
||||
#[inline]
|
||||
fn default() -> Cell<T> {
|
||||
|
|
@ -1323,7 +1324,8 @@ impl<T: Clone> Clone for RefCell<T> {
|
|||
}
|
||||
|
||||
#[stable(feature = "rust1", since = "1.0.0")]
|
||||
impl<T: Default> Default for RefCell<T> {
|
||||
#[rustc_const_unstable(feature = "const_default", issue = "67792")]
|
||||
impl<T: ~const Default> const Default for RefCell<T> {
|
||||
/// Creates a `RefCell<T>`, with the `Default` value for T.
|
||||
#[inline]
|
||||
fn default() -> RefCell<T> {
|
||||
|
|
@ -2330,7 +2332,8 @@ impl<T: ?Sized> UnsafeCell<T> {
|
|||
}
|
||||
|
||||
#[stable(feature = "unsafe_cell_default", since = "1.10.0")]
|
||||
impl<T: Default> Default for UnsafeCell<T> {
|
||||
#[rustc_const_unstable(feature = "const_default", issue = "67792")]
|
||||
impl<T: ~const Default> const Default for UnsafeCell<T> {
|
||||
/// Creates an `UnsafeCell`, with the `Default` value for T.
|
||||
fn default() -> UnsafeCell<T> {
|
||||
UnsafeCell::new(Default::default())
|
||||
|
|
@ -2434,7 +2437,8 @@ impl<T: ?Sized> SyncUnsafeCell<T> {
|
|||
}
|
||||
|
||||
#[unstable(feature = "sync_unsafe_cell", issue = "95439")]
|
||||
impl<T: Default> Default for SyncUnsafeCell<T> {
|
||||
#[rustc_const_unstable(feature = "const_default", issue = "67792")]
|
||||
impl<T: ~const Default> const Default for SyncUnsafeCell<T> {
|
||||
/// Creates an `SyncUnsafeCell`, with the `Default` value for T.
|
||||
fn default() -> SyncUnsafeCell<T> {
|
||||
SyncUnsafeCell::new(Default::default())
|
||||
|
|
|
|||
|
|
@ -103,6 +103,8 @@ use crate::ascii::Char as AsciiChar;
|
|||
/// ```
|
||||
#[rustc_diagnostic_item = "Default"]
|
||||
#[stable(feature = "rust1", since = "1.0.0")]
|
||||
#[const_trait]
|
||||
#[rustc_const_unstable(feature = "const_default", issue = "67792")]
|
||||
pub trait Default: Sized {
|
||||
/// Returns the "default value" for a type.
|
||||
///
|
||||
|
|
@ -149,7 +151,8 @@ pub macro Default($item:item) {
|
|||
macro_rules! default_impl {
|
||||
($t:ty, $v:expr, $doc:tt) => {
|
||||
#[stable(feature = "rust1", since = "1.0.0")]
|
||||
impl Default for $t {
|
||||
#[rustc_const_unstable(feature = "const_default", issue = "67792")]
|
||||
impl const Default for $t {
|
||||
#[inline(always)]
|
||||
#[doc = $doc]
|
||||
fn default() -> $t {
|
||||
|
|
|
|||
|
|
@ -81,7 +81,8 @@ impl<T> Clone for Empty<T> {
|
|||
// not #[derive] because that adds a Default bound on T,
|
||||
// which isn't necessary.
|
||||
#[stable(feature = "iter_empty", since = "1.2.0")]
|
||||
impl<T> Default for Empty<T> {
|
||||
#[rustc_const_unstable(feature = "const_default", issue = "67792")]
|
||||
impl<T> const Default for Empty<T> {
|
||||
fn default() -> Empty<T> {
|
||||
Empty(marker::PhantomData)
|
||||
}
|
||||
|
|
|
|||
|
|
@ -858,7 +858,8 @@ impl<T: PointeeSized> Clone for PhantomData<T> {
|
|||
}
|
||||
|
||||
#[stable(feature = "rust1", since = "1.0.0")]
|
||||
impl<T: PointeeSized> Default for PhantomData<T> {
|
||||
#[rustc_const_unstable(feature = "const_default", issue = "67792")]
|
||||
impl<T: PointeeSized> const Default for PhantomData<T> {
|
||||
fn default() -> Self {
|
||||
Self
|
||||
}
|
||||
|
|
|
|||
|
|
@ -2111,7 +2111,8 @@ where
|
|||
impl<T> crate::clone::UseCloned for Option<T> where T: crate::clone::UseCloned {}
|
||||
|
||||
#[stable(feature = "rust1", since = "1.0.0")]
|
||||
impl<T> Default for Option<T> {
|
||||
#[rustc_const_unstable(feature = "const_default", issue = "67792")]
|
||||
impl<T> const Default for Option<T> {
|
||||
/// Returns [`None`][Option::None].
|
||||
///
|
||||
/// # Examples
|
||||
|
|
|
|||
|
|
@ -230,7 +230,8 @@ impl hash::Hash for Alignment {
|
|||
|
||||
/// Returns [`Alignment::MIN`], which is valid for any type.
|
||||
#[unstable(feature = "ptr_alignment_type", issue = "102070")]
|
||||
impl Default for Alignment {
|
||||
#[rustc_const_unstable(feature = "const_default", issue = "67792")]
|
||||
impl const Default for Alignment {
|
||||
fn default() -> Alignment {
|
||||
Alignment::MIN
|
||||
}
|
||||
|
|
|
|||
|
|
@ -5158,7 +5158,8 @@ where
|
|||
}
|
||||
|
||||
#[stable(feature = "rust1", since = "1.0.0")]
|
||||
impl<T> Default for &[T] {
|
||||
#[rustc_const_unstable(feature = "const_default", issue = "67792")]
|
||||
impl<T> const Default for &[T] {
|
||||
/// Creates an empty slice.
|
||||
fn default() -> Self {
|
||||
&[]
|
||||
|
|
@ -5166,7 +5167,8 @@ impl<T> Default for &[T] {
|
|||
}
|
||||
|
||||
#[stable(feature = "mut_slice_default", since = "1.5.0")]
|
||||
impl<T> Default for &mut [T] {
|
||||
#[rustc_const_unstable(feature = "const_default", issue = "67792")]
|
||||
impl<T> const Default for &mut [T] {
|
||||
/// Creates a mutable empty slice.
|
||||
fn default() -> Self {
|
||||
&mut []
|
||||
|
|
|
|||
|
|
@ -3072,7 +3072,8 @@ impl AsRef<[u8]> for str {
|
|||
}
|
||||
|
||||
#[stable(feature = "rust1", since = "1.0.0")]
|
||||
impl Default for &str {
|
||||
#[rustc_const_unstable(feature = "const_default", issue = "67792")]
|
||||
impl const Default for &str {
|
||||
/// Creates an empty str
|
||||
#[inline]
|
||||
fn default() -> Self {
|
||||
|
|
@ -3081,7 +3082,8 @@ impl Default for &str {
|
|||
}
|
||||
|
||||
#[stable(feature = "default_mut_str", since = "1.28.0")]
|
||||
impl Default for &mut str {
|
||||
#[rustc_const_unstable(feature = "const_default", issue = "67792")]
|
||||
impl const Default for &mut str {
|
||||
/// Creates an empty mutable str
|
||||
#[inline]
|
||||
fn default() -> Self {
|
||||
|
|
|
|||
|
|
@ -1,5 +1,7 @@
|
|||
use crate::msrvs::Msrv;
|
||||
use crate::ty::needs_ordered_drop;
|
||||
use crate::{get_enclosing_block, path_to_local_id};
|
||||
use crate::qualify_min_const_fn::is_stable_const_fn;
|
||||
use core::ops::ControlFlow;
|
||||
use rustc_ast::visit::{VisitorResult, try_visit};
|
||||
use rustc_hir::def::{CtorKind, DefKind, Res};
|
||||
|
|
@ -343,13 +345,13 @@ pub fn is_const_evaluatable<'tcx>(cx: &LateContext<'tcx>, e: &'tcx Expr<'_>) ->
|
|||
.cx
|
||||
.qpath_res(p, hir_id)
|
||||
.opt_def_id()
|
||||
.is_some_and(|id| self.cx.tcx.is_const_fn(id)) => {},
|
||||
.is_some_and(|id| is_stable_const_fn(self.cx, id, Msrv::default())) => {},
|
||||
ExprKind::MethodCall(..)
|
||||
if self
|
||||
.cx
|
||||
.typeck_results()
|
||||
.type_dependent_def_id(e.hir_id)
|
||||
.is_some_and(|id| self.cx.tcx.is_const_fn(id)) => {},
|
||||
.is_some_and(|id| is_stable_const_fn(self.cx, id, Msrv::default())) => {},
|
||||
ExprKind::Binary(_, lhs, rhs)
|
||||
if self.cx.typeck_results().expr_ty(lhs).peel_refs().is_primitive_ty()
|
||||
&& self.cx.typeck_results().expr_ty(rhs).peel_refs().is_primitive_ty() => {},
|
||||
|
|
|
|||
|
|
@ -2,7 +2,7 @@
|
|||
//@ known-bug: #110395
|
||||
|
||||
#![crate_type = "lib"]
|
||||
#![feature(staged_api, const_trait_impl)]
|
||||
#![feature(staged_api, const_trait_impl, const_default)]
|
||||
#![stable(feature = "foo", since = "1.0.0")]
|
||||
|
||||
#[stable(feature = "potato", since = "1.27.0")]
|
||||
|
|
@ -12,8 +12,8 @@ pub struct Data {
|
|||
|
||||
#[stable(feature = "potato", since = "1.27.0")]
|
||||
#[rustc_const_unstable(feature = "data_foo", issue = "none")]
|
||||
impl const Default for Data {
|
||||
fn default() -> Data {
|
||||
Data { _data: 42 }
|
||||
impl const std::fmt::Debug for Data {
|
||||
fn fmt(&self, _: &mut std::fmt::Formatter<'_>) -> Result<(), std::fmt::Error> {
|
||||
Ok(())
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -1,8 +1,8 @@
|
|||
error: const `impl` for trait `Default` which is not marked with `#[const_trait]`
|
||||
error: const `impl` for trait `Debug` which is not marked with `#[const_trait]`
|
||||
--> $DIR/rustc-impl-const-stability.rs:15:12
|
||||
|
|
||||
LL | impl const Default for Data {
|
||||
| ^^^^^^^ this trait is not `const`
|
||||
LL | impl const std::fmt::Debug for Data {
|
||||
| ^^^^^^^^^^^^^^^ this trait is not `const`
|
||||
|
|
||||
= note: marking a trait with `#[const_trait]` ensures all default method bodies are `const`
|
||||
= note: adding a non-const method body in the future would be a breaking change
|
||||
|
|
|
|||
|
|
@ -1,4 +1,4 @@
|
|||
#![deny(dead_code)]
|
||||
#![deny(dead_code)] //~ NOTE the lint level is defined here
|
||||
|
||||
#[derive(Default)]
|
||||
struct T; //~ ERROR struct `T` is never constructed
|
||||
|
|
@ -7,7 +7,7 @@ struct T; //~ ERROR struct `T` is never constructed
|
|||
struct Used;
|
||||
|
||||
#[derive(Default)]
|
||||
enum E {
|
||||
enum E { //~ NOTE variant in this enum
|
||||
#[default]
|
||||
A,
|
||||
B, //~ ERROR variant `B` is never constructed
|
||||
|
|
|
|||
|
|
@ -2,6 +2,8 @@
|
|||
|
||||
#![feature(const_trait_impl, min_specialization, rustc_attrs)]
|
||||
|
||||
use std::fmt::Debug;
|
||||
|
||||
#[rustc_specialization_trait]
|
||||
#[const_trait]
|
||||
pub unsafe trait Sup {
|
||||
|
|
@ -31,19 +33,19 @@ pub trait A {
|
|||
fn a() -> u32;
|
||||
}
|
||||
|
||||
impl<T: [const] Default> const A for T {
|
||||
impl<T: [const] Debug> const A for T {
|
||||
default fn a() -> u32 {
|
||||
2
|
||||
}
|
||||
}
|
||||
|
||||
impl<T: [const] Default + [const] Sup> const A for T {
|
||||
impl<T: [const] Debug + [const] Sup> const A for T {
|
||||
default fn a() -> u32 {
|
||||
3
|
||||
}
|
||||
}
|
||||
|
||||
impl<T: [const] Default + [const] Sub> const A for T {
|
||||
impl<T: [const] Debug + [const] Sub> const A for T {
|
||||
fn a() -> u32 {
|
||||
T::foo()
|
||||
}
|
||||
|
|
|
|||
|
|
@ -1,58 +1,58 @@
|
|||
error: `[const]` can only be applied to `#[const_trait]` traits
|
||||
--> $DIR/const_trait_impl.rs:34:9
|
||||
--> $DIR/const_trait_impl.rs:36:9
|
||||
|
|
||||
LL | impl<T: [const] Default> const A for T {
|
||||
| ^^^^^^^ can't be applied to `Default`
|
||||
LL | impl<T: [const] Debug> const A for T {
|
||||
| ^^^^^^^ can't be applied to `Debug`
|
||||
|
|
||||
note: `Default` can't be used with `[const]` because it isn't annotated with `#[const_trait]`
|
||||
--> $SRC_DIR/core/src/default.rs:LL:COL
|
||||
note: `Debug` can't be used with `[const]` because it isn't annotated with `#[const_trait]`
|
||||
--> $SRC_DIR/core/src/fmt/mod.rs:LL:COL
|
||||
|
||||
error: `[const]` can only be applied to `#[const_trait]` traits
|
||||
--> $DIR/const_trait_impl.rs:40:9
|
||||
--> $DIR/const_trait_impl.rs:42:9
|
||||
|
|
||||
LL | impl<T: [const] Default + [const] Sup> const A for T {
|
||||
| ^^^^^^^ can't be applied to `Default`
|
||||
LL | impl<T: [const] Debug + [const] Sup> const A for T {
|
||||
| ^^^^^^^ can't be applied to `Debug`
|
||||
|
|
||||
note: `Default` can't be used with `[const]` because it isn't annotated with `#[const_trait]`
|
||||
--> $SRC_DIR/core/src/default.rs:LL:COL
|
||||
note: `Debug` can't be used with `[const]` because it isn't annotated with `#[const_trait]`
|
||||
--> $SRC_DIR/core/src/fmt/mod.rs:LL:COL
|
||||
|
||||
error: `[const]` can only be applied to `#[const_trait]` traits
|
||||
--> $DIR/const_trait_impl.rs:46:9
|
||||
--> $DIR/const_trait_impl.rs:48:9
|
||||
|
|
||||
LL | impl<T: [const] Default + [const] Sub> const A for T {
|
||||
| ^^^^^^^ can't be applied to `Default`
|
||||
LL | impl<T: [const] Debug + [const] Sub> const A for T {
|
||||
| ^^^^^^^ can't be applied to `Debug`
|
||||
|
|
||||
note: `Default` can't be used with `[const]` because it isn't annotated with `#[const_trait]`
|
||||
--> $SRC_DIR/core/src/default.rs:LL:COL
|
||||
note: `Debug` can't be used with `[const]` because it isn't annotated with `#[const_trait]`
|
||||
--> $SRC_DIR/core/src/fmt/mod.rs:LL:COL
|
||||
|
||||
error: `[const]` can only be applied to `#[const_trait]` traits
|
||||
--> $DIR/const_trait_impl.rs:40:9
|
||||
--> $DIR/const_trait_impl.rs:42:9
|
||||
|
|
||||
LL | impl<T: [const] Default + [const] Sup> const A for T {
|
||||
| ^^^^^^^ can't be applied to `Default`
|
||||
LL | impl<T: [const] Debug + [const] Sup> const A for T {
|
||||
| ^^^^^^^ can't be applied to `Debug`
|
||||
|
|
||||
note: `Default` can't be used with `[const]` because it isn't annotated with `#[const_trait]`
|
||||
--> $SRC_DIR/core/src/default.rs:LL:COL
|
||||
note: `Debug` can't be used with `[const]` because it isn't annotated with `#[const_trait]`
|
||||
--> $SRC_DIR/core/src/fmt/mod.rs:LL:COL
|
||||
= note: duplicate diagnostic emitted due to `-Z deduplicate-diagnostics=no`
|
||||
|
||||
error: `[const]` can only be applied to `#[const_trait]` traits
|
||||
--> $DIR/const_trait_impl.rs:34:9
|
||||
--> $DIR/const_trait_impl.rs:36:9
|
||||
|
|
||||
LL | impl<T: [const] Default> const A for T {
|
||||
| ^^^^^^^ can't be applied to `Default`
|
||||
LL | impl<T: [const] Debug> const A for T {
|
||||
| ^^^^^^^ can't be applied to `Debug`
|
||||
|
|
||||
note: `Default` can't be used with `[const]` because it isn't annotated with `#[const_trait]`
|
||||
--> $SRC_DIR/core/src/default.rs:LL:COL
|
||||
note: `Debug` can't be used with `[const]` because it isn't annotated with `#[const_trait]`
|
||||
--> $SRC_DIR/core/src/fmt/mod.rs:LL:COL
|
||||
= note: duplicate diagnostic emitted due to `-Z deduplicate-diagnostics=no`
|
||||
|
||||
error: `[const]` can only be applied to `#[const_trait]` traits
|
||||
--> $DIR/const_trait_impl.rs:46:9
|
||||
--> $DIR/const_trait_impl.rs:48:9
|
||||
|
|
||||
LL | impl<T: [const] Default + [const] Sub> const A for T {
|
||||
| ^^^^^^^ can't be applied to `Default`
|
||||
LL | impl<T: [const] Debug + [const] Sub> const A for T {
|
||||
| ^^^^^^^ can't be applied to `Debug`
|
||||
|
|
||||
note: `Default` can't be used with `[const]` because it isn't annotated with `#[const_trait]`
|
||||
--> $SRC_DIR/core/src/default.rs:LL:COL
|
||||
note: `Debug` can't be used with `[const]` because it isn't annotated with `#[const_trait]`
|
||||
--> $SRC_DIR/core/src/fmt/mod.rs:LL:COL
|
||||
= note: duplicate diagnostic emitted due to `-Z deduplicate-diagnostics=no`
|
||||
|
||||
error: aborting due to 6 previous errors
|
||||
|
|
|
|||
9
tests/ui/traits/const-traits/const-traits-alloc.rs
Normal file
9
tests/ui/traits/const-traits/const-traits-alloc.rs
Normal file
|
|
@ -0,0 +1,9 @@
|
|||
//@ run-pass
|
||||
#![feature(const_trait_impl, const_default)]
|
||||
#![allow(dead_code)]
|
||||
// alloc::string
|
||||
const STRING: String = Default::default();
|
||||
// alloc::vec
|
||||
const VEC: Vec<()> = Default::default();
|
||||
|
||||
fn main() {}
|
||||
46
tests/ui/traits/const-traits/const-traits-core.rs
Normal file
46
tests/ui/traits/const-traits/const-traits-core.rs
Normal file
|
|
@ -0,0 +1,46 @@
|
|||
//@ run-pass
|
||||
#![feature(
|
||||
const_trait_impl, const_default, ptr_alignment_type, ascii_char, f16, f128, sync_unsafe_cell,
|
||||
)]
|
||||
#![allow(dead_code)]
|
||||
// core::default
|
||||
const UNIT: () = Default::default();
|
||||
const BOOL: bool = Default::default();
|
||||
const CHAR: char = Default::default();
|
||||
const ASCII_CHAR: std::ascii::Char = Default::default();
|
||||
const USIZE: usize = Default::default();
|
||||
const U8: u8 = Default::default();
|
||||
const U16: u16 = Default::default();
|
||||
const U32: u32 = Default::default();
|
||||
const U64: u64 = Default::default();
|
||||
const U128: u128 = Default::default();
|
||||
const I8: i8 = Default::default();
|
||||
const I16: i16 = Default::default();
|
||||
const I32: i32 = Default::default();
|
||||
const I64: i64 = Default::default();
|
||||
const I128: i128 = Default::default();
|
||||
const F16: f16 = Default::default();
|
||||
const F32: f32 = Default::default();
|
||||
const F64: f64 = Default::default();
|
||||
const F128: f128 = Default::default();
|
||||
// core::marker
|
||||
const PHANTOM: std::marker::PhantomData<()> = Default::default();
|
||||
// core::option
|
||||
const OPT: Option<i32> = Default::default();
|
||||
// core::iter::sources::empty
|
||||
const EMPTY: std::iter::Empty<()> = Default::default();
|
||||
// core::ptr::alignment
|
||||
const ALIGNMENT: std::ptr::Alignment = Default::default();
|
||||
// core::slice
|
||||
const SLICE: &[()] = Default::default();
|
||||
const MUT_SLICE: &mut [()] = Default::default();
|
||||
// core::str
|
||||
const STR: &str = Default::default();
|
||||
const MUT_STR: &mut str = Default::default();
|
||||
// core::cell
|
||||
const CELL: std::cell::Cell<()> = Default::default();
|
||||
const REF_CELL: std::cell::RefCell<()> = Default::default();
|
||||
const UNSAFE_CELL: std::cell::UnsafeCell<()> = Default::default();
|
||||
const SYNC_UNSAFE_CELL: std::cell::SyncUnsafeCell<()> = Default::default();
|
||||
|
||||
fn main() {}
|
||||
|
|
@ -1,5 +1,6 @@
|
|||
#[derive_const(Default)] //~ ERROR use of unstable library feature
|
||||
//~^ ERROR const `impl` for trait `Default` which is not marked with `#[const_trait]`
|
||||
#[derive_const(Debug)] //~ ERROR use of unstable library feature
|
||||
//~^ ERROR const `impl` for trait `Debug` which is not marked with `#[const_trait]`
|
||||
//~| ERROR cannot call non-const method
|
||||
pub struct S;
|
||||
|
||||
fn main() {}
|
||||
|
|
|
|||
|
|
@ -1,21 +1,30 @@
|
|||
error[E0658]: use of unstable library feature `derive_const`
|
||||
--> $DIR/derive-const-gate.rs:1:3
|
||||
|
|
||||
LL | #[derive_const(Default)]
|
||||
LL | #[derive_const(Debug)]
|
||||
| ^^^^^^^^^^^^
|
||||
|
|
||||
= help: add `#![feature(derive_const)]` to the crate attributes to enable
|
||||
= note: this compiler was built on YYYY-MM-DD; consider upgrading it if it is out of date
|
||||
|
||||
error: const `impl` for trait `Default` which is not marked with `#[const_trait]`
|
||||
error: const `impl` for trait `Debug` which is not marked with `#[const_trait]`
|
||||
--> $DIR/derive-const-gate.rs:1:16
|
||||
|
|
||||
LL | #[derive_const(Default)]
|
||||
| ^^^^^^^ this trait is not `const`
|
||||
LL | #[derive_const(Debug)]
|
||||
| ^^^^^ this trait is not `const`
|
||||
|
|
||||
= note: marking a trait with `#[const_trait]` ensures all default method bodies are `const`
|
||||
= note: adding a non-const method body in the future would be a breaking change
|
||||
|
||||
error: aborting due to 2 previous errors
|
||||
error[E0015]: cannot call non-const method `Formatter::<'_>::write_str` in constant functions
|
||||
--> $DIR/derive-const-gate.rs:1:16
|
||||
|
|
||||
LL | #[derive_const(Debug)]
|
||||
| ^^^^^
|
||||
|
|
||||
= note: calls in constant functions are limited to constant functions, tuple structs and tuple variants
|
||||
|
||||
For more information about this error, try `rustc --explain E0658`.
|
||||
error: aborting due to 3 previous errors
|
||||
|
||||
Some errors have detailed explanations: E0015, E0658.
|
||||
For more information about an error, try `rustc --explain E0015`.
|
||||
|
|
|
|||
|
|
@ -3,11 +3,13 @@
|
|||
|
||||
pub struct A;
|
||||
|
||||
impl Default for A {
|
||||
fn default() -> A { A }
|
||||
impl std::fmt::Debug for A {
|
||||
fn fmt(&self, _: &mut std::fmt::Formatter<'_>) -> Result<(), std::fmt::Error> {
|
||||
panic!()
|
||||
}
|
||||
}
|
||||
|
||||
#[derive_const(Default)]
|
||||
#[derive_const(Debug)]
|
||||
pub struct S(A);
|
||||
|
||||
fn main() {}
|
||||
|
|
|
|||
|
|
@ -1,19 +1,17 @@
|
|||
error: const `impl` for trait `Default` which is not marked with `#[const_trait]`
|
||||
--> $DIR/derive-const-non-const-type.rs:10:16
|
||||
error: const `impl` for trait `Debug` which is not marked with `#[const_trait]`
|
||||
--> $DIR/derive-const-non-const-type.rs:12:16
|
||||
|
|
||||
LL | #[derive_const(Default)]
|
||||
| ^^^^^^^ this trait is not `const`
|
||||
LL | #[derive_const(Debug)]
|
||||
| ^^^^^ this trait is not `const`
|
||||
|
|
||||
= note: marking a trait with `#[const_trait]` ensures all default method bodies are `const`
|
||||
= note: adding a non-const method body in the future would be a breaking change
|
||||
|
||||
error[E0015]: cannot call non-const associated function `<A as Default>::default` in constant functions
|
||||
--> $DIR/derive-const-non-const-type.rs:11:14
|
||||
error[E0015]: cannot call non-const method `Formatter::<'_>::debug_tuple_field1_finish` in constant functions
|
||||
--> $DIR/derive-const-non-const-type.rs:12:16
|
||||
|
|
||||
LL | #[derive_const(Default)]
|
||||
| ------- in this derive macro expansion
|
||||
LL | pub struct S(A);
|
||||
| ^
|
||||
LL | #[derive_const(Debug)]
|
||||
| ^^^^^
|
||||
|
|
||||
= note: calls in constant functions are limited to constant functions, tuple structs and tuple variants
|
||||
|
||||
|
|
|
|||
|
|
@ -1,6 +1,6 @@
|
|||
//@ known-bug: #110395
|
||||
|
||||
#![feature(const_trait_impl, const_cmp, const_default_impls, derive_const)]
|
||||
#![feature(const_trait_impl, const_default, const_cmp, derive_const)]
|
||||
|
||||
pub struct A;
|
||||
|
||||
|
|
|
|||
|
|
@ -1,27 +1,3 @@
|
|||
error[E0635]: unknown feature `const_default_impls`
|
||||
--> $DIR/derive-const-use.rs:3:41
|
||||
|
|
||||
LL | #![feature(const_trait_impl, const_cmp, const_default_impls, derive_const)]
|
||||
| ^^^^^^^^^^^^^^^^^^^
|
||||
|
||||
error: const `impl` for trait `Default` which is not marked with `#[const_trait]`
|
||||
--> $DIR/derive-const-use.rs:7:12
|
||||
|
|
||||
LL | impl const Default for A {
|
||||
| ^^^^^^^ this trait is not `const`
|
||||
|
|
||||
= note: marking a trait with `#[const_trait]` ensures all default method bodies are `const`
|
||||
= note: adding a non-const method body in the future would be a breaking change
|
||||
|
||||
error: const `impl` for trait `Default` which is not marked with `#[const_trait]`
|
||||
--> $DIR/derive-const-use.rs:15:16
|
||||
|
|
||||
LL | #[derive_const(Default, PartialEq)]
|
||||
| ^^^^^^^ this trait is not `const`
|
||||
|
|
||||
= note: marking a trait with `#[const_trait]` ensures all default method bodies are `const`
|
||||
= note: adding a non-const method body in the future would be a breaking change
|
||||
|
||||
error[E0277]: the trait bound `(): [const] PartialEq` is not satisfied
|
||||
--> $DIR/derive-const-use.rs:16:14
|
||||
|
|
||||
|
|
@ -30,35 +6,6 @@ LL | #[derive_const(Default, PartialEq)]
|
|||
LL | pub struct S((), A);
|
||||
| ^^
|
||||
|
||||
error[E0015]: cannot call non-const associated function `<S as Default>::default` in constants
|
||||
--> $DIR/derive-const-use.rs:18:35
|
||||
|
|
||||
LL | const _: () = assert!(S((), A) == S::default());
|
||||
| ^^^^^^^^^^^^
|
||||
|
|
||||
= note: calls in constants are limited to constant functions, tuple structs and tuple variants
|
||||
error: aborting due to 1 previous error
|
||||
|
||||
error[E0015]: cannot call non-const associated function `<() as Default>::default` in constant functions
|
||||
--> $DIR/derive-const-use.rs:16:14
|
||||
|
|
||||
LL | #[derive_const(Default, PartialEq)]
|
||||
| ------- in this derive macro expansion
|
||||
LL | pub struct S((), A);
|
||||
| ^^
|
||||
|
|
||||
= note: calls in constant functions are limited to constant functions, tuple structs and tuple variants
|
||||
|
||||
error[E0015]: cannot call non-const associated function `<A as Default>::default` in constant functions
|
||||
--> $DIR/derive-const-use.rs:16:18
|
||||
|
|
||||
LL | #[derive_const(Default, PartialEq)]
|
||||
| ------- in this derive macro expansion
|
||||
LL | pub struct S((), A);
|
||||
| ^
|
||||
|
|
||||
= note: calls in constant functions are limited to constant functions, tuple structs and tuple variants
|
||||
|
||||
error: aborting due to 7 previous errors
|
||||
|
||||
Some errors have detailed explanations: E0015, E0277, E0635.
|
||||
For more information about an error, try `rustc --explain E0015`.
|
||||
For more information about this error, try `rustc --explain E0277`.
|
||||
|
|
|
|||
|
|
@ -1,18 +0,0 @@
|
|||
error[E0635]: unknown feature `const_default_impls`
|
||||
--> $DIR/std-impl-gate.rs:6:46
|
||||
|
|
||||
LL | #![cfg_attr(gated, feature(const_trait_impl, const_default_impls))]
|
||||
| ^^^^^^^^^^^^^^^^^^^
|
||||
|
||||
error[E0015]: cannot call non-const associated function `<Vec<usize> as Default>::default` in constant functions
|
||||
--> $DIR/std-impl-gate.rs:13:5
|
||||
|
|
||||
LL | Default::default()
|
||||
| ^^^^^^^^^^^^^^^^^^
|
||||
|
|
||||
= note: calls in constant functions are limited to constant functions, tuple structs and tuple variants
|
||||
|
||||
error: aborting due to 2 previous errors
|
||||
|
||||
Some errors have detailed explanations: E0015, E0635.
|
||||
For more information about an error, try `rustc --explain E0015`.
|
||||
|
|
@ -1,9 +1,9 @@
|
|||
// This tests feature gates for const impls in the standard library.
|
||||
|
||||
//@ revisions: stock gated
|
||||
//@[gated] known-bug: #110395
|
||||
//@[gated] run-pass
|
||||
|
||||
#![cfg_attr(gated, feature(const_trait_impl, const_default_impls))]
|
||||
#![cfg_attr(gated, feature(const_trait_impl, const_default))]
|
||||
|
||||
fn non_const_context() -> Vec<usize> {
|
||||
Default::default()
|
||||
|
|
@ -11,7 +11,8 @@ fn non_const_context() -> Vec<usize> {
|
|||
|
||||
const fn const_context() -> Vec<usize> {
|
||||
Default::default()
|
||||
//[stock]~^ ERROR cannot call non-const associated function
|
||||
//[stock]~^ ERROR cannot call conditionally-const associated function
|
||||
//[stock]~| ERROR `Default` is not yet stable as a const trait
|
||||
}
|
||||
|
||||
fn main() {
|
||||
|
|
|
|||
|
|
@ -1,11 +1,25 @@
|
|||
error[E0015]: cannot call non-const associated function `<Vec<usize> as Default>::default` in constant functions
|
||||
error[E0658]: cannot call conditionally-const associated function `<Vec<usize> as Default>::default` in constant functions
|
||||
--> $DIR/std-impl-gate.rs:13:5
|
||||
|
|
||||
LL | Default::default()
|
||||
| ^^^^^^^^^^^^^^^^^^
|
||||
|
|
||||
= note: calls in constant functions are limited to constant functions, tuple structs and tuple variants
|
||||
= note: see issue #67792 <https://github.com/rust-lang/rust/issues/67792> for more information
|
||||
= help: add `#![feature(const_trait_impl)]` to the crate attributes to enable
|
||||
= note: this compiler was built on YYYY-MM-DD; consider upgrading it if it is out of date
|
||||
|
||||
error: aborting due to 1 previous error
|
||||
error: `Default` is not yet stable as a const trait
|
||||
--> $DIR/std-impl-gate.rs:13:5
|
||||
|
|
||||
LL | Default::default()
|
||||
| ^^^^^^^^^^^^^^^^^^
|
||||
|
|
||||
help: add `#![feature(const_default)]` to the crate attributes to enable
|
||||
|
|
||||
LL + #![feature(const_default)]
|
||||
|
|
||||
|
||||
For more information about this error, try `rustc --explain E0015`.
|
||||
error: aborting due to 2 previous errors
|
||||
|
||||
For more information about this error, try `rustc --explain E0658`.
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue