From bcb1f068bc7177b1e02d64be6f01d4a50111149e Mon Sep 17 00:00:00 2001 From: YenForYang Date: Sun, 29 Nov 2020 20:16:31 -0600 Subject: [PATCH 1/4] Make char methods const `escape_unicode`, `escape_default`, `len_utf8`, `len_utf16`, to_ascii_lowercase`, `eq_ignore_ascii_case` `u8` methods `to_ascii_lowercase`, `to_ascii_uppercase` also must be made const u8 methods made const Update methods.rs Update mod.rs Update methods.rs Fix `since` in rustc_const_stable to next stable Fix `since` in rustc_const_stable to next stable Update methods.rs Update mod.rs --- library/core/src/char/methods.rs | 23 +++++++++++++++-------- library/core/src/num/mod.rs | 9 ++++++--- 2 files changed, 21 insertions(+), 11 deletions(-) diff --git a/library/core/src/char/methods.rs b/library/core/src/char/methods.rs index 4390342134d..e292fe2cb9d 100644 --- a/library/core/src/char/methods.rs +++ b/library/core/src/char/methods.rs @@ -384,8 +384,9 @@ impl char { /// assert_eq!('❤'.escape_unicode().to_string(), "\\u{2764}"); /// ``` #[stable(feature = "rust1", since = "1.0.0")] + #[rustc_const_stable(feature = "const_char_escape_unicode", since = "1.50.0")] #[inline] - pub fn escape_unicode(self) -> EscapeUnicode { + pub const fn escape_unicode(self) -> EscapeUnicode { let c = self as u32; // or-ing 1 ensures that for c==0 the code computes that one @@ -510,8 +511,9 @@ impl char { /// assert_eq!('"'.escape_default().to_string(), "\\\""); /// ``` #[stable(feature = "rust1", since = "1.0.0")] + #[rustc_const_stable(feature = "const_char_escape_default", since = "1.50.0")] #[inline] - pub fn escape_default(self) -> EscapeDefault { + pub const fn escape_default(self) -> EscapeDefault { let init_state = match self { '\t' => EscapeDefaultState::Backslash('t'), '\r' => EscapeDefaultState::Backslash('r'), @@ -569,8 +571,9 @@ impl char { /// assert_eq!(len, tokyo.len()); /// ``` #[stable(feature = "rust1", since = "1.0.0")] + #[rustc_const_stable(feature = "const_char_len_utf", since = "1.50.0")] #[inline] - pub fn len_utf8(self) -> usize { + pub const fn len_utf8(self) -> usize { len_utf8(self as u32) } @@ -594,8 +597,9 @@ impl char { /// assert_eq!(len, 2); /// ``` #[stable(feature = "rust1", since = "1.0.0")] + #[rustc_const_stable(feature = "const_char_len_utf", since = "1.50.0")] #[inline] - pub fn len_utf16(self) -> usize { + pub const fn len_utf16(self) -> usize { let ch = self as u32; if (ch & 0xFFFF) == ch { 1 } else { 2 } } @@ -1086,8 +1090,9 @@ impl char { /// [`make_ascii_uppercase()`]: #method.make_ascii_uppercase /// [`to_uppercase()`]: #method.to_uppercase #[stable(feature = "ascii_methods_on_intrinsics", since = "1.23.0")] + #[rustc_const_stable(feature = "const_ascii_methods_on_intrinsics", since = "1.50.0")] #[inline] - pub fn to_ascii_uppercase(&self) -> char { + pub const fn to_ascii_uppercase(&self) -> char { if self.is_ascii_lowercase() { (*self as u8).ascii_change_case_unchecked() as char } else { @@ -1118,8 +1123,9 @@ impl char { /// [`make_ascii_lowercase()`]: #method.make_ascii_lowercase /// [`to_lowercase()`]: #method.to_lowercase #[stable(feature = "ascii_methods_on_intrinsics", since = "1.23.0")] + #[rustc_const_stable(feature = "const_ascii_methods_on_intrinsics", since = "1.50.0")] #[inline] - pub fn to_ascii_lowercase(&self) -> char { + pub const fn to_ascii_lowercase(&self) -> char { if self.is_ascii_uppercase() { (*self as u8).ascii_change_case_unchecked() as char } else { @@ -1143,8 +1149,9 @@ impl char { /// assert!(!upper_a.eq_ignore_ascii_case(&lower_z)); /// ``` #[stable(feature = "ascii_methods_on_intrinsics", since = "1.23.0")] + #[rustc_const_stable(feature = "const_ascii_methods_on_intrinsics", since = "1.50.0")] #[inline] - pub fn eq_ignore_ascii_case(&self, other: &char) -> bool { + pub const fn eq_ignore_ascii_case(&self, other: &char) -> bool { self.to_ascii_lowercase() == other.to_ascii_lowercase() } @@ -1561,7 +1568,7 @@ impl char { } #[inline] -fn len_utf8(code: u32) -> usize { +const fn len_utf8(code: u32) -> usize { if code < MAX_ONE_B { 1 } else if code < MAX_TWO_B { diff --git a/library/core/src/num/mod.rs b/library/core/src/num/mod.rs index c13f000a736..8b81d41b8c0 100644 --- a/library/core/src/num/mod.rs +++ b/library/core/src/num/mod.rs @@ -195,8 +195,9 @@ impl u8 { /// /// [`make_ascii_uppercase`]: #method.make_ascii_uppercase #[stable(feature = "ascii_methods_on_intrinsics", since = "1.23.0")] + #[rustc_const_stable(feature = "const_ascii_methods_on_intrinsics", since = "1.50.0")] #[inline] - pub fn to_ascii_uppercase(&self) -> u8 { + pub const fn to_ascii_uppercase(&self) -> u8 { // Unset the fifth bit if this is a lowercase letter *self & !((self.is_ascii_lowercase() as u8) * ASCII_CASE_MASK) } @@ -218,8 +219,9 @@ impl u8 { /// /// [`make_ascii_lowercase`]: #method.make_ascii_lowercase #[stable(feature = "ascii_methods_on_intrinsics", since = "1.23.0")] + #[rustc_const_stable(feature = "const_ascii_methods_on_intrinsics", since = "1.50.0")] #[inline] - pub fn to_ascii_lowercase(&self) -> u8 { + pub const fn to_ascii_lowercase(&self) -> u8 { // Set the fifth bit if this is an uppercase letter *self | (self.is_ascii_uppercase() as u8 * ASCII_CASE_MASK) } @@ -243,8 +245,9 @@ impl u8 { /// assert!(lowercase_a.eq_ignore_ascii_case(&uppercase_a)); /// ``` #[stable(feature = "ascii_methods_on_intrinsics", since = "1.23.0")] + #[rustc_const_stable(feature = "const_ascii_methods_on_intrinsics", since = "1.50.0")] #[inline] - pub fn eq_ignore_ascii_case(&self, other: &u8) -> bool { + pub const fn eq_ignore_ascii_case(&self, other: &u8) -> bool { self.to_ascii_lowercase() == other.to_ascii_lowercase() } From 6a724913671061222b410d787a0fe5cb9376abd4 Mon Sep 17 00:00:00 2001 From: Ryan Lopopolo Date: Sat, 13 Feb 2021 15:16:48 -0800 Subject: [PATCH 2/4] Remove const from iterator fns --- library/core/src/char/methods.rs | 6 ++---- 1 file changed, 2 insertions(+), 4 deletions(-) diff --git a/library/core/src/char/methods.rs b/library/core/src/char/methods.rs index e292fe2cb9d..704c3817a68 100644 --- a/library/core/src/char/methods.rs +++ b/library/core/src/char/methods.rs @@ -384,9 +384,8 @@ impl char { /// assert_eq!('❤'.escape_unicode().to_string(), "\\u{2764}"); /// ``` #[stable(feature = "rust1", since = "1.0.0")] - #[rustc_const_stable(feature = "const_char_escape_unicode", since = "1.50.0")] #[inline] - pub const fn escape_unicode(self) -> EscapeUnicode { + pub fn escape_unicode(self) -> EscapeUnicode { let c = self as u32; // or-ing 1 ensures that for c==0 the code computes that one @@ -511,9 +510,8 @@ impl char { /// assert_eq!('"'.escape_default().to_string(), "\\\""); /// ``` #[stable(feature = "rust1", since = "1.0.0")] - #[rustc_const_stable(feature = "const_char_escape_default", since = "1.50.0")] #[inline] - pub const fn escape_default(self) -> EscapeDefault { + pub fn escape_default(self) -> EscapeDefault { let init_state = match self { '\t' => EscapeDefaultState::Backslash('t'), '\r' => EscapeDefaultState::Backslash('r'), From 7b41ad1c1fb68e890d039572866a3ef7ac125300 Mon Sep 17 00:00:00 2001 From: Ryan Lopopolo Date: Sat, 13 Feb 2021 15:17:44 -0800 Subject: [PATCH 3/4] Update since attributes for new const_ascii_methods_on_intrinsics to 1.52.0 --- library/core/src/char/methods.rs | 10 +++++----- library/core/src/num/mod.rs | 6 +++--- 2 files changed, 8 insertions(+), 8 deletions(-) diff --git a/library/core/src/char/methods.rs b/library/core/src/char/methods.rs index 704c3817a68..b3e1becf570 100644 --- a/library/core/src/char/methods.rs +++ b/library/core/src/char/methods.rs @@ -569,7 +569,7 @@ impl char { /// assert_eq!(len, tokyo.len()); /// ``` #[stable(feature = "rust1", since = "1.0.0")] - #[rustc_const_stable(feature = "const_char_len_utf", since = "1.50.0")] + #[rustc_const_stable(feature = "const_char_len_utf", since = "1.52.0")] #[inline] pub const fn len_utf8(self) -> usize { len_utf8(self as u32) @@ -595,7 +595,7 @@ impl char { /// assert_eq!(len, 2); /// ``` #[stable(feature = "rust1", since = "1.0.0")] - #[rustc_const_stable(feature = "const_char_len_utf", since = "1.50.0")] + #[rustc_const_stable(feature = "const_char_len_utf", since = "1.52.0")] #[inline] pub const fn len_utf16(self) -> usize { let ch = self as u32; @@ -1088,7 +1088,7 @@ impl char { /// [`make_ascii_uppercase()`]: #method.make_ascii_uppercase /// [`to_uppercase()`]: #method.to_uppercase #[stable(feature = "ascii_methods_on_intrinsics", since = "1.23.0")] - #[rustc_const_stable(feature = "const_ascii_methods_on_intrinsics", since = "1.50.0")] + #[rustc_const_stable(feature = "const_ascii_methods_on_intrinsics", since = "1.52.0")] #[inline] pub const fn to_ascii_uppercase(&self) -> char { if self.is_ascii_lowercase() { @@ -1121,7 +1121,7 @@ impl char { /// [`make_ascii_lowercase()`]: #method.make_ascii_lowercase /// [`to_lowercase()`]: #method.to_lowercase #[stable(feature = "ascii_methods_on_intrinsics", since = "1.23.0")] - #[rustc_const_stable(feature = "const_ascii_methods_on_intrinsics", since = "1.50.0")] + #[rustc_const_stable(feature = "const_ascii_methods_on_intrinsics", since = "1.52.0")] #[inline] pub const fn to_ascii_lowercase(&self) -> char { if self.is_ascii_uppercase() { @@ -1147,7 +1147,7 @@ impl char { /// assert!(!upper_a.eq_ignore_ascii_case(&lower_z)); /// ``` #[stable(feature = "ascii_methods_on_intrinsics", since = "1.23.0")] - #[rustc_const_stable(feature = "const_ascii_methods_on_intrinsics", since = "1.50.0")] + #[rustc_const_stable(feature = "const_ascii_methods_on_intrinsics", since = "1.52.0")] #[inline] pub const fn eq_ignore_ascii_case(&self, other: &char) -> bool { self.to_ascii_lowercase() == other.to_ascii_lowercase() diff --git a/library/core/src/num/mod.rs b/library/core/src/num/mod.rs index 8b81d41b8c0..1e7d75f9c26 100644 --- a/library/core/src/num/mod.rs +++ b/library/core/src/num/mod.rs @@ -195,7 +195,7 @@ impl u8 { /// /// [`make_ascii_uppercase`]: #method.make_ascii_uppercase #[stable(feature = "ascii_methods_on_intrinsics", since = "1.23.0")] - #[rustc_const_stable(feature = "const_ascii_methods_on_intrinsics", since = "1.50.0")] + #[rustc_const_stable(feature = "const_ascii_methods_on_intrinsics", since = "1.52.0")] #[inline] pub const fn to_ascii_uppercase(&self) -> u8 { // Unset the fifth bit if this is a lowercase letter @@ -219,7 +219,7 @@ impl u8 { /// /// [`make_ascii_lowercase`]: #method.make_ascii_lowercase #[stable(feature = "ascii_methods_on_intrinsics", since = "1.23.0")] - #[rustc_const_stable(feature = "const_ascii_methods_on_intrinsics", since = "1.50.0")] + #[rustc_const_stable(feature = "const_ascii_methods_on_intrinsics", since = "1.52.0")] #[inline] pub const fn to_ascii_lowercase(&self) -> u8 { // Set the fifth bit if this is an uppercase letter @@ -245,7 +245,7 @@ impl u8 { /// assert!(lowercase_a.eq_ignore_ascii_case(&uppercase_a)); /// ``` #[stable(feature = "ascii_methods_on_intrinsics", since = "1.23.0")] - #[rustc_const_stable(feature = "const_ascii_methods_on_intrinsics", since = "1.50.0")] + #[rustc_const_stable(feature = "const_ascii_methods_on_intrinsics", since = "1.52.0")] #[inline] pub const fn eq_ignore_ascii_case(&self, other: &u8) -> bool { self.to_ascii_lowercase() == other.to_ascii_lowercase() From 1ed9dd4179ffc3f7759dcb2a74dc362da647008c Mon Sep 17 00:00:00 2001 From: Ryan Lopopolo Date: Tue, 23 Feb 2021 07:20:13 -0800 Subject: [PATCH 4/4] Make ascii_change_case_unchecked const Rebases and makes changes required by the recent merge of #81837. --- library/core/src/num/mod.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/library/core/src/num/mod.rs b/library/core/src/num/mod.rs index 1e7d75f9c26..6bacaccd24a 100644 --- a/library/core/src/num/mod.rs +++ b/library/core/src/num/mod.rs @@ -228,7 +228,7 @@ impl u8 { /// Assumes self is ascii #[inline] - pub(crate) fn ascii_change_case_unchecked(&self) -> u8 { + pub(crate) const fn ascii_change_case_unchecked(&self) -> u8 { *self ^ ASCII_CASE_MASK }