Skip to content

Commit 52372f9

Browse files
committed
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
2 parents 005fc0f + cd1d0bc commit 52372f9

14 files changed

+103
-60
lines changed

src/tools/compiletest/src/header.rs

+24-2
Original file line numberDiff line numberDiff line change
@@ -162,6 +162,9 @@ pub struct TestProps {
162162
pub stderr_per_bitwidth: bool,
163163
// The MIR opt to unit test, if any
164164
pub mir_unit_test: Option<String>,
165+
// Whether to tell `rustc` to remap the "src base" directory to a fake
166+
// directory.
167+
pub remap_src_base: bool,
165168
}
166169

167170
mod directives {
@@ -196,6 +199,7 @@ mod directives {
196199
pub const INCREMENTAL: &'static str = "incremental";
197200
pub const KNOWN_BUG: &'static str = "known-bug";
198201
pub const MIR_UNIT_TEST: &'static str = "unit-test";
202+
pub const REMAP_SRC_BASE: &'static str = "remap-src-base";
199203
// This isn't a real directive, just one that is probably mistyped often
200204
pub const INCORRECT_COMPILER_FLAGS: &'static str = "compiler-flags";
201205
}
@@ -241,6 +245,7 @@ impl TestProps {
241245
should_ice: false,
242246
stderr_per_bitwidth: false,
243247
mir_unit_test: None,
248+
remap_src_base: false,
244249
}
245250
}
246251

