Skip to content

Commit 4582ecd

Browse files
committed
compiletest: Support custom normalization rules.
1 parent 18712e6 commit 4582ecd

9 files changed

+112
-80
lines changed

src/test/ui/enum-size-variance.rs

+7-11
Original file line numberDiff line numberDiff line change
@@ -9,10 +9,6 @@
99
// except according to those terms.
1010

1111
// run-pass
12-
// ignore-x86
13-
// ignore-arm
14-
// ignore-emscripten
15-
// ^ ignore 32-bit targets, as the error message is target-dependent. see PR #41968.
1612

1713
#![warn(variant_size_differences)]
1814
#![allow(dead_code)]
@@ -24,26 +20,26 @@ enum Enum1 { }
2420

2521
enum Enum2 { A, B, C }
2622

27-
enum Enum3 { D(isize), E, F }
23+
enum Enum3 { D(i64), E, F }
2824

29-
enum Enum4 { H(isize), I(isize), J }
25+
enum Enum4 { H(i64), I(i64), J }
3026

3127
enum Enum5 {
32-
L(isize, isize, isize, isize), //~ WARNING three times larger
33-
M(isize),
28+
L(i64, i64, i64, i64), //~ WARNING three times larger
29+
M(i64),
3430
N
3531
}
3632

3733
enum Enum6<T, U> {
3834
O(T),
3935
P(U),
40-
Q(isize)
36+
Q(i64)
4137
}
4238

4339
#[allow(variant_size_differences)]
4440
enum Enum7 {
45-
R(isize, isize, isize, isize),
46-
S(isize),
41+
R(i64, i64, i64, i64),
42+
S(i64),
4743
T
4844
}
4945
pub fn main() { }

src/test/ui/enum-size-variance.stderr

+5-5
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
11
warning: enum variant is more than three times larger (32 bytes) than the next largest
2-
--> $DIR/enum-size-variance.rs:32:5
2+
--> $DIR/enum-size-variance.rs:28:5
33
|
4-
32 | L(isize, isize, isize, isize), //~ WARNING three times larger
5-
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
4+
28 | L(i64, i64, i64, i64), //~ WARNING three times larger
5+
| ^^^^^^^^^^^^^^^^^^^^^
66
|
77
note: lint level defined here
8-
--> $DIR/enum-size-variance.rs:17:9
8+
--> $DIR/enum-size-variance.rs:13:9
99
|
10-
17 | #![warn(variant_size_differences)]
10+
13 | #![warn(variant_size_differences)]
1111
| ^^^^^^^^^^^^^^^^^^^^^^^^
1212

src/test/ui/transmute/main.rs

+4-4
Original file line numberDiff line numberDiff line change
@@ -8,10 +8,10 @@
88
// option. This file may not be copied, modified, or distributed
99
// except according to those terms.
1010

11-
// ignore-x86
12-
// ignore-arm
13-
// ignore-emscripten
14-
// ignore 32-bit platforms (test output is different)
11+
// normalize-stderr-32bit: "&str (64 bits)" -> "&str ($STR bits)"
12+
// normalize-stderr-64bit: "&str (128 bits)" -> "&str ($STR bits)"
13+
14+
1515

1616
#![feature(untagged_unions)]
1717
use std::mem::transmute;

src/test/ui/transmute/main.stderr

+1-1
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ error[E0512]: transmute called with types of different sizes
2222
34 | let x: u8 = transmute("test"); //~ ERROR transmute called with types of different sizes
2323
| ^^^^^^^^^
2424
|
25-
= note: source type: &str (128 bits)
25+
= note: source type: &str ($STR bits)
2626
= note: target type: u8 (8 bits)
2727

2828
error[E0512]: transmute called with types of different sizes

src/test/ui/transmute/transmute-from-fn-item-types-error.rs

+3-8
Original file line numberDiff line numberDiff line change
@@ -8,14 +8,9 @@
88
// option. This file may not be copied, modified, or distributed
99
// except according to those terms.
1010

11-
// ignore-x86
12-
// ignore-arm
13-
// ignore-emscripten
14-
// ignore 32-bit platforms (test output is different)
15-
1611
use std::mem;
1712

