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.
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.
11 //! Tidy check to ensure that unstable features are all in order
13 //! This check will ensure properties like:
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
21 use std::collections::HashMap;
24 use std::io::prelude::*;
27 #[derive(Debug, PartialEq, Clone)]
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",
41 fmt::Display::fmt(as_str, f)
45 #[derive(Debug, Clone)]
49 pub has_gate_test: bool,
50 pub tracking_issue: Option<u32>,
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");
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
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");
70 if self.tracking_issue != other.tracking_issue {
71 mismatches.push("tracking issue");
73 if mismatches.is_empty() {
81 pub type Features = HashMap<String, Feature>;
83 pub fn check(path: &Path, bad: &mut bool, quiet: bool) {
84 let mut features = collect_lang_features(path);
85 assert!(!features.is_empty());
87 let lib_features = get_and_check_lib_features(path, bad, &features);
88 assert!(!lib_features.is_empty());
90 let mut contents = String::new();
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),
99 let filename = file.file_name().unwrap().to_string_lossy();
100 if !filename.ends_with(".rs") || filename == "features.rs" ||
101 filename == "diagnostic_list.rs" {
105 let filen_underscore = filename.replace("-","_").replace(".rs","");
106 let filename_is_gate_test = test_filen_gate(&filen_underscore, &mut features);
108 contents.truncate(0);
109 t!(t!(File::open(&file), &file).read_to_string(&mut contents));
111 for (i, line) in contents.lines().enumerate() {
112 let mut err = |msg: &str| {
113 tidy_error!(bad, "{}:{}: {}", file.display(), i + 1, msg);
116 let gate_test_str = "gate-test-";
118 if !line.contains(gate_test_str) {
122 let feature_name = match line.find(gate_test_str) {
124 &line[i+gate_test_str.len()..line[i+1..].find(' ').unwrap_or(line.len())]
128 match features.get_mut(feature_name) {
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",
136 f.has_gate_test = true;
139 err(&format!("gate-test test found referencing a nonexistent feature '{}'",
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<_>>();
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.",
163 if gate_untested.len() > 0 {
164 tidy_error!(bad, "Found {} features without a gate test.", gate_untested.len());
171 println!("* {} features", features.len());
175 let mut lines = Vec::new();
176 for (name, feature) in features.iter() {
177 lines.push(format!("{:<32} {:<8} {:<12} {:<8}",
183 for (name, feature) in lib_features {
184 lines.push(format!("{:<32} {:<8} {:<12} {:<8}",
193 println!("* {}", line);
197 fn find_attr_val<'a>(line: &'a str, attr: &str) -> Option<&'a str> {
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])
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;
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));
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,
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") {
236 let s = issue_str.split("(").nth(1).unwrap().split(")").nth(0).unwrap();
237 Some(s.parse().unwrap())
239 Some((name.to_owned(),
242 since: since.to_owned(),
243 has_gate_test: false,
250 pub fn collect_lib_features(base_src_path: &Path) -> Features {
251 let mut lib_features = Features::new();
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,
264 map_lib_features(base_src_path,
267 Ok((name, feature)) => {
268 if lib_features.get(name).is_some() {
271 lib_features.insert(name.to_owned(), feature);
279 fn get_and_check_lib_features(base_src_path: &Path,
281 lang_features: &Features) -> Features {
282 let mut lib_features = Features::new();
283 map_lib_features(base_src_path,
284 &mut |res, file, line| {
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) {
291 "{}:{}: mismatches to {} in: {:?}",
299 check_features(&f, &lang_features, "corresponding lang feature");
300 check_features(&f, &lib_features, "previous");
301 lib_features.insert(name.to_owned(), f);
304 tidy_error!(bad, "{}:{}: {}", file.display(), line, msg);
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"),
318 let filename = file.file_name().unwrap().to_string_lossy();
319 if !filename.ends_with(".rs") || filename == "features.rs" ||
320 filename == "diagnostic_list.rs" {
324 contents.truncate(0);
325 t!(t!(File::open(&file), &file).read_to_string(&mut contents));
327 let mut becoming_feature: Option<(String, Feature)> = None;
328 for (i, line) in contents.lines().enumerate() {
331 mf(Err($msg), file, i + 1);
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());
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");
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") {
360 None => err!("malformed stability attribute"),
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),
373 mf(Ok((feature_name, feature)), file, i + 1);
376 let level = if line.contains("[unstable(") {
378 } else if line.contains("[stable(") {
383 let feature_name = match find_attr_val(line, "feature") {
385 None => err!("malformed stability attribute"),
387 let since = match find_attr_val(line, "since") {
389 None if level == Status::Stable => {
390 err!("malformed stability attribute");
394 let tracking_issue = find_attr_val(line, "issue").map(|s| s.parse().unwrap());
396 let feature = Feature {
398 since: since.to_owned(),
399 has_gate_test: false,
402 if line.contains("]") {
403 mf(Ok((feature_name, feature)), file, i + 1);
405 becoming_feature = Some((feature_name.to_owned(), feature));