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

Fix missing messages when --message-format=json is deeply nested #6081

Merged
merged 1 commit into from
Sep 24, 2018
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
Fix missing messages when --message-format=json is deeply nested
This commit switches from serde_json::Value to RawValue, which can
process arbitrarily deeply nested JSON content without recursion.
  • Loading branch information
dtolnay committed Sep 23, 2018
commit d1218d29614e4f338ba1077d9436086997e225e5
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(())
}