Hide line/column from DefId so that the error message will not change due to toolchain changes.

PiperOrigin-RevId: 665493677
Change-Id: Iafa6a6e9a1d368a7ea4c2ecf48c521ea8e1613db
diff --git a/cc_bindings_from_rs/BUILD b/cc_bindings_from_rs/BUILD
index d8efeb5..d94ddfa 100644
--- a/cc_bindings_from_rs/BUILD
+++ b/cc_bindings_from_rs/BUILD
@@ -72,8 +72,6 @@
     deps = [
         ":run_compiler_test_support",
         "@crate_index//:regex",
-        "@crate_index//:serde",
-        "@crate_index//:serde_json",
         "@crate_index//:tempfile",
     ],
 )
diff --git a/cc_bindings_from_rs/cc_bindings_from_rs.rs b/cc_bindings_from_rs/cc_bindings_from_rs.rs
index e396fdf..30eb716 100644
--- a/cc_bindings_from_rs/cc_bindings_from_rs.rs
+++ b/cc_bindings_from_rs/cc_bindings_from_rs.rs
@@ -126,7 +126,6 @@
     use regex::{Regex, RegexBuilder};
     use run_compiler_test_support::get_sysroot_for_testing;
     use run_compiler_test_support::setup_rustc_target_for_testing;
-    use serde::Deserialize;
     use std::path::PathBuf;
     use tempfile::{tempdir, TempDir};
     use token_stream_printer::{CLANG_FORMAT_EXE_PATH_FOR_TESTING, RUSTFMT_EXE_PATH_FOR_TESTING};
@@ -332,26 +331,18 @@
                 "#,
             );
 
-        #[derive(Deserialize, Debug, PartialEq, Eq)]
-        struct Item {
-            count: usize,
-            // NOTE: Ignore sample_message, which is non-deterministic.
-        }
-
         let test_result = test_args.run().expect("Error report generation should succeed");
         assert!(test_result.error_report_out_path.is_some());
         let error_report_out_path = test_result.error_report_out_path.as_ref().unwrap();
         assert!(error_report_out_path.exists());
-        let error_report: HashMap<String, Item> = serde_json::from_str(
-            &std::fs::read_to_string(&error_report_out_path)
-                .expect("Failed to read error report file"),
-        )
-        .expect("Failed to parse error report");
-        assert!(matches!(
-            error_report
-                .get("Unsupported use statement that refers to this type of the entity: {:#?}"),
-            Some(&Item { count: 2 }),
-        ));
+        let error_report = std::fs::read_to_string(&error_report_out_path)?;
+        let expected_error_report = r#"{
+  "Unsupported use statement that refers to this type of the entity: {:#?}": {
+    "count": 2,
+    "sample_message": "Unsupported use statement that refers to this type of the entity: [\n    Def(\n        Mod,\n        DefId(std[46ff]::collections),\n    ),\n]"
+  }
+}"#;
+        assert_eq!(expected_error_report, error_report);
         Ok(())
     }
 
diff --git a/common/BUILD b/common/BUILD
index 364d8c5..ae21cc8 100644
--- a/common/BUILD
+++ b/common/BUILD
@@ -293,6 +293,7 @@
     deps = [
         ":arc_anyhow",
         "@crate_index//:anyhow",
+        "@crate_index//:regex",
         "@crate_index//:serde",
         "@crate_index//:serde_json",
     ],
diff --git a/common/error_report.rs b/common/error_report.rs
index 1eda154..115b85f 100644
--- a/common/error_report.rs
+++ b/common/error_report.rs
@@ -2,6 +2,7 @@
 // Exceptions. See /LICENSE for license information.
 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
 
+use regex::Regex;
 use std::borrow::Cow;
 use std::cell::RefCell;
 use std::collections::BTreeMap;
@@ -134,6 +135,12 @@
     }
 }
 
+fn hide_unstable_details(input: &str) -> String {
+    // Remove line:column in def id
+    let regex = Regex::new(r"DefId\(\d+:\d+ ~ ").unwrap();
+    regex.replace_all(input, "DefId(").to_string()
+}
+
 /// An aggregate of zero or more errors.
 #[derive(Default, Debug)]
 pub struct ErrorReport {
@@ -157,13 +164,13 @@
                 .borrow_mut()
                 .entry(error.fmt.clone())
                 .or_default()
-                .add(Cow::Borrowed(sample_message));
+                .add(Cow::Owned(hide_unstable_details(sample_message)));
         } else {
             self.map
                 .borrow_mut()
                 .entry(Cow::Borrowed("{}"))
                 .or_default()
-                .add(Cow::Owned(format!("{error}")));
+                .add(Cow::Owned(hide_unstable_details(&format!("{error}"))));
         }
     }