]> git.lizzy.rs Git - rust.git/commitdiff
Gate the printing on --json=unused-externs
authorest31 <MTest31@outlook.com>
Tue, 30 Jun 2020 18:17:07 +0000 (20:17 +0200)
committerest31 <MTest31@outlook.com>
Mon, 8 Mar 2021 07:17:48 +0000 (08:17 +0100)
compiler/rustc_metadata/src/creader.rs
compiler/rustc_session/src/config.rs
compiler/rustc_session/src/options.rs
src/librustdoc/config.rs

index 3f2d312e61b7a77c0ab4c30912006a0eb98fa422..7b34374032c918ab2fa61a3051280d3fa3eaf9af 100644 (file)
@@ -931,8 +931,9 @@ fn report_unused_deps(&mut self, krate: &ast::Crate) {
                     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) {
index f25828e21618f4c31f436e7883ff05114466c9be..a66201953d61125f18d030c38ae640874f02cbed 100644 (file)
@@ -734,6 +734,7 @@ fn default() -> Options {
             remap_path_prefix: Vec::new(),
             edition: DEFAULT_EDITION,
             json_artifact_notifications: false,
+            json_unused_externs: false,
             pretty: None,
         }
     }
@@ -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
@@ -1275,6 +1277,7 @@ 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),
@@ -1282,7 +1285,7 @@ pub fn parse_json(matches: &getopts::Matches) -> (HumanReadableErrorType, bool)
             }
         }
     }
-    (json_rendered(json_color), json_artifact_notifications)
+    (json_rendered(json_color), json_artifact_notifications, json_unused_externs)
 }
 
 /// Parses the `--error-format` flag.
@@ -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);
 
@@ -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);
@@ -2050,6 +2061,7 @@ pub fn build_session_options(matches: &getopts::Matches) -> Options {
         remap_path_prefix,
         edition,
         json_artifact_notifications,
+        json_unused_externs,
         pretty,
     }
 }
index 79bbad8307ba7cb20350a270c213a31d526ada4a..cfe29d40b7460ad12a4861d40f7ad5004fb5c555 100644 (file)
@@ -147,6 +147,9 @@ pub struct 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],
     }
 );
index ecb6378f31fb452fe61e588fe82f3f0493c5b007..5d6d5aaec140427d9f61d45d8613242668cf7cdb 100644 (file)
@@ -323,7 +323,7 @@ fn println_condition(condition: Condition) {
         }
 
         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);