Skip to content

Commit 57484de

Browse files
author
Alexander Regueiro
committed
Added miri error for evaluating foreign statics.
Updated tests accordingly.
1 parent 44a5588 commit 57484de

File tree

14 files changed

+27
-23
lines changed

14 files changed

+27
-23
lines changed

src/librustc/ich/impls_ty.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -574,6 +574,7 @@ for ::mir::interpret::EvalErrorKind<'gcx, O> {
574574
InvalidNullPointerUsage |
575575
ReadPointerAsBytes |
576576
ReadBytesAsPointer |
577+
ReadForeignStatic |
577578
InvalidPointerMath |
578579
ReadUndefBytes |
579580
DeadLocal |

src/librustc/mir/interpret/error.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,7 @@ pub enum EvalErrorKind<'tcx, O> {
8383
InvalidNullPointerUsage,
8484
ReadPointerAsBytes,
8585
ReadBytesAsPointer,
86+
ReadForeignStatic,
8687
InvalidPointerMath,
8788
ReadUndefBytes,
8889
DeadLocal,
@@ -196,6 +197,8 @@ impl<'tcx, O> EvalErrorKind<'tcx, O> {
196197
"a raw memory access tried to access part of a pointer value as raw bytes",
197198
ReadBytesAsPointer =>
198199
"a memory access tried to interpret some bytes as a pointer",
200+
ReadForeignStatic =>
201+
"tried to read foreign (extern) static",
199202
InvalidPointerMath =>
200203
"attempted to do invalid arithmetic on pointers that would leak base addresses, e.g. comparing pointers into different allocations",
201204
ReadUndefBytes =>

src/librustc/ty/structural_impls.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -506,6 +506,7 @@ impl<'a, 'tcx, O: Lift<'tcx>> Lift<'tcx> for interpret::EvalErrorKind<'a, O> {
506506
InvalidNullPointerUsage => InvalidNullPointerUsage,
507507
ReadPointerAsBytes => ReadPointerAsBytes,
508508
ReadBytesAsPointer => ReadBytesAsPointer,
509+
ReadForeignStatic => ReadForeignStatic,
509510
InvalidPointerMath => InvalidPointerMath,
510511
ReadUndefBytes => ReadUndefBytes,
511512
DeadLocal => DeadLocal,

src/librustc_mir/interpret/const_eval.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -375,7 +375,7 @@ impl<'mir, 'tcx> super::Machine<'mir, 'tcx> for CompileTimeEvaluator {
375375
Ok(None)
376376
} else {
377377
Err(
378-
ConstEvalError::NeedsRfc("Pointer arithmetic or comparison".to_string()).into(),
378+
ConstEvalError::NeedsRfc("pointer arithmetic or comparison".to_string()).into(),
379379
)
380380
}
381381
}
@@ -405,7 +405,7 @@ impl<'mir, 'tcx> super::Machine<'mir, 'tcx> for CompileTimeEvaluator {
405405
_dest: Place,
406406
) -> EvalResult<'tcx> {
407407
Err(
408-
ConstEvalError::NeedsRfc("Heap allocations via `box` keyword".to_string()).into(),
408+
ConstEvalError::NeedsRfc("heap allocations via `box` keyword".to_string()).into(),
409409
)
410410
}
411411

