Repository: dtolnay/trybuild (Rust)

Issue:
`$RUST` directory normalization fails in GitHub CI tests
On a PR to the `cxx` crate (see https://github.com/dtolnay/cxx/pull/1180) their `ui` tests fail, because the normalization doesn't work properly in CI. The path to the Rust libraries is normalized properly in the local working directory, but not in CI. This results in a false-positive error in the test:
```
2023-02-25T18:57:14.9485120Z EXPECTED:
2023-02-25T18:57:14.9485950Z ┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈
2023-02-25T18:57:14.9486357Z error[E0599]: the method `to_cxx_exception` exists for reference `&NonError`, but its trait bounds were not satisfied
2023-02-25T18:57:14.9487364Z  --> tests/ui/result_no_display.rs:4:19
2023-02-25T18:57:14.9487605Z   |
2023-02-25T18:57:14.9487886Z 4 |         fn f() -> Result<()>;
2023-02-25T18:57:14.9488390Z   |                   ^^^^^^^^^^ method cannot be called on `&NonError` due to unsatisfied trait bounds
2023-02-25T18:57:14.9488681Z ...
2023-02-25T18:57:14.9488886Z 8 | pub struct NonError;
2023-02-25T18:57:14.9505278Z   | ------------------- doesn't satisfy `NonError: std::fmt::Display`
2023-02-25T18:57:14.9505559Z   |
2023-02-25T18:57:14.9505800Z   = note: the following trait bounds were not satisfied:
2023-02-25T18:57:14.9506086Z           `NonError: std::fmt::Display`
2023-02-25T18:57:14.9506370Z           which is required by `&NonError: ToCxxExceptionDefault`
2023-02-25T18:57:14.9506697Z note: the trait `std::fmt::Display` must be implemented
2023-02-25T18:57:14.9507018Z  --> $RUST/core/src/fmt/mod.rs
2023-02-25T18:57:14.9507551Z   = note: this error originates in the macro `::cxx::map_rust_error_to_cxx_exception` (in Nightly builds, run with -Z macro-backtrace for more info)
2023-02-25T18:57:14.9508072Z ┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈
2023-02-25T18:57:14.9508234Z 
2023-02-25T18:57:14.9508329Z ACTUAL OUTPUT:
2023-02-25T18:57:14.9508681Z ┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈
2023-02-25T18:57:14.9509031Z error[E0599]: the method `to_cxx_exception` exists for reference `&NonError`, but its trait bounds were not satisfied
2023-02-25T18:57:14.9509431Z  --> tests/ui/result_no_display.rs:4:19
2023-02-25T18:57:14.9509650Z   |
2023-02-25T18:57:14.9509889Z 4 |         fn f() -> Result<()>;
2023-02-25T18:57:14.9510186Z   |                   ^^^^^^^^^^ method cannot be called on `&NonError` due to unsatisfied trait bounds
2023-02-25T18:57:14.9510454Z ...
2023-02-25T18:57:14.9510647Z 8 | pub struct NonError;
2023-02-25T18:57:14.9511069Z   | ------------------- doesn't satisfy `NonError: std::fmt::Display`
2023-02-25T18:57:14.9511343Z   |
2023-02-25T18:57:14.9511576Z   = note: the following trait bounds were not satisfied:
2023-02-25T18:57:14.9511836Z           `NonError: std::fmt::Display`
2023-02-25T18:57:14.9512138Z           which is required by `&NonError: ToCxxExceptionDefault`
2023-02-25T18:57:14.9512462Z note: the trait `std::fmt::Display` must be implemented
2023-02-25T18:57:14.9512898Z  --> /rustc/c5c7d2b37780dac1092e75f12ab97dd56c30861d/library/core/src/fmt/mod.rs:786:1
2023-02-25T18:57:14.9513496Z   = note: this error originates in the macro `::cxx::map_rust_error_to_cxx_exception` (in Nightly builds, run with -Z macro-backtrace for more info)
2023-02-25T18:57:14.9514007Z ┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈
2023-02-25T18:57:14.9514325Z note: If the actual output is the correct output you can bless it by rerunning
2023-02-25T18:57:14.9514657Z       your test with the environment variable TRYBUILD=overwrite
```

Here, the normalization of `/rustc/c5c7d2b37780dac1092e75f12ab97dd56c30861d/library/core/src/fmt/mod.rs:786:1` to `$RUST/core/src/fmt/mod.rs` fails, because only `/rustlib/src/rust/src/` is considered.

I suppose, the normalization step should be extended to also normalize `/rustc/[0-9a-f]+/library/` to `$RUST/`.


Discussion:

Patch:
diff --git a/src/normalize.rs b/src/normalize.rs
index bc57893..d469cd7 100644
--- a/src/normalize.rs
+++ b/src/normalize.rs
@@ -282,6 +282,18 @@ impl<'a> Filter<'a> {
                     // ::: $RUST/std/src/net/ip.rs:83:1
                     line.replace_range(line.find(prefix).unwrap() + 4..pos + 25, "$RUST");
                     other_crate = true;
+                } else if let (Some(pos_rustc), Some(pos_library)) =
+                    (line.find("/rustc/"), line.find("/library/"))
+                {
+                    // source: /rustc/c5c7d2b37780dac1092e75f12ab97dd56c30861d/library/core/src/fmt/mod.rs:786:1
+                    // ::: $RUST/core/src/fmt/mod.rs
+                    if pos_library == pos_rustc + 47 {
+                        line.replace_range(
+                            line.find(prefix).unwrap() + 4..pos_library + 8,
+                            "$RUST",
+                        );
+                        other_crate = true;
+                    }
                 }
             }
             if self.normalization >= CargoRegistry && !other_crate {


Test patch:
diff --git a/src/tests.rs b/src/tests.rs
index 5899705..c784d3f 100644
--- a/src/tests.rs
+++ b/src/tests.rs
@@ -264,6 +264,47 @@ note: the following traits must be implemented
   = note: this error originates in the macro `anyhow` (in Nightly builds, run with -Z macro-backtrace for more info)
 "}
 
+test_normalize! {rust_lib_with_githash
+"
+error[E0599]: the method `to_cxx_exception` exists for reference `&NonError`, but its trait bounds were not satisfied
+ --> tests/ui/result_no_display.rs:4:19
+  |
+4 |         fn f() -> Result<()>;
+  |                   ^^^^^^^^^^ method cannot be called on `&NonError` due to unsatisfied trait bounds
+...
+8 | pub struct NonError;
+  | ------------------- doesn't satisfy `NonError: std::fmt::Display`
+  |
+  = note: the following trait bounds were not satisfied:
+          `NonError: std::fmt::Display`
+          which is required by `&NonError: ToCxxExceptionDefault`
+note: the trait `std::fmt::Display` must be implemented
+ --> /rustc/c5c7d2b37780dac1092e75f12ab97dd56c30861d/library/core/src/fmt/mod.rs:786:1
+  |
+  | pub trait Display {
+  | ^^^^^^^^^^^^^^^^^
+  = note: this error originates in the macro `::cxx::map_rust_error_to_cxx_exception` (in Nightly builds, run with -Z macro-backtrace for more info)
+" "
+error[E0599]: the method `to_cxx_exception` exists for reference `&NonError`, but its trait bounds were not satisfied
+ --> tests/ui/result_no_display.rs:4:19
+  |
+4 |         fn f() -> Result<()>;
+  |                   ^^^^^^^^^^ method cannot be called on `&NonError` due to unsatisfied trait bounds
+...
+8 | pub struct NonError;
+  | ------------------- doesn't satisfy `NonError: std::fmt::Display`
+  |
+  = note: the following trait bounds were not satisfied:
+          `NonError: std::fmt::Display`
+          which is required by `&NonError: ToCxxExceptionDefault`
+note: the trait `std::fmt::Display` must be implemented
+ --> $RUST/core/src/fmt/mod.rs
+  |
+  | pub trait Display {
+  | ^^^^^^^^^^^^^^^^^
+  = note: this error originates in the macro `::cxx::map_rust_error_to_cxx_exception` (in Nightly builds, run with -Z macro-backtrace for more info)
+"}
+
 test_normalize! {test_pyo3_url
     DIR="/pyo3"
     WORKSPACE="/pyo3"