Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Handle $message_type in JSON diagnostics #13016

Merged
merged 1 commit into from
Nov 20, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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") {
Comment on lines +597 to +601
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Specializing this for the compiler runs counter to a (slow moving) effort I'm working on to use a generic, third party library rather than us using bespoke testing tools.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We can delete this code as soon as Rust 1.76 is stable (February 8) and Cargo's MSRV is raised. From #12775 it looks like MSRV update is immediate upon stabilization. I have filed #13017 to follow up.

If the generic third-party library adoption proceeds faster than that, you would need to figure out how to accommodate this situation in a principled way. This will not be the last time that rustc's JSON is tweaked.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

imo this should have been done principled before merging rather than punting that onto others.

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