From dba9cdf2223557cba8934cce6b9c6b3defb87eb0 Mon Sep 17 00:00:00 2001 From: dollaransh17 Date: Fri, 3 Oct 2025 22:20:15 +0530 Subject: [PATCH 1/2] Make bool::then_some and bool::then const methods This change makes both `then_some` and `then` methods on bool const-compatible, allowing them to be used in const contexts for compile-time evaluation. Benefits: - Enables compile-time conditional Option creation - Allows usage in const generics and static initialization - Provides zero-cost abstraction in const contexts - Improves ergonomics for const programming patterns The `then_some` method is straightforward to make const as it only uses basic conditional logic. The `then` method requires a `~const FnOnce` bound to ensure the closure can be called in const contexts. This resolves the FIXME in the test suite and enables the const tests for these methods that were previously disabled. --- library/core/src/bool.rs | 7 +++++-- library/coretests/tests/bool.rs | 2 -- 2 files changed, 5 insertions(+), 4 deletions(-) diff --git a/library/core/src/bool.rs b/library/core/src/bool.rs index 99268d6182f6c..21b136d420506 100644 --- a/library/core/src/bool.rs +++ b/library/core/src/bool.rs @@ -30,7 +30,7 @@ impl bool { /// ``` #[stable(feature = "bool_to_option", since = "1.62.0")] #[inline] - pub fn then_some(self, t: T) -> Option { + pub const fn then_some(self, t: T) -> Option { if self { Some(t) } else { None } } @@ -58,7 +58,10 @@ impl bool { #[stable(feature = "lazy_bool_to_option", since = "1.50.0")] #[rustc_diagnostic_item = "bool_then"] #[inline] - pub fn then T>(self, f: F) -> Option { + pub const fn then T>(self, f: F) -> Option + where + F: [const] FnOnce() -> T, + { if self { Some(f()) } else { None } } diff --git a/library/coretests/tests/bool.rs b/library/coretests/tests/bool.rs index eb5f0f50663e1..49dd035c37810 100644 --- a/library/coretests/tests/bool.rs +++ b/library/coretests/tests/bool.rs @@ -89,7 +89,6 @@ fn test_bool_to_option() { assert_eq!(false.then(|| 0), None); assert_eq!(true.then(|| 0), Some(0)); - /* FIXME(#110395) const fn zero() -> i32 { 0 } @@ -103,7 +102,6 @@ fn test_bool_to_option() { assert_eq!(B, Some(0)); assert_eq!(C, None); assert_eq!(D, Some(0)); - */ } #[test] From 7e6ad283995aff3284b616c0b55f56715b4361cf Mon Sep 17 00:00:00 2001 From: dollaransh17 Date: Sat, 4 Oct 2025 00:21:26 +0530 Subject: [PATCH 2/2] Add missing const stability attributes for bool methods This fixes compilation errors in bootstrap tests by adding: - rustc_const_stable attribute for then_some method - rustc_const_unstable attribute for then method - [const] Destruct bound for const trait compatibility - Required Destruct import Resolves E0493 destructor evaluation errors and missing const stability attribute errors during bootstrap. --- library/core/src/bool.rs | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/library/core/src/bool.rs b/library/core/src/bool.rs index 21b136d420506..cb64a994bf526 100644 --- a/library/core/src/bool.rs +++ b/library/core/src/bool.rs @@ -1,5 +1,7 @@ //! impl bool {} +use crate::marker::Destruct; + impl bool { /// Returns `Some(t)` if the `bool` is [`true`](../std/keyword.true.html), /// or `None` otherwise. @@ -29,6 +31,7 @@ impl bool { /// assert_eq!(a, 2); /// ``` #[stable(feature = "bool_to_option", since = "1.62.0")] + #[rustc_const_stable(feature = "bool_to_option", since = "1.62.0")] #[inline] pub const fn then_some(self, t: T) -> Option { if self { Some(t) } else { None } @@ -56,11 +59,12 @@ impl bool { /// ``` #[doc(alias = "then_with")] #[stable(feature = "lazy_bool_to_option", since = "1.50.0")] + #[rustc_const_unstable(feature = "const_option_ops", issue = "143956")] #[rustc_diagnostic_item = "bool_then"] #[inline] pub const fn then T>(self, f: F) -> Option where - F: [const] FnOnce() -> T, + F: [const] FnOnce() -> T + [const] Destruct, { if self { Some(f()) } else { None } }