Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Remap paths in UI tests by default #105924

Merged
merged 3 commits into from
Jan 21, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 24 additions & 2 deletions src/tools/compiletest/src/header.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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";
}
Expand Down Expand Up @@ -241,6 +245,7 @@ impl TestProps {
should_ice: false,
stderr_per_bitwidth: false,
mir_unit_test: None,
remap_src_base: false,
}
}

Expand Down Expand Up @@ -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();
Expand Down Expand Up @@ -433,6 +441,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);
});
}

Expand Down Expand Up @@ -725,6 +734,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':') {
Expand Down Expand Up @@ -754,8 +767,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;
}
}
}
}

Expand Down
35 changes: 30 additions & 5 deletions src/tools/compiletest/src/runtest.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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.
Expand All @@ -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 {
Expand Down Expand Up @@ -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 => {}
Expand Down Expand Up @@ -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()),
Expand Down
1 change: 1 addition & 0 deletions tests/ui-fulldeps/mod_dir_path_canonicalized.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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)]

Expand Down
1 change: 1 addition & 0 deletions tests/ui/errors/auxiliary/remapped_dep.rs
Original file line number Diff line number Diff line change
@@ -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.
6 changes: 3 additions & 3 deletions tests/ui/errors/remap-path-prefix-reverse.local-self.stderr
Original file line number Diff line number Diff line change
@@ -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
Expand Down
Original file line number Diff line number Diff line change
@@ -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
Expand Down
12 changes: 3 additions & 9 deletions tests/ui/errors/remap-path-prefix-reverse.rs
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -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
}
1 change: 1 addition & 0 deletions tests/ui/errors/remap-path-prefix.rs
Original file line number Diff line number Diff line change
@@ -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"
Expand Down
2 changes: 1 addition & 1 deletion tests/ui/errors/remap-path-prefix.stderr
Original file line number Diff line number Diff line change
@@ -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
Expand Down
4 changes: 3 additions & 1 deletion tests/ui/proc-macro/expand-expr.rs
Original file line number Diff line number Diff line change
@@ -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;

Expand All @@ -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", "!"));
Expand Down
14 changes: 7 additions & 7 deletions tests/ui/proc-macro/expand-expr.stderr
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -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
Expand All @@ -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!();
| ^^^^^^^^^^^^^^^^^^^
Expand Down
Loading