@@ -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
}
@@ -1459,8 +1464,6 @@ options! {DebuggingOptions, DebuggingSetter, basic_debugging_options,
1459
1464
the same values as the target option of the same name" ) ,
1460
1465
allow_features: Option <Vec <String >> = ( None , parse_opt_comma_list, [ TRACKED ] ,
1461
1466
"only allow the listed language features to be enabled in code (space separated)" ) ,
1462
- emit_artifact_notifications: bool = ( false , parse_bool, [ UNTRACKED ] ,
1463
- "emit notifications after each artifact has been output (only in the JSON format)" ) ,
1464
1467
symbol_mangling_version: SymbolManglingVersion = ( SymbolManglingVersion :: Legacy ,
1465
1468
parse_symbol_mangling_version, [ TRACKED ] ,
1466
1469
"which mangling version to use for symbol names" ) ,
@@ -1818,11 +1821,11 @@ pub fn rustc_optgroups() -> Vec<RustcOptGroup> {
1818
1821
"How errors and other messages are produced" ,
1819
1822
"human|json|short" ,
1820
1823
) ,
1821
- opt:: opt (
1824
+ opt:: multi_s (
1822
1825
"" ,
1823
- "json-rendered " ,
1824
- "Choose `rendered` field of json diagnostics render scheme " ,
1825
- "plain|termcolor " ,
1826
+ "json" ,
1827
+ "Configure the JSON output of the compiler " ,
1828
+ "CONFIG " ,
1826
1829
) ,
1827
1830
opt:: opt_s(
1828
1831
"" ,
@@ -1918,10 +1921,9 @@ pub fn get_cmd_lint_options(matches: &getopts::Matches,
1918
1921
( lint_opts, describe_lints, lint_cap)
1919
1922
}
1920
1923
1921
- pub fn build_session_options_and_crate_config (
1922
- matches : & getopts:: Matches ,
1923
- ) -> ( Options , FxHashSet < ( String , Option < String > ) > ) {
1924
- let color = match matches. opt_str ( "color" ) . as_ref ( ) . map ( |s| & s[ ..] ) {
1924
+ /// Parse the `--color` flag
1925
+ pub fn parse_color ( matches : & getopts:: Matches ) -> ColorConfig {
1926
+ match matches. opt_str ( "color" ) . as_ref ( ) . map ( |s| & s[ ..] ) {
1925
1927
Some ( "auto" ) => ColorConfig :: Auto ,
1926
1928
Some ( "always" ) => ColorConfig :: Always ,
1927
1929
Some ( "never" ) => ColorConfig :: Never ,
@@ -1936,46 +1938,52 @@ pub fn build_session_options_and_crate_config(
1936
1938
arg
1937
1939
) ,
1938
1940
) ,
1939
- } ;
1941
+ }
1942
+ }
1940
1943
1941
- let edition = match matches. opt_str ( "edition" ) {
1942
- Some ( arg) => Edition :: from_str ( & arg) . unwrap_or_else ( |_|
1944
+ /// Parse the `--json` flag.
1945
+ ///
1946
+ /// The first value returned is how to render JSON diagnostics, and the second
1947
+ /// is whether or not artifact notifications are enabled.
1948
+ pub fn parse_json ( matches : & getopts:: Matches ) -> ( HumanReadableErrorType , bool ) {
1949
+ let mut json_rendered: fn ( ColorConfig ) -> HumanReadableErrorType =
1950
+ HumanReadableErrorType :: Default ;
1951
+ let mut json_color = ColorConfig :: Never ;
1952
+ let mut json_artifact_notifications = false ;
1953
+ for option in matches. opt_strs ( "json" ) {
1954
+ // For now conservatively forbid `--color` with `--json` since `--json`
1955
+ // won't actually be emitting any colors and anything colorized is
1956
+ // embedded in a diagnostic message anyway.
1957
+ if matches. opt_str ( "color" ) . is_some ( ) {
1943
1958
early_error (
1944
1959
ErrorOutputType :: default ( ) ,
1945
- & format ! (
1946
- "argument for --edition must be one of: \
1947
- {}. (instead was `{}`)",
1948
- EDITION_NAME_LIST ,
1949
- arg
1950
- ) ,
1951
- ) ,
1952
- ) ,
1953
- None => DEFAULT_EDITION ,
1954
- } ;
1960
+ "cannot specify the `--color` option with `--json`" ,
1961
+ ) ;
1962
+ }
1955
1963
1956
- if !edition. is_stable ( ) && !nightly_options:: is_nightly_build ( ) {
1957
- early_error (
1958
- ErrorOutputType :: default ( ) ,
1959
- & format ! (
1960
- "Edition {} is unstable and only \
1961
- available for nightly builds of rustc.",
1962
- edition,
1963
- )
1964
- )
1964
+ for sub_option in option. split ( ',' ) {
1965
+ match sub_option {
1966
+ "diagnostic-short" => json_rendered = HumanReadableErrorType :: Short ,
1967
+ "diagnostic-rendered-ansi" => json_color = ColorConfig :: Always ,
1968
+ "artifacts" => json_artifact_notifications = true ,
1969
+ s => {
1970
+ early_error (
1971
+ ErrorOutputType :: default ( ) ,
1972
+ & format ! ( "unknown `--json` option `{}`" , s) ,
1973
+ )
1974
+ }
1975
+ }
1976
+ }
1965
1977
}
1978
+ ( json_rendered ( json_color) , json_artifact_notifications)
1979
+ }
1966
1980
1967
- let json_rendered = matches. opt_str ( "json-rendered" ) . and_then ( |s| match s. as_str ( ) {
1968
- "plain" => None ,
1969
- "termcolor" => Some ( HumanReadableErrorType :: Default ( ColorConfig :: Always ) ) ,
1970
- _ => early_error (
1971
- ErrorOutputType :: default ( ) ,
1972
- & format ! (
1973
- "argument for --json-rendered must be `plain` or `termcolor` (instead was `{}`)" ,
1974
- s,
1975
- ) ,
1976
- ) ,
1977
- } ) . unwrap_or ( HumanReadableErrorType :: Default ( ColorConfig :: Never ) ) ;
1978
-
1981
+ /// Parse the `--error-format` flag
1982
+ pub fn parse_error_format (
1983
+ matches : & getopts:: Matches ,
1984
+ color : ColorConfig ,
1985
+ json_rendered : HumanReadableErrorType ,
1986
+ ) -> ErrorOutputType {
1979
1987
// We need the opts_present check because the driver will send us Matches
1980
1988
// with only stable options if no unstable options are used. Since error-format
1981
1989
// is unstable, it will not be present. We have to use opts_present not
@@ -2004,6 +2012,60 @@ pub fn build_session_options_and_crate_config(
2004
2012
ErrorOutputType :: HumanReadable ( HumanReadableErrorType :: Default ( color) )
2005
2013
} ;
2006
2014
2015
+ match error_format {
2016
+ ErrorOutputType :: Json { .. } => { }
2017
+
2018
+ // Conservatively require that the `--json` argument is coupled with
2019
+ // `--error-format=json`. This means that `--json` is specified we
2020
+ // should actually be emitting JSON blobs.
2021
+ _ if matches. opt_strs ( "json" ) . len ( ) > 0 => {
2022
+ early_error (
2023
+ ErrorOutputType :: default ( ) ,
2024
+ "using `--json` requires also using `--error-format=json`" ,
2025
+ ) ;
2026
+ }
2027
+
2028
+ _ => { }
2029
+ }
2030
+
2031
+ return error_format;
2032
+ }
2033
+
2034
+ pub fn build_session_options_and_crate_config (
2035
+ matches : & getopts:: Matches ,
2036
+ ) -> ( Options , FxHashSet < ( String , Option < String > ) > ) {
2037
+ let color = parse_color ( matches) ;
2038
+
2039
+ let edition = match matches. opt_str ( "edition" ) {
2040
+ Some ( arg) => Edition :: from_str ( & arg) . unwrap_or_else ( |_|
2041
+ early_error (
2042
+ ErrorOutputType :: default ( ) ,
2043
+ & format ! (
2044
+ "argument for --edition must be one of: \
2045
+ {}. (instead was `{}`)",
2046
+ EDITION_NAME_LIST ,
2047
+ arg
2048
+ ) ,
2049
+ ) ,
2050
+ ) ,
2051
+ None => DEFAULT_EDITION ,
2052
+ } ;
2053
+
2054
+ if !edition. is_stable ( ) && !nightly_options:: is_nightly_build ( ) {
2055
+ early_error (
2056
+ ErrorOutputType :: default ( ) ,
2057
+ & format ! (
2058
+ "Edition {} is unstable and only \
2059
+ available for nightly builds of rustc.",
2060
+ edition,
2061
+ )
2062
+ )
2063
+ }
2064
+
2065
+ let ( json_rendered, json_artifact_notifications) = parse_json ( matches) ;
2066
+
2067
+ let error_format = parse_error_format ( matches, color, json_rendered) ;
2068
+
2007
2069
let unparsed_crate_types = matches. opt_strs ( "crate-type" ) ;
2008
2070
let crate_types = parse_crate_types_from_list ( unparsed_crate_types)
2009
2071
. unwrap_or_else ( |e| early_error ( error_format, & e[ ..] ) ) ;
@@ -2014,9 +2076,6 @@ pub fn build_session_options_and_crate_config(
2014
2076
let mut debugging_opts = build_debugging_options ( matches, error_format) ;
2015
2077
2016
2078
if !debugging_opts. unstable_options {
2017
- if matches. opt_str ( "json-rendered" ) . is_some ( ) {
2018
- early_error ( error_format, "`--json-rendered=x` is unstable" ) ;
2019
- }
2020
2079
if let ErrorOutputType :: Json { pretty : true , json_rendered } = error_format {
2021
2080
early_error (
2022
2081
ErrorOutputType :: Json { pretty : false , json_rendered } ,
@@ -2441,6 +2500,7 @@ pub fn build_session_options_and_crate_config(
2441
2500
cli_forced_thinlto_off : disable_thinlto,
2442
2501
remap_path_prefix,
2443
2502
edition,
2503
+ json_artifact_notifications,
2444
2504
} ,
2445
2505
cfg,
2446
2506
)
0 commit comments