]> git.proxmox.com Git - rustc.git/blame - tests/ui/cleanup-rvalue-for-scope.rs
New upstream version 1.76.0+dfsg1
[rustc.git] / tests / ui / cleanup-rvalue-for-scope.rs
CommitLineData
416331ca
XL
1// run-pass
2
0bf4aa26
XL
3#![allow(non_snake_case)]
4#![allow(dead_code)]
5#![allow(unused_variables)]
1a4d82fc
JJ
6// Test that the lifetime of rvalues in for loops is extended
7// to the for loop itself.
1a4d82fc
JJ
8static mut FLAGS: u64 = 0;
9
10struct Box<T> { f: T }
11struct AddFlags { bits: u64 }
12
13fn AddFlags(bits: u64) -> AddFlags {
14 AddFlags { bits: bits }
15}
16
17fn arg(exp: u64, _x: &AddFlags) {
18 check_flags(exp);
19}
20
21fn pass<T>(v: T) -> T {
22 v
23}
24
25fn check_flags(exp: u64) {
26 unsafe {
27 let x = FLAGS;
28 FLAGS = 0;
29 println!("flags {}, expected {}", x, exp);
30 assert_eq!(x, exp);
31 }
32}
33
34impl AddFlags {
35 fn check_flags(&self, exp: u64) -> &AddFlags {
36 check_flags(exp);
37 self
38 }
39
40 fn bits(&self) -> u64 {
41 self.bits
42 }
43}
44
45impl Drop for AddFlags {
46 fn drop(&mut self) {
47 unsafe {
48 FLAGS = FLAGS + self.bits;
49 }
50 }
51}
52
53pub fn main() {
54 // The array containing [AddFlags] should not be dropped until
55 // after the for loop:
85aaf69f 56 for x in &[AddFlags(1)] {
1a4d82fc
JJ
57 check_flags(0);
58 }
59 check_flags(1);
60}