From 38d2c1158479b11cd3cae15a2e3470f419759511 Mon Sep 17 00:00:00 2001 From: Zalathar Date: Mon, 11 May 2026 21:43:35 +1000 Subject: [PATCH] Enforce that directives are consistently used with or without a colon --- src/tools/compiletest/src/directives.rs | 28 +++++++++++++------ src/tools/compiletest/src/directives/tests.rs | 1 - .../printing_valtrees_supports_non_values.rs | 2 +- tests/ui/consts/issue-miri-1910.rs | 1 - tests/ui/consts/issue-miri-1910.stderr | 2 +- ...tion-gated-async-move-syntax-issue89699.rs | 2 +- .../ui/rust-2021/generic-type-collision.fixed | 3 +- tests/ui/rust-2021/generic-type-collision.rs | 3 +- .../rust-2021/generic-type-collision.stderr | 6 ++-- 9 files changed, 27 insertions(+), 21 deletions(-) diff --git a/src/tools/compiletest/src/directives.rs b/src/tools/compiletest/src/directives.rs index 51a9e638fd7a4..f23b020e1c789 100644 --- a/src/tools/compiletest/src/directives.rs +++ b/src/tools/compiletest/src/directives.rs @@ -618,7 +618,11 @@ impl Config { } fn parse_pp_exact(&self, line: &DirectiveLine<'_>) -> Option { - if let Some(s) = self.parse_name_value_directive(line, "pp-exact") { + // Unusually, `//@ pp-exact` can be used with or without a colon, so to avoid a panic + // in the parse method we need to make sure there is a colon before calling it. + if line.value_after_colon().is_some() + && let Some(s) = self.parse_name_value_directive(line, "pp-exact") + { Some(Utf8PathBuf::from(&s)) } else if self.parse_name_directive(line, "pp-exact") { line.file_path.file_name().map(Utf8PathBuf::from) @@ -648,10 +652,17 @@ impl Config { } fn parse_name_directive(&self, line: &DirectiveLine<'_>, directive: &str) -> bool { - // FIXME(Zalathar): Ideally, this should raise an error if a name-only - // directive is followed by a colon, since that's the wrong syntax. - // But we would need to fix tests that rely on the current behaviour. - line.name == directive + if line.name != directive { + return false; + } + + if line.value_after_colon().is_some() { + let &DirectiveLine { file_path, line_number, .. } = line; + panic!( + "{file_path}:{line_number}: directive `{directive}` must not be followed by a colon" + ); + } + true } fn parse_name_value_directive( @@ -665,10 +676,9 @@ impl Config { return None; }; - // FIXME(Zalathar): This silently discards directives with a matching - // name but no colon. Unfortunately, some directives (e.g. "pp-exact") - // currently rely on _not_ panicking here. - let value = line.value_after_colon()?; + let value = line.value_after_colon().unwrap_or_else(|| { + panic!("{file_path}:{line_number}: directive `{directive}` must be followed by a colon and value"); + }); debug!("{}: {}", directive, value); let value = expand_variables(value.to_owned(), self); diff --git a/src/tools/compiletest/src/directives/tests.rs b/src/tools/compiletest/src/directives/tests.rs index c2d73f95c5446..bc016cfb1cb43 100644 --- a/src/tools/compiletest/src/directives/tests.rs +++ b/src/tools/compiletest/src/directives/tests.rs @@ -1153,7 +1153,6 @@ fn edition_order() { #[test] fn test_parse_edition_range() { assert_eq!(None, parse_edition_range("hello-world")); - assert_eq!(None, parse_edition_range("edition")); assert_eq!(Some(EditionRange::Exact(2018.into())), parse_edition_range("edition: 2018")); assert_eq!(Some(EditionRange::Exact(2021.into())), parse_edition_range("edition:2021")); diff --git a/tests/ui/const-generics/mgca/printing_valtrees_supports_non_values.rs b/tests/ui/const-generics/mgca/printing_valtrees_supports_non_values.rs index 16ed24a7b2870..4829196d1078c 100644 --- a/tests/ui/const-generics/mgca/printing_valtrees_supports_non_values.rs +++ b/tests/ui/const-generics/mgca/printing_valtrees_supports_non_values.rs @@ -1,5 +1,5 @@ //! Regression test for -//@ edition 2024 +//@ edition: 2024 #![allow(incomplete_features)] #![feature(min_generic_const_args, adt_const_params)] diff --git a/tests/ui/consts/issue-miri-1910.rs b/tests/ui/consts/issue-miri-1910.rs index 78587bbb4dd90..97cc92417bf46 100644 --- a/tests/ui/consts/issue-miri-1910.rs +++ b/tests/ui/consts/issue-miri-1910.rs @@ -1,5 +1,4 @@ //@ ignore-backends: gcc -//@ error-pattern unable to turn pointer into raw bytes //@ normalize-stderr: "alloc[0-9]+\+0x[a-z0-9]+" -> "ALLOC" const C: () = unsafe { diff --git a/tests/ui/consts/issue-miri-1910.stderr b/tests/ui/consts/issue-miri-1910.stderr index 2b6e079e3809e..140b1861bb42a 100644 --- a/tests/ui/consts/issue-miri-1910.stderr +++ b/tests/ui/consts/issue-miri-1910.stderr @@ -1,5 +1,5 @@ error[E0080]: unable to turn pointer into integer - --> $DIR/issue-miri-1910.rs:8:5 + --> $DIR/issue-miri-1910.rs:7:5 | LL | (&foo as *const _ as *const u8).add(one_and_a_half_pointers).read(); | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ evaluation of `C` failed here diff --git a/tests/ui/proc-macro/edition-gated-async-move-syntax-issue89699.rs b/tests/ui/proc-macro/edition-gated-async-move-syntax-issue89699.rs index e4878a93af123..da5d34336e582 100644 --- a/tests/ui/proc-macro/edition-gated-async-move-syntax-issue89699.rs +++ b/tests/ui/proc-macro/edition-gated-async-move-syntax-issue89699.rs @@ -2,7 +2,7 @@ //@ edition: 2015 // Non-regression test for issue #89699, where a proc-macro emitting syntax only available in -//@ edition 2018 and up (`async move`) is used on edition 2015 +// edition 2018 and up (`async move`) is used on edition 2015 extern crate edition_gated_async_move_syntax; diff --git a/tests/ui/rust-2021/generic-type-collision.fixed b/tests/ui/rust-2021/generic-type-collision.fixed index 7a6ce117b77d2..e3481273c1767 100644 --- a/tests/ui/rust-2021/generic-type-collision.fixed +++ b/tests/ui/rust-2021/generic-type-collision.fixed @@ -1,7 +1,6 @@ -//@ edition:2015 //@ check-pass //@ run-rustfix -//@ edition 2018 +//@ edition: 2018 #![warn(rust_2021_prelude_collisions)] trait MyTrait { diff --git a/tests/ui/rust-2021/generic-type-collision.rs b/tests/ui/rust-2021/generic-type-collision.rs index 09077678a228d..600b3efba23a0 100644 --- a/tests/ui/rust-2021/generic-type-collision.rs +++ b/tests/ui/rust-2021/generic-type-collision.rs @@ -1,7 +1,6 @@ -//@ edition:2015 //@ check-pass //@ run-rustfix -//@ edition 2018 +//@ edition: 2018 #![warn(rust_2021_prelude_collisions)] trait MyTrait { diff --git a/tests/ui/rust-2021/generic-type-collision.stderr b/tests/ui/rust-2021/generic-type-collision.stderr index cd64b491b4a4f..2adb5bafc83e1 100644 --- a/tests/ui/rust-2021/generic-type-collision.stderr +++ b/tests/ui/rust-2021/generic-type-collision.stderr @@ -1,13 +1,13 @@ warning: trait-associated function `from_iter` will become ambiguous in Rust 2021 - --> $DIR/generic-type-collision.rs:16:5 + --> $DIR/generic-type-collision.rs:15:5 | LL | >::from_iter(None); | ^^^^^^^^^^^^^^^^^^^^^ help: disambiguate the associated function: ` as MyTrait<_>>::from_iter` | - = warning: this is accepted in the current edition (Rust 2015) but is a hard error in Rust 2021! + = warning: this is accepted in the current edition (Rust 2018) but is a hard error in Rust 2021! = note: for more information, see note: the lint level is defined here - --> $DIR/generic-type-collision.rs:5:9 + --> $DIR/generic-type-collision.rs:4:9 | LL | #![warn(rust_2021_prelude_collisions)] | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^