From 498fee7f7b908cb14e6ac18dca8442e19c89c0a9 Mon Sep 17 00:00:00 2001 From: Robin Krahl Date: Fri, 24 May 2024 11:15:17 +0200 Subject: [PATCH] Enforce const evaluation in path! macro By using a temporary const value, we can enforce that the path! macro always causes a compiler error for illegal values even if it is not used in a const context. This makes it easier to write correct code. --- CHANGELOG.md | 4 ++++ src/lib.rs | 19 +++++++++++++++---- 2 files changed, 19 insertions(+), 4 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index cdb8ec91a..d074c7244 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -18,6 +18,10 @@ and this project adheres to [Semantic Versioning](http://semver.org/). - Fixed build error that would occur on Windows systems. - Added path iteration utilities ([#47][]) +## Changed + +- Enforced const evaluation for `path!`. + [#47]: https://github.com/trussed-dev/littlefs2/pull/47 [#57]: https://github.com/trussed-dev/littlefs2/pull/57 diff --git a/src/lib.rs b/src/lib.rs index a7a78e2b5..8186606d6 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -165,7 +165,7 @@ pub struct Version { /// Creates a path from a string without a trailing null. /// -/// Panics if the string contains null bytes or non-ascii characters. +/// Panics and causes a compiler error if the string contains null bytes or non-ascii characters. /// /// # Examples /// @@ -173,6 +173,7 @@ pub struct Version { /// use littlefs2::{path, path::Path}; /// /// const HOME: &Path = path!("/home"); +/// let root = path!("/"); /// ``` /// /// Illegal values: @@ -186,11 +187,21 @@ pub struct Version { /// # use littlefs2::{path, path::Path}; /// const WITH_UTF8: &Path = path!("/höme"); // does not compile /// ``` +/// +/// The macro enforces const evaluation so that compilation fails for illegal values even if the +/// macro is not used in a const context: +/// +/// ```compile_fail +/// # use littlefs2::path; +/// let path = path!("te\0st"); // does not compile +/// ``` #[macro_export] macro_rules! path { - ($path:literal) => { - $crate::path::Path::from_str_with_nul(::core::concat!($path, "\0")) - }; + ($path:literal) => {{ + const _PATH: &$crate::path::Path = + $crate::path::Path::from_str_with_nul(::core::concat!($path, "\0")); + _PATH + }}; } #[cfg(test)]