Skip to content

Commit 9d5cdf7

Browse files
committed
Auto merge of #123601 - jieyouxu:compiletest-run-rustfix-revisions, r=WaffleLapkin
compiletest: properly handle revisioned run-rustfix tests Before this PR, if you have a revisioned `//@ run-rustfix` test like `//`@[foo]` run-rustfix`, you would run into an error saying crate name cannot contain `.` characters because the fixed test file trying to be compiled is named `<test-name>.<revision>.fixed`, from which `rustc` infers the crate name to be `<test-name>.<revision>` which is not a valid crate name. This PR fixes the problem by constructing a synthetic crate name from `<test-name>.<revision>`, by 1. replacing all `-` with `_`, and 2. replacing all `.` with `__` and pass that constructed crate name with `--crate-name` to rustc to compile the fixed file. Fixes #123596.
2 parents e78913b + de3857e commit 9d5cdf7

8 files changed

+57
-9
lines changed

src/tools/compiletest/src/runtest.rs

+21-1
Original file line numberDiff line numberDiff line change
@@ -4252,14 +4252,34 @@ impl<'test> TestCx<'test> {
42524252
if self.props.run_rustfix && self.config.compare_mode.is_none() {
42534253
// And finally, compile the fixed code and make sure it both
42544254
// succeeds and has no diagnostics.
4255-
let rustc = self.make_compile_args(
4255+
let mut rustc = self.make_compile_args(
42564256
&self.expected_output_path(UI_FIXED),
42574257
TargetLocation::ThisFile(self.make_exe_name()),
42584258
emit_metadata,
42594259
AllowUnused::No,
42604260
LinkToAux::Yes,
42614261
Vec::new(),
42624262
);
4263+
4264+
// If a test is revisioned, it's fixed source file can be named "a.foo.fixed", which,
4265+
// well, "a.foo" isn't a valid crate name. So we explicitly mangle the test name
4266+
// (including the revision) here to avoid the test writer having to manually specify a
4267+
// `#![crate_name = "..."]` as a workaround. This is okay since we're only checking if
4268+
// the fixed code is compilable.
4269+
if self.revision.is_some() {
4270+
let crate_name =
4271+
self.testpaths.file.file_stem().expect("test must have a file stem");
4272+
// crate name must be alphanumeric or `_`.
4273+
let crate_name =
4274+
crate_name.to_str().expect("crate name implies file name must be valid UTF-8");
4275+
// replace `a.foo` -> `a__foo` for crate name purposes.
4276+
// replace `revision-name-with-dashes` -> `revision_name_with_underscore`
4277+
let crate_name = crate_name.replace(".", "__");
4278+
let crate_name = crate_name.replace("-", "_");
4279+
rustc.arg("--crate-name");
4280+
rustc.arg(crate_name);
4281+
}
4282+
42634283
let res = self.compose_and_run_compiler(rustc, None);
42644284
if !res.status.success() {
42654285
self.fatal_proc_rec("failed to compile fixed code", &res);
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
// Check that revisioned `run-rustfix` does not fail with issues related to `.` in crate name.
2+
//@ revisions: foo
3+
//@[foo] run-rustfix
4+
#![deny(unused_variables)]
5+
6+
fn main() {
7+
let _x = 0usize;
8+
//~^ ERROR unused variable: `x`
9+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
error: unused variable: `x`
2+
--> $DIR/run-rustfix-revisions.rs:7:9
3+
|
4+
LL | let x = 0usize;
5+
| ^ help: if this is intentional, prefix it with an underscore: `_x`
6+
|
7+
note: the lint level is defined here
8+
--> $DIR/run-rustfix-revisions.rs:4:9
9+
|
10+
LL | #![deny(unused_variables)]
11+
| ^^^^^^^^^^^^^^^^
12+
13+
error: aborting due to 1 previous error
14+
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
// Check that revisioned `run-rustfix` does not fail with issues related to `.` in crate name.
2+
//@ revisions: foo
3+
//@[foo] run-rustfix
4+
#![deny(unused_variables)]
5+
6+
fn main() {
7+
let x = 0usize;
8+
//~^ ERROR unused variable: `x`
9+
}

tests/ui/object-safety/bare-trait-dont-suggest-dyn.new.fixed

-2
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,6 @@
22
//@[old] edition:2015
33
//@[new] edition:2021
44
//@[new] run-rustfix
5-
// FIXME: the test suite tries to create a crate called `bare_trait_dont_suggest_dyn.new`
6-
#![crate_name="bare_trait_dont_suggest_dyn"]
75
#![deny(bare_trait_objects)]
86
fn ord_prefer_dot(s: String) -> impl Ord {
97
//~^ ERROR the trait `Ord` cannot be made into an object

tests/ui/object-safety/bare-trait-dont-suggest-dyn.new.stderr

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
error[E0038]: the trait `Ord` cannot be made into an object
2-
--> $DIR/bare-trait-dont-suggest-dyn.rs:8:33
2+
--> $DIR/bare-trait-dont-suggest-dyn.rs:6:33
33
|
44
LL | fn ord_prefer_dot(s: String) -> Ord {
55
| ^^^ `Ord` cannot be made into an object

tests/ui/object-safety/bare-trait-dont-suggest-dyn.old.stderr

+3-3
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,13 @@
11
error: trait objects without an explicit `dyn` are deprecated
2-
--> $DIR/bare-trait-dont-suggest-dyn.rs:8:33
2+
--> $DIR/bare-trait-dont-suggest-dyn.rs:6:33
33
|
44
LL | fn ord_prefer_dot(s: String) -> Ord {
55
| ^^^
66
|
77
= warning: this is accepted in the current edition (Rust 2015) but is a hard error in Rust 2021!
88
= note: for more information, see <https://doc.rust-lang.org/nightly/edition-guide/rust-2021/warnings-promoted-to-error.html>
99
note: the lint level is defined here
10-
--> $DIR/bare-trait-dont-suggest-dyn.rs:7:9
10+
--> $DIR/bare-trait-dont-suggest-dyn.rs:5:9
1111
|
1212
LL | #![deny(bare_trait_objects)]
1313
| ^^^^^^^^^^^^^^^^^^
@@ -17,7 +17,7 @@ LL | fn ord_prefer_dot(s: String) -> dyn Ord {
1717
| +++
1818

1919
error[E0038]: the trait `Ord` cannot be made into an object
20-
--> $DIR/bare-trait-dont-suggest-dyn.rs:8:33
20+
--> $DIR/bare-trait-dont-suggest-dyn.rs:6:33
2121
|
2222
LL | fn ord_prefer_dot(s: String) -> Ord {
2323
| ^^^ `Ord` cannot be made into an object

tests/ui/object-safety/bare-trait-dont-suggest-dyn.rs

-2
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,6 @@
22
//@[old] edition:2015
33
//@[new] edition:2021
44
//@[new] run-rustfix
5-
// FIXME: the test suite tries to create a crate called `bare_trait_dont_suggest_dyn.new`
6-
#![crate_name="bare_trait_dont_suggest_dyn"]
75
#![deny(bare_trait_objects)]
86
fn ord_prefer_dot(s: String) -> Ord {
97
//~^ ERROR the trait `Ord` cannot be made into an object

0 commit comments

Comments
 (0)