File: tail-expr-drop-order.rs

package info (click to toggle)
rustc 1.85.0%2Bdfsg3-1
  • links: PTS, VCS
  • area: main
  • in suites: experimental, sid, trixie
  • size: 893,396 kB
  • sloc: xml: 158,127; python: 35,830; javascript: 19,497; cpp: 19,002; sh: 17,245; ansic: 13,127; asm: 4,376; makefile: 1,051; perl: 29; lisp: 29; ruby: 19; sql: 11
file content (107 lines) | stat: -rw-r--r-- 2,483 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
//@ aux-build:edition-2021-macros.rs
//@ aux-build:edition-2024-macros.rs
//@ compile-flags: -Z validate-mir
//@ edition: 2024
//@ run-pass

#![allow(unused_imports)]
#![allow(dead_code)]
#![allow(unused_variables)]

#[macro_use]
extern crate edition_2021_macros;
#[macro_use]
extern crate edition_2024_macros;
use std::cell::RefCell;
use std::convert::TryInto;

#[derive(Default)]
struct DropOrderCollector(RefCell<Vec<u32>>);

struct LoudDrop<'a>(&'a DropOrderCollector, u32);

impl Drop for LoudDrop<'_> {
    fn drop(&mut self) {
        println!("{}", self.1);
        self.0.0.borrow_mut().push(self.1);
    }
}

impl DropOrderCollector {
    fn option_loud_drop(&self, n: u32) -> Option<LoudDrop> {
        Some(LoudDrop(self, n))
    }

    fn loud_drop(&self, n: u32) -> LoudDrop {
        LoudDrop(self, n)
    }

    fn assert_sorted(&self, expected: usize) {
        let result = self.0.borrow();
        assert_eq!(result.len(), expected);
        for i in 1..result.len() {
            assert!(
                result[i - 1] < result[i],
                "inversion at {} ({} followed by {})",
                i - 1,
                result[i - 1],
                result[i]
            );
        }
    }
}

fn edition_2021_around_2021() {
    let c = DropOrderCollector::default();
    let _ = edition_2021_block! {
        let a = c.loud_drop(1);
        edition_2021_block! {
            let b = c.loud_drop(0);
            c.loud_drop(2).1
        }
    };
    c.assert_sorted(3);
}

fn edition_2021_around_2024() {
    let c = DropOrderCollector::default();
    let _ = edition_2021_block! {
        let a = c.loud_drop(2);
        edition_2024_block! {
            let b = c.loud_drop(1);
            c.loud_drop(0).1
        }
    };
    c.assert_sorted(3);
}

fn edition_2024_around_2021() {
    let c = DropOrderCollector::default();
    let _ = edition_2024_block! {
        let a = c.loud_drop(2);
        edition_2021_block! {
            let b = c.loud_drop(0);
            c.loud_drop(1).1
        }
    };
    c.assert_sorted(3);
}

fn edition_2024_around_2024() {
    let c = DropOrderCollector::default();
    let _ = edition_2024_block! {
        let a = c.loud_drop(2);
        edition_2024_block! {
            let b = c.loud_drop(1);
            c.loud_drop(0).1
        }
    };
    c.assert_sorted(3);
}

fn main() {
    edition_2021_around_2021();
    edition_2021_around_2024();
    edition_2024_around_2021();
    edition_2024_around_2024();
}