rust/tests/ui/statics/static-mut-xc.rs

Ignoring revisions in .git-blame-ignore-revs. Click here to bypass and see the normal blame view.

41 lines
1.0 KiB
Rust
Raw Normal View History

//@ run-pass
#![allow(non_upper_case_globals)]
2013-06-22 01:46:34 +00:00
// Constants (static variables) can be used to match in patterns, but mutable
// statics cannot. This ensures that there's some form of error if this is
// attempted.
//@ aux-build:static_mut_xc.rs
extern crate static_mut_xc;
2013-06-22 01:46:34 +00:00
unsafe fn static_bound(_: &'static isize) {}
2013-06-22 01:46:34 +00:00
fn static_bound_set(a: &'static mut isize) {
2013-06-22 01:46:34 +00:00
*a = 3;
}
unsafe fn run() {
assert_eq!(static_mut_xc::a, 3);
2013-06-22 01:46:34 +00:00
static_mut_xc::a = 4;
assert_eq!(static_mut_xc::a, 4);
2013-06-22 01:46:34 +00:00
static_mut_xc::a += 1;
assert_eq!(static_mut_xc::a, 5);
2013-06-22 01:46:34 +00:00
static_mut_xc::a *= 3;
assert_eq!(static_mut_xc::a, 15);
2013-06-22 01:46:34 +00:00
static_mut_xc::a = -3;
assert_eq!(static_mut_xc::a, -3);
2013-06-22 01:46:34 +00:00
static_bound(&static_mut_xc::a);
//~^ WARN shared reference to mutable static is discouraged [static_mut_refs]
2013-06-22 01:46:34 +00:00
static_bound_set(&mut static_mut_xc::a);
//~^ WARN mutable reference to mutable static is discouraged [static_mut_refs]
2013-06-22 01:46:34 +00:00
}
2013-06-24 14:42:22 +00:00
pub fn main() {
2013-06-22 01:46:34 +00:00
unsafe { run() }
}
pub mod inner {
pub static mut a: isize = 4;
2013-06-22 01:46:34 +00:00
}