From 3ec89746ba8129c68ff8a278acc10fb12ad55939 Mon Sep 17 00:00:00 2001 From: Ralf Jung Date: Sat, 3 Nov 2018 14:10:34 +0100 Subject: [PATCH] test diagnostics for more ranges --- src/test/ui/consts/const-eval/ub-nonnull.rs | 16 +++++++++++++++- .../ui/consts/const-eval/ub-nonnull.stderr | 18 +++++++++++++++++- 2 files changed, 32 insertions(+), 2 deletions(-) diff --git a/src/test/ui/consts/const-eval/ub-nonnull.rs b/src/test/ui/consts/const-eval/ub-nonnull.rs index 8b83a1747ca..113221959fb 100644 --- a/src/test/ui/consts/const-eval/ub-nonnull.rs +++ b/src/test/ui/consts/const-eval/ub-nonnull.rs @@ -8,7 +8,7 @@ // option. This file may not be copied, modified, or distributed // except according to those terms. -#![feature(const_transmute)] +#![feature(rustc_attrs, const_transmute)] #![allow(const_err)] // make sure we cannot allow away the errors tested here use std::mem; @@ -23,4 +23,18 @@ const NULL_U8: NonZeroU8 = unsafe { mem::transmute(0u8) }; const NULL_USIZE: NonZeroUsize = unsafe { mem::transmute(0usize) }; //~^ ERROR it is undefined behavior to use this value +// Also test other uses of rustc_layout_scalar_valid_range_start + +#[rustc_layout_scalar_valid_range_start(10)] +#[rustc_layout_scalar_valid_range_end(30)] +struct RestrictedRange1(u32); +const BAD_RANGE1: RestrictedRange1 = unsafe { RestrictedRange1(42) }; +//~^ ERROR it is undefined behavior to use this value + +#[rustc_layout_scalar_valid_range_start(30)] +#[rustc_layout_scalar_valid_range_end(10)] +struct RestrictedRange2(u32); +const BAD_RANGE2: RestrictedRange2 = unsafe { RestrictedRange2(20) }; +//~^ ERROR it is undefined behavior to use this value + fn main() {} diff --git a/src/test/ui/consts/const-eval/ub-nonnull.stderr b/src/test/ui/consts/const-eval/ub-nonnull.stderr index 7c0ff851d88..c50b0208f15 100644 --- a/src/test/ui/consts/const-eval/ub-nonnull.stderr +++ b/src/test/ui/consts/const-eval/ub-nonnull.stderr @@ -22,6 +22,22 @@ LL | const NULL_USIZE: NonZeroUsize = unsafe { mem::transmute(0usize) }; | = note: The rules on what exactly is undefined behavior aren't clear, so this check might be overzealous. Please open an issue on the rust compiler repository if you believe it should not be considered undefined behavior -error: aborting due to 3 previous errors +error[E0080]: it is undefined behavior to use this value + --> $DIR/ub-nonnull.rs:31:1 + | +LL | const BAD_RANGE1: RestrictedRange1 = unsafe { RestrictedRange1(42) }; + | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ type validation failed: encountered 42, but expected something in the range 10..=30 + | + = note: The rules on what exactly is undefined behavior aren't clear, so this check might be overzealous. Please open an issue on the rust compiler repository if you believe it should not be considered undefined behavior + +error[E0080]: it is undefined behavior to use this value + --> $DIR/ub-nonnull.rs:37:1 + | +LL | const BAD_RANGE2: RestrictedRange2 = unsafe { RestrictedRange2(20) }; + | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ type validation failed: encountered 20, but expected something less or equal to 10, or greater or equal to 30 + | + = note: The rules on what exactly is undefined behavior aren't clear, so this check might be overzealous. Please open an issue on the rust compiler repository if you believe it should not be considered undefined behavior + +error: aborting due to 5 previous errors For more information about this error, try `rustc --explain E0080`.