diff options
| author | Matthias KrΓΌger <matthias.krueger@famsik.de> | 2024-11-21 11:58:36 +0100 | 
|---|---|---|
| committer | GitHub <noreply@github.com> | 2024-11-21 11:58:36 +0100 | 
| commit | fe5403f517617e1ecdf1cd3cd33bf205727f92c9 (patch) | |
| tree | b7c9380d41a326807565657bda20807f167f0bf0 /compiler/rustc_driver_impl/src/lib.rs | |
| parent | 0b1bf71a71c2a1d34c212285362530ec2c4e4775 (diff) | |
| parent | 0a14f712d7a2cf42e2e5705b2f5a8f1b8e67c7c2 (diff) | |
| download | rust-fe5403f517617e1ecdf1cd3cd33bf205727f92c9.tar.gz rust-fe5403f517617e1ecdf1cd3cd33bf205727f92c9.zip | |
Rollup merge of #130236 - yaahc:unstable-feature-usage, r=estebank
unstable feature usage metrics
example output
```
test-lib on ξ  master [?] is π¦ v0.1.0 via π¦ v1.80.1
β― cat src/lib.rs
ββββββββ¬βββββββββββββββββββββββββββββββββββββββββββββββββββββββ
       β File: src/lib.rs
ββββββββΌβββββββββββββββββββββββββββββββββββββββββββββββββββββββ
   1   β #![feature(unix_set_mark)]
   2   β pub fn add(left: u64, right: u64) -> u64 {
   3   β     left + right
   4   β }
   5   β
   6   β #[cfg(test)]
   7   β mod tests {
   8   β     use super::*;
   9   β
  10   β     #[test]
  11   β     fn it_works() {
  12   β         let result = add(2, 2);
  13   β         assert_eq!(result, 4);
  14   β     }
  15   β }
ββββββββ΄βββββββββββββββββββββββββββββββββββββββββββββββββββββββ
test-lib on ξ  master [?] is π¦ v0.1.0 via π¦ v1.80.1
β― cargo +stage1 rustc -- -Zmetrics-dir=$PWD/metrics
   Compiling test-lib v0.1.0 (/home/yaahc/tmp/test-lib)
    Finished `dev` profile [unoptimized + debuginfo] target(s) in 0.08s
test-lib on ξ  master [?] is π¦ v0.1.0 via π¦ v1.80.1
β― cat metrics/unstable_feature_usage.json
ββββββββ¬βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
       β File: metrics/unstable_feature_usage.json
ββββββββΌβββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
   1   β {"lib_features":[{"symbol":"unix_set_mark"}],"lang_features":[]}
   ```
   related to https://github.com/rust-lang/rust/issues/129485
Diffstat (limited to 'compiler/rustc_driver_impl/src/lib.rs')
| -rw-r--r-- | compiler/rustc_driver_impl/src/lib.rs | 24 | 
1 files changed, 23 insertions, 1 deletions
| diff --git a/compiler/rustc_driver_impl/src/lib.rs b/compiler/rustc_driver_impl/src/lib.rs index d2333454f28..c270ce16726 100644 --- a/compiler/rustc_driver_impl/src/lib.rs +++ b/compiler/rustc_driver_impl/src/lib.rs @@ -51,6 +51,7 @@ use rustc_interface::{Linker, Queries, interface, passes}; use rustc_lint::unerased_lint_store; use rustc_metadata::creader::MetadataLoader; use rustc_metadata::locator; +use rustc_middle::ty::TyCtxt; use rustc_parse::{new_parser_from_file, new_parser_from_source_str, unwrap_or_emit_fatal}; use rustc_session::config::{ CG_OPTIONS, ErrorOutputType, Input, OutFileName, OutputType, UnstableOptions, Z_OPTIONS, @@ -103,7 +104,7 @@ mod signal_handler { use crate::session_diagnostics::{ RLinkEmptyVersionNumber, RLinkEncodingVersionMismatch, RLinkRustcVersionMismatch, - RLinkWrongFileType, RlinkCorruptFile, RlinkNotAFile, RlinkUnableToRead, + RLinkWrongFileType, RlinkCorruptFile, RlinkNotAFile, RlinkUnableToRead, UnstableFeatureUsage, }; rustc_fluent_macro::fluent_messages! { "../messages.ftl" } @@ -431,6 +432,10 @@ fn run_compiler( // Make sure name resolution and macro expansion is run. queries.global_ctxt()?.enter(|tcx| tcx.resolver_for_lowering()); + if let Some(metrics_dir) = &sess.opts.unstable_opts.metrics_dir { + queries.global_ctxt()?.enter(|tcxt| dump_feature_usage_metrics(tcxt, metrics_dir)); + } + if callbacks.after_expansion(compiler, queries) == Compilation::Stop { return early_exit(); } @@ -475,6 +480,23 @@ fn run_compiler( }) } +fn dump_feature_usage_metrics(tcxt: TyCtxt<'_>, metrics_dir: &PathBuf) { + let output_filenames = tcxt.output_filenames(()); + let mut metrics_file_name = std::ffi::OsString::from("unstable_feature_usage_metrics-"); + let mut metrics_path = output_filenames.with_directory_and_extension(metrics_dir, "json"); + let metrics_file_stem = + metrics_path.file_name().expect("there should be a valid default output filename"); + metrics_file_name.push(metrics_file_stem); + metrics_path.pop(); + metrics_path.push(metrics_file_name); + if let Err(error) = tcxt.features().dump_feature_usage_metrics(metrics_path) { + // FIXME(yaahc): once metrics can be enabled by default we will want "failure to emit + // default metrics" to only produce a warning when metrics are enabled by default and emit + // an error only when the user manually enables metrics + tcxt.dcx().emit_err(UnstableFeatureUsage { error }); + } +} + // Extract output directory and file from matches. fn make_output(matches: &getopts::Matches) -> (Option<PathBuf>, Option<OutFileName>) { let odir = matches.opt_str("out-dir").map(|o| PathBuf::from(&o)); | 
