Files
rust/tests/ui/codegen/sret-aliasing-rules.rs

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

29 lines
646 B
Rust
Raw Normal View History

2025-07-01 02:46:47 +05:00
//! Check that functions with sret results don't violate aliasing rules.
//!
//! When `foo = func(&mut foo)` is called, the compiler must avoid creating
//! two mutable references to the same variable simultaneously (one for the
//! parameter and one for the hidden sret out-pointer).
//!
//! Regression test for <https://github.com/rust-lang/rust/pull/18250>.
//@ run-pass
2015-03-30 09:38:27 -04:00
#[derive(Copy, Clone)]
pub struct Foo {
f1: isize,
_f2: isize,
}
#[inline(never)]
pub fn foo(f: &mut Foo) -> Foo {
let ret = *f;
f.f1 = 0;
ret
}
pub fn main() {
2025-07-01 02:46:47 +05:00
let mut f = Foo { f1: 8, _f2: 9 };
f = foo(&mut f);
assert_eq!(f.f1, 8);
}