@@ -273,6 +278,9 @@ impl TestProps {
273278
/// `//[foo]`), then the property is ignored unless `cfg` is
274279
/// `Some("foo")`.
275280
fn load_from(&mut self, testfile: &Path, cfg: Option<&str>, config: &Config) {
281+
// Mode-dependent defaults.
282+
self.remap_src_base = config.mode == Mode::Ui && !config.suite.contains("rustdoc");
283+
276284
let mut has_edition = false;
277285
if !testfile.is_dir() {
278286
let file = File::open(testfile).unwrap();
@@ -438,6 +446,7 @@ impl TestProps {
438446
config.set_name_value_directive(ln, MIR_UNIT_TEST, &mut self.mir_unit_test, |s| {
439447
s.trim().to_string()
440448
});
449+
config.set_name_directive(ln, REMAP_SRC_BASE, &mut self.remap_src_base);
441450
});
442451
}
443452

@@ -730,6 +739,10 @@ impl Config {
730739
&& matches!(line.as_bytes().get(directive.len()), None | Some(&b' ') | Some(&b':'))
731740
}
732741

742+
fn parse_negative_name_directive(&self, line: &str, directive: &str) -> bool {
743+
line.starts_with("no-") && self.parse_name_directive(&line[3..], directive)
744+
}
745+
733746
pub fn parse_name_value_directive(&self, line: &str, directive: &str) -> Option<String> {
734747
let colon = directive.len();
735748
if line.starts_with(directive) && line.as_bytes().get(colon) == Some(&b':') {
@@ -759,8 +772,17 @@ impl Config {
759772
}
760773

761774
fn set_name_directive(&self, line: &str, directive: &str, value: &mut bool) {
762-
if !*value {
763-
*value = self.parse_name_directive(line, directive)
775+
match value {
776+
true => {
777+
if self.parse_negative_name_directive(line, directive) {
778+
*value = false;
779+
}
780+
}
781+
false => {
782+
if self.parse_name_directive(line, directive) {
783+
*value = true;
784+
}
785+
}
764786
}
765787
}
766788

src/tools/compiletest/src/runtest.rs

+30-5
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,8 @@ use debugger::{check_debugger_output, DebuggerCommands};
4444
#[cfg(test)]
4545
mod tests;
4646

47+
const FAKE_SRC_BASE: &str = "fake-test-src-base";
48+
4749
#[cfg(windows)]
4850
fn disable_error_reporting<F: FnOnce() -> R, R>(f: F) -> R {
4951
use std::sync::Mutex;
@@ -1328,12 +1330,19 @@ impl<'test> TestCx<'test> {
13281330
return;
13291331
}
13301332

1333+
// On Windows, translate all '\' path separators to '/'
1334+
let file_name = format!("{}", self.testpaths.file.display()).replace(r"\", "/");
1335+
13311336
// On Windows, keep all '\' path separators to match the paths reported in the JSON output
13321337
// from the compiler
1333-
let os_file_name = self.testpaths.file.display().to_string();
1334-
1335-
// on windows, translate all '\' path separators to '/'
1336-
let file_name = format!("{}", self.testpaths.file.display()).replace(r"\", "/");
1338+
let diagnostic_file_name = if self.props.remap_src_base {
1339+
let mut p = PathBuf::from(FAKE_SRC_BASE);
1340+
p.push(&self.testpaths.relative_dir);
1341+
p.push(self.testpaths.file.file_name().unwrap());
1342+
p.display().to_string()
1343+
} else {
1344+
self.testpaths.file.display().to_string()
1345+
};
13371346

13381347
// If the testcase being checked contains at least one expected "help"
13391348
// message, then we'll ensure that all "help" messages are expected.
@@ -1343,7 +1352,7 @@ impl<'test> TestCx<'test> {
13431352
let expect_note = expected_errors.iter().any(|ee| ee.kind == Some(ErrorKind::Note));
13441353

13451354
// Parse the JSON output from the compiler and extract out the messages.
1346-
let actual_errors = json::parse_output(&os_file_name, &proc_res.stderr, proc_res);
1355+
let actual_errors = json::parse_output(&diagnostic_file_name, &proc_res.stderr, proc_res);
13471356
let mut unexpected = Vec::new();
13481357
let mut found = vec![false; expected_errors.len()];
13491358
for actual_error in &actual_errors {
@@ -1970,6 +1979,14 @@ impl<'test> TestCx<'test> {
19701979
}
19711980
}
19721981

1982+
if self.props.remap_src_base {
1983+
rustc.arg(format!(
1984+
"--remap-path-prefix={}={}",
1985+
self.config.src_base.display(),
1986+
FAKE_SRC_BASE,
1987+
));
1988+
}
1989+
19731990
match emit {
19741991
Emit::None => {}
19751992
Emit::Metadata if is_rustdoc => {}
@@ -3545,6 +3562,14 @@ impl<'test> TestCx<'test> {
35453562
let parent_dir = self.testpaths.file.parent().unwrap();
35463563
normalize_path(parent_dir, "$DIR");
35473564

3565+
if self.props.remap_src_base {
3566+
let mut remapped_parent_dir = PathBuf::from(FAKE_SRC_BASE);
3567+
if self.testpaths.relative_dir != Path::new("") {
3568+
remapped_parent_dir.push(&self.testpaths.relative_dir);
3569+
}
3570+
normalize_path(&remapped_parent_dir, "$DIR");
3571+
}
3572+
35483573
let source_bases = &[
35493574
// Source base on the current filesystem (calculated as parent of `tests/$suite`):
35503575
Some(self.config.src_base.parent().unwrap().parent().unwrap().into()),

tests/ui-fulldeps/mod_dir_path_canonicalized.rs

+1
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
// Testing that a librustc_ast can parse modules with canonicalized base path
33
// ignore-cross-compile
44
// ignore-remote
5+
// no-remap-src-base: Reading `file!()` (expectedly) fails when enabled.
56

67
#![feature(rustc_private)]
78

Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
11
// compile-flags: --remap-path-prefix={{src-base}}/errors/auxiliary=remapped-aux
2+
// no-remap-src-base: Manually remap, so the remapped path remains in .stderr file.
23

34
pub struct SomeStruct {} // This line should be show as part of the error.

tests/ui/errors/remap-path-prefix-reverse.local-self.stderr

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

tests/ui/errors/remap-path-prefix-reverse.remapped-self.stderr

+3-3
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
error[E0423]: expected value, found struct `remapped_dep::SomeStruct`
2-
--> remapped/errors/remap-path-prefix-reverse.rs:22:13
2+
--> $DIR/remap-path-prefix-reverse.rs:16:13
33
|
4-
LL | let _ = remapped_dep::SomeStruct;
4+
LL | let _ = remapped_dep::SomeStruct; // ~ERROR E0423
55
| ^^^^^^^^^^^^^^^^^^^^^^^^ help: use struct literal syntax instead: `remapped_dep::SomeStruct {}`
66
|
7-
::: remapped-aux/remapped_dep.rs:3:1
7+
::: remapped-aux/remapped_dep.rs:4:1
88
|
99
LL | pub struct SomeStruct {} // This line should be show as part of the error.
1010
| --------------------- `remapped_dep::SomeStruct` defined here
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,9 @@
11
// aux-build:remapped_dep.rs
22
// compile-flags: --remap-path-prefix={{src-base}}/errors/auxiliary=remapped-aux
33

4-
// The remapped paths are not normalized by compiletest.
5-
// normalize-stderr-test: "\\(errors)" -> "/$1"
6-
74
// revisions: local-self remapped-self
8-
// [remapped-self]compile-flags: --remap-path-prefix={{src-base}}=remapped
9-
10-
// The paths from `remapped-self` aren't recognized by compiletest, so we
11-
// cannot use line-specific patterns for the actual error.
12-
// error-pattern: E0423
5+
// [local-self] no-remap-src-base: The hack should work regardless of remapping.
6+
// [remapped-self] remap-src-base
137

148
// Verify that the expected source code is shown.
159
// error-pattern: pub struct SomeStruct {} // This line should be show
@@ -19,5 +13,5 @@ extern crate remapped_dep;
1913
fn main() {
2014
// The actual error is irrelevant. The important part it that is should show
2115
// a snippet of the dependency's source.
22-
let _ = remapped_dep::SomeStruct;
16+
let _ = remapped_dep::SomeStruct; // ~ERROR E0423
2317
}

tests/ui/errors/remap-path-prefix.rs

+1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
// compile-flags: --remap-path-prefix={{src-base}}=remapped
2+
// no-remap-src-base: Manually remap, so the remapped path remains in .stderr file.
23

34
// The remapped paths are not normalized by compiletest.
45
// normalize-stderr-test: "\\(errors)" -> "/$1"

tests/ui/errors/remap-path-prefix.stderr

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
error[E0425]: cannot find value `ferris` in this scope
2-
--> remapped/errors/remap-path-prefix.rs:15:5
2+
--> remapped/errors/remap-path-prefix.rs:16:5
33
|
44
LL | ferris
55
| ^^^^^^ not found in this scope

tests/ui/proc-macro/expand-expr.rs

+3-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
11
// aux-build:expand-expr.rs
2+
// no-remap-src-base: check_expand_expr_file!() fails when enabled.
3+
24
#![feature(concat_bytes)]
35
extern crate expand_expr;
46

@@ -8,7 +10,7 @@ use expand_expr::{
810

911
// Check builtin macros can be expanded.
1012

11-
expand_expr_is!(11u32, line!());
13+
expand_expr_is!(13u32, line!());
1214
expand_expr_is!(24u32, column!());
1315

1416
expand_expr_is!("Hello, World!", concat!("Hello, ", "World", "!"));

tests/ui/proc-macro/expand-expr.stderr

+7-7
Original file line numberDiff line numberDiff line change
@@ -1,29 +1,29 @@
11
error: expected one of `.`, `?`, or an operator, found `;`
2-
--> $DIR/expand-expr.rs:106:27
2+
--> $DIR/expand-expr.rs:108:27
33
|
44
LL | expand_expr_fail!("string"; hello);
55
| ^ expected one of `.`, `?`, or an operator
66

77
error: expected expression, found `$`
8-
--> $DIR/expand-expr.rs:109:19
8+
--> $DIR/expand-expr.rs:111:19
99
|
1010
LL | expand_expr_fail!($);
1111
| ^ expected expression
1212

1313
error: expected expression, found `$`
14-
--> $DIR/expand-expr.rs:38:23
14+
--> $DIR/expand-expr.rs:40:23
1515
|
1616
LL | ($($t:tt)*) => { $($t)* };
1717
| ^^^^ expected expression
1818

1919
error: expected expression, found `$`
20-
--> $DIR/expand-expr.rs:111:28
20+
--> $DIR/expand-expr.rs:113:28
2121
|
2222
LL | expand_expr_fail!(echo_pm!($));
2323
| ^ expected expression
2424

2525
error: macro expansion ignores token `hello` and any following
26-
--> $DIR/expand-expr.rs:115:47
26+
--> $DIR/expand-expr.rs:117:47
2727
|
2828
LL | expand_expr_is!("string", echo_tts!("string"; hello));
2929
| --------------------^^^^^- caused by the macro expansion here
@@ -35,7 +35,7 @@ LL | expand_expr_is!("string", echo_tts!("string"; hello););
3535
| +
3636

3737
error: macro expansion ignores token `;` and any following
38-
--> $DIR/expand-expr.rs:116:44
38+
--> $DIR/expand-expr.rs:118:44
3939
|
4040
LL | expand_expr_is!("string", echo_pm!("string"; hello));
4141
| -----------------^------- caused by the macro expansion here
@@ -47,7 +47,7 @@ LL | expand_expr_is!("string", echo_pm!("string"; hello););
4747
| +
4848

4949
error: recursion limit reached while expanding `recursive_expand!`
50-
--> $DIR/expand-expr.rs:124:16
50+
--> $DIR/expand-expr.rs:126:16
5151
|
5252
LL | const _: u32 = recursive_expand!();
5353
| ^^^^^^^^^^^^^^^^^^^

0 commit comments

Comments
 (0)