Skip to content

Commit 58345bb

Browse files
authored
Rollup merge of #147903 - Zalathar:edition, r=jieyouxu
compiletest: Store the selected edition in `TestProps` While working on a larger overhaul of directive processing, I ran into difficulty with the `has_edition` local variable. Storing the selected edition in `TestProps` should make it easier to extract parts of directive processing into independent handler functions, because the `//@ edition` handler won't need access to additional mutable state outside of `TestProps`. We still automatically add the edition to `compile_flags`, because there is too much existing compiletest code relying on that behaviour. r? jieyouxu
2 parents 40475b4 + e9bcded commit 58345bb

File tree

2 files changed

+21
-11
lines changed

2 files changed

+21
-11
lines changed

src/tools/compiletest/src/directives.rs

Lines changed: 9 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -77,11 +77,15 @@ impl EarlyProps {
7777
}
7878

7979
#[derive(Clone, Debug)]
80-
pub struct TestProps {
80+
pub(crate) struct TestProps {
8181
// Lines that should be expected, in order, on standard out
8282
pub error_patterns: Vec<String>,
8383
// Regexes that should be expected, in order, on standard out
8484
pub regex_error_patterns: Vec<String>,
85+
/// Edition selected by an `//@ edition` directive, if any.
86+
///
87+
/// Automatically added to `compile_flags` during directive processing.
88+
pub edition: Option<Edition>,
8589
// Extra flags to pass to the compiler
8690
pub compile_flags: Vec<String>,
8791
// Extra flags to pass when the compiled code is run (such as --bench)
@@ -263,6 +267,7 @@ impl TestProps {
263267
TestProps {
264268
error_patterns: vec![],
265269
regex_error_patterns: vec![],
270+
edition: None,
266271
compile_flags: vec![],
267272
run_flags: vec![],
268273
doc_flags: vec![],
@@ -351,7 +356,6 @@ impl TestProps {
351356
/// `//@[foo]`), then the property is ignored unless `test_revision` is
352357
/// `Some("foo")`.
353358
fn load_from(&mut self, testfile: &Utf8Path, test_revision: Option<&str>, config: &Config) {
354-
let mut has_edition = false;
355359
if !testfile.is_dir() {
356360
let file_contents = fs::read_to_string(testfile).unwrap();
357361
let file_directives = FileDirectives::from_file_contents(testfile, &file_contents);
@@ -419,13 +423,7 @@ impl TestProps {
419423
}
420424

421425
if let Some(range) = parse_edition_range(config, ln) {
422-
// The edition is added at the start, since flags from //@compile-flags must
423-
// be passed to rustc last.
424-
self.compile_flags.insert(
425-
0,
426-
format!("--edition={}", range.edition_to_test(config.edition)),
427-
);
428-
has_edition = true;
426+
self.edition = Some(range.edition_to_test(config.edition));
429427
}
430428

431429
config.parse_and_update_revisions(ln, &mut self.revisions);
@@ -674,10 +672,10 @@ impl TestProps {
674672
}
675673
}
676674

677-
if let (Some(edition), false) = (&config.edition, has_edition) {
675+
if let Some(edition) = self.edition.or(config.edition) {
678676
// The edition is added at the start, since flags from //@compile-flags must be passed
679677
// to rustc last.
680-
self.compile_flags.insert(0, format!("--edition={}", edition));
678+
self.compile_flags.insert(0, format!("--edition={edition}"));
681679
}
682680
}
683681

src/tools/compiletest/src/directives/tests.rs

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -946,6 +946,18 @@ fn parse_edition_range(line: &str) -> Option<EditionRange> {
946946
super::parse_edition_range(&config, &line)
947947
}
948948

949+
#[test]
950+
fn edition_order() {
951+
let editions = &[
952+
Edition::Year(2015),
953+
Edition::Year(2018),
954+
Edition::Year(2021),
955+
Edition::Year(2024),
956+
Edition::Future,
957+
];
958+
assert!(editions.is_sorted(), "{editions:#?}");
959+
}
960+
949961
#[test]
950962
fn test_parse_edition_range() {
951963
assert_eq!(None, parse_edition_range("hello-world"));

0 commit comments

Comments
 (0)