Skip to content

Commit b8fa4cb

Browse files
committed
Auto merge of #60464 - eddyb:not-overly-specific-pipelining, r=alexcrichton
rustc: rename -Z emit-directives to -Z emit-artifact-notifications and simplify the output. This is my take on #60006 / #60419 (see #60006 (comment)). I'm not too attached the "notifications" part, it's pretty much bikeshed material. **EDIT**: for "artifact", @matklad pointed out Cargo already uses it (in #60464 (comment)) The first two commits are fixes that could be landed independently, especially the `compiletest` one, which removes the need for any of the normalization added in #60006 to land the test. The last commit enables the emission for all outputs, which was my main suggestion for #60006, mostly to show that it's minimal and not really a "scope creep" (as suggested in #60006 (comment)). cc @alexcrichton @nnethercote
2 parents eeedd3a + c89a131 commit b8fa4cb

16 files changed

+87
-74
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_codegen_ssa/back/link.rs

+3
Original file line numberDiff line numberDiff line change
@@ -95,6 +95,9 @@ pub fn link_binary<'a, B: ArchiveBuilder<'a>>(sess: &'a Session,
9595
);
9696
}
9797
}
98+
if sess.opts.debugging_opts.emit_artifact_notifications {
99+
sess.parse_sess.span_diagnostic.emit_artifact_notification(&out_filename);
100+
}
98101
}
99102

