Skip to content

Commit 1618c07

Browse files
committed
rustc: rename -Z emit-directives to -Z emit-artifact-notifications and simplify the output.
1 parent f0e43fc commit 1618c07

File tree

11 files changed

+38
-43
lines changed

11 files changed

+38
-43
lines changed

src/librustc/session/config.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -1462,8 +1462,8 @@ options! {DebuggingOptions, DebuggingSetter, basic_debugging_options,
14621462
the same values as the target option of the same name"),
14631463
allow_features: Option<Vec<String>> = (None, parse_opt_comma_list, [TRACKED],
14641464
"only allow the listed language features to be enabled in code (space separated)"),
1465-
emit_directives: bool = (false, parse_bool, [UNTRACKED],
1466-
"emit build directives if producing JSON output"),
1465+
emit_artifact_notifications: bool = (false, parse_bool, [UNTRACKED],
1466+
"emit notifications after each artifact has been output (only in the JSON format)"),
14671467
}
14681468

14691469
pub fn default_lib_output() -> CrateType {

src/librustc_errors/emitter.rs

+5-3
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ use std::borrow::Cow;
1616
use std::io::prelude::*;
1717
use std::io;
1818
use std::cmp::{min, Reverse};
19+
use std::path::Path;
1920
use termcolor::{StandardStream, ColorChoice, ColorSpec, BufferWriter, Ansi};
2021
use termcolor::{WriteColor, Color, Buffer};
2122

@@ -52,9 +53,10 @@ pub trait Emitter {
5253
/// Emit a structured diagnostic.
5354
fn emit_diagnostic(&mut self, db: &DiagnosticBuilder<'_>);
5455

55-
/// Emit a JSON directive. The default is to do nothing; this should only
56-
/// be emitted with --error-format=json.
57-
fn maybe_emit_json_directive(&mut self, _directive: String) {}
56+
/// Emit a notification that an artifact has been output.
57+
/// This is currently only supported for the JSON format,
58+
/// other formats can, and will, simply ignore it.
59+
fn emit_artifact_notification(&mut self, _path: &Path) {}
5860

5961
/// Checks if should show explanations about "rustc --explain"
6062
fn should_show_explain(&self) -> bool {

src/librustc_errors/lib.rs

+6-12
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ use std::borrow::Cow;
2626
use std::cell::Cell;
2727
use std::{error, fmt};
2828
use std::panic;
29+
use std::path::Path;
2930

3031
use termcolor::{ColorSpec, Color};
3132

@@ -294,16 +295,9 @@ impl error::Error for ExplicitBug {
294295
pub use diagnostic::{Diagnostic, SubDiagnostic, DiagnosticStyledString, DiagnosticId};
295296
pub use diagnostic_builder::DiagnosticBuilder;
296297

297-
/// A handler deals with two kinds of compiler output.
298-
/// - Errors: certain errors (fatal, bug, unimpl) may cause immediate exit,
299-
/// others log errors for later reporting.
300-
/// - Directives: with --error-format=json, the compiler produces directives
301-
/// that indicate when certain actions have completed, which are useful for
302-
/// Cargo. They may change at any time and should not be considered a public
303-
/// API.
304-
///
305-
/// This crate's name (rustc_errors) doesn't encompass the directives, because
306-
/// directives were added much later.
298+
/// A handler deals with errors and other compiler output.
299+
/// Certain errors (fatal, bug, unimpl) may cause immediate exit,
300+
/// others log errors for later reporting.
307301
pub struct Handler {
308302
pub flags: HandlerFlags,
309303

@@ -775,8 +769,8 @@ impl Handler {
775769
}
776770
}
777771

778-
pub fn maybe_emit_json_directive(&self, directive: String) {
779-
self.emitter.borrow_mut().maybe_emit_json_directive(directive);
772+
pub fn emit_artifact_notification(&self, path: &Path) {
773+
self.emitter.borrow_mut().emit_artifact_notification(path);
780774
}
781775
}
782776

src/librustc_interface/passes.rs

+5-8
Original file line numberDiff line numberDiff line change
@@ -1048,14 +1048,11 @@ fn encode_and_write_metadata<'tcx>(
10481048
tcx.sess.fatal(&format!("couldn't create a temp dir: {}", err))
10491049
});
10501050
let metadata_filename = emit_metadata(tcx.sess, &metadata, &metadata_tmpdir);
1051-
match std::fs::rename(&metadata_filename, &out_filename) {
1052-
Ok(_) => {
1053-
if tcx.sess.opts.debugging_opts.emit_directives {
1054-
tcx.sess.parse_sess.span_diagnostic.maybe_emit_json_directive(
1055-
format!("metadata file written: {}", out_filename.display()));
1056-
}
1057-
}
1058-
Err(e) => tcx.sess.fatal(&format!("failed to write {}: {}", out_filename.display(), e)),
1051+
if let Err(e) = fs::rename(&metadata_filename, &out_filename) {
1052+
tcx.sess.fatal(&format!("failed to write {}: {}", out_filename.display(), e));
1053+
}
1054+
if tcx.sess.opts.debugging_opts.emit_artifact_notifications {
1055+
tcx.sess.parse_sess.span_diagnostic.emit_artifact_notification(&out_filename);
10591056
}
10601057
}
10611058

src/libsyntax/json.rs

+7-6
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ use errors::emitter::{Emitter, HumanReadableErrorType};
1919
use syntax_pos::{MacroBacktrace, Span, SpanLabel, MultiSpan};
2020
use rustc_data_structures::sync::{self, Lrc};
2121
use std::io::{self, Write};
22+
use std::path::Path;
2223
use std::vec;
2324
use std::sync::{Arc, Mutex};
2425

@@ -91,15 +92,15 @@ impl Emitter for JsonEmitter {
9192
}
9293
}
9394

94-
fn maybe_emit_json_directive(&mut self, directive: String) {
95-
let data = Directive { directive };
95+
fn emit_artifact_notification(&mut self, path: &Path) {
96+
let data = ArtifactNotification { artifact: path };
9697
let result = if self.pretty {
9798
writeln!(&mut self.dst, "{}", as_pretty_json(&data))
9899
} else {
99100
writeln!(&mut self.dst, "{}", as_json(&data))
100101
};
101102
if let Err(e) = result {
102-
panic!("failed to print message: {:?}", e);
103+
panic!("failed to print notification: {:?}", e);
103104
}
104105
}
105106
}
@@ -181,9 +182,9 @@ struct DiagnosticCode {
181182
}
182183

183184
#[derive(RustcEncodable)]
184-
struct Directive {
185-
/// The directive itself.
186-
directive: String,
185+
struct ArtifactNotification<'a> {
186+
/// The path of the artifact.
187+
artifact: &'a Path,
187188
}
188189

189190
impl Diagnostic {
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
{"artifact":"$TEST_BUILD_DIR/emit-artifact-notifications.nll/libemit_artifact_notifications.rmeta"}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
// compile-flags:--emit=metadata --error-format=json -Z emit-artifact-notifications
2+
// compile-pass
3+
4+
// A very basic test for the emission of artifact notifications in JSON output.
5+
6+
fn main() {}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
{"artifact":"$TEST_BUILD_DIR/emit-artifact-notifications/libemit_artifact_notifications.rmeta"}

src/test/ui/emit-directives.rs

-6
This file was deleted.

src/test/ui/emit-directives.stderr

-1
This file was deleted.

src/tools/compiletest/src/json.rs

+5-5
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
use crate::errors::{Error, ErrorKind};
55
use crate::runtest::ProcRes;
66
use serde_json;
7-
use std::path::Path;
7+
use std::path::{Path, PathBuf};
88
use std::str::FromStr;
99

1010
#[derive(Deserialize)]
@@ -18,9 +18,9 @@ struct Diagnostic {
1818
}
1919

2020
#[derive(Deserialize)]
21-
struct Directive {
21+
struct ArtifactNotification {
2222
#[allow(dead_code)]
23-
directive: String,
23+
artifact: PathBuf,
2424
}
2525

2626
#[derive(Deserialize, Clone)]
@@ -75,8 +75,8 @@ pub fn extract_rendered(output: &str) -> String {
7575
if line.starts_with('{') {
7676
if let Ok(diagnostic) = serde_json::from_str::<Diagnostic>(line) {
7777
diagnostic.rendered
78-
} else if let Ok(_directive) = serde_json::from_str::<Directive>(line) {
79-
// Swallow the directive.
78+
} else if let Ok(_) = serde_json::from_str::<ArtifactNotification>(line) {
79+
// Ignore the notification.
8080
None
8181
} else {
8282
print!(

0 commit comments

Comments
 (0)