Skip to content

Commit

Permalink
Auto merge of #6081 - dtolnay:raw, r=alexcrichton
Browse files Browse the repository at this point in the history
Fix missing messages when --message-format=json is deeply nested

This commit switches from `serde_json::Value` to [`RawValue`](https://docs.rs/serde_json/1.0/serde_json/value/struct.RawValue.html), which can process arbitrarily deeply nested JSON content without recursion.

Fixes #5992. @ehuss
  • Loading branch information
bors committed Sep 24, 2018
2 parents 650b5d8 + d1218d2 commit b2b34ac
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 6 deletions.
2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ semver = { version = "0.9.0", features = ["serde"] }
serde = "1.0"
serde_derive = "1.0"
serde_ignored = "0.0.4"
serde_json = "1.0.24"
serde_json = { version = "1.0.30", features = ["raw_value"] }
shell-escape = "0.1.4"
tar = { version = "0.4.15", default-features = false }
tempfile = "3.0"
Expand Down
11 changes: 6 additions & 5 deletions src/cargo/util/machine_message.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use serde::ser;
use serde_json::{self, Value};
use serde_json::{self, value::RawValue};

use core::{PackageId, Target};

Expand All @@ -8,16 +8,17 @@ pub trait Message: ser::Serialize {
}

pub fn emit<T: Message>(t: &T) {
let mut json: Value = serde_json::to_value(t).unwrap();
json["reason"] = json!(t.reason());
println!("{}", json);
let json = serde_json::to_string(t).unwrap();
assert!(json.starts_with("{\""));
let reason = json!(t.reason());
println!("{{\"reason\":{},{}", reason, &json[1..]);
}

#[derive(Serialize)]
pub struct FromCompiler<'a> {
pub package_id: &'a PackageId,
pub target: &'a Target,
pub message: serde_json::Value,
pub message: Box<RawValue>,
}

impl<'a> Message for FromCompiler<'a> {
Expand Down
23 changes: 23 additions & 0 deletions tests/testsuite/check.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
use std::fmt::{self, Write};

use glob::glob;
use support::install::exe;
use support::is_nightly;
Expand Down Expand Up @@ -690,3 +692,24 @@ fn does_not_use_empty_rustc_wrapper() {
let p = project().file("src/lib.rs", "").build();
p.cargo("check").env("RUSTC_WRAPPER", "").run();
}

#[test]
fn error_from_deep_recursion() -> Result<(), fmt::Error> {
let mut big_macro = String::new();
writeln!(big_macro, "macro_rules! m {{")?;
for i in 0..130 {
writeln!(big_macro, "({}) => {{ m!({}); }};", i, i + 1)?;
}
writeln!(big_macro, "}}")?;
writeln!(big_macro, "m!(0);")?;

let p = project().file("src/lib.rs", &big_macro).build();
p.cargo("check --message-format=json")
.with_status(101)
.with_stdout_contains(
"[..]\"message\":\"recursion limit reached while expanding the macro `m`\"[..]",
)
.run();

Ok(())
}

0 comments on commit b2b34ac

Please sign in to comment.