100103
if sess.opts.cg.save_temps {

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/libserialize/serialize.rs

+7-1
Original file line numberDiff line numberDiff line change
@@ -764,12 +764,18 @@ macro_rules! tuple {
764764

765765
tuple! { T0, T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11, }
766766

767-
impl Encodable for path::PathBuf {
767+
impl Encodable for path::Path {
768768
fn encode<S: Encoder>(&self, e: &mut S) -> Result<(), S::Error> {
769769
self.to_str().unwrap().encode(e)
770770
}
771771
}
772772

773+
impl Encodable for path::PathBuf {
774+
fn encode<S: Encoder>(&self, e: &mut S) -> Result<(), S::Error> {
775+
path::Path::encode(self, e)
776+
}
777+
}
778+
773779
impl Decodable for path::PathBuf {
774780
fn decode<D: Decoder>(d: &mut D) -> Result<path::PathBuf, D::Error> {
775781
let bytes: String = Decodable::decode(d)?;

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 {

src/test/ui/consts/const-eval/unused-broken-const.stderr

-2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,3 @@
1-
warning: due to multiple output types requested, the explicitly specified output file name will be adapted for each output type
2-
31
error: any use of this value will cause an error
42
--> $DIR/unused-broken-const.rs:5:18
53
|
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

-12
This file was deleted.

src/test/ui/emit-directives.stderr

-1
This file was deleted.

src/test/ui/traits/trait-object-with-self-in-projection-output-repeated-supertrait.rs

+3
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,8 @@
11
// compile-pass
22

3+
// FIXME(eddyb) shorten the name so windows doesn't choke on it.
4+
#![crate_name = "trait_test"]
5+
36
// Regression test related to #56288. Check that a supertrait projection (of
47
// `Output`) that references `Self` is ok if there is another occurence of
58
// the same supertrait that specifies the projection explicitly, even if

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!(

src/tools/compiletest/src/runtest.rs

+36-22
Original file line numberDiff line numberDiff line change
@@ -1422,10 +1422,21 @@ impl<'test> TestCx<'test> {
14221422
}
14231423

14241424
fn compile_test(&self) -> ProcRes {
1425-
let mut rustc = self.make_compile_args(
1426-
&self.testpaths.file,
1427-
TargetLocation::ThisFile(self.make_exe_name()),
1428-
);
1425+
// Only use `make_exe_name` when the test ends up being executed.
1426+
let will_execute = match self.config.mode {
1427+
RunPass | Ui => self.should_run_successfully(),
1428+
Incremental => self.revision.unwrap().starts_with("r"),
1429+
RunFail | RunPassValgrind | MirOpt |
1430+
DebugInfoBoth | DebugInfoGdb | DebugInfoLldb => true,
1431+
_ => false,
1432+
};
1433+
let output_file = if will_execute {
1434+
TargetLocation::ThisFile(self.make_exe_name())
1435+
} else {
1436+
TargetLocation::ThisDirectory(self.output_base_dir())
1437+
};
1438+
1439+
let mut rustc = self.make_compile_args(&self.testpaths.file, output_file);
14291440

14301441
rustc.arg("-L").arg(&self.aux_output_dir_name());
14311442

@@ -1882,7 +1893,12 @@ impl<'test> TestCx<'test> {
18821893
rustc.arg("-o").arg(path);
18831894
}
18841895
TargetLocation::ThisDirectory(path) => {
1885-
rustc.arg("--out-dir").arg(path);
1896+
if is_rustdoc {
1897+
// `rustdoc` uses `-o` for the output directory.
1898+
rustc.arg("-o").arg(path);
1899+
} else {
1900+
rustc.arg("--out-dir").arg(path);
1901+
}
18861902
}
18871903
}
18881904

@@ -3138,42 +3154,40 @@ impl<'test> TestCx<'test> {
31383154
}
31393155

31403156
fn normalize_output(&self, output: &str, custom_rules: &[(String, String)]) -> String {
3141-
let parent_dir = self.testpaths.file.parent().unwrap();
31423157
let cflags = self.props.compile_flags.join(" ");
31433158
let json = cflags.contains("--error-format json")
31443159
|| cflags.contains("--error-format pretty-json")
31453160
|| cflags.contains("--error-format=json")
31463161
|| cflags.contains("--error-format=pretty-json");
3147-
let parent_dir_str = if json {
3148-
parent_dir.display().to_string().replace("\\", "\\\\")
3149-
} else {
3150-
parent_dir.display().to_string()
3162+
3163+
let mut normalized = output.to_string();
3164+
3165+
let mut normalize_path = |from: &Path, to: &str| {
3166+
let mut from = from.display().to_string();
3167+
if json {
3168+
from = from.replace("\\", "\\\\");
3169+
}
3170+
normalized = normalized.replace(&from, to);
31513171
};
31523172

3153-
let mut normalized = output.replace(&parent_dir_str, "$DIR");
3173+
let parent_dir = self.testpaths.file.parent().unwrap();
3174+
normalize_path(parent_dir, "$DIR");
31543175

31553176
// Paths into the libstd/libcore
31563177
let src_dir = self.config.src_base.parent().unwrap().parent().unwrap();
3157-
let src_dir_str = if json {
3158-
src_dir.display().to_string().replace("\\", "\\\\")
3159-
} else {
3160-
src_dir.display().to_string()
3161-
};
3162-
normalized = normalized.replace(&src_dir_str, "$SRC_DIR");
3178+
normalize_path(src_dir, "$SRC_DIR");
31633179

31643180
// Paths into the build directory
31653181
let test_build_dir = &self.config.build_base;
31663182
let parent_build_dir = test_build_dir.parent().unwrap().parent().unwrap().parent().unwrap();
31673183

31683184
// eg. /home/user/rust/build/x86_64-unknown-linux-gnu/test/ui
3169-
normalized = normalized.replace(test_build_dir.to_str().unwrap(), "$TEST_BUILD_DIR");
3185+
normalize_path(test_build_dir, "$TEST_BUILD_DIR");
31703186
// eg. /home/user/rust/build
3171-
normalized = normalized.replace(&parent_build_dir.to_str().unwrap(), "$BUILD_DIR");
3187+
normalize_path(parent_build_dir, "$BUILD_DIR");
31723188

31733189
// Paths into lib directory.
3174-
let mut lib_dir = parent_build_dir.parent().unwrap().to_path_buf();
3175-
lib_dir.push("lib");
3176-
normalized = normalized.replace(&lib_dir.to_str().unwrap(), "$LIB_DIR");
3190+
normalize_path(&parent_build_dir.parent().unwrap().join("lib"), "$LIB_DIR");
31773191

31783192
if json {
31793193
// escaped newlines in json strings should be readable

0 commit comments

Comments
 (0)