1 //! Tidy check to ensure that unstable features are all in order.
3 //! This check will ensure properties like:
5 //! * All stability attributes look reasonably well formed.
6 //! * The set of library features is disjoint from the set of language features.
7 //! * Library features have at most one stability level.
8 //! * Library features have at most one `since` value.
9 //! * All unstable lang features have tests to ensure they are actually unstable.
10 //! * Language features in a group are sorted by `since` value.
12 use std
::collections
::HashMap
;
15 use std
::num
::NonZeroU32
;
26 const FEATURE_GROUP_START_PREFIX
: &str = "// feature-group-start";
27 const FEATURE_GROUP_END_PREFIX
: &str = "// feature-group-end";
29 #[derive(Debug, PartialEq, Clone)]
36 impl fmt
::Display
for Status
{
37 fn fmt(&self, f
: &mut fmt
::Formatter
<'_
>) -> fmt
::Result
{
38 let as_str
= match *self {
39 Status
::Stable
=> "stable",
40 Status
::Unstable
=> "unstable",
41 Status
::Removed
=> "removed",
43 fmt
::Display
::fmt(as_str
, f
)
47 #[derive(Debug, Clone)]
50 pub since
: Option
<Version
>,
51 pub has_gate_test
: bool
,
52 pub tracking_issue
: Option
<NonZeroU32
>,
55 fn tracking_issue_display(&self) -> impl fmt
::Display
{
56 match self.tracking_issue
{
57 None
=> "none".to_string(),
58 Some(x
) => x
.to_string(),
63 pub type Features
= HashMap
<String
, Feature
>;
65 pub struct CollectedFeatures
{
70 // Currently only used for unstable book generation
71 pub fn collect_lib_features(base_src_path
: &Path
) -> Features
{
72 let mut lib_features
= Features
::new();
74 map_lib_features(base_src_path
, &mut |res
, _
, _
| {
75 if let Ok((name
, feature
)) = res
{
76 lib_features
.insert(name
.to_owned(), feature
);
88 ) -> CollectedFeatures
{
89 let mut features
= collect_lang_features(compiler_path
, bad
);
90 assert
!(!features
.is_empty());
92 let lib_features
= get_and_check_lib_features(lib_path
, bad
, &features
);
93 assert
!(!lib_features
.is_empty());
97 &src_path
.join("test/ui"),
98 &src_path
.join("test/ui-fulldeps"),
99 &src_path
.join("test/rustdoc-ui"),
101 &mut |path
| super::filter_dirs(path
),
102 &mut |entry
, contents
| {
103 let file
= entry
.path();
104 let filename
= file
.file_name().unwrap().to_string_lossy();
105 if !filename
.ends_with(".rs")
106 || filename
== "features.rs"
107 || filename
== "diagnostic_list.rs"
112 let filen_underscore
= filename
.replace('
-'
, "_").replace(".rs", "");
113 let filename_is_gate_test
= test_filen_gate(&filen_underscore
, &mut features
);
115 for (i
, line
) in contents
.lines().enumerate() {
116 let mut err
= |msg
: &str| {
117 tidy_error
!(bad
, "{}:{}: {}", file
.display(), i
+ 1, msg
);
120 let gate_test_str
= "gate-test-";
122 let feature_name
= match line
.find(gate_test_str
) {
123 // NB: the `splitn` always succeeds, even if the delimiter is not present.
124 Some(i
) => line
[i
+ gate_test_str
.len()..].splitn(2, ' '
).next().unwrap(),
127 match features
.get_mut(feature_name
) {
129 if filename_is_gate_test
{
131 "The file is already marked as gate test \
132 through its name, no need for a \
133 'gate-test-{}' comment",
137 f
.has_gate_test
= true;
141 "gate-test test found referencing a nonexistent feature '{}'",
150 // Only check the number of lang features.
151 // Obligatory testing for library features is dumb.
152 let gate_untested
= features
154 .filter(|&(_
, f
)| f
.level
== Status
::Unstable
)
155 .filter(|&(_
, f
)| !f
.has_gate_test
)
156 .collect
::<Vec
<_
>>();
158 for &(name
, _
) in gate_untested
.iter() {
159 println
!("Expected a gate test for the feature '{}'.", name
);
161 "Hint: create a failing test file named 'feature-gate-{}.rs'\
162 \n in the 'ui' test suite, with its failures due to\
163 \n missing usage of `#![feature({})]`.",
167 "Hint: If you already have such a test and don't want to rename it,\
168 \n you can also add a // gate-test-{} line to the test file.",
173 if !gate_untested
.is_empty() {
174 tidy_error
!(bad
, "Found {} features without a gate test.", gate_untested
.len());
178 return CollectedFeatures { lib: lib_features, lang: features }
;
182 let mut lines
= Vec
::new();
183 lines
.extend(format_features(&features
, "lang"));
184 lines
.extend(format_features(&lib_features
, "lib"));
188 println
!("* {}", line
);
191 println
!("* {} features", features
.len());
194 CollectedFeatures { lib: lib_features, lang: features }
197 fn format_features
<'a
>(
198 features
: &'a Features
,
200 ) -> impl Iterator
<Item
= String
> + 'a
{
201 features
.iter().map(move |(name
, feature
)| {
203 "{:<32} {:<8} {:<12} {:<8}",
207 feature
.since
.map_or("None".to_owned(), |since
| since
.to_string())
212 fn find_attr_val
<'a
>(line
: &'a
str, attr
: &str) -> Option
<&'a
str> {
213 lazy_static
::lazy_static
! {
214 static ref ISSUE
: Regex
= Regex
::new(r
#"issue\s*=\s*"([^"]*)""#).unwrap();
215 static ref FEATURE: Regex = Regex::new(r#"feature\s*=\s*"([^"]*)""#).unwrap();
216 static ref SINCE
: Regex
= Regex
::new(r
#"since\s*=\s*"([^"]*)""#).unwrap();
221 "feature" => &*FEATURE,
223 _ => unimplemented!("{} not handled", attr),
226 r.captures(line).and_then(|c| c.get(1)).map(|m| m.as_str())
229 fn test_filen_gate(filen_underscore: &str, features: &mut Features) -> bool {
230 let prefix = "feature_gate_";
231 if filen_underscore.starts_with(prefix) {
232 for (n, f) in features.iter_mut() {
233 // Equivalent to filen_underscore == format!("feature_gate_{}", n)
234 if &filen_underscore[prefix.len()..] == n {
235 f.has_gate_test = true;
243 pub fn collect_lang_features(base_compiler_path: &Path, bad: &mut bool) -> Features {
244 let mut all = collect_lang_features_in(base_compiler_path, "active.rs", bad);
245 all.extend(collect_lang_features_in(base_compiler_path, "accepted.rs", bad));
246 all.extend(collect_lang_features_in(base_compiler_path, "removed.rs", bad));
250 fn collect_lang_features_in(base: &Path, file: &str, bad: &mut bool) -> Features {
251 let path = base.join("rustc_feature").join("src").join(file);
252 let contents = t!(fs::read_to_string(&path));
254 // We allow rustc-internal features to omit a tracking issue.
255 // To make tidy accept omitting a tracking issue, group the list of features
256 // without one inside `// no-tracking-issue` and `// no-tracking-issue-end`.
257 let mut next_feature_omits_tracking_issue = false;
259 let mut in_feature_group = false;
260 let mut prev_since = None;
265 .filter_map(|(line, line_number)| {
266 let line = line.trim();
268 // Within -start and -end, the tracking issue can be omitted.
270 "// no-tracking-issue-start" => {
271 next_feature_omits_tracking_issue
= true;
274 "// no-tracking-issue-end" => {
275 next_feature_omits_tracking_issue
= false;
281 if line
.starts_with(FEATURE_GROUP_START_PREFIX
) {
282 if in_feature_group
{
286 new feature group is started without ending the previous one",
292 in_feature_group
= true;
295 } else if line
.starts_with(FEATURE_GROUP_END_PREFIX
) {
296 in_feature_group
= false;
301 let mut parts
= line
.split('
,'
);
302 let level
= match parts
.next().map(|l
| l
.trim().trim_start_matches('
('
)) {
303 Some("active") => Status
::Unstable
,
304 Some("removed") => Status
::Removed
,
305 Some("accepted") => Status
::Stable
,
308 let name
= parts
.next().unwrap().trim();
310 let since_str
= parts
.next().unwrap().trim().trim_matches('
"');
311 let since = match since_str.parse() {
312 Ok(since) => Some(since),
316 "{}
:{}
: failed to parse since
: {}
({:?}
)",
325 if in_feature_group {
326 if prev_since > since {
329 "{}
:{}
: feature {} is not sorted by
\"since
\" (version number
)",
338 let issue_str = parts.next().unwrap().trim();
339 let tracking_issue = if issue_str.starts_with("None
") {
340 if level == Status::Unstable && !next_feature_omits_tracking_issue {
343 "{}
:{}
: no tracking issue
for feature {}
",
351 let s = issue_str.split('(').nth(1).unwrap().split(')').next().unwrap();
352 Some(s.parse().unwrap())
354 Some((name.to_owned(), Feature { level, since, has_gate_test: false, tracking_issue }))
359 fn get_and_check_lib_features(
360 base_src_path: &Path,
362 lang_features: &Features,
364 let mut lib_features = Features::new();
365 map_lib_features(base_src_path, &mut |res, file, line| match res {
367 let mut check_features = |f: &Feature, list: &Features, display: &str| {
368 if let Some(ref s) = list.get(name) {
369 if f.tracking_issue != s.tracking_issue && f.level != Status::Stable {
372 "{}
:{}
: `issue`
\"{}
\" mismatches the {} `issue` of
\"{}
\"",
375 f.tracking_issue_display(),
377 s.tracking_issue_display(),
382 check_features(&f, &lang_features, "corresponding lang feature
");
383 check_features(&f, &lib_features, "previous
");
384 lib_features.insert(name.to_owned(), f);
387 tidy_error!(bad, "{}
:{}
: {}
", file.display(), line, msg);
394 base_src_path: &Path,
395 mf: &mut dyn FnMut(Result<(&str, Feature), &str>, &Path, usize),
399 &mut |path| super::filter_dirs(path) || path.ends_with("src
/test
"),
400 &mut |entry, contents| {
401 let file = entry.path();
402 let filename = file.file_name().unwrap().to_string_lossy();
403 if !filename.ends_with(".rs
")
404 || filename == "features
.rs
"
405 || filename == "diagnostic_list
.rs
"
406 || filename == "error_codes
.rs
"
411 // This is an early exit -- all the attributes we're concerned with must contain this:
412 // * rustc_const_unstable(
415 if !contents.contains("stable(") {
419 let handle_issue_none = |s| match s {
422 let n = issue.parse().expect("issue number is not a valid integer
");
423 assert_ne!(n, 0, "\"none
\" should be used when there is no issue
, not
\"0\"");
427 let mut becoming_feature: Option<(&str, Feature)> = None;
428 let mut iter_lines = contents.lines().enumerate().peekable();
429 while let Some((i, line)) = iter_lines.next() {
432 mf(Err($msg), file, i + 1);
437 lazy_static::lazy_static! {
438 static ref COMMENT_LINE: Regex = Regex::new(r"^
\s*/
/").unwrap();
440 // exclude commented out lines
441 if COMMENT_LINE.is_match(line) {
445 if let Some((ref name, ref mut f)) = becoming_feature {
446 if f.tracking_issue.is_none() {
447 f.tracking_issue = find_attr_val(line, "issue
").and_then(handle_issue_none);
449 if line.ends_with(']') {
450 mf(Ok((name, f.clone())), file, i + 1);
451 } else if !line.ends_with(',') && !line.ends_with('\\') && !line.ends_with('"'
)
453 // We need to bail here because we might have missed the
454 // end of a stability attribute above because the ']'
455 // might not have been at the end of the line.
456 // We could then get into the very unfortunate situation that
457 // we continue parsing the file assuming the current stability
458 // attribute has not ended, and ignoring possible feature
459 // attributes in the process.
460 err
!("malformed stability attribute");
465 becoming_feature
= None
;
466 if line
.contains("rustc_const_unstable(") {
467 // `const fn` features are handled specially.
468 let feature_name
= match find_attr_val(line
, "feature") {
470 None
=> err
!("malformed stability attribute: missing `feature` key"),
472 let feature
= Feature
{
473 level
: Status
::Unstable
,
475 has_gate_test
: false,
476 tracking_issue
: find_attr_val(line
, "issue").and_then(handle_issue_none
),
478 mf(Ok((feature_name
, feature
)), file
, i
+ 1);
481 let level
= if line
.contains("[unstable(") {
483 } else if line
.contains("[stable(") {
488 let feature_name
= match find_attr_val(line
, "feature")
489 .or_else(|| iter_lines
.peek().and_then(|next
| find_attr_val(next
.1, "feature")))
492 None
=> err
!("malformed stability attribute: missing `feature` key"),
494 let since
= match find_attr_val(line
, "since").map(|x
| x
.parse()) {
495 Some(Ok(since
)) => Some(since
),
497 err
!("malformed stability attribute: can't parse `since` key");
499 None
if level
== Status
::Stable
=> {
500 err
!("malformed stability attribute: missing the `since` key");
504 let tracking_issue
= find_attr_val(line
, "issue").and_then(handle_issue_none
);
506 let feature
= Feature { level, since, has_gate_test: false, tracking_issue }
;
507 if line
.contains('
]'
) {
508 mf(Ok((feature_name
, feature
)), file
, i
+ 1);
510 becoming_feature
= Some((feature_name
, feature
));