]> git.lizzy.rs Git - rust.git/blob - src/tools/tidy/src/features.rs
Tidy: allow feature-gate tests to be ui tests
[rust.git] / src / tools / tidy / src / features.rs
1 // Copyright 2015 The Rust Project Developers. See the COPYRIGHT
2 // file at the top-level directory of this distribution and at
3 // http://rust-lang.org/COPYRIGHT.
4 //
5 // Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
6 // http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
7 // <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
8 // option. This file may not be copied, modified, or distributed
9 // except according to those terms.
10
11 //! Tidy check to ensure that unstable features are all in order
12 //!
13 //! This check will ensure properties like:
14 //!
15 //! * All stability attributes look reasonably well formed
16 //! * The set of library features is disjoint from the set of language features
17 //! * Library features have at most one stability level
18 //! * Library features have at most one `since` value
19 //! * All unstable lang features have tests to ensure they are actually unstable
20
21 use std::collections::HashMap;
22 use std::fmt;
23 use std::fs::File;
24 use std::io::prelude::*;
25 use std::path::Path;
26
27 #[derive(Debug, PartialEq, Clone)]
28 pub enum Status {
29     Stable,
30     Removed,
31     Unstable,
32 }
33
34 impl fmt::Display for Status {
35     fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
36         let as_str = match *self {
37             Status::Stable => "stable",
38             Status::Unstable => "unstable",
39             Status::Removed => "removed",
40         };
41         fmt::Display::fmt(as_str, f)
42     }
43 }
44
45 #[derive(Debug, Clone)]
46 pub struct Feature {
47     pub level: Status,
48     pub since: String,
49     pub has_gate_test: bool,
50     pub tracking_issue: Option<u32>,
51 }
52
53 impl Feature {
54     fn check_match(&self, other: &Feature)-> Result<(), Vec<&'static str>> {
55         let mut mismatches = Vec::new();
56         if self.level != other.level {
57             mismatches.push("stability level");
58         }
59         if self.level == Status::Stable || other.level == Status::Stable {
60             // As long as a feature is unstable, the since field tracks
61             // when the given part of the feature has been implemented.
62             // Mismatches are tolerable as features evolve and functionality
63             // gets added.
64             // Once a feature is stable, the since field tracks the first version
65             // it was part of the stable distribution, and mismatches are disallowed.
66             if self.since != other.since {
67                 mismatches.push("since");
68             }
69         }
70         if self.tracking_issue != other.tracking_issue {
71             mismatches.push("tracking issue");
72         }
73         if mismatches.is_empty() {
74             Ok(())
75         } else {
76             Err(mismatches)
77         }
78     }
79 }
80
81 pub type Features = HashMap<String, Feature>;
82
83 pub fn check(path: &Path, bad: &mut bool, quiet: bool) {
84     let mut features = collect_lang_features(path);
85     assert!(!features.is_empty());
86
87     let lib_features = get_and_check_lib_features(path, bad, &features);
88     assert!(!lib_features.is_empty());
89
90     let mut contents = String::new();
91
92     super::walk_many(&[&path.join("test/ui-fulldeps"),
93                        &path.join("test/ui"),
94                        &path.join("test/compile-fail"),
95                        &path.join("test/compile-fail-fulldeps"),
96                        &path.join("test/parse-fail"),],
97                      &mut |path| super::filter_dirs(path),
98                      &mut |file| {
99         let filename = file.file_name().unwrap().to_string_lossy();
100         if !filename.ends_with(".rs") || filename == "features.rs" ||
101            filename == "diagnostic_list.rs" {
102             return;
103         }
104
105         let filen_underscore = filename.replace("-","_").replace(".rs","");
106         let filename_is_gate_test = test_filen_gate(&filen_underscore, &mut features);
107
108         contents.truncate(0);
109         t!(t!(File::open(&file), &file).read_to_string(&mut contents));
110
111         for (i, line) in contents.lines().enumerate() {
112             let mut err = |msg: &str| {
113                 tidy_error!(bad, "{}:{}: {}", file.display(), i + 1, msg);
114             };
115
116             let gate_test_str = "gate-test-";
117
118             if !line.contains(gate_test_str) {
119                 continue;
120             }
121
122             let feature_name = match line.find(gate_test_str) {
123                 Some(i) => {
124                     &line[i+gate_test_str.len()..line[i+1..].find(' ').unwrap_or(line.len())]
125                 },
126                 None => continue,
127             };
128             match features.get_mut(feature_name) {
129                 Some(f) => {
130                     if filename_is_gate_test {
131                         err(&format!("The file is already marked as gate test \
132                                       through its name, no need for a \
133                                       'gate-test-{}' comment",
134                                      feature_name));
135                     }
136                     f.has_gate_test = true;
137                 }
138                 None => {
139                     err(&format!("gate-test test found referencing a nonexistent feature '{}'",
140                                  feature_name));
141                 }
142             }
143         }
144     });
145
146     // Only check the number of lang features.
147     // Obligatory testing for library features is dumb.
148     let gate_untested = features.iter()
149                                 .filter(|&(_, f)| f.level == Status::Unstable)
150                                 .filter(|&(_, f)| !f.has_gate_test)
151                                 .collect::<Vec<_>>();
152
153     for &(name, _) in gate_untested.iter() {
154         println!("Expected a gate test for the feature '{}'.", name);
155         println!("Hint: create a failing test file named 'feature-gate-{}.rs'\
156                 \n      in the 'ui' test suite, with its failures due to\
157                 \n      missing usage of #![feature({})].", name, name);
158         println!("Hint: If you already have such a test and don't want to rename it,\
159                 \n      you can also add a // gate-test-{} line to the test file.",
160                  name);
161     }
162
163     if gate_untested.len() > 0 {
164         tidy_error!(bad, "Found {} features without a gate test.", gate_untested.len());
165     }
166
167     if *bad {
168         return;
169     }
170     if quiet {
171         println!("* {} features", features.len());
172         return;
173     }
174
175     let mut lines = Vec::new();
176     for (name, feature) in features.iter() {
177         lines.push(format!("{:<32} {:<8} {:<12} {:<8}",
178                            name,
179                            "lang",
180                            feature.level,
181                            feature.since));
182     }
183     for (name, feature) in lib_features {
184         lines.push(format!("{:<32} {:<8} {:<12} {:<8}",
185                            name,
186                            "lib",
187                            feature.level,
188                            feature.since));
189     }
190
191     lines.sort();
192     for line in lines {
193         println!("* {}", line);
194     }
195 }
196
197 fn find_attr_val<'a>(line: &'a str, attr: &str) -> Option<&'a str> {
198     line.find(attr)
199         .and_then(|i| line[i..].find('"').map(|j| i + j + 1))
200         .and_then(|i| line[i..].find('"').map(|j| (i, i + j)))
201         .map(|(i, j)| &line[i..j])
202 }
203
204 fn test_filen_gate(filen_underscore: &str, features: &mut Features) -> bool {
205     if filen_underscore.starts_with("feature_gate") {
206         for (n, f) in features.iter_mut() {
207             if filen_underscore == format!("feature_gate_{}", n) {
208                 f.has_gate_test = true;
209                 return true;
210             }
211         }
212     }
213     return false;
214 }
215
216 pub fn collect_lang_features(base_src_path: &Path) -> Features {
217     let mut contents = String::new();
218     let path = base_src_path.join("libsyntax/feature_gate.rs");
219     t!(t!(File::open(path)).read_to_string(&mut contents));
220
221     contents.lines()
222         .filter_map(|line| {
223             let mut parts = line.trim().split(",");
224             let level = match parts.next().map(|l| l.trim().trim_left_matches('(')) {
225                 Some("active") => Status::Unstable,
226                 Some("removed") => Status::Removed,
227                 Some("accepted") => Status::Stable,
228                 _ => return None,
229             };
230             let name = parts.next().unwrap().trim();
231             let since = parts.next().unwrap().trim().trim_matches('"');
232             let issue_str = parts.next().unwrap().trim();
233             let tracking_issue = if issue_str.starts_with("None") {
234                 None
235             } else {
236                 let s = issue_str.split("(").nth(1).unwrap().split(")").nth(0).unwrap();
237                 Some(s.parse().unwrap())
238             };
239             Some((name.to_owned(),
240                 Feature {
241                     level,
242                     since: since.to_owned(),
243                     has_gate_test: false,
244                     tracking_issue,
245                 }))
246         })
247         .collect()
248 }
249
250 pub fn collect_lib_features(base_src_path: &Path) -> Features {
251     let mut lib_features = Features::new();
252
253     // This library feature is defined in the `compiler_builtins` crate, which
254     // has been moved out-of-tree. Now it can no longer be auto-discovered by
255     // `tidy`, because we need to filter out its (submodule) directory. Manually
256     // add it to the set of known library features so we can still generate docs.
257     lib_features.insert("compiler_builtins_lib".to_owned(), Feature {
258         level: Status::Unstable,
259         since: "".to_owned(),
260         has_gate_test: false,
261         tracking_issue: None,
262     });
263
264     map_lib_features(base_src_path,
265                      &mut |res, _, _| {
266         match res {
267             Ok((name, feature)) => {
268                 if lib_features.get(name).is_some() {
269                     return;
270                 }
271                 lib_features.insert(name.to_owned(), feature);
272             },
273             Err(_) => (),
274         }
275     });
276    lib_features
277 }
278
279 fn get_and_check_lib_features(base_src_path: &Path,
280                               bad: &mut bool,
281                               lang_features: &Features) -> Features {
282     let mut lib_features = Features::new();
283     map_lib_features(base_src_path,
284                      &mut |res, file, line| {
285             match res {
286                 Ok((name, f)) => {
287                     let mut check_features = |f: &Feature, list: &Features, display: &str| {
288                         if let Some(ref s) = list.get(name) {
289                             if let Err(m) = (&f).check_match(s) {
290                                 tidy_error!(bad,
291                                             "{}:{}: mismatches to {} in: {:?}",
292                                             file.display(),
293                                             line,
294                                             display,
295                                             &m);
296                             }
297                         }
298                     };
299                     check_features(&f, &lang_features, "corresponding lang feature");
300                     check_features(&f, &lib_features, "previous");
301                     lib_features.insert(name.to_owned(), f);
302                 },
303                 Err(msg) => {
304                     tidy_error!(bad, "{}:{}: {}", file.display(), line, msg);
305                 },
306             }
307
308     });
309     lib_features
310 }
311
312 fn map_lib_features(base_src_path: &Path,
313                     mf: &mut FnMut(Result<(&str, Feature), &str>, &Path, usize)) {
314     let mut contents = String::new();
315     super::walk(base_src_path,
316                 &mut |path| super::filter_dirs(path) || path.ends_with("src/test"),
317                 &mut |file| {
318         let filename = file.file_name().unwrap().to_string_lossy();
319         if !filename.ends_with(".rs") || filename == "features.rs" ||
320            filename == "diagnostic_list.rs" {
321             return;
322         }
323
324         contents.truncate(0);
325         t!(t!(File::open(&file), &file).read_to_string(&mut contents));
326
327         let mut becoming_feature: Option<(String, Feature)> = None;
328         for (i, line) in contents.lines().enumerate() {
329             macro_rules! err {
330                 ($msg:expr) => {{
331                     mf(Err($msg), file, i + 1);
332                     continue;
333                 }};
334             };
335             if let Some((ref name, ref mut f)) = becoming_feature {
336                 if f.tracking_issue.is_none() {
337                     f.tracking_issue = find_attr_val(line, "issue")
338                     .map(|s| s.parse().unwrap());
339                 }
340                 if line.ends_with("]") {
341                     mf(Ok((name, f.clone())), file, i + 1);
342                 } else if !line.ends_with(",") && !line.ends_with("\\") {
343                     // We need to bail here because we might have missed the
344                     // end of a stability attribute above because the "]"
345                     // might not have been at the end of the line.
346                     // We could then get into the very unfortunate situation that
347                     // we continue parsing the file assuming the current stability
348                     // attribute has not ended, and ignoring possible feature
349                     // attributes in the process.
350                     err!("malformed stability attribute");
351                 } else {
352                     continue;
353                 }
354             }
355             becoming_feature = None;
356             if line.contains("rustc_const_unstable(") {
357                 // const fn features are handled specially
358                 let feature_name = match find_attr_val(line, "feature") {
359                     Some(name) => name,
360                     None => err!("malformed stability attribute"),
361                 };
362                 let feature = Feature {
363                     level: Status::Unstable,
364                     since: "None".to_owned(),
365                     has_gate_test: false,
366                     // Whether there is a common tracking issue
367                     // for these feature gates remains an open question
368                     // https://github.com/rust-lang/rust/issues/24111#issuecomment-340283184
369                     // But we take 24111 otherwise they will be shown as
370                     // "internal to the compiler" which they are not.
371                     tracking_issue: Some(24111),
372                 };
373                 mf(Ok((feature_name, feature)), file, i + 1);
374                 continue;
375             }
376             let level = if line.contains("[unstable(") {
377                 Status::Unstable
378             } else if line.contains("[stable(") {
379                 Status::Stable
380             } else {
381                 continue;
382             };
383             let feature_name = match find_attr_val(line, "feature") {
384                 Some(name) => name,
385                 None => err!("malformed stability attribute"),
386             };
387             let since = match find_attr_val(line, "since") {
388                 Some(name) => name,
389                 None if level == Status::Stable => {
390                     err!("malformed stability attribute");
391                 }
392                 None => "None",
393             };
394             let tracking_issue = find_attr_val(line, "issue").map(|s| s.parse().unwrap());
395
396             let feature = Feature {
397                 level,
398                 since: since.to_owned(),
399                 has_gate_test: false,
400                 tracking_issue,
401             };
402             if line.contains("]") {
403                 mf(Ok((feature_name, feature)), file, i + 1);
404             } else {
405                 becoming_feature = Some((feature_name.to_owned(), feature));
406             }
407         }
408     });
409 }