rust/src/test/ui/consts/const-mut-refs/const_mut_refs.rs

38 lines
682 B
Rust
Raw Normal View History

#![feature(const_mut_refs)]
struct Foo {
x: usize
}
2019-11-23 20:00:14 +00:00
const fn foo() -> Foo {
Foo { x: 0 }
}
impl Foo {
const fn bar(&mut self) -> usize {
self.x = 1;
self.x
}
}
const fn baz(foo: &mut Foo) -> usize {
2019-11-23 00:59:34 +00:00
let x = &mut foo.x;
2019-11-23 20:00:14 +00:00
*x = 2;
2019-11-23 00:59:34 +00:00
*x
}
2019-11-23 20:00:14 +00:00
const fn bazz(foo: &mut Foo) -> usize {
foo.x = 3;
foo.x
}
fn main() {
2019-11-23 20:00:14 +00:00
let _: [(); foo().bar()] = [(); 1];
//~^ ERROR mutable references are not allowed in constants
2019-11-23 20:00:14 +00:00
let _: [(); baz(&mut foo())] = [(); 2];
//~^ ERROR mutable references are not allowed in constants
2019-11-23 20:00:14 +00:00
let _: [(); bazz(&mut foo())] = [(); 3];
//~^ ERROR mutable references are not allowed in constants
}