From e9bcded695f274fcd9c8fe8d5ffcf1d063a8ee85 Mon Sep 17 00:00:00 2001 From: Zalathar Date: Mon, 20 Oct 2025 20:11:45 +1100 Subject: [PATCH] Store the selected edition in `TestProps` --- src/tools/compiletest/src/directives.rs | 20 +++++++++---------- src/tools/compiletest/src/directives/tests.rs | 12 +++++++++++ 2 files changed, 21 insertions(+), 11 deletions(-) diff --git a/src/tools/compiletest/src/directives.rs b/src/tools/compiletest/src/directives.rs index e8b6b377bf40d..02dee5ee17874 100644 --- a/src/tools/compiletest/src/directives.rs +++ b/src/tools/compiletest/src/directives.rs @@ -81,11 +81,15 @@ impl EarlyProps { } #[derive(Clone, Debug)] -pub struct TestProps { +pub(crate) struct TestProps { // Lines that should be expected, in order, on standard out pub error_patterns: Vec, // Regexes that should be expected, in order, on standard out pub regex_error_patterns: Vec, + /// Edition selected by an `//@ edition` directive, if any. + /// + /// Automatically added to `compile_flags` during directive processing. + pub edition: Option, // Extra flags to pass to the compiler pub compile_flags: Vec, // Extra flags to pass when the compiled code is run (such as --bench) @@ -267,6 +271,7 @@ impl TestProps { TestProps { error_patterns: vec![], regex_error_patterns: vec![], + edition: None, compile_flags: vec![], run_flags: vec![], doc_flags: vec![], @@ -355,7 +360,6 @@ impl TestProps { /// `//@[foo]`), then the property is ignored unless `test_revision` is /// `Some("foo")`. fn load_from(&mut self, testfile: &Utf8Path, test_revision: Option<&str>, config: &Config) { - let mut has_edition = false; if !testfile.is_dir() { let file_contents = fs::read_to_string(testfile).unwrap(); let file_directives = FileDirectives::from_file_contents(testfile, &file_contents); @@ -423,13 +427,7 @@ impl TestProps { } if let Some(range) = parse_edition_range(config, ln) { - // The edition is added at the start, since flags from //@compile-flags must - // be passed to rustc last. - self.compile_flags.insert( - 0, - format!("--edition={}", range.edition_to_test(config.edition)), - ); - has_edition = true; + self.edition = Some(range.edition_to_test(config.edition)); } config.parse_and_update_revisions(ln, &mut self.revisions); @@ -678,10 +676,10 @@ impl TestProps { } } - if let (Some(edition), false) = (&config.edition, has_edition) { + if let Some(edition) = self.edition.or(config.edition) { // The edition is added at the start, since flags from //@compile-flags must be passed // to rustc last. - self.compile_flags.insert(0, format!("--edition={}", edition)); + self.compile_flags.insert(0, format!("--edition={edition}")); } } diff --git a/src/tools/compiletest/src/directives/tests.rs b/src/tools/compiletest/src/directives/tests.rs index b683c8317e49b..d50067a8f16b5 100644 --- a/src/tools/compiletest/src/directives/tests.rs +++ b/src/tools/compiletest/src/directives/tests.rs @@ -961,6 +961,18 @@ fn parse_edition_range(line: &str) -> Option { super::parse_edition_range(&config, &line) } +#[test] +fn edition_order() { + let editions = &[ + Edition::Year(2015), + Edition::Year(2018), + Edition::Year(2021), + Edition::Year(2024), + Edition::Future, + ]; + assert!(editions.is_sorted(), "{editions:#?}"); +} + #[test] fn test_parse_edition_range() { assert_eq!(None, parse_edition_range("hello-world"));