@@ -438,6 +438,10 @@ top_level_options!(
438438 remap_path_prefix: Vec <( PathBuf , PathBuf ) > [ UNTRACKED ] ,
439439
440440 edition: Edition [ TRACKED ] ,
441+
442+ // Whether or not we're emitting JSON blobs about each artifact produced
443+ // by the compiler.
444+ json_artifact_notifications: bool [ TRACKED ] ,
441445 }
442446) ;
443447
@@ -625,6 +629,7 @@ impl Default for Options {
625629 cli_forced_thinlto_off : false ,
626630 remap_path_prefix : Vec :: new ( ) ,
627631 edition : DEFAULT_EDITION ,
632+ json_artifact_notifications : false ,
628633 }
629634 }
630635}
@@ -1463,8 +1468,6 @@ options! {DebuggingOptions, DebuggingSetter, basic_debugging_options,
14631468 the same values as the target option of the same name" ) ,
14641469 allow_features: Option <Vec <String >> = ( None , parse_opt_comma_list, [ TRACKED ] ,
14651470 "only allow the listed language features to be enabled in code (space separated)" ) ,
1466- emit_artifact_notifications: bool = ( false , parse_bool, [ UNTRACKED ] ,
1467- "emit notifications after each artifact has been output (only in the JSON format)" ) ,
14681471 symbol_mangling_version: SymbolManglingVersion = ( SymbolManglingVersion :: Legacy ,
14691472 parse_symbol_mangling_version, [ TRACKED ] ,
14701473 "which mangling version to use for symbol names" ) ,
@@ -1822,11 +1825,11 @@ pub fn rustc_optgroups() -> Vec<RustcOptGroup> {
18221825 "How errors and other messages are produced" ,
18231826 "human|json|short" ,
18241827 ) ,
1825- opt:: opt (
1828+ opt:: multi_s (
18261829 "" ,
1827- "json-rendered " ,
1828- "Choose `rendered` field of json diagnostics render scheme " ,
1829- "plain|termcolor " ,
1830+ "json" ,
1831+ "Configure the JSON output of the compiler " ,
1832+ "CONFIG " ,
18301833 ) ,
18311834 opt:: opt_s(
18321835 "" ,
@@ -1922,10 +1925,9 @@ pub fn get_cmd_lint_options(matches: &getopts::Matches,
19221925 ( lint_opts, describe_lints, lint_cap)
19231926}
19241927
1925- pub fn build_session_options_and_crate_config (
1926- matches : & getopts:: Matches ,
1927- ) -> ( Options , FxHashSet < ( String , Option < String > ) > ) {
1928- let color = match matches. opt_str ( "color" ) . as_ref ( ) . map ( |s| & s[ ..] ) {
1928+ /// Parse the `--color` flag
1929+ pub fn parse_color ( matches : & getopts:: Matches ) -> ColorConfig {
1930+ match matches. opt_str ( "color" ) . as_ref ( ) . map ( |s| & s[ ..] ) {
19291931 Some ( "auto" ) => ColorConfig :: Auto ,
19301932 Some ( "always" ) => ColorConfig :: Always ,
19311933 Some ( "never" ) => ColorConfig :: Never ,
@@ -1940,46 +1942,52 @@ pub fn build_session_options_and_crate_config(
19401942 arg
19411943 ) ,
19421944 ) ,
1943- } ;
1945+ }
1946+ }
19441947
1945- let edition = match matches. opt_str ( "edition" ) {
1946- Some ( arg) => Edition :: from_str ( & arg) . unwrap_or_else ( |_|
1948+ /// Parse the `--json` flag.
1949+ ///
1950+ /// The first value returned is how to render JSON diagnostics, and the second
1951+ /// is whether or not artifact notifications are enabled.
1952+ pub fn parse_json ( matches : & getopts:: Matches ) -> ( HumanReadableErrorType , bool ) {
1953+ let mut json_rendered: fn ( ColorConfig ) -> HumanReadableErrorType =
1954+ HumanReadableErrorType :: Default ;
1955+ let mut json_color = ColorConfig :: Never ;
1956+ let mut json_artifact_notifications = false ;
1957+ for option in matches. opt_strs ( "json" ) {
1958+ // For now conservatively forbid `--color` with `--json` since `--json`
1959+ // won't actually be emitting any colors and anything colorized is
1960+ // embedded in a diagnostic message anyway.
1961+ if matches. opt_str ( "color" ) . is_some ( ) {
19471962 early_error (
19481963 ErrorOutputType :: default ( ) ,
1949- & format ! (
1950- "argument for --edition must be one of: \
1951- {}. (instead was `{}`)",
1952- EDITION_NAME_LIST ,
1953- arg
1954- ) ,
1955- ) ,
1956- ) ,
1957- None => DEFAULT_EDITION ,
1958- } ;
1964+ "cannot specify the `--color` option with `--json`" ,
1965+ ) ;
1966+ }
19591967
1960- if !edition. is_stable ( ) && !nightly_options:: is_nightly_build ( ) {
1961- early_error (
1962- ErrorOutputType :: default ( ) ,
1963- & format ! (
1964- "Edition {} is unstable and only \
1965- available for nightly builds of rustc.",
1966- edition,
1967- )
1968- )
1968+ for sub_option in option. split ( ',' ) {
1969+ match sub_option {
1970+ "diagnostic-short" => json_rendered = HumanReadableErrorType :: Short ,
1971+ "diagnostic-rendered-ansi" => json_color = ColorConfig :: Always ,
1972+ "artifacts" => json_artifact_notifications = true ,
1973+ s => {
1974+ early_error (
1975+ ErrorOutputType :: default ( ) ,
1976+ & format ! ( "unknown `--json` option `{}`" , s) ,
1977+ )
1978+ }
1979+ }
1980+ }
19691981 }
1982+ ( json_rendered ( json_color) , json_artifact_notifications)
1983+ }
19701984
1971- let json_rendered = matches. opt_str ( "json-rendered" ) . and_then ( |s| match s. as_str ( ) {
1972- "plain" => None ,
1973- "termcolor" => Some ( HumanReadableErrorType :: Default ( ColorConfig :: Always ) ) ,
1974- _ => early_error (
1975- ErrorOutputType :: default ( ) ,
1976- & format ! (
1977- "argument for --json-rendered must be `plain` or `termcolor` (instead was `{}`)" ,
1978- s,
1979- ) ,
1980- ) ,
1981- } ) . unwrap_or ( HumanReadableErrorType :: Default ( ColorConfig :: Never ) ) ;
1982-
1985+ /// Parse the `--error-format` flag
1986+ pub fn parse_error_format (
1987+ matches : & getopts:: Matches ,
1988+ color : ColorConfig ,
1989+ json_rendered : HumanReadableErrorType ,
1990+ ) -> ErrorOutputType {
19831991 // We need the opts_present check because the driver will send us Matches
19841992 // with only stable options if no unstable options are used. Since error-format
19851993 // is unstable, it will not be present. We have to use opts_present not
@@ -2008,6 +2016,60 @@ pub fn build_session_options_and_crate_config(
20082016 ErrorOutputType :: HumanReadable ( HumanReadableErrorType :: Default ( color) )
20092017 } ;
20102018
2019+ match error_format {
2020+ ErrorOutputType :: Json { .. } => { }
2021+
2022+ // Conservatively require that the `--json` argument is coupled with
2023+ // `--error-format=json`. This means that `--json` is specified we
2024+ // should actually be emitting JSON blobs.
2025+ _ if matches. opt_strs ( "json" ) . len ( ) > 0 => {
2026+ early_error (
2027+ ErrorOutputType :: default ( ) ,
2028+ "using `--json` requires also using `--error-format=json`" ,
2029+ ) ;
2030+ }
2031+
2032+ _ => { }
2033+ }
2034+
2035+ return error_format;
2036+ }
2037+
2038+ pub fn build_session_options_and_crate_config (
2039+ matches : & getopts:: Matches ,
2040+ ) -> ( Options , FxHashSet < ( String , Option < String > ) > ) {
2041+ let color = parse_color ( matches) ;
2042+
2043+ let edition = match matches. opt_str ( "edition" ) {
2044+ Some ( arg) => Edition :: from_str ( & arg) . unwrap_or_else ( |_|
2045+ early_error (
2046+ ErrorOutputType :: default ( ) ,
2047+ & format ! (
2048+ "argument for --edition must be one of: \
2049+ {}. (instead was `{}`)",
2050+ EDITION_NAME_LIST ,
2051+ arg
2052+ ) ,
2053+ ) ,
2054+ ) ,
2055+ None => DEFAULT_EDITION ,
2056+ } ;
2057+
2058+ if !edition. is_stable ( ) && !nightly_options:: is_nightly_build ( ) {
2059+ early_error (
2060+ ErrorOutputType :: default ( ) ,
2061+ & format ! (
2062+ "Edition {} is unstable and only \
2063+ available for nightly builds of rustc.",
2064+ edition,
2065+ )
2066+ )
2067+ }
2068+
2069+ let ( json_rendered, json_artifact_notifications) = parse_json ( matches) ;
2070+
2071+ let error_format = parse_error_format ( matches, color, json_rendered) ;
2072+
20112073 let unparsed_crate_types = matches. opt_strs ( "crate-type" ) ;
20122074 let crate_types = parse_crate_types_from_list ( unparsed_crate_types)
20132075 . unwrap_or_else ( |e| early_error ( error_format, & e[ ..] ) ) ;
@@ -2018,9 +2080,6 @@ pub fn build_session_options_and_crate_config(
20182080 let mut debugging_opts = build_debugging_options ( matches, error_format) ;
20192081
20202082 if !debugging_opts. unstable_options {
2021- if matches. opt_str ( "json-rendered" ) . is_some ( ) {
2022- early_error ( error_format, "`--json-rendered=x` is unstable" ) ;
2023- }
20242083 if let ErrorOutputType :: Json { pretty : true , json_rendered } = error_format {
20252084 early_error (
20262085 ErrorOutputType :: Json { pretty : false , json_rendered } ,
@@ -2445,6 +2504,7 @@ pub fn build_session_options_and_crate_config(
24452504 cli_forced_thinlto_off : disable_thinlto,
24462505 remap_path_prefix,
24472506 edition,
2507+ json_artifact_notifications,
24482508 } ,
24492509 cfg,
24502510 )
0 commit comments