From f3de4aa564d17fe6b19849b3e31422509cac31c3 Mon Sep 17 00:00:00 2001 From: Christiaan Dirkx Date: Wed, 2 Sep 2020 03:28:50 +0200 Subject: [PATCH 1/3] Make some `std::path::Prefix` methods const Insta-stabilize the method `std::path::Prefix::is_verbatim` as const. Also make private methods `is_drive` and `has_implicit_root` const. Possible because of the recent stabilization of const control flow. Also adds a test for `is_verbatim` in a const context. --- library/std/src/path.rs | 7 ++++--- src/test/ui/consts/std/path.rs | 10 ++++++++++ 2 files changed, 14 insertions(+), 3 deletions(-) create mode 100644 src/test/ui/consts/std/path.rs diff --git a/library/std/src/path.rs b/library/std/src/path.rs index d71e89d0eee68..c6911af056f6a 100644 --- a/library/std/src/path.rs +++ b/library/std/src/path.rs @@ -214,19 +214,20 @@ impl<'a> Prefix<'a> { /// assert!(!Disk(b'C').is_verbatim()); /// ``` #[inline] + #[rustc_const_stable(feature = "const_ip", since = "1.48.0")] #[stable(feature = "rust1", since = "1.0.0")] - pub fn is_verbatim(&self) -> bool { + pub const fn is_verbatim(&self) -> bool { use self::Prefix::*; matches!(*self, Verbatim(_) | VerbatimDisk(_) | VerbatimUNC(..)) } #[inline] - fn is_drive(&self) -> bool { + const fn is_drive(&self) -> bool { matches!(*self, Prefix::Disk(_)) } #[inline] - fn has_implicit_root(&self) -> bool { + const fn has_implicit_root(&self) -> bool { !self.is_drive() } } diff --git a/src/test/ui/consts/std/path.rs b/src/test/ui/consts/std/path.rs new file mode 100644 index 0000000000000..af6f99b8c07a3 --- /dev/null +++ b/src/test/ui/consts/std/path.rs @@ -0,0 +1,10 @@ +// run-pass + +use std::path::Prefix; + +fn main() { + const PREFIX : Prefix = Prefix::Disk(2); + + const IS_VERBATIM : bool = PREFIX.is_verbatim(); + assert!(!IS_VERBATIM); +} From 770ecd28d5e94ddfff843250ba3ac32fb1e31e38 Mon Sep 17 00:00:00 2001 From: CDirkx Date: Wed, 2 Sep 2020 03:38:12 +0200 Subject: [PATCH 2/3] Update to correct feature `const_path_prefix` --- library/std/src/path.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/library/std/src/path.rs b/library/std/src/path.rs index c6911af056f6a..26909ede0c7da 100644 --- a/library/std/src/path.rs +++ b/library/std/src/path.rs @@ -214,7 +214,7 @@ impl<'a> Prefix<'a> { /// assert!(!Disk(b'C').is_verbatim()); /// ``` #[inline] - #[rustc_const_stable(feature = "const_ip", since = "1.48.0")] + #[rustc_const_stable(feature = "const_path_prefix", since = "1.48.0")] #[stable(feature = "rust1", since = "1.0.0")] pub const fn is_verbatim(&self) -> bool { use self::Prefix::*; From ba79257bc84f2c17a729ed0ece1991fcc159791b Mon Sep 17 00:00:00 2001 From: Christiaan Dirkx Date: Fri, 4 Sep 2020 01:56:14 +0200 Subject: [PATCH 3/3] Move const tests for `std::path::Prefix` to `library\core` Part of #76268 --- library/std/src/path/tests.rs | 10 ++++++++++ src/test/ui/consts/std/path.rs | 10 ---------- 2 files changed, 10 insertions(+), 10 deletions(-) delete mode 100644 src/test/ui/consts/std/path.rs diff --git a/library/std/src/path/tests.rs b/library/std/src/path/tests.rs index ff94fda5a227b..bf512ac674096 100644 --- a/library/std/src/path/tests.rs +++ b/library/std/src/path/tests.rs @@ -1392,3 +1392,13 @@ fn into_rc() { assert_eq!(&*rc2, path); assert_eq!(&*arc2, path); } + +#[test] +fn prefix_const() { + // test that the methods of `Prefix` are usable in a const context + + const PREFIX: Prefix<'_> = Prefix::Disk(2); + + const IS_VERBATIM: bool = PREFIX.is_verbatim(); + assert!(!IS_VERBATIM); +} diff --git a/src/test/ui/consts/std/path.rs b/src/test/ui/consts/std/path.rs deleted file mode 100644 index af6f99b8c07a3..0000000000000 --- a/src/test/ui/consts/std/path.rs +++ /dev/null @@ -1,10 +0,0 @@ -// run-pass - -use std::path::Prefix; - -fn main() { - const PREFIX : Prefix = Prefix::Disk(2); - - const IS_VERBATIM : bool = PREFIX.is_verbatim(); - assert!(!IS_VERBATIM); -}