src/librustc_mir/interpret/memory.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -280,6 +280,9 @@ impl<'a, 'mir, 'tcx, M: Machine<'mir, 'tcx>> Memory<'a, 'mir, 'tcx, M> {
280280
/// Allocation accessors
281281
impl<'a, 'mir, 'tcx, M: Machine<'mir, 'tcx>> Memory<'a, 'mir, 'tcx, M> {
282282
fn const_eval_static(&self, def_id: DefId) -> EvalResult<'tcx, &'tcx Allocation> {
283+
if self.tcx.is_foreign_item(def_id) {
284+
return err!(ReadForeignStatic);
285+
}
283286
let instance = Instance::mono(self.tcx.tcx, def_id);
284287
let gid = GlobalId {
285288
instance,

src/test/compile-fail/const-fn-not-safe-for-const.rs

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,6 @@ static Y: u32 = 0;
2929
const fn get_Y() -> u32 {
3030
Y
3131
//~^ ERROR E0013
32-
//~| ERROR cannot refer to statics by value
3332
}
3433

3534
const fn get_Y_addr() -> &'static u32 {
@@ -49,5 +48,4 @@ const fn get() -> u32 {
4948
//~| ERROR let bindings in constant functions are unstable
5049
}
5150

52-
fn main() {
53-
}
51+
fn main() {}

src/test/compile-fail/issue-14227.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
extern {
1414
pub static symbol: ();
1515
}
16-
static CRASH: () = symbol; //~ cannot refer to other statics by value
16+
static CRASH: () = symbol;
17+
//~^ ERROR constant evaluation error
1718

1819
fn main() {}

src/test/compile-fail/issue-17718-references.rs

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -22,14 +22,13 @@ static T4: &'static usize = &S;
2222

2323
const T5: usize = C;
2424
const T6: usize = S; //~ ERROR: constants cannot refer to statics
25-
//~^ cannot refer to statics
2625
static T7: usize = C;
27-
static T8: usize = S; //~ ERROR: cannot refer to other statics by value
26+
static T8: usize = S;
2827

2928
const T9: Struct = Struct { a: C };
30-
const T10: Struct = Struct { a: S }; //~ ERROR: cannot refer to statics by value
29+
const T10: Struct = Struct { a: S };
3130
//~^ ERROR: constants cannot refer to statics
3231
static T11: Struct = Struct { a: C };
33-
static T12: Struct = Struct { a: S }; //~ ERROR: cannot refer to other statics by value
32+
static T12: Struct = Struct { a: S };
3433

3534
fn main() {}

src/test/compile-fail/issue-28324.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,6 @@ extern {
1515
}
1616

1717
pub static BAZ: u32 = *&error_message_count;
18-
//~^ ERROR cannot refer to other statics by value
18+
//~^ ERROR constant evaluation error
1919

2020
fn main() {}

src/test/compile-fail/thread-local-in-ctfe.rs

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,22 +15,19 @@ static A: u32 = 1;
1515

1616
static B: u32 = A;
1717
//~^ ERROR thread-local statics cannot be accessed at compile-time
18-
//~| ERROR cannot refer to other statics by value
1918

2019
static C: &u32 = &A;
2120
//~^ ERROR thread-local statics cannot be accessed at compile-time
2221

2322
const D: u32 = A;
2423
//~^ ERROR thread-local statics cannot be accessed at compile-time
25-
//~| ERROR cannot refer to statics by value
2624

2725
const E: &u32 = &A;
2826
//~^ ERROR thread-local statics cannot be accessed at compile-time
2927

3028
const fn f() -> u32 {
3129
A
3230
//~^ ERROR thread-local statics cannot be accessed at compile-time
33-
//~| ERROR cannot refer to statics by value
3431
}
3532

3633
fn main() {}

src/test/compile-fail/issue-17450.rs renamed to src/test/run-pass/issue-17450.rs

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -11,9 +11,6 @@
1111
#![allow(dead_code, warnings)]
1212

1313
static mut x: isize = 3;
14-
static mut y: isize = unsafe {
15-
x
16-
//~^ ERROR cannot refer to other statics by value, use the address-of operator or a constant instea
17-
};
14+
static mut y: isize = unsafe { x };
1815

1916
fn main() {}

src/test/run-pass/issue-17718-borrow-interior.rs

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,12 +10,18 @@
1010

1111
struct S { a: usize }
1212

13-
static A: S = S { a: 3 };
13+
static A: S = S { a: 3 };
1414
static B: &'static usize = &A.a;
1515
static C: &'static usize = &(A.a);
1616

1717
static D: [usize; 1] = [1];
1818
static E: usize = D[0];
1919
static F: &'static usize = &D[0];
2020

21-
fn main() {}
21+
fn main() {
22+
assert_eq!(*B, A.a);
23+
assert_eq!(*B, A.a);
24+
25+
assert_eq!(E, D[0]);
26+
assert_eq!(*F, D[0]);
27+
}

src/test/compile-fail/issue-34194.rs renamed to src/test/run-pass/issue-34194.rs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,5 @@ struct A {
1616

1717
static B: &'static A = &A { a: &() };
1818
static C: &'static A = &B;
19-
//~^ ERROR cannot refer to other statics by value
2019

2120
fn main() {}

src/test/compile-fail/issue-6991.rs renamed to src/test/run-pass/issue-6991.rs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,5 @@
1010

1111
static x: &'static usize = &1;
1212
static y: usize = *x;
13-
//~^ ERROR cannot refer to other statics by value,
14-
// use the address-of operator or a constant instead
13+
1514
fn main() {}

0 commit comments

Comments
 (0)