-
Notifications
You must be signed in to change notification settings - Fork 12.6k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
9 changed files
with
143 additions
and
22 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
35 changes: 35 additions & 0 deletions
35
src/test/ui/consts/const-eval/assign-to-static-within-other-static.rs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
// 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 <LICENSE-APACHE or | ||
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license | ||
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your | ||
// option. This file may not be copied, modified, or distributed | ||
// except according to those terms. | ||
|
||
// New test for #53818: modifying static memory at compile-time is not allowed. | ||
// The test should never compile successfully | ||
|
||
#![feature(const_raw_ptr_deref)] | ||
#![feature(const_let)] | ||
|
||
use std::cell::UnsafeCell; | ||
|
||
struct Foo(UnsafeCell<u32>); | ||
|
||
unsafe impl Send for Foo {} | ||
unsafe impl Sync for Foo {} | ||
|
||
static FOO: Foo = Foo(UnsafeCell::new(42)); | ||
|
||
static BAR: () = unsafe { | ||
*FOO.0.get() = 5; | ||
}; | ||
|
||
static mut FOO2: u32 = 42; | ||
static BOO2: () = unsafe { | ||
FOO2 = 5; | ||
}; | ||
|
||
fn main() {} |
8 changes: 8 additions & 0 deletions
8
src/test/ui/consts/const-eval/assign-to-static-within-other-static.stderr
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
error: cannot mutate statics in the initializer of another static | ||
--> $DIR/assign-to-static-within-other-static.rs:32:5 | ||
| | ||
LL | FOO2 = 5; | ||
| ^^^^^^^^ | ||
|
||
error: aborting due to previous error | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
15 changes: 3 additions & 12 deletions
15
src/test/ui/consts/const-eval/mod-static-with-const-fn.stderr
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,18 +1,9 @@ | ||
error[E0658]: statements in statics are unstable (see issue #48821) | ||
--> $DIR/mod-static-with-const-fn.rs:29:5 | ||
| | ||
LL | *FOO.0.get() = 5; | ||
| ^^^^^^^^^^^^^^^^ | ||
| | ||
= help: add #![feature(const_let)] to the crate attributes to enable | ||
|
||
error[E0015]: calls in statics are limited to constant functions, tuple structs and tuple variants | ||
--> $DIR/mod-static-with-const-fn.rs:34:5 | ||
--> $DIR/mod-static-with-const-fn.rs:31:5 | ||
| | ||
LL | foo(); | ||
| ^^^^^ | ||
|
||
error: aborting due to 2 previous errors | ||
error: aborting due to previous error | ||
|
||
Some errors occurred: E0015, E0658. | ||
For more information about an error, try `rustc --explain E0015`. | ||
For more information about this error, try `rustc --explain E0015`. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
// compile-pass | ||
|
||
#![feature(const_let)] | ||
|
||
struct S(i32); | ||
|
||
const A: () = { | ||
let mut s = S(0); | ||
s.0 = 1; | ||
}; | ||
|
||
fn main() {} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
// compile-pass | ||
|
||
#![feature(const_let)] | ||
#![feature(const_fn)] | ||
|
||
pub struct AA { | ||
pub data: [u8; 10], | ||
} | ||
|
||
impl AA { | ||
pub const fn new() -> Self { | ||
let mut res: AA = AA { data: [0; 10] }; | ||
res.data[0] = 5; | ||
res | ||
} | ||
} | ||
|
||
static mut BB: AA = AA::new(); | ||
|
||
fn main() { | ||
let ptr = unsafe { &mut BB }; | ||
for a in ptr.data.iter() { | ||
println!("{}", a); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
#![feature(const_let)] | ||
#![feature(const_fn)] | ||
|
||
struct S { | ||
state: u32, | ||
} | ||
|
||
impl S { | ||
const fn foo(&mut self, x: u32) { | ||
self.state = x; | ||
} | ||
} | ||
|
||
const FOO: S = { | ||
let mut s = S { state: 42 }; | ||
s.foo(3); //~ ERROR references in constants may only refer to immutable values | ||
s | ||
}; | ||
|
||
fn main() { | ||
assert_eq!(FOO.state, 3); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
error[E0017]: references in constants may only refer to immutable values | ||
--> $DIR/const_let_assign3.rs:16:5 | ||
| | ||
LL | s.foo(3); //~ ERROR references in constants may only refer to immutable values | ||
| ^ constants require immutable values | ||
|
||
error: aborting due to previous error | ||
|
||
For more information about this error, try `rustc --explain E0017`. |