@@ -438,6 +438,10 @@ top_level_options!(
438
438
remap_path_prefix: Vec <( PathBuf , PathBuf ) > [ UNTRACKED ] ,
439
439
440
440
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 ] ,
441
445
}
442
446
) ;
443
447
@@ -625,6 +629,7 @@ impl Default for Options {
625
629
cli_forced_thinlto_off : false ,
626
630
remap_path_prefix : Vec :: new ( ) ,
627
631
edition : DEFAULT_EDITION ,
632
+ json_artifact_notifications : false ,
628
633
}
629
634
}
630
635
}
@@ -1463,8 +1468,6 @@ options! {DebuggingOptions, DebuggingSetter, basic_debugging_options,
1463
1468
the same values as the target option of the same name" ) ,
1464
1469
allow_features: Option <Vec <String >> = ( None , parse_opt_comma_list, [ TRACKED ] ,
1465
1470
"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)" ) ,
1468
1471
symbol_mangling_version: SymbolManglingVersion = ( SymbolManglingVersion :: Legacy ,
1469
1472
parse_symbol_mangling_version, [ TRACKED ] ,
1470
1473
"which mangling version to use for symbol names" ) ,
@@ -1822,11 +1825,11 @@ pub fn rustc_optgroups() -> Vec<RustcOptGroup> {
1822
1825
"How errors and other messages are produced" ,
1823
1826
"human|json|short" ,
1824
1827
) ,
1825
- opt:: opt (
1828
+ opt:: multi_s (
1826
1829
"" ,
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 " ,
1830
1833
) ,
1831
1834
opt:: opt_s(
1832
1835
"" ,
@@ -1922,10 +1925,9 @@ pub fn get_cmd_lint_options(matches: &getopts::Matches,
1922
1925
( lint_opts, describe_lints, lint_cap)
1923
1926
}
1924
1927
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[ ..] ) {
1929
1931
Some ( "auto" ) => ColorConfig :: Auto ,
1930
1932
Some ( "always" ) => ColorConfig :: Always ,
1931
1933
Some ( "never" ) => ColorConfig :: Never ,
@@ -1940,46 +1942,52 @@ pub fn build_session_options_and_crate_config(
1940
1942
arg
1941
1943
) ,
1942
1944
) ,
1943
- } ;
1945
+ }
1946
+ }
1944
1947
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 ( ) {
1947
1962
early_error (
1948
1963
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
+ }
1959
1967
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
+ }
1969
1981
}
1982
+ ( json_rendered ( json_color) , json_artifact_notifications)
1983
+ }
1970
1984
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 {
1983
1991
// We need the opts_present check because the driver will send us Matches
1984
1992
// with only stable options if no unstable options are used. Since error-format
1985
1993
// 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(
2008
2016
ErrorOutputType :: HumanReadable ( HumanReadableErrorType :: Default ( color) )
2009
2017
} ;
2010
2018
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
+
2011
2073
let unparsed_crate_types = matches. opt_strs ( "crate-type" ) ;
2012
2074
let crate_types = parse_crate_types_from_list ( unparsed_crate_types)
2013
2075
. unwrap_or_else ( |e| early_error ( error_format, & e[ ..] ) ) ;
@@ -2018,9 +2080,6 @@ pub fn build_session_options_and_crate_config(
2018
2080
let mut debugging_opts = build_debugging_options ( matches, error_format) ;
2019
2081
2020
2082
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
- }
2024
2083
if let ErrorOutputType :: Json { pretty : true , json_rendered } = error_format {
2025
2084
early_error (
2026
2085
ErrorOutputType :: Json { pretty : false , json_rendered } ,
@@ -2445,6 +2504,7 @@ pub fn build_session_options_and_crate_config(
2445
2504
cli_forced_thinlto_off : disable_thinlto,
2446
2505
remap_path_prefix,
2447
2506
edition,
2507
+ json_artifact_notifications,
2448
2508
} ,
2449
2509
cfg,
2450
2510
)
0 commit comments