Skip to content

Commit 3d6c260

Browse files
committed
Add a test that tries to modify static memory at compile-time
1 parent 8adc69a commit 3d6c260

File tree

2 files changed

+55
-0
lines changed

2 files changed

+55
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
// Copyright 2018 The Rust Project Developers. See the COPYRIGHT
2+
// file at the top-level directory of this distribution and at
3+
// http://rust-lang.org/COPYRIGHT.
4+
//
5+
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
6+
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
7+
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
8+
// option. This file may not be copied, modified, or distributed
9+
// except according to those terms.
10+
11+
// New test for #53818: modifying static memory at compile-time is not allowed.
12+
// The test should never succeed.
13+
14+
#![feature(const_raw_ptr_deref)]
15+
#![feature(const_let)]
16+
17+
use std::cell::UnsafeCell;
18+
19+
struct Foo(UnsafeCell<u32>);
20+
21+
unsafe impl Send for Foo {}
22+
unsafe impl Sync for Foo {}
23+
24+
static FOO: Foo = Foo(UnsafeCell::new(42));
25+
26+
static BAR: () = unsafe {
27+
*FOO.0.get() = 5;
28+
//~^ ERROR calls in statics are limited to constant functions, tuple structs and tuple variants
29+
30+
// This error is caused by a separate bug that the feature gate error is reported
31+
// even though the feature gate "const_let" is active.
32+
//~| statements in statics are unstable (see issue #48821)
33+
};
34+
35+
fn main() {
36+
println!("{}", unsafe { *FOO.0.get() });
37+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
error[E0015]: calls in statics are limited to constant functions, tuple structs and tuple variants
2+
--> $DIR/mod-static-with-const-fn.rs:27:6
3+
|
4+
LL | *FOO.0.get() = 5;
5+
| ^^^^^^^^^^^
6+
7+
error[E0658]: statements in statics are unstable (see issue #48821)
8+
--> $DIR/mod-static-with-const-fn.rs:27:5
9+
|
10+
LL | *FOO.0.get() = 5;
11+
| ^^^^^^^^^^^^^^^^
12+
|
13+
= help: add #![feature(const_let)] to the crate attributes to enable
14+
15+
error: aborting due to 2 previous errors
16+
17+
Some errors occurred: E0015, E0658.
18+
For more information about an error, try `rustc --explain E0015`.

0 commit comments

Comments
 (0)