diff options
| author | Alexander Regueiro <alexreg@me.com> | 2018-05-29 01:38:18 +0100 |
|---|---|---|
| committer | Alexander Regueiro <alexreg@me.com> | 2018-06-30 23:52:33 +0100 |
| commit | 349d53c2a90e7adc45b2c07bdb038cfb03669b37 (patch) | |
| tree | 79406fd168572f1a3a49f1fc5dfcab8fccf05d5b | |
| parent | 13931762e9832d215bb6ffcfbe4cbd126050b91c (diff) | |
| download | rust-349d53c2a90e7adc45b2c07bdb038cfb03669b37.tar.gz rust-349d53c2a90e7adc45b2c07bdb038cfb03669b37.zip | |
Added miri error for evaluating foreign statics.
Updated tests accordingly.
| -rw-r--r-- | src/librustc/ich/impls_ty.rs | 1 | ||||
| -rw-r--r-- | src/librustc/mir/interpret/error.rs | 3 | ||||
| -rw-r--r-- | src/librustc/ty/structural_impls.rs | 1 | ||||
| -rw-r--r-- | src/librustc_mir/interpret/const_eval.rs | 4 | ||||
| -rw-r--r-- | src/librustc_mir/interpret/memory.rs | 3 | ||||
| -rw-r--r-- | src/test/compile-fail/const-fn-not-safe-for-const.rs | 4 | ||||
| -rw-r--r-- | src/test/compile-fail/issue-14227.rs | 3 | ||||
| -rw-r--r-- | src/test/compile-fail/issue-17718-references.rs | 7 | ||||
| -rw-r--r-- | src/test/compile-fail/issue-28324.rs | 2 | ||||
| -rw-r--r-- | src/test/compile-fail/thread-local-in-ctfe.rs | 3 | ||||
| -rw-r--r-- | src/test/run-pass/issue-17450.rs (renamed from src/test/compile-fail/issue-17450.rs) | 5 | ||||
| -rw-r--r-- | src/test/run-pass/issue-17718-borrow-interior.rs | 10 | ||||
| -rw-r--r-- | src/test/run-pass/issue-34194.rs (renamed from src/test/compile-fail/issue-34194.rs) | 1 | ||||
| -rw-r--r-- | src/test/run-pass/issue-6991.rs (renamed from src/test/compile-fail/issue-6991.rs) | 3 |
14 files changed, 27 insertions, 23 deletions
diff --git a/src/librustc/ich/impls_ty.rs b/src/librustc/ich/impls_ty.rs index 8391cc6d9ba..5753557a102 100644 --- a/src/librustc/ich/impls_ty.rs +++ b/src/librustc/ich/impls_ty.rs @@ -521,6 +521,7 @@ for ::mir::interpret::EvalErrorKind<'gcx, O> { InvalidNullPointerUsage | ReadPointerAsBytes | ReadBytesAsPointer | + ReadForeignStatic | InvalidPointerMath | ReadUndefBytes | DeadLocal | diff --git a/src/librustc/mir/interpret/error.rs b/src/librustc/mir/interpret/error.rs index 86427bb2382..2363e2870ec 100644 --- a/src/librustc/mir/interpret/error.rs +++ b/src/librustc/mir/interpret/error.rs @@ -188,6 +188,7 @@ pub enum EvalErrorKind<'tcx, O> { InvalidNullPointerUsage, ReadPointerAsBytes, ReadBytesAsPointer, + ReadForeignStatic, InvalidPointerMath, ReadUndefBytes, DeadLocal, @@ -304,6 +305,8 @@ impl<'tcx, O> EvalErrorKind<'tcx, O> { "a raw memory access tried to access part of a pointer value as raw bytes", ReadBytesAsPointer => "a memory access tried to interpret some bytes as a pointer", + ReadForeignStatic => + "tried to read foreign (extern) static", InvalidPointerMath => "attempted to do invalid arithmetic on pointers that would leak base addresses, e.g. comparing pointers into different allocations", ReadUndefBytes => diff --git a/src/librustc/ty/structural_impls.rs b/src/librustc/ty/structural_impls.rs index a648dc6e7e7..c84999a7e59 100644 --- a/src/librustc/ty/structural_impls.rs +++ b/src/librustc/ty/structural_impls.rs @@ -506,6 +506,7 @@ impl<'a, 'tcx, O: Lift<'tcx>> Lift<'tcx> for interpret::EvalErrorKind<'a, O> { InvalidNullPointerUsage => InvalidNullPointerUsage, ReadPointerAsBytes => ReadPointerAsBytes, ReadBytesAsPointer => ReadBytesAsPointer, + ReadForeignStatic => ReadForeignStatic, InvalidPointerMath => InvalidPointerMath, ReadUndefBytes => ReadUndefBytes, DeadLocal => DeadLocal, diff --git a/src/librustc_mir/interpret/const_eval.rs b/src/librustc_mir/interpret/const_eval.rs index 35422b11bd7..749c0d04ae9 100644 --- a/src/librustc_mir/interpret/const_eval.rs +++ b/src/librustc_mir/interpret/const_eval.rs @@ -374,7 +374,7 @@ impl<'mir, 'tcx> super::Machine<'mir, 'tcx> for CompileTimeEvaluator { Ok(None) } else { Err( - ConstEvalError::NeedsRfc("Pointer arithmetic or comparison".to_string()).into(), + ConstEvalError::NeedsRfc("pointer arithmetic or comparison".to_string()).into(), ) } } @@ -404,7 +404,7 @@ impl<'mir, 'tcx> super::Machine<'mir, 'tcx> for CompileTimeEvaluator { _dest: Place, ) -> EvalResult<'tcx> { Err( - ConstEvalError::NeedsRfc("Heap allocations via `box` keyword".to_string()).into(), + ConstEvalError::NeedsRfc("heap allocations via `box` keyword".to_string()).into(), ) } diff --git a/src/librustc_mir/interpret/memory.rs b/src/librustc_mir/interpret/memory.rs index 9e5b6be3e91..daa30fb187c 100644 --- a/src/librustc_mir/interpret/memory.rs +++ b/src/librustc_mir/interpret/memory.rs @@ -279,6 +279,9 @@ impl<'a, 'mir, 'tcx, M: Machine<'mir, 'tcx>> Memory<'a, 'mir, 'tcx, M> { /// Allocation accessors impl<'a, 'mir, 'tcx, M: Machine<'mir, 'tcx>> Memory<'a, 'mir, 'tcx, M> { fn const_eval_static(&self, def_id: DefId) -> EvalResult<'tcx, &'tcx Allocation> { + if self.tcx.is_foreign_item(def_id) { + return err!(ReadForeignStatic); + } let instance = Instance::mono(self.tcx.tcx, def_id); let gid = GlobalId { instance, diff --git a/src/test/compile-fail/const-fn-not-safe-for-const.rs b/src/test/compile-fail/const-fn-not-safe-for-const.rs index d985bae1f24..341cc7bb491 100644 --- a/src/test/compile-fail/const-fn-not-safe-for-const.rs +++ b/src/test/compile-fail/const-fn-not-safe-for-const.rs @@ -29,7 +29,6 @@ static Y: u32 = 0; const fn get_Y() -> u32 { Y //~^ ERROR E0013 - //~| ERROR cannot refer to statics by value } const fn get_Y_addr() -> &'static u32 { @@ -49,5 +48,4 @@ const fn get() -> u32 { //~| ERROR let bindings in constant functions are unstable } -fn main() { -} +fn main() {} diff --git a/src/test/compile-fail/issue-14227.rs b/src/test/compile-fail/issue-14227.rs index d8f9f5543e4..1516e18a86f 100644 --- a/src/test/compile-fail/issue-14227.rs +++ b/src/test/compile-fail/issue-14227.rs @@ -13,6 +13,7 @@ extern { pub static symbol: (); } -static CRASH: () = symbol; //~ cannot refer to other statics by value +static CRASH: () = symbol; +//~^ ERROR constant evaluation error fn main() {} diff --git a/src/test/compile-fail/issue-17718-references.rs b/src/test/compile-fail/issue-17718-references.rs index 8e0df283cdb..586cfebcd16 100644 --- a/src/test/compile-fail/issue-17718-references.rs +++ b/src/test/compile-fail/issue-17718-references.rs @@ -22,14 +22,13 @@ static T4: &'static usize = &S; const T5: usize = C; const T6: usize = S; //~ ERROR: constants cannot refer to statics -//~^ cannot refer to statics static T7: usize = C; -static T8: usize = S; //~ ERROR: cannot refer to other statics by value +static T8: usize = S; const T9: Struct = Struct { a: C }; -const T10: Struct = Struct { a: S }; //~ ERROR: cannot refer to statics by value +const T10: Struct = Struct { a: S }; //~^ ERROR: constants cannot refer to statics static T11: Struct = Struct { a: C }; -static T12: Struct = Struct { a: S }; //~ ERROR: cannot refer to other statics by value +static T12: Struct = Struct { a: S }; fn main() {} diff --git a/src/test/compile-fail/issue-28324.rs b/src/test/compile-fail/issue-28324.rs index 3c4d6b42b50..4179048b461 100644 --- a/src/test/compile-fail/issue-28324.rs +++ b/src/test/compile-fail/issue-28324.rs @@ -15,6 +15,6 @@ extern { } pub static BAZ: u32 = *&error_message_count; -//~^ ERROR cannot refer to other statics by value +//~^ ERROR constant evaluation error fn main() {} diff --git a/src/test/compile-fail/thread-local-in-ctfe.rs b/src/test/compile-fail/thread-local-in-ctfe.rs index dc220bd1cc9..62e26f28b06 100644 --- a/src/test/compile-fail/thread-local-in-ctfe.rs +++ b/src/test/compile-fail/thread-local-in-ctfe.rs @@ -15,14 +15,12 @@ static A: u32 = 1; static B: u32 = A; //~^ ERROR thread-local statics cannot be accessed at compile-time -//~| ERROR cannot refer to other statics by value static C: &u32 = &A; //~^ ERROR thread-local statics cannot be accessed at compile-time const D: u32 = A; //~^ ERROR thread-local statics cannot be accessed at compile-time -//~| ERROR cannot refer to statics by value const E: &u32 = &A; //~^ ERROR thread-local statics cannot be accessed at compile-time @@ -30,7 +28,6 @@ const E: &u32 = &A; const fn f() -> u32 { A //~^ ERROR thread-local statics cannot be accessed at compile-time - //~| ERROR cannot refer to statics by value } fn main() {} diff --git a/src/test/compile-fail/issue-17450.rs b/src/test/run-pass/issue-17450.rs index cde1bbbe492..242d8c20cd7 100644 --- a/src/test/compile-fail/issue-17450.rs +++ b/src/test/run-pass/issue-17450.rs @@ -11,9 +11,6 @@ #![allow(dead_code, warnings)] static mut x: isize = 3; -static mut y: isize = unsafe { - x -//~^ ERROR cannot refer to other statics by value, use the address-of operator or a constant instea -}; +static mut y: isize = unsafe { x }; fn main() {} diff --git a/src/test/run-pass/issue-17718-borrow-interior.rs b/src/test/run-pass/issue-17718-borrow-interior.rs index 77df168c257..cafc0375257 100644 --- a/src/test/run-pass/issue-17718-borrow-interior.rs +++ b/src/test/run-pass/issue-17718-borrow-interior.rs @@ -10,7 +10,7 @@ struct S { a: usize } -static A: S = S { a: 3 }; +static A: S = S { a: 3 }; static B: &'static usize = &A.a; static C: &'static usize = &(A.a); @@ -18,4 +18,10 @@ static D: [usize; 1] = [1]; static E: usize = D[0]; static F: &'static usize = &D[0]; -fn main() {} +fn main() { + assert_eq!(*B, A.a); + assert_eq!(*B, A.a); + + assert_eq!(E, D[0]); + assert_eq!(*F, D[0]); +} diff --git a/src/test/compile-fail/issue-34194.rs b/src/test/run-pass/issue-34194.rs index dd607ebad62..e1aef899619 100644 --- a/src/test/compile-fail/issue-34194.rs +++ b/src/test/run-pass/issue-34194.rs @@ -16,6 +16,5 @@ struct A { static B: &'static A = &A { a: &() }; static C: &'static A = &B; -//~^ ERROR cannot refer to other statics by value fn main() {} diff --git a/src/test/compile-fail/issue-6991.rs b/src/test/run-pass/issue-6991.rs index 0cc5898adfc..32a9a055d49 100644 --- a/src/test/compile-fail/issue-6991.rs +++ b/src/test/run-pass/issue-6991.rs @@ -10,6 +10,5 @@ static x: &'static usize = &1; static y: usize = *x; -//~^ ERROR cannot refer to other statics by value, -// use the address-of operator or a constant instead + fn main() {} |
