Skip to content

Commit d06e0e5

Browse files
committed
tidy: forbid since values for features that point to the current release or future ones
It's a common phenomenon that feature stabilizations don't make it into a particular release, but the version is still inaccurate. Often this leads to subsequent changes to adjust/correct the version. Instead, require people to put a placeholder that gets replaced during beta branching time with the current rust version. That way, there is no chance that an error can be introduced. Usage of the placeholder is required on the nightly channel, and forbidden on the stable and beta channels.
1 parent 3b3f3b7 commit d06e0e5

File tree

3 files changed

+55
-5
lines changed

3 files changed

+55
-5
lines changed

src/tools/tidy/src/features.rs

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -175,6 +175,36 @@ pub fn check(
175175
tidy_error!(bad, "Found {} features without a gate test.", gate_untested.len());
176176
}
177177

178+
let (version, channel) = get_version_and_channel(src_path);
179+
180+
let all_features_iter = features
181+
.iter()
182+
.map(|feat| (feat, "lang"))
183+
.chain(lib_features.iter().map(|feat| (feat, "lib")));
184+
for ((feature_name, feature), kind) in all_features_iter {
185+
let since = if let Some(since) = feature.since { since } else { continue };
186+
if since > version && since != Version::CurrentPlaceholder {
187+
tidy_error!(
188+
bad,
189+
"The stabilization version {since} of {kind} feature `{feature_name}` is newer than the current {version}"
190+
);
191+
}
192+
if channel == "nightly" && since == version {
193+
tidy_error!(
194+
bad,
195+
"The stabilization version {since} of {kind} feature `{feature_name}` is written out but should be {}",
196+
version::VERSION_PLACEHOLDER
197+
);
198+
}
199+
if channel != "nightly" && since == Version::CurrentPlaceholder {
200+
tidy_error!(
201+
bad,
202+
"The placeholder use of {kind} feature `{feature_name}` is not allowed on the {} channel",
203+
version::VERSION_PLACEHOLDER
204+
);
205+
}
206+
}
207+
178208
if *bad {
179209
return CollectedFeatures { lib: lib_features, lang: features };
180210
}
@@ -195,6 +225,14 @@ pub fn check(
195225
CollectedFeatures { lib: lib_features, lang: features }
196226
}
197227

228+
fn get_version_and_channel(src_path: &Path) -> (Version, String) {
229+
let version_str = t!(std::fs::read_to_string(src_path.join("version")));
230+
let version_str = version_str.trim();
231+
let version = t!(std::str::FromStr::from_str(&version_str).map_err(|e| format!("{e:?}")));
232+
let channel_str = t!(std::fs::read_to_string(src_path.join("ci").join("channel")));
233+
(version, channel_str.trim().to_owned())
234+
}
235+
198236
fn format_features<'a>(
199237
features: &'a Features,
200238
family: &'a str,

src/tools/tidy/src/features/version.rs

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,14 +5,22 @@ use std::str::FromStr;
55
#[cfg(test)]
66
mod tests;
77

8+
pub const VERSION_PLACEHOLDER: &str = "CURRENT_RUSTC_VERSION";
9+
810
#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord)]
9-
pub struct Version {
10-
parts: [u32; 3],
11+
pub enum Version {
12+
Explicit { parts: [u32; 3] },
13+
CurrentPlaceholder,
1114
}
1215

1316
impl fmt::Display for Version {
1417
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
15-
f.pad(&format!("{}.{}.{}", self.parts[0], self.parts[1], self.parts[2]))
18+
match self {
19+
Version::Explicit { parts } => {
20+
f.pad(&format!("{}.{}.{}", parts[0], parts[1], parts[2]))
21+
}
22+
Version::CurrentPlaceholder => f.pad(&format!("CURRENT")),
23+
}
1624
}
1725
}
1826

@@ -32,6 +40,9 @@ impl FromStr for Version {
3240
type Err = ParseVersionError;
3341

3442
fn from_str(s: &str) -> Result<Self, Self::Err> {
43+
if s == VERSION_PLACEHOLDER {
44+
return Ok(Version::CurrentPlaceholder);
45+
}
3546
let mut iter = s.split('.').map(|part| Ok(part.parse()?));
3647

3748
let mut part = || iter.next().unwrap_or(Err(ParseVersionError::WrongNumberOfParts));
@@ -43,6 +54,6 @@ impl FromStr for Version {
4354
return Err(ParseVersionError::WrongNumberOfParts);
4455
}
4556

46-
Ok(Self { parts })
57+
Ok(Version::Explicit { parts })
4758
}
4859
}

src/tools/tidy/src/lib.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,8 @@ macro_rules! t {
2828
macro_rules! tidy_error {
2929
($bad:expr, $fmt:expr) => ({
3030
*$bad = true;
31-
eprintln!("tidy error: {}", $fmt);
31+
eprint!("tidy error: ");
32+
eprintln!($fmt);
3233
});
3334
($bad:expr, $fmt:expr, $($arg:tt)*) => ({
3435
*$bad = true;

0 commit comments

Comments
 (0)