@@ -50,6 +50,7 @@ use rustc_interface::{Linker, Queries, interface, passes};
5050use rustc_lint:: unerased_lint_store;
5151use rustc_metadata:: creader:: MetadataLoader ;
5252use rustc_metadata:: locator;
53+ use rustc_middle:: ty:: TyCtxt ;
5354use rustc_parse:: { new_parser_from_file, new_parser_from_source_str, unwrap_or_emit_fatal} ;
5455use rustc_session:: config:: {
5556 CG_OPTIONS , ErrorOutputType , Input , OutFileName , OutputType , UnstableOptions , Z_OPTIONS ,
@@ -102,7 +103,7 @@ mod signal_handler {
102103
103104use crate :: session_diagnostics:: {
104105 RLinkEmptyVersionNumber , RLinkEncodingVersionMismatch , RLinkRustcVersionMismatch ,
105- RLinkWrongFileType , RlinkCorruptFile , RlinkNotAFile , RlinkUnableToRead ,
106+ RLinkWrongFileType , RlinkCorruptFile , RlinkNotAFile , RlinkUnableToRead , UnstableFeatureUsage ,
106107} ;
107108
108109rustc_fluent_macro:: fluent_messages! { "../messages.ftl" }
@@ -430,6 +431,10 @@ fn run_compiler(
430431 // Make sure name resolution and macro expansion is run.
431432 queries. global_ctxt( ) ?. enter( |tcx| tcx. resolver_for_lowering( ) ) ;
432433
434+ if let Some ( metrics_dir) = & sess. opts. unstable_opts. metrics_dir {
435+ queries. global_ctxt( ) ?. enter( |tcxt| dump_feature_usage_metrics( tcxt, metrics_dir) ) ;
436+ }
437+
433438 if callbacks. after_expansion( compiler, queries) == Compilation :: Stop {
434439 return early_exit( ) ;
435440 }
@@ -474,6 +479,23 @@ fn run_compiler(
474479 } )
475480}
476481
482+ fn dump_feature_usage_metrics( tcxt: TyCtxt <' _>, metrics_dir: & PathBuf ) {
483+ let output_filenames = tcxt. output_filenames( ( ) ) ;
484+ let mut metrics_file_name = std:: ffi:: OsString :: from( "unstable_feature_usage_metrics-" ) ;
485+ let mut metrics_path = output_filenames. with_directory_and_extension( metrics_dir, "json" ) ;
486+ let metrics_file_stem =
487+ metrics_path. file_name( ) . expect( "there should be a valid default output filename" ) ;
488+ metrics_file_name. push( metrics_file_stem) ;
489+ metrics_path. pop( ) ;
490+ metrics_path. push( metrics_file_name) ;
491+ if let Err ( error) = tcxt. features( ) . dump_feature_usage_metrics( metrics_path) {
492+ // FIXME(yaahc): once metrics can be enabled by default we will want "failure to emit
493+ // default metrics" to only produce a warning when metrics are enabled by default and emit
494+ // an error only when the user manually enables metrics
495+ tcxt. dcx( ) . emit_err( UnstableFeatureUsage { error } ) ;
496+ }
497+ }
498+
477499// Extract output directory and file from matches.
478500fn make_output( matches: & getopts:: Matches ) -> ( Option <PathBuf >, Option <OutFileName >) {
479501 let odir = matches. opt_str( "out-dir" ) . map( |o| PathBuf :: from( & o) ) ;
0 commit comments