compiletest/runtest/
codegen_units.rs

1use std::collections::HashSet;
2
3use super::{Emit, TestCx, WillExecute};
4use crate::errors;
5use crate::util::static_regex;
6
7impl TestCx<'_> {
8    pub(super) fn run_codegen_units_test(&self) {
9        assert!(self.revision.is_none(), "revisions not relevant here");
10
11        let proc_res = self.compile_test(WillExecute::No, Emit::None);
12
13        if !proc_res.status.success() {
14            self.fatal_proc_rec("compilation failed!", &proc_res);
15        }
16
17        self.check_no_compiler_crash(&proc_res, self.props.should_ice);
18
19        const PREFIX: &str = "MONO_ITEM ";
20        const CGU_MARKER: &str = "@@";
21
22        // Some MonoItems can contain {closure@/path/to/checkout/tests/codgen-units/test.rs}
23        // To prevent the current dir from leaking, we just replace the entire path to the test
24        // file with TEST_PATH.
25        let actual: Vec<MonoItem> = proc_res
26            .stdout
27            .lines()
28            .filter(|line| line.starts_with(PREFIX))
29            .map(|line| line.replace(&self.testpaths.file.as_str(), "TEST_PATH").to_string())
30            .map(|line| str_to_mono_item(&line, true))
31            .collect();
32
33        let expected: Vec<MonoItem> = errors::load_errors(&self.testpaths.file, None)
34            .iter()
35            .map(|e| str_to_mono_item(&e.msg[..], false))
36            .collect();
37
38        let mut missing = Vec::new();
39        let mut wrong_cgus = Vec::new();
40
41        for expected_item in &expected {
42            let actual_item_with_same_name = actual.iter().find(|ti| ti.name == expected_item.name);
43
44            if let Some(actual_item) = actual_item_with_same_name {
45                if !expected_item.codegen_units.is_empty() &&
46                   // Also check for codegen units
47                   expected_item.codegen_units != actual_item.codegen_units
48                {
49                    wrong_cgus.push((expected_item.clone(), actual_item.clone()));
50                }
51            } else {
52                missing.push(expected_item.string.clone());
53            }
54        }
55
56        let unexpected: Vec<_> = actual
57            .iter()
58            .filter(|acgu| !expected.iter().any(|ecgu| acgu.name == ecgu.name))
59            .map(|acgu| acgu.string.clone())
60            .collect();
61
62        if !missing.is_empty() {
63            missing.sort();
64
65            println!("\nThese items should have been contained but were not:\n");
66
67            for item in &missing {
68                println!("{}", item);
69            }
70
71            println!("\n");
72        }
73
74        if !unexpected.is_empty() {
75            let sorted = {
76                let mut sorted = unexpected.clone();
77                sorted.sort();
78                sorted
79            };
80
81            println!("\nThese items were contained but should not have been:\n");
82
83            for item in sorted {
84                println!("{}", item);
85            }
86
87            println!("\n");
88        }
89
90        if !wrong_cgus.is_empty() {
91            wrong_cgus.sort_by_key(|pair| pair.0.name.clone());
92            println!("\nThe following items were assigned to wrong codegen units:\n");
93
94            for &(ref expected_item, ref actual_item) in &wrong_cgus {
95                println!("{}", expected_item.name);
96                println!("  expected: {}", codegen_units_to_str(&expected_item.codegen_units));
97                println!("  actual:   {}", codegen_units_to_str(&actual_item.codegen_units));
98                println!();
99            }
100        }
101
102        if !(missing.is_empty() && unexpected.is_empty() && wrong_cgus.is_empty()) {
103            panic!();
104        }
105
106        #[derive(Clone, Eq, PartialEq)]
107        struct MonoItem {
108            name: String,
109            codegen_units: HashSet<String>,
110            string: String,
111        }
112
113        // [MONO_ITEM] name [@@ (cgu)+]
114        fn str_to_mono_item(s: &str, cgu_has_crate_disambiguator: bool) -> MonoItem {
115            let s = if s.starts_with(PREFIX) { (&s[PREFIX.len()..]).trim() } else { s.trim() };
116
117            let full_string = format!("{}{}", PREFIX, s);
118
119            let parts: Vec<&str> =
120                s.split(CGU_MARKER).map(str::trim).filter(|s| !s.is_empty()).collect();
121
122            let name = parts[0].trim();
123
124            let cgus = if parts.len() > 1 {
125                let cgus_str = parts[1];
126
127                cgus_str
128                    .split(' ')
129                    .map(str::trim)
130                    .filter(|s| !s.is_empty())
131                    .map(|s| {
132                        if cgu_has_crate_disambiguator {
133                            remove_crate_disambiguators_from_set_of_cgu_names(s)
134                        } else {
135                            s.to_string()
136                        }
137                    })
138                    .collect()
139            } else {
140                HashSet::new()
141            };
142
143            MonoItem { name: name.to_owned(), codegen_units: cgus, string: full_string }
144        }
145
146        fn codegen_units_to_str(cgus: &HashSet<String>) -> String {
147            let mut cgus: Vec<_> = cgus.iter().collect();
148            cgus.sort();
149
150            let mut string = String::new();
151            for cgu in cgus {
152                string.push_str(&cgu[..]);
153                string.push(' ');
154            }
155
156            string
157        }
158
159        // Given a cgu-name-prefix of the form <crate-name>.<crate-disambiguator> or
160        // the form <crate-name1>.<crate-disambiguator1>-in-<crate-name2>.<crate-disambiguator2>,
161        // remove all crate-disambiguators.
162        fn remove_crate_disambiguator_from_cgu(cgu: &str) -> String {
163            let Some(captures) =
164                static_regex!(r"^[^\.]+(?P<d1>\.[[:alnum:]]+)(-in-[^\.]+(?P<d2>\.[[:alnum:]]+))?")
165                    .captures(cgu)
166            else {
167                panic!("invalid cgu name encountered: {cgu}");
168            };
169
170            let mut new_name = cgu.to_owned();
171
172            if let Some(d2) = captures.name("d2") {
173                new_name.replace_range(d2.start()..d2.end(), "");
174            }
175
176            let d1 = captures.name("d1").unwrap();
177            new_name.replace_range(d1.start()..d1.end(), "");
178
179            new_name
180        }
181
182        // The name of merged CGUs is constructed as the names of the original
183        // CGUs joined with "--". This function splits such composite CGU names
184        // and handles each component individually.
185        fn remove_crate_disambiguators_from_set_of_cgu_names(cgus: &str) -> String {
186            cgus.split("--").map(remove_crate_disambiguator_from_cgu).collect::<Vec<_>>().join("--")
187        }
188    }
189}