Skip to content

Commit

Permalink
Auto merge of #13016 - dtolnay-contrib:messagetype, r=ehuss
Browse files Browse the repository at this point in the history
Handle $message_type in JSON diagnostics

### What does this PR try to resolve?

Unblocks rust-lang/rust#115691.

Without this change, Cargo's testsuite fails in `doc::doc_message_format` and `metabuild::metabuild_failed_build_json`.

### How should we test and review this PR?

Tested with and without rust-lang/rust#115691.

In Cargo repo: `cargo test --test testsuite`
In Rust repo: `x.py test src/tools/cargo` (separately on master and $message_type PR)
  • Loading branch information
bors committed Nov 20, 2023
2 parents 8e00f03 + 65bb09d commit 71cd3a9
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 8 deletions.
37 changes: 29 additions & 8 deletions crates/cargo-test-support/src/compare.rs
Original file line number Diff line number Diff line change
Expand Up @@ -591,15 +591,36 @@ fn find_json_mismatch_r<'a>(
.next()
}
(&Object(ref l), &Object(ref r)) => {
let same_keys = l.len() == r.len() && l.keys().all(|k| r.contains_key(k));
if !same_keys {
return Some((expected, actual));
let mut expected_entries = l.iter();
let mut actual_entries = r.iter();

// Compilers older than 1.76 do not produce $message_type.
// Treat it as optional for now.
let mut expected_entries_without_message_type;
let expected_entries: &mut dyn Iterator<Item = _> =
if l.contains_key("$message_type") && !r.contains_key("$message_type") {
expected_entries_without_message_type =
expected_entries.filter(|entry| entry.0 != "$message_type");
&mut expected_entries_without_message_type
} else {
&mut expected_entries
};

loop {
match (expected_entries.next(), actual_entries.next()) {
(None, None) => return None,
(Some((expected_key, expected_value)), Some((actual_key, actual_value)))
if expected_key == actual_key =>
{
if let mismatch @ Some(_) =
find_json_mismatch_r(expected_value, actual_value, cwd)
{
return mismatch;
}
}
_ => return Some((expected, actual)),
}
}

l.values()
.zip(r.values())
.filter_map(|(l, r)| find_json_mismatch_r(l, r, cwd))
.next()
}
(&Null, &Null) => None,
// Magic string literal `"{...}"` acts as wildcard for any sub-JSON.
Expand Down
1 change: 1 addition & 0 deletions tests/testsuite/doc.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1698,6 +1698,7 @@ fn doc_message_format() {
r#"
{
"message": {
"$message_type": "diagnostic",
"children": "{...}",
"code": "{...}",
"level": "error",
Expand Down
1 change: 1 addition & 0 deletions tests/testsuite/metabuild.rs
Original file line number Diff line number Diff line change
Expand Up @@ -740,6 +740,7 @@ fn metabuild_failed_build_json() {
r#"
{
"message": {
"$message_type": "diagnostic",
"children": "{...}",
"code": "{...}",
"level": "error",
Expand Down

0 comments on commit 71cd3a9

Please sign in to comment.