Skip to content

Commit

Permalink
Gate the printing on --json=unused-externs
Browse files Browse the repository at this point in the history
  • Loading branch information
est31 committed Mar 8, 2021
1 parent aef1e35 commit 3f2ca47
Show file tree
Hide file tree
Showing 4 changed files with 22 additions and 6 deletions.
5 changes: 3 additions & 2 deletions compiler/rustc_metadata/src/creader.rs
Expand Up @@ -931,8 +931,9 @@ impl<'a> CrateLoader<'a> {
diag,
);
}
// FIXME: add gating
self.sess.parse_sess.span_diagnostic.emit_unused_externs(&unused_externs);
if self.sess.opts.json_unused_externs {
self.sess.parse_sess.span_diagnostic.emit_unused_externs(&unused_externs);
}
}

pub fn postprocess(&mut self, krate: &ast::Crate) {
Expand Down
18 changes: 15 additions & 3 deletions compiler/rustc_session/src/config.rs
Expand Up @@ -734,6 +734,7 @@ impl Default for Options {
remap_path_prefix: Vec::new(),
edition: DEFAULT_EDITION,
json_artifact_notifications: false,
json_unused_externs: false,
pretty: None,
}
}
Expand Down Expand Up @@ -1254,11 +1255,12 @@ pub fn parse_color(matches: &getopts::Matches) -> ColorConfig {
///
/// The first value returned is how to render JSON diagnostics, and the second
/// is whether or not artifact notifications are enabled.
pub fn parse_json(matches: &getopts::Matches) -> (HumanReadableErrorType, bool) {
pub fn parse_json(matches: &getopts::Matches) -> (HumanReadableErrorType, bool, bool) {
let mut json_rendered: fn(ColorConfig) -> HumanReadableErrorType =
HumanReadableErrorType::Default;
let mut json_color = ColorConfig::Never;
let mut json_artifact_notifications = false;
let mut json_unused_externs = false;
for option in matches.opt_strs("json") {
// For now conservatively forbid `--color` with `--json` since `--json`
// won't actually be emitting any colors and anything colorized is
Expand All @@ -1275,14 +1277,15 @@ pub fn parse_json(matches: &getopts::Matches) -> (HumanReadableErrorType, bool)
"diagnostic-short" => json_rendered = HumanReadableErrorType::Short,
"diagnostic-rendered-ansi" => json_color = ColorConfig::Always,
"artifacts" => json_artifact_notifications = true,
"unused-externs" => json_unused_externs = true,
s => early_error(
ErrorOutputType::default(),
&format!("unknown `--json` option `{}`", s),
),
}
}
}
(json_rendered(json_color), json_artifact_notifications)
(json_rendered(json_color), json_artifact_notifications, json_unused_externs)
}

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

let edition = parse_crate_edition(matches);

let (json_rendered, json_artifact_notifications) = parse_json(matches);
let (json_rendered, json_artifact_notifications, json_unused_externs) = parse_json(matches);

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

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

if !debugging_opts.unstable_options && json_unused_externs {
early_error(
error_format,
"the `-Z unstable-options` flag must also be passed to enable \
the flag `--json=unused-externs`",
);
}

let output_types = parse_output_types(&debugging_opts, matches, error_format);

let mut cg = build_codegen_options(matches, error_format);
Expand Down Expand Up @@ -2050,6 +2061,7 @@ pub fn build_session_options(matches: &getopts::Matches) -> Options {
remap_path_prefix,
edition,
json_artifact_notifications,
json_unused_externs,
pretty,
}
}
Expand Down
3 changes: 3 additions & 0 deletions compiler/rustc_session/src/options.rs
Expand Up @@ -147,6 +147,9 @@ top_level_options!(
// by the compiler.
json_artifact_notifications: bool [TRACKED],

// `true` if we're emitting a JSON blob containing the unused externs
json_unused_externs: bool [UNTRACKED],

pretty: Option<PpMode> [UNTRACKED],
}
);
Expand Down
2 changes: 1 addition & 1 deletion src/librustdoc/config.rs
Expand Up @@ -323,7 +323,7 @@ impl Options {
}

let color = config::parse_color(&matches);
let (json_rendered, _artifacts) = config::parse_json(&matches);
let (json_rendered, ..) = config::parse_json(&matches);
let error_format = config::parse_error_format(&matches, color, json_rendered);

let codegen_options = build_codegen_options(matches, error_format);
Expand Down

0 comments on commit 3f2ca47

Please sign in to comment.