From 7d1a8f9c1d7dca982f320f4b2c0717cd7e2d9377 Mon Sep 17 00:00:00 2001 From: David Tolnay Date: Thu, 31 Oct 2024 22:26:27 -0700 Subject: [PATCH 1/3] Add test of raw addr expression syntax --- tests/test_ensure.rs | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/tests/test_ensure.rs b/tests/test_ensure.rs index f41ad37..4fc80e6 100644 --- a/tests/test_ensure.rs +++ b/tests/test_ensure.rs @@ -184,6 +184,12 @@ fn test_unary() { let test = || Ok(ensure!(&mut x == *&&mut &2)); assert_err(test, "Condition failed: `&mut x == *&&mut &2` (1 vs 2)"); + + let test = || Ok(ensure!(S + &raw const x != S + &raw mut x)); + assert_err( + test, + "Condition failed: `S + &raw const x != S + &raw mut x`", // FIXME + ); } #[test] From 1e7e9fe1edeb6e28767ac52c32f778462b181a19 Mon Sep 17 00:00:00 2001 From: David Tolnay Date: Thu, 31 Oct 2024 22:30:43 -0700 Subject: [PATCH 2/3] Parse raw address expression syntax --- src/ensure.rs | 16 ++++++++++++++++ tests/test_ensure.rs | 2 +- 2 files changed, 17 insertions(+), 1 deletion(-) diff --git a/src/ensure.rs b/src/ensure.rs index 9d0a676..795757d 100644 --- a/src/ensure.rs +++ b/src/ensure.rs @@ -155,6 +155,14 @@ macro_rules! __parse_ensure { $crate::__parse_ensure!(0 $stack $bail ($($fuel)*) {($($buf)* $and $mut) $($parse)*} ($($rest)*) $($rest)*) }; + (0 $stack:tt $bail:tt (~$($fuel:tt)*) {($($buf:tt)*) $($parse:tt)*} ($and:tt $raw:tt $mut:tt $($dup:tt)*) &raw mut $($rest:tt)*) => { + $crate::__parse_ensure!(0 $stack $bail ($($fuel)*) {($($buf)* $and $raw $mut) $($parse)*} ($($rest)*) $($rest)*) + }; + + (0 $stack:tt $bail:tt (~$($fuel:tt)*) {($($buf:tt)*) $($parse:tt)*} ($and:tt $raw:tt $const:tt $($dup:tt)*) &raw const $($rest:tt)*) => { + $crate::__parse_ensure!(0 $stack $bail ($($fuel)*) {($($buf)* $and $raw $const) $($parse)*} ($($rest)*) $($rest)*) + }; + (0 $stack:tt $bail:tt (~$($fuel:tt)*) {($($buf:tt)*) $($parse:tt)*} ($and:tt $($dup:tt)*) & $($rest:tt)*) => { $crate::__parse_ensure!(0 $stack $bail ($($fuel)*) {($($buf)* $and) $($parse)*} ($($rest)*) $($rest)*) }; @@ -163,6 +171,14 @@ macro_rules! __parse_ensure { $crate::__parse_ensure!(0 $stack $bail ($($fuel)*) {($($buf)* $andand $mut) $($parse)*} ($($rest)*) $($rest)*) }; + (0 $stack:tt $bail:tt (~$($fuel:tt)*) {($($buf:tt)*) $($parse:tt)*} ($andand:tt $raw:tt $mut:tt $($dup:tt)*) &&raw mut $($rest:tt)*) => { + $crate::__parse_ensure!(0 $stack $bail ($($fuel)*) {($($buf)* $andand $raw $mut) $($parse)*} ($($rest)*) $($rest)*) + }; + + (0 $stack:tt $bail:tt (~$($fuel:tt)*) {($($buf:tt)*) $($parse:tt)*} ($andand:tt $raw:tt $const:tt $($dup:tt)*) &&raw const $($rest:tt)*) => { + $crate::__parse_ensure!(0 $stack $bail ($($fuel)*) {($($buf)* $andand $raw $const) $($parse)*} ($($rest)*) $($rest)*) + }; + (0 $stack:tt $bail:tt (~$($fuel:tt)*) {($($buf:tt)*) $($parse:tt)*} ($andand:tt $($dup:tt)*) && $($rest:tt)*) => { $crate::__parse_ensure!(0 $stack $bail ($($fuel)*) {($($buf)* $andand) $($parse)*} ($($rest)*) $($rest)*) }; diff --git a/tests/test_ensure.rs b/tests/test_ensure.rs index 4fc80e6..9660df7 100644 --- a/tests/test_ensure.rs +++ b/tests/test_ensure.rs @@ -188,7 +188,7 @@ fn test_unary() { let test = || Ok(ensure!(S + &raw const x != S + &raw mut x)); assert_err( test, - "Condition failed: `S + &raw const x != S + &raw mut x`", // FIXME + "Condition failed: `S + &raw const x != S + &raw mut x` (false vs false)", ); } From fcf2ef8da58179fbb96121ca39c0149b84f038f2 Mon Sep 17 00:00:00 2001 From: David Tolnay Date: Thu, 31 Oct 2024 22:32:50 -0700 Subject: [PATCH 3/3] Compile &raw test on Rust 1.82+ only --- tests/test_ensure.rs | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/tests/test_ensure.rs b/tests/test_ensure.rs index 9660df7..b43490a 100644 --- a/tests/test_ensure.rs +++ b/tests/test_ensure.rs @@ -184,7 +184,12 @@ fn test_unary() { let test = || Ok(ensure!(&mut x == *&&mut &2)); assert_err(test, "Condition failed: `&mut x == *&&mut &2` (1 vs 2)"); +} +#[rustversion::since(1.82)] +#[test] +fn test_raw_addr() { + let mut x = 1; let test = || Ok(ensure!(S + &raw const x != S + &raw mut x)); assert_err( test,