mirror of
https://github.com/rust-lang/cargo.git
synced 2025-10-01 11:30:39 +00:00
Auto merge of #14608 - weihanglo:deserialize, r=epage
refactor(compiler): zero-copy deserialization when possible
This commit is contained in:
commit
5e2878f786
@ -103,7 +103,7 @@ impl<'a, 'gctx> JobState<'a, 'gctx> {
|
|||||||
}
|
}
|
||||||
|
|
||||||
/// See [`Message::Diagnostic`] and [`Message::WarningCount`].
|
/// See [`Message::Diagnostic`] and [`Message::WarningCount`].
|
||||||
pub fn emit_diag(&self, level: String, diag: String, fixable: bool) -> CargoResult<()> {
|
pub fn emit_diag(&self, level: &str, diag: String, fixable: bool) -> CargoResult<()> {
|
||||||
if let Some(dedupe) = self.output {
|
if let Some(dedupe) = self.output {
|
||||||
let emitted = dedupe.emit_diag(&diag)?;
|
let emitted = dedupe.emit_diag(&diag)?;
|
||||||
if level == "warning" {
|
if level == "warning" {
|
||||||
@ -116,7 +116,7 @@ impl<'a, 'gctx> JobState<'a, 'gctx> {
|
|||||||
} else {
|
} else {
|
||||||
self.messages.push_bounded(Message::Diagnostic {
|
self.messages.push_bounded(Message::Diagnostic {
|
||||||
id: self.id,
|
id: self.id,
|
||||||
level,
|
level: level.to_string(),
|
||||||
diag,
|
diag,
|
||||||
fixable,
|
fixable,
|
||||||
});
|
});
|
||||||
|
@ -54,6 +54,7 @@ mod unit;
|
|||||||
pub mod unit_dependencies;
|
pub mod unit_dependencies;
|
||||||
pub mod unit_graph;
|
pub mod unit_graph;
|
||||||
|
|
||||||
|
use std::borrow::Cow;
|
||||||
use std::collections::{HashMap, HashSet};
|
use std::collections::{HashMap, HashSet};
|
||||||
use std::env;
|
use std::env;
|
||||||
use std::ffi::{OsStr, OsString};
|
use std::ffi::{OsStr, OsString};
|
||||||
@ -1756,10 +1757,15 @@ fn on_stderr_line_inner(
|
|||||||
..
|
..
|
||||||
} => {
|
} => {
|
||||||
#[derive(serde::Deserialize)]
|
#[derive(serde::Deserialize)]
|
||||||
struct CompilerMessage {
|
struct CompilerMessage<'a> {
|
||||||
|
// `rendered` contains escape sequences, which can't be
|
||||||
|
// zero-copy deserialized by serde_json.
|
||||||
|
// See https://github.com/serde-rs/json/issues/742
|
||||||
rendered: String,
|
rendered: String,
|
||||||
message: String,
|
#[serde(borrow)]
|
||||||
level: String,
|
message: Cow<'a, str>,
|
||||||
|
#[serde(borrow)]
|
||||||
|
level: Cow<'a, str>,
|
||||||
children: Vec<PartialDiagnostic>,
|
children: Vec<PartialDiagnostic>,
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -1782,7 +1788,8 @@ fn on_stderr_line_inner(
|
|||||||
suggestion_applicability: Option<Applicability>,
|
suggestion_applicability: Option<Applicability>,
|
||||||
}
|
}
|
||||||
|
|
||||||
if let Ok(mut msg) = serde_json::from_str::<CompilerMessage>(compiler_message.get()) {
|
if let Ok(mut msg) = serde_json::from_str::<CompilerMessage<'_>>(compiler_message.get())
|
||||||
|
{
|
||||||
if msg.message.starts_with("aborting due to")
|
if msg.message.starts_with("aborting due to")
|
||||||
|| msg.message.ends_with("warning emitted")
|
|| msg.message.ends_with("warning emitted")
|
||||||
|| msg.message.ends_with("warnings emitted")
|
|| msg.message.ends_with("warnings emitted")
|
||||||
@ -1808,7 +1815,7 @@ fn on_stderr_line_inner(
|
|||||||
})
|
})
|
||||||
.any(|b| b);
|
.any(|b| b);
|
||||||
count_diagnostic(&msg.level, options);
|
count_diagnostic(&msg.level, options);
|
||||||
state.emit_diag(msg.level, rendered, machine_applicable)?;
|
state.emit_diag(&msg.level, rendered, machine_applicable)?;
|
||||||
}
|
}
|
||||||
return Ok(true);
|
return Ok(true);
|
||||||
}
|
}
|
||||||
@ -1819,16 +1826,17 @@ fn on_stderr_line_inner(
|
|||||||
// cached replay to enable/disable colors without re-invoking rustc.
|
// cached replay to enable/disable colors without re-invoking rustc.
|
||||||
MessageFormat::Json { ansi: false, .. } => {
|
MessageFormat::Json { ansi: false, .. } => {
|
||||||
#[derive(serde::Deserialize, serde::Serialize)]
|
#[derive(serde::Deserialize, serde::Serialize)]
|
||||||
struct CompilerMessage {
|
struct CompilerMessage<'a> {
|
||||||
rendered: String,
|
rendered: String,
|
||||||
#[serde(flatten)]
|
#[serde(flatten, borrow)]
|
||||||
other: std::collections::BTreeMap<String, serde_json::Value>,
|
other: std::collections::BTreeMap<Cow<'a, str>, serde_json::Value>,
|
||||||
}
|
}
|
||||||
if let Ok(mut error) = serde_json::from_str::<CompilerMessage>(compiler_message.get()) {
|
if let Ok(mut error) =
|
||||||
|
serde_json::from_str::<CompilerMessage<'_>>(compiler_message.get())
|
||||||
|
{
|
||||||
error.rendered = anstream::adapter::strip_str(&error.rendered).to_string();
|
error.rendered = anstream::adapter::strip_str(&error.rendered).to_string();
|
||||||
let new_line = serde_json::to_string(&error)?;
|
let new_line = serde_json::to_string(&error)?;
|
||||||
let new_msg: Box<serde_json::value::RawValue> = serde_json::from_str(&new_line)?;
|
compiler_message = serde_json::value::RawValue::from_string(new_line)?;
|
||||||
compiler_message = new_msg;
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -1844,11 +1852,12 @@ fn on_stderr_line_inner(
|
|||||||
// Look for a matching directive and inform Cargo internally that a
|
// Look for a matching directive and inform Cargo internally that a
|
||||||
// metadata file has been produced.
|
// metadata file has been produced.
|
||||||
#[derive(serde::Deserialize)]
|
#[derive(serde::Deserialize)]
|
||||||
struct ArtifactNotification {
|
struct ArtifactNotification<'a> {
|
||||||
artifact: String,
|
#[serde(borrow)]
|
||||||
|
artifact: Cow<'a, str>,
|
||||||
}
|
}
|
||||||
|
|
||||||
if let Ok(artifact) = serde_json::from_str::<ArtifactNotification>(compiler_message.get()) {
|
if let Ok(artifact) = serde_json::from_str::<ArtifactNotification<'_>>(compiler_message.get()) {
|
||||||
trace!("found directive from rustc: `{}`", artifact.artifact);
|
trace!("found directive from rustc: `{}`", artifact.artifact);
|
||||||
if artifact.artifact.ends_with(".rmeta") {
|
if artifact.artifact.ends_with(".rmeta") {
|
||||||
debug!("looks like metadata finished early!");
|
debug!("looks like metadata finished early!");
|
||||||
@ -1866,12 +1875,14 @@ fn on_stderr_line_inner(
|
|||||||
}
|
}
|
||||||
|
|
||||||
#[derive(serde::Deserialize)]
|
#[derive(serde::Deserialize)]
|
||||||
struct CompilerMessage {
|
struct CompilerMessage<'a> {
|
||||||
message: String,
|
#[serde(borrow)]
|
||||||
level: String,
|
message: Cow<'a, str>,
|
||||||
|
#[serde(borrow)]
|
||||||
|
level: Cow<'a, str>,
|
||||||
}
|
}
|
||||||
|
|
||||||
if let Ok(msg) = serde_json::from_str::<CompilerMessage>(compiler_message.get()) {
|
if let Ok(msg) = serde_json::from_str::<CompilerMessage<'_>>(compiler_message.get()) {
|
||||||
if msg.message.starts_with("aborting due to")
|
if msg.message.starts_with("aborting due to")
|
||||||
|| msg.message.ends_with("warning emitted")
|
|| msg.message.ends_with("warning emitted")
|
||||||
|| msg.message.ends_with("warnings emitted")
|
|| msg.message.ends_with("warnings emitted")
|
||||||
|
Loading…
x
Reference in New Issue
Block a user