diff --git a/src/librustc_mir/interpret/validity.rs b/src/librustc_mir/interpret/validity.rs index ef2ae8f5337c6..329651f038f8b 100644 --- a/src/librustc_mir/interpret/validity.rs +++ b/src/librustc_mir/interpret/validity.rs @@ -118,6 +118,24 @@ impl<'a, 'mir, 'tcx, M: Machine<'mir, 'tcx>> EvalContext<'a, 'mir, 'tcx, M> { bits }, Scalar::Ptr(_) => { + match ty.sty { + ty::Bool | + ty::Char | + ty::Float(_) | + ty::Int(_) | + ty::Uint(_) => { + return validation_failure!( + "a pointer", + path, + format!("the type {}", ty.sty) + ); + } + ty::RawPtr(_) | + ty::Ref(_, _, _) | + ty::FnPtr(_) => {} + _ => { unreachable!(); } + } + let ptr_size = self.pointer_size(); let ptr_max = u128::max_value() >> (128 - ptr_size.bits()); return if lo > hi { diff --git a/src/test/ui/consts/const-eval/const-pointer-values-in-various-types.rs b/src/test/ui/consts/const-eval/const-pointer-values-in-various-types.rs new file mode 100644 index 0000000000000..dc84e2a88d64a --- /dev/null +++ b/src/test/ui/consts/const-eval/const-pointer-values-in-various-types.rs @@ -0,0 +1,110 @@ +// only-x86_64 + +union Nonsense { + u: usize, + int_32_ref: &'static i32, + uint_8: u8, + uint_16: u16, + uint_32: u32, + uint_64: u64, + uint_128: u128, + int_8: i8, + int_16: i16, + int_32: i32, + int_64: i64, + int_128: i128, + float_32: f32, + float_64: f64, + truthy_falsey: bool, + character: char, + stringy: &'static str, +} + +fn main() { + const I32_REF_USIZE_UNION: usize = unsafe { Nonsense { int_32_ref: &3 }.u }; + //~^ ERROR this constant likely exhibits undefined behavior + + const I32_REF_U8_UNION: u8 = unsafe { Nonsense { int_32_ref: &3 }.uint_8 }; + //~^ ERROR this constant cannot be used + + const I32_REF_U16_UNION: u16 = unsafe { Nonsense { int_32_ref: &3 }.uint_16 }; + //~^ ERROR this constant cannot be used + + const I32_REF_U32_UNION: u32 = unsafe { Nonsense { int_32_ref: &3 }.uint_32 }; + //~^ ERROR this constant cannot be used + + const I32_REF_U64_UNION: u64 = unsafe { Nonsense { int_32_ref: &3 }.uint_64 }; + //~^ ERROR this constant likely exhibits undefined behavior + + const I32_REF_U128_UNION: u128 = unsafe { Nonsense { int_32_ref: &3 }.uint_128 }; + //~^ ERROR this constant cannot be used + + const I32_REF_I8_UNION: i8 = unsafe { Nonsense { int_32_ref: &3 }.int_8 }; + //~^ ERROR this constant cannot be used + + const I32_REF_I16_UNION: i16 = unsafe { Nonsense { int_32_ref: &3 }.int_16 }; + //~^ ERROR this constant cannot be used + + const I32_REF_I32_UNION: i32 = unsafe { Nonsense { int_32_ref: &3 }.int_32 }; + //~^ ERROR this constant cannot be used + + const I32_REF_I64_UNION: i64 = unsafe { Nonsense { int_32_ref: &3 }.int_64 }; + //~^ ERROR this constant likely exhibits undefined behavior + + const I32_REF_I128_UNION: i128 = unsafe { Nonsense { int_32_ref: &3 }.int_128 }; + //~^ ERROR this constant cannot be used + + const I32_REF_F32_UNION: f32 = unsafe { Nonsense { int_32_ref: &3 }.float_32 }; + //~^ ERROR this constant cannot be used + + const I32_REF_F64_UNION: f64 = unsafe { Nonsense { int_32_ref: &3 }.float_64 }; + //~^ ERROR this constant likely exhibits undefined behavior + + const I32_REF_BOOL_UNION: bool = unsafe { Nonsense { int_32_ref: &3 }.truthy_falsey }; + //~^ ERROR this constant cannot be used + + const I32_REF_CHAR_UNION: char = unsafe { Nonsense { int_32_ref: &3 }.character }; + //~^ ERROR this constant cannot be used + + const STR_U8_UNION: u8 = unsafe { Nonsense { stringy: "3" }.uint_8 }; + //~^ ERROR this constant cannot be used + + const STR_U16_UNION: u16 = unsafe { Nonsense { stringy: "3" }.uint_16 }; + //~^ ERROR this constant cannot be used + + const STR_U32_UNION: u32 = unsafe { Nonsense { stringy: "3" }.uint_32 }; + //~^ ERROR this constant cannot be used + + const STR_U64_UNION: u64 = unsafe { Nonsense { stringy: "3" }.uint_64 }; + //~^ ERROR this constant likely exhibits undefined behavior + + const STR_U128_UNION: u128 = unsafe { Nonsense { stringy: "3" }.uint_128 }; + //~^ ERROR this constant cannot be used + + const STR_I8_UNION: i8 = unsafe { Nonsense { stringy: "3" }.int_8 }; + //~^ ERROR this constant cannot be used + + const STR_I16_UNION: i16 = unsafe { Nonsense { stringy: "3" }.int_16 }; + //~^ ERROR this constant cannot be used + + const STR_I32_UNION: i32 = unsafe { Nonsense { stringy: "3" }.int_32 }; + //~^ ERROR this constant cannot be used + + const STR_I64_UNION: i64 = unsafe { Nonsense { stringy: "3" }.int_64 }; + //~^ ERROR this constant likely exhibits undefined behavior + + const STR_I128_UNION: i128 = unsafe { Nonsense { stringy: "3" }.int_128 }; + //~^ ERROR this constant cannot be used + + const STR_F32_UNION: f32 = unsafe { Nonsense { stringy: "3" }.float_32 }; + //~^ ERROR this constant cannot be used + + const STR_F64_UNION: f64 = unsafe { Nonsense { stringy: "3" }.float_64 }; + //~^ ERROR this constant likely exhibits undefined behavior + + const STR_BOOL_UNION: bool = unsafe { Nonsense { stringy: "3" }.truthy_falsey }; + //~^ ERROR this constant cannot be used + + const STR_CHAR_UNION: char = unsafe { Nonsense { stringy: "3" }.character }; + //~^ ERROR this constant cannot be used +} diff --git a/src/test/ui/consts/const-eval/const-pointer-values-in-various-types.stderr b/src/test/ui/consts/const-eval/const-pointer-values-in-various-types.stderr new file mode 100644 index 0000000000000..751f4113fbf60 --- /dev/null +++ b/src/test/ui/consts/const-eval/const-pointer-values-in-various-types.stderr @@ -0,0 +1,233 @@ +error[E0080]: this constant likely exhibits undefined behavior + --> $DIR/const-pointer-values-in-various-types.rs:24:5 + | +LL | const I32_REF_USIZE_UNION: usize = unsafe { Nonsense { int_32_ref: &3 }.u }; + | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ type validation failed: encountered a pointer, but expected the type usize + | + = 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: this constant cannot be used + --> $DIR/const-pointer-values-in-various-types.rs:27:5 + | +LL | const I32_REF_U8_UNION: u8 = unsafe { Nonsense { int_32_ref: &3 }.uint_8 }; + | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^----------------------------------^^^ + | | + | a raw memory access tried to access part of a pointer value as raw bytes + | + = note: #[deny(const_err)] on by default + +error: this constant cannot be used + --> $DIR/const-pointer-values-in-various-types.rs:30:5 + | +LL | const I32_REF_U16_UNION: u16 = unsafe { Nonsense { int_32_ref: &3 }.uint_16 }; + | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^-----------------------------------^^^ + | | + | a raw memory access tried to access part of a pointer value as raw bytes + +error: this constant cannot be used + --> $DIR/const-pointer-values-in-various-types.rs:33:5 + | +LL | const I32_REF_U32_UNION: u32 = unsafe { Nonsense { int_32_ref: &3 }.uint_32 }; + | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^-----------------------------------^^^ + | | + | a raw memory access tried to access part of a pointer value as raw bytes + +error[E0080]: this constant likely exhibits undefined behavior + --> $DIR/const-pointer-values-in-various-types.rs:36:5 + | +LL | const I32_REF_U64_UNION: u64 = unsafe { Nonsense { int_32_ref: &3 }.uint_64 }; + | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ type validation failed: encountered a pointer, but expected the type u64 + | + = 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: this constant cannot be used + --> $DIR/const-pointer-values-in-various-types.rs:39:5 + | +LL | const I32_REF_U128_UNION: u128 = unsafe { Nonsense { int_32_ref: &3 }.uint_128 }; + | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ attempted to read undefined bytes + +error: this constant cannot be used + --> $DIR/const-pointer-values-in-various-types.rs:42:5 + | +LL | const I32_REF_I8_UNION: i8 = unsafe { Nonsense { int_32_ref: &3 }.int_8 }; + | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^---------------------------------^^^ + | | + | a raw memory access tried to access part of a pointer value as raw bytes + +error: this constant cannot be used + --> $DIR/const-pointer-values-in-various-types.rs:45:5 + | +LL | const I32_REF_I16_UNION: i16 = unsafe { Nonsense { int_32_ref: &3 }.int_16 }; + | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^----------------------------------^^^ + | | + | a raw memory access tried to access part of a pointer value as raw bytes + +error: this constant cannot be used + --> $DIR/const-pointer-values-in-various-types.rs:48:5 + | +LL | const I32_REF_I32_UNION: i32 = unsafe { Nonsense { int_32_ref: &3 }.int_32 }; + | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^----------------------------------^^^ + | | + | a raw memory access tried to access part of a pointer value as raw bytes + +error[E0080]: this constant likely exhibits undefined behavior + --> $DIR/const-pointer-values-in-various-types.rs:51:5 + | +LL | const I32_REF_I64_UNION: i64 = unsafe { Nonsense { int_32_ref: &3 }.int_64 }; + | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ type validation failed: encountered a pointer, but expected the type i64 + | + = 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: this constant cannot be used + --> $DIR/const-pointer-values-in-various-types.rs:54:5 + | +LL | const I32_REF_I128_UNION: i128 = unsafe { Nonsense { int_32_ref: &3 }.int_128 }; + | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ attempted to read undefined bytes + +error: this constant cannot be used + --> $DIR/const-pointer-values-in-various-types.rs:57:5 + | +LL | const I32_REF_F32_UNION: f32 = unsafe { Nonsense { int_32_ref: &3 }.float_32 }; + | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^------------------------------------^^^ + | | + | a raw memory access tried to access part of a pointer value as raw bytes + +error[E0080]: this constant likely exhibits undefined behavior + --> $DIR/const-pointer-values-in-various-types.rs:60:5 + | +LL | const I32_REF_F64_UNION: f64 = unsafe { Nonsense { int_32_ref: &3 }.float_64 }; + | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ type validation failed: encountered a pointer, but expected the type f64 + | + = 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: this constant cannot be used + --> $DIR/const-pointer-values-in-various-types.rs:63:5 + | +LL | const I32_REF_BOOL_UNION: bool = unsafe { Nonsense { int_32_ref: &3 }.truthy_falsey }; + | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^-----------------------------------------^^^ + | | + | a raw memory access tried to access part of a pointer value as raw bytes + +error: this constant cannot be used + --> $DIR/const-pointer-values-in-various-types.rs:66:5 + | +LL | const I32_REF_CHAR_UNION: char = unsafe { Nonsense { int_32_ref: &3 }.character }; + | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^-------------------------------------^^^ + | | + | a raw memory access tried to access part of a pointer value as raw bytes + +error: this constant cannot be used + --> $DIR/const-pointer-values-in-various-types.rs:69:5 + | +LL | const STR_U8_UNION: u8 = unsafe { Nonsense { stringy: "3" }.uint_8 }; + | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^--------------------------------^^^ + | | + | a raw memory access tried to access part of a pointer value as raw bytes + +error: this constant cannot be used + --> $DIR/const-pointer-values-in-various-types.rs:72:5 + | +LL | const STR_U16_UNION: u16 = unsafe { Nonsense { stringy: "3" }.uint_16 }; + | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^---------------------------------^^^ + | | + | a raw memory access tried to access part of a pointer value as raw bytes + +error: this constant cannot be used + --> $DIR/const-pointer-values-in-various-types.rs:75:5 + | +LL | const STR_U32_UNION: u32 = unsafe { Nonsense { stringy: "3" }.uint_32 }; + | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^---------------------------------^^^ + | | + | a raw memory access tried to access part of a pointer value as raw bytes + +error[E0080]: this constant likely exhibits undefined behavior + --> $DIR/const-pointer-values-in-various-types.rs:78:5 + | +LL | const STR_U64_UNION: u64 = unsafe { Nonsense { stringy: "3" }.uint_64 }; + | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ type validation failed: encountered a pointer, but expected the type u64 + | + = 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: this constant cannot be used + --> $DIR/const-pointer-values-in-various-types.rs:81:5 + | +LL | const STR_U128_UNION: u128 = unsafe { Nonsense { stringy: "3" }.uint_128 }; + | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^----------------------------------^^^ + | | + | a raw memory access tried to access part of a pointer value as raw bytes + +error: this constant cannot be used + --> $DIR/const-pointer-values-in-various-types.rs:84:5 + | +LL | const STR_I8_UNION: i8 = unsafe { Nonsense { stringy: "3" }.int_8 }; + | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^-------------------------------^^^ + | | + | a raw memory access tried to access part of a pointer value as raw bytes + +error: this constant cannot be used + --> $DIR/const-pointer-values-in-various-types.rs:87:5 + | +LL | const STR_I16_UNION: i16 = unsafe { Nonsense { stringy: "3" }.int_16 }; + | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^--------------------------------^^^ + | | + | a raw memory access tried to access part of a pointer value as raw bytes + +error: this constant cannot be used + --> $DIR/const-pointer-values-in-various-types.rs:90:5 + | +LL | const STR_I32_UNION: i32 = unsafe { Nonsense { stringy: "3" }.int_32 }; + | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^--------------------------------^^^ + | | + | a raw memory access tried to access part of a pointer value as raw bytes + +error[E0080]: this constant likely exhibits undefined behavior + --> $DIR/const-pointer-values-in-various-types.rs:93:5 + | +LL | const STR_I64_UNION: i64 = unsafe { Nonsense { stringy: "3" }.int_64 }; + | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ type validation failed: encountered a pointer, but expected the type i64 + | + = 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: this constant cannot be used + --> $DIR/const-pointer-values-in-various-types.rs:96:5 + | +LL | const STR_I128_UNION: i128 = unsafe { Nonsense { stringy: "3" }.int_128 }; + | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^---------------------------------^^^ + | | + | a raw memory access tried to access part of a pointer value as raw bytes + +error: this constant cannot be used + --> $DIR/const-pointer-values-in-various-types.rs:99:5 + | +LL | const STR_F32_UNION: f32 = unsafe { Nonsense { stringy: "3" }.float_32 }; + | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^----------------------------------^^^ + | | + | a raw memory access tried to access part of a pointer value as raw bytes + +error[E0080]: this constant likely exhibits undefined behavior + --> $DIR/const-pointer-values-in-various-types.rs:102:5 + | +LL | const STR_F64_UNION: f64 = unsafe { Nonsense { stringy: "3" }.float_64 }; + | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ type validation failed: encountered a pointer, but expected the type f64 + | + = 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: this constant cannot be used + --> $DIR/const-pointer-values-in-various-types.rs:105:5 + | +LL | const STR_BOOL_UNION: bool = unsafe { Nonsense { stringy: "3" }.truthy_falsey }; + | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^---------------------------------------^^^ + | | + | a raw memory access tried to access part of a pointer value as raw bytes + +error: this constant cannot be used + --> $DIR/const-pointer-values-in-various-types.rs:108:5 + | +LL | const STR_CHAR_UNION: char = unsafe { Nonsense { stringy: "3" }.character }; + | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^-----------------------------------^^^ + | | + | a raw memory access tried to access part of a pointer value as raw bytes + +error: aborting due to 29 previous errors + +For more information about this error, try `rustc --explain E0080`. diff --git a/src/test/ui/consts/const-eval/ref_to_float_transmute.rs b/src/test/ui/consts/const-eval/ref_to_float_transmute.rs deleted file mode 100644 index 1758ac72b633b..0000000000000 --- a/src/test/ui/consts/const-eval/ref_to_float_transmute.rs +++ /dev/null @@ -1,30 +0,0 @@ -// Copyright 2017 The Rust Project Developers. See the COPYRIGHT -// file at the top-level directory of this distribution and at -// http://rust-lang.org/COPYRIGHT. -// -// Licensed under the Apache License, Version 2.0 or the MIT license -// , at your -// option. This file may not be copied, modified, or distributed -// except according to those terms. - -//compile-pass - -#![feature(const_fn_union)] - -fn main() {} - -static FOO: u32 = 42; - -union Foo { - f: Float, - r: &'static u32, -} - -#[cfg(target_pointer_width="64")] -type Float = f64; - -#[cfg(target_pointer_width="32")] -type Float = f32; - -static BAR: Float = unsafe { Foo { r: &FOO }.f }; diff --git a/src/test/ui/consts/const-eval/ub-ptr-in-usize.rs b/src/test/ui/consts/const-eval/ub-ptr-in-usize.rs deleted file mode 100644 index b405f766f9132..0000000000000 --- a/src/test/ui/consts/const-eval/ub-ptr-in-usize.rs +++ /dev/null @@ -1,22 +0,0 @@ -// Copyright 2018 The Rust Project Developers. See the COPYRIGHT -// file at the top-level directory of this distribution and at -// http://rust-lang.org/COPYRIGHT. -// -// Licensed under the Apache License, Version 2.0 or the MIT license -// , at your -// option. This file may not be copied, modified, or distributed -// except according to those terms. - -// compile-pass - -union Foo { - a: &'static u8, - b: usize, -} - -// a usize's value may be a pointer, that's fine -const PTR_AS_USIZE: usize = unsafe { Foo { a: &1 }.b}; - -fn main() { -}