Skip to content

Commit fef27bc

Browse files
committed
Add compiletest directive show-std-source
Signed-off-by: xizheyin <[email protected]>
1 parent f8e355c commit fef27bc

File tree

6 files changed

+146
-10
lines changed

6 files changed

+146
-10
lines changed

src/tools/compiletest/src/directives.rs

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -197,6 +197,10 @@ pub struct TestProps {
197197
/// Build and use `minicore` as `core` stub for `no_core` tests in cross-compilation scenarios
198198
/// that don't otherwise want/need `-Z build-std`.
199199
pub add_core_stubs: bool,
200+
/// Whether to show std library source code in error messages and backtraces.
201+
/// When false (default), std source paths are remapped to "/rustc/FAKE_PREFIX".
202+
/// When true, actual std source paths are shown.
203+
pub show_std_source: bool,
200204
/// Whether line annotatins are required for the given error kind.
201205
pub dont_require_annotations: HashSet<ErrorKind>,
202206
}
@@ -245,6 +249,7 @@ mod directives {
245249
pub const FILECHECK_FLAGS: &'static str = "filecheck-flags";
246250
pub const NO_AUTO_CHECK_CFG: &'static str = "no-auto-check-cfg";
247251
pub const ADD_CORE_STUBS: &'static str = "add-core-stubs";
252+
pub const SHOW_STD_SOURCE: &'static str = "show-std-source";
248253
// This isn't a real directive, just one that is probably mistyped often
249254
pub const INCORRECT_COMPILER_FLAGS: &'static str = "compiler-flags";
250255
}
@@ -301,6 +306,7 @@ impl TestProps {
301306
no_auto_check_cfg: false,
302307
has_enzyme: false,
303308
add_core_stubs: false,
309+
show_std_source: false,
304310
dont_require_annotations: Default::default(),
305311
}
306312
}
@@ -588,6 +594,8 @@ impl TestProps {
588594

589595
self.update_add_core_stubs(ln, config);
590596

597+
config.set_name_directive(ln, SHOW_STD_SOURCE, &mut self.show_std_source);
598+
591599
if let Some(err_kind) =
592600
config.parse_name_value_directive(ln, DONT_REQUIRE_ANNOTATIONS)
593601
{
@@ -1027,6 +1035,7 @@ const KNOWN_DIRECTIVE_NAMES: &[&str] = &[
10271035
"rustfix-only-machine-applicable",
10281036
"should-fail",
10291037
"should-ice",
1038+
"show-std-source",
10301039
"stderr-per-bitwidth",
10311040
"test-mir-pass",
10321041
"unique-doc-out-dir",

src/tools/compiletest/src/runtest.rs

Lines changed: 12 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1522,16 +1522,18 @@ impl<'test> TestCx<'test> {
15221522
// Use a single thread for efficiency and a deterministic error message order
15231523
rustc.arg("-Zthreads=1");
15241524

1525-
// Hide libstd sources from ui tests to make sure we generate the stderr
1526-
// output that users will see.
1527-
// Without this, we may be producing good diagnostics in-tree but users
1528-
// will not see half the information.
1529-
//
1530-
// This also has the benefit of more effectively normalizing output between different
1531-
// compilers, so that we don't have to know the `/rustc/$sha` output to normalize after the
1532-
// fact.
1533-
rustc.arg("-Zsimulate-remapped-rust-src-base=/rustc/FAKE_PREFIX");
1534-
rustc.arg("-Ztranslate-remapped-path-to-local-path=no");
1525+
if !self.props.show_std_source {
1526+
// Hide libstd sources from ui tests to make sure we generate the stderr
1527+
// output that users will see.
1528+
// Without this, we may be producing good diagnostics in-tree but users
1529+
// will not see half the information.
1530+
//
1531+
// This also has the benefit of more effectively normalizing output between different
1532+
// compilers, so that we don't have to know the `/rustc/$sha` output to normalize after the
1533+
// fact.
1534+
rustc.arg("-Zsimulate-remapped-rust-src-base=/rustc/FAKE_PREFIX");
1535+
rustc.arg("-Ztranslate-remapped-path-to-local-path=no");
1536+
}
15351537

15361538
// Hide Cargo dependency sources from ui tests to make sure the error message doesn't
15371539
// change depending on whether $CARGO_HOME is remapped or not. If this is not present,
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
// This test without //@ show-std-source directive, as a contrast to show-std-source.rs
2+
3+
enum Foo<T> {
4+
Bar {
5+
v23: T,
6+
y: isize
7+
}
8+
}
9+
10+
fn f(x: &Foo) { //~ ERROR missing generics for enum `Foo` [E0107]
11+
match *x {
12+
Foo::Bar { y: y, v23: x } => {
13+
assert_eq!(x, 1);
14+
assert_eq!(y, 2); //~ ERROR can't compare `&isize` with `{integer}` [E0277]
15+
}
16+
}
17+
}
18+
19+
pub fn main() {
20+
let x = Foo::Bar { x: 1, y: 2 }; //~ ERROR variant `Foo<_>::Bar` has no field named `x` [E0559]
21+
f(&x);
22+
}
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
error[E0107]: missing generics for enum `Foo`
2+
--> $DIR/show-std-source-without-flag.rs:10:10
3+
|
4+
LL | fn f(x: &Foo) {
5+
| ^^^ expected 1 generic argument
6+
|
7+
note: enum defined here, with 1 generic parameter: `T`
8+
--> $DIR/show-std-source-without-flag.rs:3:6
9+
|
10+
LL | enum Foo<T> {
11+
| ^^^ -
12+
help: add missing generic argument
13+
|
14+
LL | fn f(x: &Foo<T>) {
15+
| +++
16+
17+
error[E0277]: can't compare `&isize` with `{integer}`
18+
--> $DIR/show-std-source-without-flag.rs:14:13
19+
|
20+
LL | assert_eq!(y, 2);
21+
| ^^^^^^^^^^^^^^^^ no implementation for `&isize == {integer}`
22+
|
23+
= help: the trait `PartialEq<{integer}>` is not implemented for `&isize`
24+
= note: this error originates in the macro `assert_eq` (in Nightly builds, run with -Z macro-backtrace for more info)
25+
26+
error[E0559]: variant `Foo<_>::Bar` has no field named `x`
27+
--> $DIR/show-std-source-without-flag.rs:20:24
28+
|
29+
LL | let x = Foo::Bar { x: 1, y: 2 };
30+
| ^ `Foo<_>::Bar` does not have this field
31+
|
32+
= note: available fields are: `v23`
33+
34+
error: aborting due to 3 previous errors
35+
36+
Some errors have detailed explanations: E0107, E0277, E0559.
37+
For more information about an error, try `rustc --explain E0107`.
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
// This test with //@ show-std-source directive,
2+
// as a contrast to show-std-source-without-flag.rs
3+
//@ show-std-source
4+
5+
enum Foo<T> {
6+
Bar {
7+
v23: T,
8+
y: isize
9+
}
10+
}
11+
12+
fn f(x: &Foo) { //~ ERROR missing generics for enum `Foo` [E0107]
13+
match *x {
14+
Foo::Bar { y: y, v23: x } => {
15+
assert_eq!(x, 1);
16+
assert_eq!(y, 2); //~ ERROR can't compare `&isize` with `{integer}` [E0277]
17+
}
18+
}
19+
}
20+
21+
pub fn main() {
22+
let x = Foo::Bar { x: 1, y: 2 }; //~ ERROR variant `Foo<_>::Bar` has no field named `x` [E0559]
23+
f(&x);
24+
}
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
error[E0107]: missing generics for enum `Foo`
2+
--> $DIR/show-std-source.rs:12:10
3+
|
4+
LL | fn f(x: &Foo) {
5+
| ^^^ expected 1 generic argument
6+
|
7+
note: enum defined here, with 1 generic parameter: `T`
8+
--> $DIR/show-std-source.rs:5:6
9+
|
10+
LL | enum Foo<T> {
11+
| ^^^ -
12+
help: add missing generic argument
13+
|
14+
LL | fn f(x: &Foo<T>) {
15+
| +++
16+
17+
error[E0277]: can't compare `&isize` with `{integer}`
18+
--> $DIR/show-std-source.rs:16:13
19+
|
20+
LL | assert_eq!(y, 2);
21+
| ^^^^^^^^^^^^^^^^ no implementation for `&isize == {integer}`
22+
|
23+
= help: the trait `PartialEq<{integer}>` is not implemented for `&isize`
24+
= note: this error originates in the macro `assert_eq` (in Nightly builds, run with -Z macro-backtrace for more info)
25+
help: consider dereferencing here
26+
--> $SRC_DIR_REAL/core/src/macros/mod.rs:LL:COL
27+
|
28+
LL | if !(**left_val == *right_val) {
29+
| +
30+
31+
error[E0559]: variant `Foo<_>::Bar` has no field named `x`
32+
--> $DIR/show-std-source.rs:22:24
33+
|
34+
LL | let x = Foo::Bar { x: 1, y: 2 };
35+
| ^ `Foo<_>::Bar` does not have this field
36+
|
37+
= note: available fields are: `v23`
38+
39+
error: aborting due to 3 previous errors
40+
41+
Some errors have detailed explanations: E0107, E0277, E0559.
42+
For more information about an error, try `rustc --explain E0107`.

0 commit comments

Comments
 (0)