From 698f8c2f01ea549d77d7dc3338a12e04c11057b9 Mon Sep 17 00:00:00 2001 From: Daniel Baumann Date: Wed, 17 Apr 2024 14:02:58 +0200 Subject: Adding upstream version 1.64.0+dfsg1. Signed-off-by: Daniel Baumann --- .../const_constructor/const-construct-call.rs | 110 +++++++++++++++++++++ .../const_constructor/const_constructor_qpath.rs | 37 +++++++ 2 files changed, 147 insertions(+) create mode 100644 src/test/ui/consts/const_constructor/const-construct-call.rs create mode 100644 src/test/ui/consts/const_constructor/const_constructor_qpath.rs (limited to 'src/test/ui/consts/const_constructor') diff --git a/src/test/ui/consts/const_constructor/const-construct-call.rs b/src/test/ui/consts/const_constructor/const-construct-call.rs new file mode 100644 index 000000000..cb735d7b3 --- /dev/null +++ b/src/test/ui/consts/const_constructor/const-construct-call.rs @@ -0,0 +1,110 @@ +// Test that constructors are considered to be const fns + +// run-pass + +// Ctor(..) is transformed to Ctor { 0: ... } in THIR lowering, so directly +// calling constructors doesn't require them to be const. + +type ExternalType = std::panic::AssertUnwindSafe<(Option, Result)>; + +const fn call_external_constructors_in_local_vars() -> ExternalType { + let f = Some; + let g = Err; + let h = std::panic::AssertUnwindSafe; + let x = f(5); + let y = g(false); + let z = h((x, y)); + z +} + +const CALL_EXTERNAL_CONSTRUCTORS_IN_LOCAL_VARS: ExternalType = { + let f = Some; + let g = Err; + let h = std::panic::AssertUnwindSafe; + let x = f(5); + let y = g(false); + let z = h((x, y)); + z +}; + +const fn call_external_constructors_in_temps() -> ExternalType { + let x = { Some }(5); + let y = (*&Err)(false); + let z = [std::panic::AssertUnwindSafe][0]((x, y)); + z +} + +const CALL_EXTERNAL_CONSTRUCTORS_IN_TEMPS: ExternalType = { + let x = { Some }(5); + let y = (*&Err)(false); + let z = [std::panic::AssertUnwindSafe][0]((x, y)); + z +}; + +#[derive(Debug, PartialEq)] +enum LocalOption { + Some(T), + _None, +} + +#[derive(Debug, PartialEq)] +enum LocalResult { + _Ok(T), + Err(E), +} + +#[derive(Debug, PartialEq)] +struct LocalAssertUnwindSafe(T); + +type LocalType = LocalAssertUnwindSafe<(LocalOption, LocalResult)>; + +const fn call_local_constructors_in_local_vars() -> LocalType { + let f = LocalOption::Some; + let g = LocalResult::Err; + let h = LocalAssertUnwindSafe; + let x = f(5); + let y = g(false); + let z = h((x, y)); + z +} + +const CALL_LOCAL_CONSTRUCTORS_IN_LOCAL_VARS: LocalType = { + let f = LocalOption::Some; + let g = LocalResult::Err; + let h = LocalAssertUnwindSafe; + let x = f(5); + let y = g(false); + let z = h((x, y)); + z +}; + +const fn call_local_constructors_in_temps() -> LocalType { + let x = { LocalOption::Some }(5); + let y = (*&LocalResult::Err)(false); + let z = [LocalAssertUnwindSafe][0]((x, y)); + z +} + +const CALL_LOCAL_CONSTRUCTORS_IN_TEMPS: LocalType = { + let x = { LocalOption::Some }(5); + let y = (*&LocalResult::Err)(false); + let z = [LocalAssertUnwindSafe][0]((x, y)); + z +}; + +fn main() { + assert_eq!( + ( + call_external_constructors_in_local_vars().0, + call_external_constructors_in_temps().0, + call_local_constructors_in_local_vars(), + call_local_constructors_in_temps(), + ), + ( + CALL_EXTERNAL_CONSTRUCTORS_IN_LOCAL_VARS.0, + CALL_EXTERNAL_CONSTRUCTORS_IN_TEMPS.0, + CALL_LOCAL_CONSTRUCTORS_IN_LOCAL_VARS, + CALL_LOCAL_CONSTRUCTORS_IN_TEMPS, + ) + ); +} diff --git a/src/test/ui/consts/const_constructor/const_constructor_qpath.rs b/src/test/ui/consts/const_constructor/const_constructor_qpath.rs new file mode 100644 index 000000000..7c55f470f --- /dev/null +++ b/src/test/ui/consts/const_constructor/const_constructor_qpath.rs @@ -0,0 +1,37 @@ +// run-pass + +trait ConstDefault { + const DEFAULT: Self; +} + +#[derive(PartialEq)] +enum E { + V(i32), + W(usize), +} + +impl ConstDefault for E { + const DEFAULT: Self = Self::V(23); +} + +impl ConstDefault for Option { + const DEFAULT: Self = Self::Some(23); +} + +impl E { + const NON_DEFAULT: Self = Self::W(12); + const fn local_fn() -> Self { + Self::V(23) + } +} + +const fn explicit_qpath() -> E { + let _x = >::Some(23); + ::W(12) +} + +fn main() { + assert!(E::DEFAULT == E::local_fn()); + assert!(Option::DEFAULT == Some(23)); + assert!(E::NON_DEFAULT == explicit_qpath()); +} -- cgit v1.2.3