From 9224b6fe205e20c6b88570977ad1c27d2c2ecfb3 Mon Sep 17 00:00:00 2001 From: Joshua Nelson Date: Sun, 20 Jun 2021 20:22:25 -0400 Subject: [PATCH 1/3] Remove unnecessary call to queries.crate_name() It's much more complicated than just going through the TyCtxt. --- src/librustdoc/doctest.rs | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/src/librustdoc/doctest.rs b/src/librustdoc/doctest.rs index 88e2f6048e9c7..90ec0eb273e7a 100644 --- a/src/librustdoc/doctest.rs +++ b/src/librustdoc/doctest.rs @@ -4,6 +4,7 @@ use rustc_data_structures::sync::Lrc; use rustc_errors::{ColorConfig, ErrorReported}; use rustc_hir as hir; use rustc_hir::intravisit; +use rustc_hir::def_id::LOCAL_CRATE; use rustc_hir::{HirId, CRATE_HIR_ID}; use rustc_interface::interface; use rustc_middle::hir::map::Map; @@ -111,8 +112,6 @@ crate fn run(options: Options) -> Result<(), ErrorReported> { let res = interface::run_compiler(config, |compiler| { compiler.enter(|queries| { let _lower_to_hir = queries.lower_to_hir()?; - - let crate_name = queries.crate_name()?.peek().to_string(); let mut global_ctxt = queries.global_ctxt()?.take(); let collector = global_ctxt.enter(|tcx| { @@ -123,7 +122,7 @@ crate fn run(options: Options) -> Result<(), ErrorReported> { opts.display_warnings |= options.display_warnings; let enable_per_target_ignores = options.enable_per_target_ignores; let mut collector = Collector::new( - crate_name, + tcx.crate_name(LOCAL_CRATE).to_string(), options, false, opts, From 1c557dada20ef79f7a46666b3152ac42fe58e6f9 Mon Sep 17 00:00:00 2001 From: Joshua Nelson Date: Sun, 20 Jun 2021 20:24:59 -0400 Subject: [PATCH 2/3] Rename cratename -> crate_name This makes it consistent with rustc_session::Options. --- src/librustdoc/doctest.rs | 34 +++++++++++++++++----------------- 1 file changed, 17 insertions(+), 17 deletions(-) diff --git a/src/librustdoc/doctest.rs b/src/librustdoc/doctest.rs index 90ec0eb273e7a..d791a995d863e 100644 --- a/src/librustdoc/doctest.rs +++ b/src/librustdoc/doctest.rs @@ -292,7 +292,7 @@ struct UnusedExterns { fn run_test( test: &str, - cratename: &str, + crate_name: &str, line: usize, options: Options, should_panic: bool, @@ -311,7 +311,7 @@ fn run_test( report_unused_externs: impl Fn(UnusedExterns), ) -> Result<(), TestFailure> { let (test, line_offset, supports_color) = - make_test(test, Some(cratename), as_test_harness, opts, edition, Some(test_id)); + make_test(test, Some(crate_name), as_test_harness, opts, edition, Some(test_id)); let output_file = outdir.path().join("rust_out"); @@ -478,7 +478,7 @@ fn run_test( /// lines before the test code begins as well as if the output stream supports colors or not. crate fn make_test( s: &str, - cratename: Option<&str>, + crate_name: Option<&str>, dont_insert_main: bool, opts: &TestOptions, edition: Edition, @@ -539,7 +539,7 @@ crate fn make_test( let sess = ParseSess::with_span_handler(handler, sm); let mut found_main = false; - let mut found_extern_crate = cratename.is_none(); + let mut found_extern_crate = crate_name.is_none(); let mut found_macro = false; let mut parser = match maybe_new_parser_from_source_str(&sess, filename, source) { @@ -566,13 +566,13 @@ crate fn make_test( if !found_extern_crate { if let ast::ItemKind::ExternCrate(original) = item.kind { - // This code will never be reached if `cratename` is none because + // This code will never be reached if `crate_name` is none because // `found_extern_crate` is initialized to `true` if it is none. - let cratename = cratename.unwrap(); + let crate_name = crate_name.unwrap(); match original { - Some(name) => found_extern_crate = name.as_str() == cratename, - None => found_extern_crate = item.ident.as_str() == cratename, + Some(name) => found_extern_crate = name.as_str() == crate_name, + None => found_extern_crate = item.ident.as_str() == crate_name, } } } @@ -630,14 +630,14 @@ crate fn make_test( // Don't inject `extern crate std` because it's already injected by the // compiler. - if !already_has_extern_crate && !opts.no_crate_inject && cratename != Some("std") { - if let Some(cratename) = cratename { + if !already_has_extern_crate && !opts.no_crate_inject && crate_name != Some("std") { + if let Some(crate_name) = crate_name { // Don't inject `extern crate` if the crate is never used. // NOTE: this is terribly inaccurate because it doesn't actually // parse the source, but only has false positives, not false // negatives. - if s.contains(cratename) { - prog.push_str(&format!("extern crate r#{};\n", cratename)); + if s.contains(crate_name) { + prog.push_str(&format!("extern crate r#{};\n", crate_name)); line_offset += 1; } } @@ -796,7 +796,7 @@ crate struct Collector { options: Options, use_headers: bool, enable_per_target_ignores: bool, - cratename: String, + crate_name: String, opts: TestOptions, position: Span, source_map: Option>, @@ -808,7 +808,7 @@ crate struct Collector { impl Collector { crate fn new( - cratename: String, + crate_name: String, options: Options, use_headers: bool, opts: TestOptions, @@ -822,7 +822,7 @@ impl Collector { options, use_headers, enable_per_target_ignores, - cratename, + crate_name, opts, position: DUMMY_SP, source_map, @@ -870,7 +870,7 @@ impl Tester for Collector { fn add_test(&mut self, test: String, config: LangString, line: usize) { let filename = self.get_filename(); let name = self.generate_name(line, &filename); - let cratename = self.cratename.to_string(); + let crate_name = self.crate_name.to_string(); let opts = self.opts.clone(); let edition = config.edition.unwrap_or(self.options.edition); let options = self.options.clone(); @@ -953,7 +953,7 @@ impl Tester for Collector { }; let res = run_test( &test, - &cratename, + &crate_name, line, options, config.should_panic, From ff0e0466abc364f90bd5930b9e48cce41aa08137 Mon Sep 17 00:00:00 2001 From: Joshua Nelson Date: Sun, 20 Jun 2021 20:34:38 -0400 Subject: [PATCH 3/3] Don't reallocate the crate name when running doctests --- src/librustdoc/doctest.rs | 9 +++++---- src/librustdoc/markdown.rs | 3 ++- 2 files changed, 7 insertions(+), 5 deletions(-) diff --git a/src/librustdoc/doctest.rs b/src/librustdoc/doctest.rs index d791a995d863e..1750f05b4dcff 100644 --- a/src/librustdoc/doctest.rs +++ b/src/librustdoc/doctest.rs @@ -3,8 +3,8 @@ use rustc_data_structures::fx::{FxHashMap, FxHashSet}; use rustc_data_structures::sync::Lrc; use rustc_errors::{ColorConfig, ErrorReported}; use rustc_hir as hir; -use rustc_hir::intravisit; use rustc_hir::def_id::LOCAL_CRATE; +use rustc_hir::intravisit; use rustc_hir::{HirId, CRATE_HIR_ID}; use rustc_interface::interface; use rustc_middle::hir::map::Map; @@ -14,6 +14,7 @@ use rustc_session::{lint, DiagnosticOutput, Session}; use rustc_span::edition::Edition; use rustc_span::source_map::SourceMap; use rustc_span::symbol::sym; +use rustc_span::Symbol; use rustc_span::{BytePos, FileName, Pos, Span, DUMMY_SP}; use rustc_target::spec::TargetTriple; use tempfile::Builder as TempFileBuilder; @@ -122,7 +123,7 @@ crate fn run(options: Options) -> Result<(), ErrorReported> { opts.display_warnings |= options.display_warnings; let enable_per_target_ignores = options.enable_per_target_ignores; let mut collector = Collector::new( - tcx.crate_name(LOCAL_CRATE).to_string(), + tcx.crate_name(LOCAL_CRATE), options, false, opts, @@ -796,7 +797,7 @@ crate struct Collector { options: Options, use_headers: bool, enable_per_target_ignores: bool, - crate_name: String, + crate_name: Symbol, opts: TestOptions, position: Span, source_map: Option>, @@ -808,7 +809,7 @@ crate struct Collector { impl Collector { crate fn new( - crate_name: String, + crate_name: Symbol, options: Options, use_headers: bool, opts: TestOptions, diff --git a/src/librustdoc/markdown.rs b/src/librustdoc/markdown.rs index 963f2cf71f389..5da3a75e87631 100644 --- a/src/librustdoc/markdown.rs +++ b/src/librustdoc/markdown.rs @@ -4,6 +4,7 @@ use std::path::Path; use rustc_span::edition::Edition; use rustc_span::source_map::DUMMY_SP; +use rustc_span::Symbol; use crate::config::{Options, RenderOptions}; use crate::doctest::{Collector, TestOptions}; @@ -121,7 +122,7 @@ crate fn test(mut options: Options) -> Result<(), String> { opts.no_crate_inject = true; opts.display_warnings = options.display_warnings; let mut collector = Collector::new( - options.input.display().to_string(), + Symbol::intern(&options.input.display().to_string()), options.clone(), true, opts,