18-
unsafe fn foo() -> (i32, *const (), Option<fn()>) {
13+
unsafe fn foo() -> (i8, *const (), Option<fn()>) {
1914
let i = mem::transmute(bar);
2015
//~^ ERROR is zero-sized and can't be transmuted
2116
//~^^ NOTE cast with `as` to a pointer instead
@@ -46,7 +41,7 @@ unsafe fn bar() {
4641
//~^^ NOTE cast with `as` to a pointer instead
4742

4843
// No error if a coercion would otherwise occur.
49-
mem::transmute::<fn(), u32>(main);
44+
mem::transmute::<fn(), usize>(main);
5045
}
5146

5247
unsafe fn baz() {
@@ -63,7 +58,7 @@ unsafe fn baz() {
6358
//~^^ NOTE cast with `as` to a pointer instead
6459

6560
// No error if a coercion would otherwise occur.
66-
mem::transmute::<Option<fn()>, u32>(Some(main));
61+
mem::transmute::<Option<fn()>, usize>(Some(main));
6762
}
6863

6964
fn main() {
Original file line numberDiff line numberDiff line change
@@ -1,108 +1,90 @@
11
error[E0512]: transmute called with types of different sizes
2-
--> $DIR/transmute-from-fn-item-types-error.rs:19:13
2+
--> $DIR/transmute-from-fn-item-types-error.rs:14:13
33
|
4-
19 | let i = mem::transmute(bar);
4+
14 | let i = mem::transmute(bar);
55
| ^^^^^^^^^^^^^^
66
|
77
= note: source type: unsafe fn() {bar} (0 bits)
8-
= note: target type: i32 (32 bits)
8+
= note: target type: i8 (8 bits)
99

1010
error[E0591]: can't transmute zero-sized type
11-
--> $DIR/transmute-from-fn-item-types-error.rs:23:13
11+
--> $DIR/transmute-from-fn-item-types-error.rs:18:13
1212
|
13-
23 | let p = mem::transmute(foo);
13+
18 | let p = mem::transmute(foo);
1414
| ^^^^^^^^^^^^^^
1515
|
16-
= note: source type: unsafe fn() -> (i32, *const (), std::option::Option<fn()>) {foo}
16+
= note: source type: unsafe fn() -> (i8, *const (), std::option::Option<fn()>) {foo}
1717
= note: target type: *const ()
1818
= help: cast with `as` to a pointer instead
1919

2020
error[E0591]: can't transmute zero-sized type
21-
--> $DIR/transmute-from-fn-item-types-error.rs:27:14
21+
--> $DIR/transmute-from-fn-item-types-error.rs:22:14
2222
|
23-
27 | let of = mem::transmute(main);
23+
22 | let of = mem::transmute(main);
2424
| ^^^^^^^^^^^^^^
2525
|
2626
= note: source type: fn() {main}
2727
= note: target type: std::option::Option<fn()>
2828
= help: cast with `as` to a pointer instead
2929

3030
error[E0512]: transmute called with types of different sizes
31-
--> $DIR/transmute-from-fn-item-types-error.rs:36:5
31+
--> $DIR/transmute-from-fn-item-types-error.rs:31:5
3232
|
33-
36 | mem::transmute::<_, u8>(main);
33+
31 | mem::transmute::<_, u8>(main);
3434
| ^^^^^^^^^^^^^^^^^^^^^^^
3535
|
3636
= note: source type: fn() {main} (0 bits)
3737
= note: target type: u8 (8 bits)
3838

3939
error[E0591]: can't transmute zero-sized type
40-
--> $DIR/transmute-from-fn-item-types-error.rs:40:5
40+
--> $DIR/transmute-from-fn-item-types-error.rs:35:5
4141
|
42-
40 | mem::transmute::<_, *mut ()>(foo);
42+
35 | mem::transmute::<_, *mut ()>(foo);
4343
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^
4444
|
45-
= note: source type: unsafe fn() -> (i32, *const (), std::option::Option<fn()>) {foo}
45+
= note: source type: unsafe fn() -> (i8, *const (), std::option::Option<fn()>) {foo}
4646
= note: target type: *mut ()
4747
= help: cast with `as` to a pointer instead
4848

4949
error[E0591]: can't transmute zero-sized type
50-
--> $DIR/transmute-from-fn-item-types-error.rs:44:5
50+
--> $DIR/transmute-from-fn-item-types-error.rs:39:5
5151
|
52-
44 | mem::transmute::<_, fn()>(bar);
52+
39 | mem::transmute::<_, fn()>(bar);
5353
| ^^^^^^^^^^^^^^^^^^^^^^^^^
5454
|
5555
= note: source type: unsafe fn() {bar}
5656
= note: target type: fn()
5757
= help: cast with `as` to a pointer instead
5858

59-
error[E0512]: transmute called with types of different sizes
60-
--> $DIR/transmute-from-fn-item-types-error.rs:49:5
61-
|
62-
49 | mem::transmute::<fn(), u32>(main);
63-
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^
64-
|
65-
= note: source type: fn() (64 bits)
66-
= note: target type: u32 (32 bits)
67-
6859
error[E0591]: can't transmute zero-sized type
69-
--> $DIR/transmute-from-fn-item-types-error.rs:53:5
60+
--> $DIR/transmute-from-fn-item-types-error.rs:48:5
7061
|
71-
53 | mem::transmute::<_, *mut ()>(Some(foo));
62+
48 | mem::transmute::<_, *mut ()>(Some(foo));
7263
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^
7364
|
74-
= note: source type: unsafe fn() -> (i32, *const (), std::option::Option<fn()>) {foo}
65+
= note: source type: unsafe fn() -> (i8, *const (), std::option::Option<fn()>) {foo}
7566
= note: target type: *mut ()
7667
= help: cast with `as` to a pointer instead
7768

7869
error[E0591]: can't transmute zero-sized type
79-
--> $DIR/transmute-from-fn-item-types-error.rs:57:5
70+
--> $DIR/transmute-from-fn-item-types-error.rs:52:5
8071
|
81-
57 | mem::transmute::<_, fn()>(Some(bar));
72+
52 | mem::transmute::<_, fn()>(Some(bar));
8273
| ^^^^^^^^^^^^^^^^^^^^^^^^^
8374
|
8475
= note: source type: unsafe fn() {bar}
8576
= note: target type: fn()
8677
= help: cast with `as` to a pointer instead
8778

8879
error[E0591]: can't transmute zero-sized type
89-
--> $DIR/transmute-from-fn-item-types-error.rs:61:5
80+
--> $DIR/transmute-from-fn-item-types-error.rs:56:5
9081
|
91-
61 | mem::transmute::<_, Option<fn()>>(Some(baz));
82+
56 | mem::transmute::<_, Option<fn()>>(Some(baz));
9283
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
9384
|
9485
= note: source type: unsafe fn() {baz}
9586
= note: target type: std::option::Option<fn()>
9687
= help: cast with `as` to a pointer instead
9788

98-
error[E0512]: transmute called with types of different sizes
99-
--> $DIR/transmute-from-fn-item-types-error.rs:66:5
100-
|
101-
66 | mem::transmute::<Option<fn()>, u32>(Some(main));
102-
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
103-
|
104-
= note: source type: std::option::Option<fn()> (64 bits)
105-
= note: target type: u32 (32 bits)
106-
107-
error: aborting due to 11 previous errors
89+
error: aborting due to 9 previous errors
10890

src/test/ui/transmute/transmute-type-parameters.rs

+4-4
Original file line numberDiff line numberDiff line change
@@ -8,10 +8,10 @@
88
// option. This file may not be copied, modified, or distributed
99
// except according to those terms.
1010

11-
// ignore-x86
12-
// ignore-arm
13-
// ignore-emscripten
14-
// ignore 32-bit platforms (test output is different)
11+
12+
13+
14+
1515

1616
// Tests that `transmute` cannot be called on type parameters.
1717

src/tools/compiletest/src/header.rs

+54-1
Original file line numberDiff line numberDiff line change
@@ -211,6 +211,9 @@ pub struct TestProps {
211211
// The test must be compiled and run successfully. Only used in UI tests for
212212
// now.
213213
pub run_pass: bool,
214+
// customized normalization rules
215+
pub normalize_stdout: Vec<(String, String)>,
216+
pub normalize_stderr: Vec<(String, String)>,
214217
}
215218

216219
impl TestProps {
@@ -237,6 +240,8 @@ impl TestProps {
237240
must_compile_successfully: false,
238241
check_test_line_numbers_match: false,
239242
run_pass: false,
243+
normalize_stdout: vec![],
244+
normalize_stderr: vec![],
240245
}
241246
}
242247

@@ -351,6 +356,13 @@ impl TestProps {
351356
if !self.run_pass {
352357
self.run_pass = config.parse_run_pass(ln);
353358
}
359+
360+
if let Some(rule) = config.parse_custom_normalization(ln, "normalize-stdout") {
361+
self.normalize_stdout.push(rule);
362+
}
363+
if let Some(rule) = config.parse_custom_normalization(ln, "normalize-stderr") {
364+
self.normalize_stderr.push(rule);
365+
}
354366
});
355367

356368
for key in &["RUST_TEST_NOCAPTURE", "RUST_TEST_THREADS"] {
@@ -399,7 +411,6 @@ fn iter_header(testfile: &Path, cfg: Option<&str>, it: &mut FnMut(&str)) {
399411
}
400412

401413
impl Config {
402-
403414
fn parse_error_pattern(&self, line: &str) -> Option<String> {
404415
self.parse_name_value_directive(line, "error-pattern")
405416
}
@@ -497,6 +508,22 @@ impl Config {
497508
}
498509
}
499510

511+
fn parse_custom_normalization(&self, mut line: &str, prefix: &str) -> Option<(String, String)> {
512+
if self.parse_cfg_name_directive(line, prefix) {
513+
let from = match parse_normalization_string(&mut line) {
514+
Some(s) => s,
515+
None => return None,
516+
};
517+
let to = match parse_normalization_string(&mut line) {
518+
Some(s) => s,
519+
None => return None,
520+
};
521+
Some((from, to))
522+
} else {
523+
None
524+
}
525+
}
526+
500527
/// Parses a name-value directive which contains config-specific information, e.g. `ignore-x86`
501528
/// or `normalize-stderr-32bit`. Returns `true` if the line matches it.
502529
fn parse_cfg_name_directive(&self, line: &str, prefix: &str) -> bool {
@@ -568,3 +595,29 @@ fn expand_variables(mut value: String, config: &Config) -> String {
568595

569596
value
570597
}
598+
599+
/// Finds the next quoted string `"..."` in `line`, and extract the content from it. Move the `line`
600+
/// variable after the end of the quoted string.
601+
///
602+
/// # Examples
603+
///
604+
/// ```
605+
/// let mut s = "normalize-stderr-32bit: \"something (32 bits)\" -> \"something ($WORD bits)\".";
606+
/// let first = parse_normalization_string(&mut s);
607+
/// assert_eq!(first, Some("something (32 bits)".to_owned()));
608+
/// assert_eq!(s, " -> \"something ($WORD bits)\".");
609+
/// ```
610+
fn parse_normalization_string(line: &mut &str) -> Option<String> {
611+
// FIXME support escapes in strings.
612+
let begin = match line.find('"') {
613+
Some(i) => i + 1,
614+
None => return None,
615+
};
616+
let end = match line[begin..].find('"') {
617+
Some(i) => i + begin,
618+
None => return None,
619+
};
620+
let result = line[begin..end].to_owned();
621+
*line = &line[end+1..];
622+
Some(result)
623+
}

src/tools/compiletest/src/runtest.rs

+11-5
Original file line numberDiff line numberDiff line change
@@ -2228,8 +2228,10 @@ actual:\n\
22282228
let expected_stdout_path = self.expected_output_path("stdout");
22292229
let expected_stdout = self.load_expected_output(&expected_stdout_path);
22302230

2231-
let normalized_stdout = self.normalize_output(&proc_res.stdout);
2232-
let normalized_stderr = self.normalize_output(&proc_res.stderr);
2231+
let normalized_stdout =
2232+
self.normalize_output(&proc_res.stdout, &self.props.normalize_stdout);
2233+
let normalized_stderr =
2234+
self.normalize_output(&proc_res.stderr, &self.props.normalize_stderr);
22332235

22342236
let mut errors = 0;
22352237
errors += self.compare_output("stdout", &normalized_stdout, &expected_stdout);
@@ -2375,13 +2377,17 @@ actual:\n\
23752377
mir_dump_dir
23762378
}
23772379

2378-
fn normalize_output(&self, output: &str) -> String {
2380+
fn normalize_output(&self, output: &str, custom_rules: &[(String, String)]) -> String {
23792381
let parent_dir = self.testpaths.file.parent().unwrap();
23802382
let parent_dir_str = parent_dir.display().to_string();
2381-
output.replace(&parent_dir_str, "$DIR")
2383+
let mut normalized = output.replace(&parent_dir_str, "$DIR")
23822384
.replace("\\", "/") // normalize for paths on windows
23832385
.replace("\r\n", "\n") // normalize for linebreaks on windows
2384-
.replace("\t", "\\t") // makes tabs visible
2386+
.replace("\t", "\\t"); // makes tabs visible
2387+
for rule in custom_rules {
2388+
normalized = normalized.replace(&rule.0, &rule.1);
2389+
}
2390+
normalized
23852391
}
23862392

23872393
fn expected_output_path(&self, kind: &str) -> PathBuf {

0 commit comments

Comments
 (0)