Auto merge of #105924 - TimNN:ui-remap, r=Mark-Simulacrum

Remap paths in UI tests by default

If you think this needs further discussions / something RFC-like, please let me know the best forum for that.

This PR runs UI tests with a remapped "src base" directory by default.

Why? Because some UI tests currently depend on the length of the absolute path to the `src/test/ui` directory. Remapping makes the tests independent of the absolute path.

The path to the source file (which is absolute on CI) is part of the type name of closures. `rustc` diagnostic output depends on the length of type names (long type names are truncated). So a long absolute path leads to long closure type names, which leads to truncation and changed diagnostics.

(I initially tried just disabling type name truncation, but that made some error messages stupid long (thousands of characters, IIRC)).

Additional changes:

* All boolean `compiletest` directives now support explicit `no-` versions to disable them.
* Adapt existing tests when necessary:
  * Disable remapping for individual tests that fail with it enabled (when there's no obvious alternative fix).
  * For tests that already check something remapping related switch to the new option unless we gain something significant by keeping the manual remap.

Passed Windows CI in https://github.com/rust-lang/rust/actions/runs/3933100590
This commit is contained in:
bors 2023-01-21 17:44:20 +00:00
commit 52372f9c71
14 changed files with 103 additions and 60 deletions

View File

@ -162,6 +162,9 @@ pub struct TestProps {
pub stderr_per_bitwidth: bool,
// The MIR opt to unit test, if any
pub mir_unit_test: Option<String>,
// Whether to tell `rustc` to remap the "src base" directory to a fake
// directory.
pub remap_src_base: bool,
}
mod directives {
@ -196,6 +199,7 @@ mod directives {
pub const INCREMENTAL: &'static str = "incremental";
pub const KNOWN_BUG: &'static str = "known-bug";
pub const MIR_UNIT_TEST: &'static str = "unit-test";
pub const REMAP_SRC_BASE: &'static str = "remap-src-base";
// This isn't a real directive, just one that is probably mistyped often
pub const INCORRECT_COMPILER_FLAGS: &'static str = "compiler-flags";
}
@ -241,6 +245,7 @@ impl TestProps {
should_ice: false,
stderr_per_bitwidth: false,
mir_unit_test: None,
remap_src_base: false,
}
}
@ -273,6 +278,9 @@ impl TestProps {
/// `//[foo]`), then the property is ignored unless `cfg` is
/// `Some("foo")`.
fn load_from(&mut self, testfile: &Path, cfg: Option<&str>, config: &Config) {
// Mode-dependent defaults.
self.remap_src_base = config.mode == Mode::Ui && !config.suite.contains("rustdoc");
let mut has_edition = false;
if !testfile.is_dir() {
let file = File::open(testfile).unwrap();
@ -438,6 +446,7 @@ impl TestProps {
config.set_name_value_directive(ln, MIR_UNIT_TEST, &mut self.mir_unit_test, |s| {
s.trim().to_string()
});
config.set_name_directive(ln, REMAP_SRC_BASE, &mut self.remap_src_base);
});
}
@ -730,6 +739,10 @@ impl Config {
&& matches!(line.as_bytes().get(directive.len()), None | Some(&b' ') | Some(&b':'))
}
fn parse_negative_name_directive(&self, line: &str, directive: &str) -> bool {
line.starts_with("no-") && self.parse_name_directive(&line[3..], directive)
}
pub fn parse_name_value_directive(&self, line: &str, directive: &str) -> Option<String> {
let colon = directive.len();
if line.starts_with(directive) && line.as_bytes().get(colon) == Some(&b':') {
@ -759,8 +772,17 @@ impl Config {
}
fn set_name_directive(&self, line: &str, directive: &str, value: &mut bool) {
if !*value {
*value = self.parse_name_directive(line, directive)
match value {
true => {
if self.parse_negative_name_directive(line, directive) {
*value = false;
}
}
false => {
if self.parse_name_directive(line, directive) {
*value = true;
}
}
}
}

View File

@ -44,6 +44,8 @@ use debugger::{check_debugger_output, DebuggerCommands};
#[cfg(test)]
mod tests;
const FAKE_SRC_BASE: &str = "fake-test-src-base";
#[cfg(windows)]
fn disable_error_reporting<F: FnOnce() -> R, R>(f: F) -> R {
use std::sync::Mutex;
@ -1328,12 +1330,19 @@ impl<'test> TestCx<'test> {
return;
}
// On Windows, translate all '\' path separators to '/'
let file_name = format!("{}", self.testpaths.file.display()).replace(r"\", "/");
// On Windows, keep all '\' path separators to match the paths reported in the JSON output
// from the compiler
let os_file_name = self.testpaths.file.display().to_string();
// on windows, translate all '\' path separators to '/'
let file_name = format!("{}", self.testpaths.file.display()).replace(r"\", "/");
let diagnostic_file_name = if self.props.remap_src_base {
let mut p = PathBuf::from(FAKE_SRC_BASE);
p.push(&self.testpaths.relative_dir);
p.push(self.testpaths.file.file_name().unwrap());
p.display().to_string()
} else {
self.testpaths.file.display().to_string()
};
// If the testcase being checked contains at least one expected "help"
// message, then we'll ensure that all "help" messages are expected.
@ -1343,7 +1352,7 @@ impl<'test> TestCx<'test> {
let expect_note = expected_errors.iter().any(|ee| ee.kind == Some(ErrorKind::Note));
// Parse the JSON output from the compiler and extract out the messages.
let actual_errors = json::parse_output(&os_file_name, &proc_res.stderr, proc_res);
let actual_errors = json::parse_output(&diagnostic_file_name, &proc_res.stderr, proc_res);
let mut unexpected = Vec::new();
let mut found = vec![false; expected_errors.len()];
for actual_error in &actual_errors {
@ -1970,6 +1979,14 @@ impl<'test> TestCx<'test> {
}
}
if self.props.remap_src_base {
rustc.arg(format!(
"--remap-path-prefix={}={}",
self.config.src_base.display(),
FAKE_SRC_BASE,
));
}
match emit {
Emit::None => {}
Emit::Metadata if is_rustdoc => {}
@ -3545,6 +3562,14 @@ impl<'test> TestCx<'test> {
let parent_dir = self.testpaths.file.parent().unwrap();
normalize_path(parent_dir, "$DIR");
if self.props.remap_src_base {
let mut remapped_parent_dir = PathBuf::from(FAKE_SRC_BASE);
if self.testpaths.relative_dir != Path::new("") {
remapped_parent_dir.push(&self.testpaths.relative_dir);
}
normalize_path(&remapped_parent_dir, "$DIR");
}
let source_bases = &[
// Source base on the current filesystem (calculated as parent of `tests/$suite`):
Some(self.config.src_base.parent().unwrap().parent().unwrap().into()),

View File

@ -2,6 +2,7 @@
// Testing that a librustc_ast can parse modules with canonicalized base path
// ignore-cross-compile
// ignore-remote
// no-remap-src-base: Reading `file!()` (expectedly) fails when enabled.
#![feature(rustc_private)]

View File

@ -1,3 +1,4 @@
// compile-flags: --remap-path-prefix={{src-base}}/errors/auxiliary=remapped-aux
// no-remap-src-base: Manually remap, so the remapped path remains in .stderr file.
pub struct SomeStruct {} // This line should be show as part of the error.

View File

@ -1,10 +1,10 @@
error[E0423]: expected value, found struct `remapped_dep::SomeStruct`
--> $DIR/remap-path-prefix-reverse.rs:22:13
--> $DIR/remap-path-prefix-reverse.rs:16:13
|
LL | let _ = remapped_dep::SomeStruct;
LL | let _ = remapped_dep::SomeStruct; // ~ERROR E0423
| ^^^^^^^^^^^^^^^^^^^^^^^^ help: use struct literal syntax instead: `remapped_dep::SomeStruct {}`
|
::: remapped-aux/remapped_dep.rs:3:1
::: remapped-aux/remapped_dep.rs:4:1
|
LL | pub struct SomeStruct {} // This line should be show as part of the error.
| --------------------- `remapped_dep::SomeStruct` defined here

View File

@ -1,10 +1,10 @@
error[E0423]: expected value, found struct `remapped_dep::SomeStruct`
--> remapped/errors/remap-path-prefix-reverse.rs:22:13
--> $DIR/remap-path-prefix-reverse.rs:16:13
|
LL | let _ = remapped_dep::SomeStruct;
LL | let _ = remapped_dep::SomeStruct; // ~ERROR E0423
| ^^^^^^^^^^^^^^^^^^^^^^^^ help: use struct literal syntax instead: `remapped_dep::SomeStruct {}`
|
::: remapped-aux/remapped_dep.rs:3:1
::: remapped-aux/remapped_dep.rs:4:1
|
LL | pub struct SomeStruct {} // This line should be show as part of the error.
| --------------------- `remapped_dep::SomeStruct` defined here

View File

@ -1,15 +1,9 @@
// aux-build:remapped_dep.rs
// compile-flags: --remap-path-prefix={{src-base}}/errors/auxiliary=remapped-aux
// The remapped paths are not normalized by compiletest.
// normalize-stderr-test: "\\(errors)" -> "/$1"
// revisions: local-self remapped-self
// [remapped-self]compile-flags: --remap-path-prefix={{src-base}}=remapped
// The paths from `remapped-self` aren't recognized by compiletest, so we
// cannot use line-specific patterns for the actual error.
// error-pattern: E0423
// [local-self] no-remap-src-base: The hack should work regardless of remapping.
// [remapped-self] remap-src-base
// Verify that the expected source code is shown.
// error-pattern: pub struct SomeStruct {} // This line should be show
@ -19,5 +13,5 @@ extern crate remapped_dep;
fn main() {
// The actual error is irrelevant. The important part it that is should show
// a snippet of the dependency's source.
let _ = remapped_dep::SomeStruct;
let _ = remapped_dep::SomeStruct; // ~ERROR E0423
}

View File

@ -1,4 +1,5 @@
// compile-flags: --remap-path-prefix={{src-base}}=remapped
// no-remap-src-base: Manually remap, so the remapped path remains in .stderr file.
// The remapped paths are not normalized by compiletest.
// normalize-stderr-test: "\\(errors)" -> "/$1"

View File

@ -1,5 +1,5 @@
error[E0425]: cannot find value `ferris` in this scope
--> remapped/errors/remap-path-prefix.rs:15:5
--> remapped/errors/remap-path-prefix.rs:16:5
|
LL | ferris
| ^^^^^^ not found in this scope

View File

@ -1,4 +1,6 @@
// aux-build:expand-expr.rs
// no-remap-src-base: check_expand_expr_file!() fails when enabled.
#![feature(concat_bytes)]
extern crate expand_expr;
@ -8,7 +10,7 @@ use expand_expr::{
// Check builtin macros can be expanded.
expand_expr_is!(11u32, line!());
expand_expr_is!(13u32, line!());
expand_expr_is!(24u32, column!());
expand_expr_is!("Hello, World!", concat!("Hello, ", "World", "!"));

View File

@ -1,29 +1,29 @@
error: expected one of `.`, `?`, or an operator, found `;`
--> $DIR/expand-expr.rs:106:27
--> $DIR/expand-expr.rs:108:27
|
LL | expand_expr_fail!("string"; hello);
| ^ expected one of `.`, `?`, or an operator
error: expected expression, found `$`
--> $DIR/expand-expr.rs:109:19
--> $DIR/expand-expr.rs:111:19
|
LL | expand_expr_fail!($);
| ^ expected expression
error: expected expression, found `$`
--> $DIR/expand-expr.rs:38:23
--> $DIR/expand-expr.rs:40:23
|
LL | ($($t:tt)*) => { $($t)* };
| ^^^^ expected expression
error: expected expression, found `$`
--> $DIR/expand-expr.rs:111:28
--> $DIR/expand-expr.rs:113:28
|
LL | expand_expr_fail!(echo_pm!($));
| ^ expected expression
error: macro expansion ignores token `hello` and any following
--> $DIR/expand-expr.rs:115:47
--> $DIR/expand-expr.rs:117:47
|
LL | expand_expr_is!("string", echo_tts!("string"; hello));
| --------------------^^^^^- caused by the macro expansion here
@ -35,7 +35,7 @@ LL | expand_expr_is!("string", echo_tts!("string"; hello););
| +
error: macro expansion ignores token `;` and any following
--> $DIR/expand-expr.rs:116:44
--> $DIR/expand-expr.rs:118:44
|
LL | expand_expr_is!("string", echo_pm!("string"; hello));
| -----------------^------- caused by the macro expansion here
@ -47,7 +47,7 @@ LL | expand_expr_is!("string", echo_pm!("string"; hello););
| +
error: recursion limit reached while expanding `recursive_expand!`
--> $DIR/expand-expr.rs:124:16
--> $DIR/expand-expr.rs:126:16
|
LL | const _: u32 = recursive_expand!();
| ^^^^^^^^^^^^^^^^^^^

View File

@ -1,5 +1,5 @@
error: using an old version of `rental`
--> remapped/proc-macro/pretty-print-hack/allsorts-rental-0.5.6/src/lib.rs:4:6
--> $DIR/pretty-print-hack/allsorts-rental-0.5.6/src/lib.rs:4:6
|
LL | enum ProceduralMasqueradeDummyType {
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
@ -10,7 +10,7 @@ LL | enum ProceduralMasqueradeDummyType {
= note: `#[deny(proc_macro_back_compat)]` on by default
error: using an old version of `rental`
--> remapped/proc-macro/pretty-print-hack/allsorts-rental-0.5.6/src/lib.rs:4:6
--> $DIR/pretty-print-hack/allsorts-rental-0.5.6/src/lib.rs:4:6
|
LL | enum ProceduralMasqueradeDummyType {
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
@ -20,7 +20,7 @@ LL | enum ProceduralMasqueradeDummyType {
= note: older versions of the `rental` crate will stop compiling in future versions of Rust; please update to `rental` v0.5.6, or switch to one of the `rental` alternatives
error: using an old version of `rental`
--> remapped/proc-macro/pretty-print-hack/allsorts-rental-0.5.6/src/lib.rs:4:6
--> $DIR/pretty-print-hack/allsorts-rental-0.5.6/src/lib.rs:4:6
|
LL | enum ProceduralMasqueradeDummyType {
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
@ -30,7 +30,7 @@ LL | enum ProceduralMasqueradeDummyType {
= note: older versions of the `rental` crate will stop compiling in future versions of Rust; please update to `rental` v0.5.6, or switch to one of the `rental` alternatives
error: using an old version of `rental`
--> remapped/proc-macro/pretty-print-hack/allsorts-rental-0.5.6/src/lib.rs:4:6
--> $DIR/pretty-print-hack/allsorts-rental-0.5.6/src/lib.rs:4:6
|
LL | enum ProceduralMasqueradeDummyType {
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
@ -40,7 +40,7 @@ LL | enum ProceduralMasqueradeDummyType {
= note: older versions of the `rental` crate will stop compiling in future versions of Rust; please update to `rental` v0.5.6, or switch to one of the `rental` alternatives
error: using an old version of `rental`
--> remapped/proc-macro/pretty-print-hack/rental-0.5.5/src/lib.rs:4:6
--> $DIR/pretty-print-hack/rental-0.5.5/src/lib.rs:4:6
|
LL | enum ProceduralMasqueradeDummyType {
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
@ -50,7 +50,7 @@ LL | enum ProceduralMasqueradeDummyType {
= note: older versions of the `rental` crate will stop compiling in future versions of Rust; please update to `rental` v0.5.6, or switch to one of the `rental` alternatives
error: using an old version of `rental`
--> remapped/proc-macro/pretty-print-hack/rental-0.5.5/src/lib.rs:4:6
--> $DIR/pretty-print-hack/rental-0.5.5/src/lib.rs:4:6
|
LL | enum ProceduralMasqueradeDummyType {
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
@ -60,7 +60,7 @@ LL | enum ProceduralMasqueradeDummyType {
= note: older versions of the `rental` crate will stop compiling in future versions of Rust; please update to `rental` v0.5.6, or switch to one of the `rental` alternatives
error: using an old version of `rental`
--> remapped/proc-macro/pretty-print-hack/rental-0.5.5/src/lib.rs:4:6
--> $DIR/pretty-print-hack/rental-0.5.5/src/lib.rs:4:6
|
LL | enum ProceduralMasqueradeDummyType {
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
@ -70,7 +70,7 @@ LL | enum ProceduralMasqueradeDummyType {
= note: older versions of the `rental` crate will stop compiling in future versions of Rust; please update to `rental` v0.5.6, or switch to one of the `rental` alternatives
error: using an old version of `rental`
--> remapped/proc-macro/pretty-print-hack/rental-0.5.5/src/lib.rs:4:6
--> $DIR/pretty-print-hack/rental-0.5.5/src/lib.rs:4:6
|
LL | enum ProceduralMasqueradeDummyType {
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
@ -83,7 +83,7 @@ error: aborting due to 8 previous errors
Future incompatibility report: Future breakage diagnostic:
error: using an old version of `rental`
--> remapped/proc-macro/pretty-print-hack/allsorts-rental-0.5.6/src/lib.rs:4:6
--> $DIR/pretty-print-hack/allsorts-rental-0.5.6/src/lib.rs:4:6
|
LL | enum ProceduralMasqueradeDummyType {
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
@ -95,7 +95,7 @@ LL | enum ProceduralMasqueradeDummyType {
Future breakage diagnostic:
error: using an old version of `rental`
--> remapped/proc-macro/pretty-print-hack/allsorts-rental-0.5.6/src/lib.rs:4:6
--> $DIR/pretty-print-hack/allsorts-rental-0.5.6/src/lib.rs:4:6
|
LL | enum ProceduralMasqueradeDummyType {
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
@ -107,7 +107,7 @@ LL | enum ProceduralMasqueradeDummyType {
Future breakage diagnostic:
error: using an old version of `rental`
--> remapped/proc-macro/pretty-print-hack/allsorts-rental-0.5.6/src/lib.rs:4:6
--> $DIR/pretty-print-hack/allsorts-rental-0.5.6/src/lib.rs:4:6
|
LL | enum ProceduralMasqueradeDummyType {
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
@ -119,7 +119,7 @@ LL | enum ProceduralMasqueradeDummyType {
Future breakage diagnostic:
error: using an old version of `rental`
--> remapped/proc-macro/pretty-print-hack/allsorts-rental-0.5.6/src/lib.rs:4:6
--> $DIR/pretty-print-hack/allsorts-rental-0.5.6/src/lib.rs:4:6
|
LL | enum ProceduralMasqueradeDummyType {
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
@ -131,7 +131,7 @@ LL | enum ProceduralMasqueradeDummyType {
Future breakage diagnostic:
error: using an old version of `rental`
--> remapped/proc-macro/pretty-print-hack/rental-0.5.5/src/lib.rs:4:6
--> $DIR/pretty-print-hack/rental-0.5.5/src/lib.rs:4:6
|
LL | enum ProceduralMasqueradeDummyType {
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
@ -143,7 +143,7 @@ LL | enum ProceduralMasqueradeDummyType {
Future breakage diagnostic:
error: using an old version of `rental`
--> remapped/proc-macro/pretty-print-hack/rental-0.5.5/src/lib.rs:4:6
--> $DIR/pretty-print-hack/rental-0.5.5/src/lib.rs:4:6
|
LL | enum ProceduralMasqueradeDummyType {
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
@ -155,7 +155,7 @@ LL | enum ProceduralMasqueradeDummyType {
Future breakage diagnostic:
error: using an old version of `rental`
--> remapped/proc-macro/pretty-print-hack/rental-0.5.5/src/lib.rs:4:6
--> $DIR/pretty-print-hack/rental-0.5.5/src/lib.rs:4:6
|
LL | enum ProceduralMasqueradeDummyType {
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
@ -167,7 +167,7 @@ LL | enum ProceduralMasqueradeDummyType {
Future breakage diagnostic:
error: using an old version of `rental`
--> remapped/proc-macro/pretty-print-hack/rental-0.5.5/src/lib.rs:4:6
--> $DIR/pretty-print-hack/rental-0.5.5/src/lib.rs:4:6
|
LL | enum ProceduralMasqueradeDummyType {
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

View File

@ -3,21 +3,21 @@ PRINT-DERIVE RE-COLLECTED (DISPLAY): enum ProceduralMasqueradeDummyType { Input
PRINT-DERIVE INPUT (DEBUG): TokenStream [
Ident {
ident: "enum",
span: remapped/proc-macro/pretty-print-hack/allsorts-rental-0.5.6/src/lib.rs:4:1: 4:5 (#0),
span: $DIR/pretty-print-hack/allsorts-rental-0.5.6/src/lib.rs:4:1: 4:5 (#0),
},
Ident {
ident: "ProceduralMasqueradeDummyType",
span: remapped/proc-macro/pretty-print-hack/allsorts-rental-0.5.6/src/lib.rs:4:6: 4:35 (#0),
span: $DIR/pretty-print-hack/allsorts-rental-0.5.6/src/lib.rs:4:6: 4:35 (#0),
},
Group {
delimiter: Brace,
stream: TokenStream [
Ident {
ident: "Input",
span: remapped/proc-macro/pretty-print-hack/allsorts-rental-0.5.6/src/lib.rs:13:5: 13:10 (#0),
span: $DIR/pretty-print-hack/allsorts-rental-0.5.6/src/lib.rs:13:5: 13:10 (#0),
},
],
span: remapped/proc-macro/pretty-print-hack/allsorts-rental-0.5.6/src/lib.rs:4:36: 14:2 (#0),
span: $DIR/pretty-print-hack/allsorts-rental-0.5.6/src/lib.rs:4:36: 14:2 (#0),
},
]
PRINT-DERIVE INPUT (DISPLAY): enum ProceduralMasqueradeDummyType { Input, }
@ -25,20 +25,20 @@ PRINT-DERIVE RE-COLLECTED (DISPLAY): enum ProceduralMasqueradeDummyType { Input
PRINT-DERIVE INPUT (DEBUG): TokenStream [
Ident {
ident: "enum",
span: remapped/proc-macro/pretty-print-hack/rental-0.5.5/src/lib.rs:4:1: 4:5 (#0),
span: $DIR/pretty-print-hack/rental-0.5.5/src/lib.rs:4:1: 4:5 (#0),
},
Ident {
ident: "ProceduralMasqueradeDummyType",
span: remapped/proc-macro/pretty-print-hack/rental-0.5.5/src/lib.rs:4:6: 4:35 (#0),
span: $DIR/pretty-print-hack/rental-0.5.5/src/lib.rs:4:6: 4:35 (#0),
},
Group {
delimiter: Brace,
stream: TokenStream [
Ident {
ident: "Input",
span: remapped/proc-macro/pretty-print-hack/rental-0.5.5/src/lib.rs:13:5: 13:10 (#0),
span: $DIR/pretty-print-hack/rental-0.5.5/src/lib.rs:13:5: 13:10 (#0),
},
],
span: remapped/proc-macro/pretty-print-hack/rental-0.5.5/src/lib.rs:4:36: 14:2 (#0),
span: $DIR/pretty-print-hack/rental-0.5.5/src/lib.rs:4:36: 14:2 (#0),
},
]

View File

@ -1,11 +1,8 @@
// aux-build:test-macros.rs
// compile-flags: -Z span-debug
// revisions: local remapped
// [remapped]compile-flags: --remap-path-prefix={{src-base}}=remapped
// The remapped paths are not normalized by compiletest.
// normalize-stdout-test: "\\(proc-macro|pretty-print-hack)" -> "/$1"
// normalize-stderr-test: "\\(proc-macro|pretty-print-hack)" -> "/$1"
// [local] no-remap-src-base: The hack should work regardless of remapping.
// [remapped] remap-src-base
#![no_std] // Don't load unnecessary hygiene information from std
extern crate std;