From a4bff741d0bd128dc92a7e36c96af91461bd3611 Mon Sep 17 00:00:00 2001 From: David Tolnay Date: Sun, 21 Nov 2021 19:09:14 -0800 Subject: [PATCH 1/4] Test not never Currently fails to build: error[E0600]: cannot apply unary operator `!` to type `!` --> library/core/tests/ops.rs:239:8 | 239 | if !return () {} | ^^^^^^^^^^ cannot apply unary operator `!` --- library/core/tests/ops.rs | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/library/core/tests/ops.rs b/library/core/tests/ops.rs index aa79dbac8f3..0c81cba35b3 100644 --- a/library/core/tests/ops.rs +++ b/library/core/tests/ops.rs @@ -232,3 +232,9 @@ fn deref_on_ref() { let y = deref(&mut x); assert_eq!(y, 4); } + +#[test] +#[allow(unreachable_code)] +fn test_not_never() { + if !return () {} +} From 9e834785787961646fd3951757b48d87f1afe6ef Mon Sep 17 00:00:00 2001 From: David Tolnay Date: Sun, 21 Nov 2021 19:06:35 -0800 Subject: [PATCH 2/4] impl Not for ! --- library/core/src/ops/bit.rs | 11 +++++++++++ 1 file changed, 11 insertions(+) diff --git a/library/core/src/ops/bit.rs b/library/core/src/ops/bit.rs index 255f6cb7933..d9ce84541cf 100644 --- a/library/core/src/ops/bit.rs +++ b/library/core/src/ops/bit.rs @@ -68,6 +68,17 @@ macro_rules! not_impl { not_impl! { bool usize u8 u16 u32 u64 u128 isize i8 i16 i32 i64 i128 } +#[stable(feature = "not_never", since = "1.58.0")] +#[rustc_const_unstable(feature = "const_ops", issue = "90080")] +impl const Not for ! { + type Output = !; + + #[inline] + fn not(self) -> ! { + match self {} + } +} + /// The bitwise AND operator `&`. /// /// Note that `Rhs` is `Self` by default, but this is not mandatory. From 881e093f6d3c68caaad64fa28a5def6d308c52cd Mon Sep 17 00:00:00 2001 From: David Tolnay Date: Sun, 21 Nov 2021 19:30:17 -0800 Subject: [PATCH 3/4] Replace ! with * in ui test of unary expr reachability --- src/test/ui/reachable/expr_unary.rs | 4 ++-- src/test/ui/reachable/expr_unary.stderr | 10 +++++----- 2 files changed, 7 insertions(+), 7 deletions(-) diff --git a/src/test/ui/reachable/expr_unary.rs b/src/test/ui/reachable/expr_unary.rs index e229d22ebc7..190c7447dcc 100644 --- a/src/test/ui/reachable/expr_unary.rs +++ b/src/test/ui/reachable/expr_unary.rs @@ -5,8 +5,8 @@ #![deny(unreachable_code)] fn foo() { - let x: ! = ! { return; }; //~ ERROR unreachable - //~| ERROR cannot apply unary operator `!` to type `!` + let x: ! = * { return; }; //~ ERROR unreachable + //~| ERROR type `!` cannot be dereferenced } fn main() { } diff --git a/src/test/ui/reachable/expr_unary.stderr b/src/test/ui/reachable/expr_unary.stderr index 063d841c25e..0a763087c6f 100644 --- a/src/test/ui/reachable/expr_unary.stderr +++ b/src/test/ui/reachable/expr_unary.stderr @@ -1,13 +1,13 @@ -error[E0600]: cannot apply unary operator `!` to type `!` +error[E0614]: type `!` cannot be dereferenced --> $DIR/expr_unary.rs:8:16 | -LL | let x: ! = ! { return; }; - | ^^^^^^^^^^^^^ cannot apply unary operator `!` +LL | let x: ! = * { return; }; + | ^^^^^^^^^^^^^ error: unreachable expression --> $DIR/expr_unary.rs:8:16 | -LL | let x: ! = ! { return; }; +LL | let x: ! = * { return; }; | ^^^^------^^^ | | | | | any code following this expression is unreachable @@ -21,4 +21,4 @@ LL | #![deny(unreachable_code)] error: aborting due to 2 previous errors -For more information about this error, try `rustc --explain E0600`. +For more information about this error, try `rustc --explain E0614`. From 3136c5f752837ce48d2e6cf1cc643929d294cfe0 Mon Sep 17 00:00:00 2001 From: David Tolnay Date: Wed, 19 Jan 2022 23:30:42 -0800 Subject: [PATCH 4/4] Update stabilization version of impl Not for ! --- library/core/src/ops/bit.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/library/core/src/ops/bit.rs b/library/core/src/ops/bit.rs index d9ce84541cf..7c664226fc2 100644 --- a/library/core/src/ops/bit.rs +++ b/library/core/src/ops/bit.rs @@ -68,7 +68,7 @@ macro_rules! not_impl { not_impl! { bool usize u8 u16 u32 u64 u128 isize i8 i16 i32 i64 i128 } -#[stable(feature = "not_never", since = "1.58.0")] +#[stable(feature = "not_never", since = "1.60.0")] #[rustc_const_unstable(feature = "const_ops", issue = "90080")] impl const Not for ! { type Output = !;