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