Skip to content

Commit 3f2ca47

Browse files
committed
Gate the printing on --json=unused-externs
1 parent aef1e35 commit 3f2ca47

File tree

4 files changed

+22
-6
lines changed

4 files changed

+22
-6
lines changed

compiler/rustc_metadata/src/creader.rs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -931,8 +931,9 @@ impl<'a> CrateLoader<'a> {
931931
diag,
932932
);
933933
}
934-
// FIXME: add gating
935-
self.sess.parse_sess.span_diagnostic.emit_unused_externs(&unused_externs);
934+
if self.sess.opts.json_unused_externs {
935+
self.sess.parse_sess.span_diagnostic.emit_unused_externs(&unused_externs);
936+
}
936937
}
937938

938939
pub fn postprocess(&mut self, krate: &ast::Crate) {

compiler/rustc_session/src/config.rs

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -734,6 +734,7 @@ impl Default for Options {
734734
remap_path_prefix: Vec::new(),
735735
edition: DEFAULT_EDITION,
736736
json_artifact_notifications: false,
737+
json_unused_externs: false,
737738
pretty: None,
738739
}
739740
}
@@ -1254,11 +1255,12 @@ pub fn parse_color(matches: &getopts::Matches) -> ColorConfig {
12541255
///
12551256
/// The first value returned is how to render JSON diagnostics, and the second
12561257
/// is whether or not artifact notifications are enabled.
1257-
pub fn parse_json(matches: &getopts::Matches) -> (HumanReadableErrorType, bool) {
1258+
pub fn parse_json(matches: &getopts::Matches) -> (HumanReadableErrorType, bool, bool) {
12581259
let mut json_rendered: fn(ColorConfig) -> HumanReadableErrorType =
12591260
HumanReadableErrorType::Default;
12601261
let mut json_color = ColorConfig::Never;
12611262
let mut json_artifact_notifications = false;
1263+
let mut json_unused_externs = false;
12621264
for option in matches.opt_strs("json") {
12631265
// For now conservatively forbid `--color` with `--json` since `--json`
12641266
// won't actually be emitting any colors and anything colorized is
@@ -1275,14 +1277,15 @@ pub fn parse_json(matches: &getopts::Matches) -> (HumanReadableErrorType, bool)
12751277
"diagnostic-short" => json_rendered = HumanReadableErrorType::Short,
12761278
"diagnostic-rendered-ansi" => json_color = ColorConfig::Always,
12771279
"artifacts" => json_artifact_notifications = true,
1280+
"unused-externs" => json_unused_externs = true,
12781281
s => early_error(
12791282
ErrorOutputType::default(),
12801283
&format!("unknown `--json` option `{}`", s),
12811284
),
12821285
}
12831286
}
12841287
}
1285-
(json_rendered(json_color), json_artifact_notifications)
1288+
(json_rendered(json_color), json_artifact_notifications, json_unused_externs)
12861289
}
12871290

12881291
/// Parses the `--error-format` flag.
@@ -1860,7 +1863,7 @@ pub fn build_session_options(matches: &getopts::Matches) -> Options {
18601863

18611864
let edition = parse_crate_edition(matches);
18621865

1863-
let (json_rendered, json_artifact_notifications) = parse_json(matches);
1866+
let (json_rendered, json_artifact_notifications, json_unused_externs) = parse_json(matches);
18641867

18651868
let error_format = parse_error_format(matches, color, json_rendered);
18661869

@@ -1873,6 +1876,14 @@ pub fn build_session_options(matches: &getopts::Matches) -> Options {
18731876
let mut debugging_opts = build_debugging_options(matches, error_format);
18741877
check_debug_option_stability(&debugging_opts, error_format, json_rendered);
18751878

1879+
if !debugging_opts.unstable_options && json_unused_externs {
1880+
early_error(
1881+
error_format,
1882+
"the `-Z unstable-options` flag must also be passed to enable \
1883+
the flag `--json=unused-externs`",
1884+
);
1885+
}
1886+
18761887
let output_types = parse_output_types(&debugging_opts, matches, error_format);
18771888

18781889
let mut cg = build_codegen_options(matches, error_format);
@@ -2050,6 +2061,7 @@ pub fn build_session_options(matches: &getopts::Matches) -> Options {
20502061
remap_path_prefix,
20512062
edition,
20522063
json_artifact_notifications,
2064+
json_unused_externs,
20532065
pretty,
20542066
}
20552067
}

compiler/rustc_session/src/options.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -147,6 +147,9 @@ top_level_options!(
147147
// by the compiler.
148148
json_artifact_notifications: bool [TRACKED],
149149

150+
// `true` if we're emitting a JSON blob containing the unused externs
151+
json_unused_externs: bool [UNTRACKED],
152+
150153
pretty: Option<PpMode> [UNTRACKED],
151154
}
152155
);

src/librustdoc/config.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -323,7 +323,7 @@ impl Options {
323323
}
324324

325325
let color = config::parse_color(&matches);
326-
let (json_rendered, _artifacts) = config::parse_json(&matches);
326+
let (json_rendered, ..) = config::parse_json(&matches);
327327
let error_format = config::parse_error_format(&matches, color, json_rendered);
328328

329329
let codegen_options = build_codegen_options(matches, error_format);

0 commit comments

Comments
 (0)