From 457dbbfc55da44dbcee82e1f6eeab1dcf460dfc3 Mon Sep 17 00:00:00 2001 From: bjorn3 <17426603+bjorn3@users.noreply.github.com> Date: Sat, 4 Nov 2023 13:20:09 +0000 Subject: [PATCH 1/7] Inline and remove pre_configure --- compiler/rustc_interface/src/queries.rs | 19 ++++--------------- 1 file changed, 4 insertions(+), 15 deletions(-) diff --git a/compiler/rustc_interface/src/queries.rs b/compiler/rustc_interface/src/queries.rs index bee27dc2d69..3d65c03309d 100644 --- a/compiler/rustc_interface/src/queries.rs +++ b/compiler/rustc_interface/src/queries.rs @@ -85,7 +85,6 @@ pub struct Queries<'tcx> { hir_arena: WorkerLocal>, parse: Query, - pre_configure: Query<(ast::Crate, ast::AttrVec)>, // This just points to what's in `gcx_cell`. gcx: Query<&'tcx GlobalCtxt<'tcx>>, } @@ -98,7 +97,6 @@ impl<'tcx> Queries<'tcx> { arena: WorkerLocal::new(|_| Arena::default()), hir_arena: WorkerLocal::new(|_| rustc_hir::Arena::default()), parse: Default::default(), - pre_configure: Default::default(), gcx: Default::default(), } } @@ -113,12 +111,12 @@ impl<'tcx> Queries<'tcx> { }) } - #[deprecated = "pre_configure may be made private in the future. If you need it please open an issue with your use case."] - pub fn pre_configure(&self) -> Result> { - self.pre_configure.compute(|| { + pub fn global_ctxt(&'tcx self) -> Result>> { + self.gcx.compute(|| { + let sess = &self.compiler.sess; + let mut krate = self.parse()?.steal(); - let sess = &self.compiler.sess; rustc_builtin_macros::cmdline_attrs::inject( &mut krate, &sess.parse_sess, @@ -127,15 +125,6 @@ impl<'tcx> Queries<'tcx> { let pre_configured_attrs = rustc_expand::config::pre_configure_attrs(sess, &krate.attrs); - Ok((krate, pre_configured_attrs)) - }) - } - - pub fn global_ctxt(&'tcx self) -> Result>> { - self.gcx.compute(|| { - let sess = &self.compiler.sess; - #[allow(deprecated)] - let (krate, pre_configured_attrs) = self.pre_configure()?.steal(); // parse `#[crate_name]` even if `--crate-name` was passed, to make sure it matches. let crate_name = find_crate_name(sess, &pre_configured_attrs); From eacbe65dfea942f40bf475658a154292077fde07 Mon Sep 17 00:00:00 2001 From: bjorn3 <17426603+bjorn3@users.noreply.github.com> Date: Sat, 4 Nov 2023 15:38:54 +0000 Subject: [PATCH 2/7] Accept crate name instead of attributes in build_output_filenames --- compiler/rustc_attr/src/builtin.rs | 4 ---- compiler/rustc_driver_impl/src/lib.rs | 2 +- compiler/rustc_interface/src/interface.rs | 5 ++++- compiler/rustc_interface/src/passes.rs | 3 +-- compiler/rustc_interface/src/util.rs | 14 ++++---------- 5 files changed, 10 insertions(+), 18 deletions(-) diff --git a/compiler/rustc_attr/src/builtin.rs b/compiler/rustc_attr/src/builtin.rs index 7e87d1c3130..e713b403813 100644 --- a/compiler/rustc_attr/src/builtin.rs +++ b/compiler/rustc_attr/src/builtin.rs @@ -492,10 +492,6 @@ fn parse_unstability(sess: &Session, attr: &Attribute) -> Option<(Symbol, Stabil } } -pub fn find_crate_name(attrs: &[Attribute]) -> Option { - attr::first_attr_value_str_by_name(attrs, sym::crate_name) -} - #[derive(Clone, Debug)] pub struct Condition { pub name: Symbol, diff --git a/compiler/rustc_driver_impl/src/lib.rs b/compiler/rustc_driver_impl/src/lib.rs index e7cc3ae4d55..a2a11ef5ac2 100644 --- a/compiler/rustc_driver_impl/src/lib.rs +++ b/compiler/rustc_driver_impl/src/lib.rs @@ -765,8 +765,8 @@ fn print_crate_info( // no crate attributes, print out an error and exit return Compilation::Continue; }; - let t_outputs = rustc_interface::util::build_output_filenames(attrs, sess); let id = rustc_session::output::find_crate_name(sess, attrs); + let t_outputs = rustc_interface::util::build_output_filenames(sess, id.to_string()); let crate_types = collect_crate_types(sess, attrs); for &style in &crate_types { let fname = diff --git a/compiler/rustc_interface/src/interface.rs b/compiler/rustc_interface/src/interface.rs index 91fd4b4a1d0..a55cffbf6df 100644 --- a/compiler/rustc_interface/src/interface.rs +++ b/compiler/rustc_interface/src/interface.rs @@ -49,7 +49,10 @@ impl Compiler { sess: &Session, attrs: &[ast::Attribute], ) -> OutputFilenames { - util::build_output_filenames(attrs, sess) + util::build_output_filenames( + sess, + rustc_session::output::find_crate_name(sess, attrs).to_string(), + ) } } diff --git a/compiler/rustc_interface/src/passes.rs b/compiler/rustc_interface/src/passes.rs index 99bea647bd5..cd8c475c94d 100644 --- a/compiler/rustc_interface/src/passes.rs +++ b/compiler/rustc_interface/src/passes.rs @@ -556,10 +556,9 @@ fn resolver_for_lowering<'tcx>( fn output_filenames(tcx: TyCtxt<'_>, (): ()) -> Arc { let sess = tcx.sess; let _timer = sess.timer("prepare_outputs"); - let (_, krate) = &*tcx.resolver_for_lowering(()).borrow(); let crate_name = tcx.crate_name(LOCAL_CRATE); - let outputs = util::build_output_filenames(&krate.attrs, sess); + let outputs = util::build_output_filenames(sess, crate_name.to_string()); let output_paths = generated_output_paths(tcx, &outputs, sess.io.output_file.is_some(), crate_name); diff --git a/compiler/rustc_interface/src/util.rs b/compiler/rustc_interface/src/util.rs index b3ab01a740a..5266cdcec17 100644 --- a/compiler/rustc_interface/src/util.rs +++ b/compiler/rustc_interface/src/util.rs @@ -471,7 +471,7 @@ fn multiple_output_types_to_stdout( } } -pub fn build_output_filenames(attrs: &[ast::Attribute], sess: &Session) -> OutputFilenames { +pub fn build_output_filenames(sess: &Session, crate_name: String) -> OutputFilenames { if multiple_output_types_to_stdout( &sess.opts.output_types, sess.io.output_file == Some(OutFileName::Stdout), @@ -479,12 +479,6 @@ pub fn build_output_filenames(attrs: &[ast::Attribute], sess: &Session) -> Outpu sess.emit_fatal(errors::MultipleOutputTypesToStdout); } - let crate_name = sess - .opts - .crate_name - .clone() - .or_else(|| rustc_attr::find_crate_name(attrs).map(|n| n.to_string())); - match sess.io.output_file { None => { // "-" as input file will cause the parser to read from stdin so we @@ -493,11 +487,11 @@ pub fn build_output_filenames(attrs: &[ast::Attribute], sess: &Session) -> Outpu let dirpath = sess.io.output_dir.clone().unwrap_or_default(); // If a crate name is present, we use it as the link name - let stem = crate_name.clone().unwrap_or_else(|| sess.io.input.filestem().to_owned()); + let stem = crate_name.clone(); OutputFilenames::new( dirpath, - crate_name.unwrap_or_else(|| stem.replace('-', "_")), + crate_name, stem, None, sess.io.temps_dir.clone(), @@ -526,7 +520,7 @@ pub fn build_output_filenames(attrs: &[ast::Attribute], sess: &Session) -> Outpu out_file.filestem().unwrap_or_default().to_str().unwrap().to_string(); OutputFilenames::new( out_file.parent().unwrap_or_else(|| Path::new("")).to_path_buf(), - crate_name.unwrap_or_else(|| out_filestem.replace('-', "_")), + crate_name, out_filestem, ofile, sess.io.temps_dir.clone(), From 98a6eaa7f8436cbedc79a21fe3ec62e8a35ef392 Mon Sep 17 00:00:00 2001 From: bjorn3 <17426603+bjorn3@users.noreply.github.com> Date: Sat, 4 Nov 2023 15:49:57 +0000 Subject: [PATCH 3/7] Serialize OutputFilenames into rmeta file This ensures that linking will use the correct crate name even when `#![crate_name = "..."]` is used to specify the crate name. --- compiler/rustc_codegen_ssa/src/lib.rs | 10 ++++++++-- compiler/rustc_driver_impl/src/lib.rs | 5 ++--- compiler/rustc_interface/src/interface.rs | 19 ++----------------- compiler/rustc_interface/src/queries.rs | 9 +++++++-- compiler/rustc_session/src/config.rs | 6 +++--- 5 files changed, 22 insertions(+), 27 deletions(-) diff --git a/compiler/rustc_codegen_ssa/src/lib.rs b/compiler/rustc_codegen_ssa/src/lib.rs index a0d6e1885c2..bfd0a458884 100644 --- a/compiler/rustc_codegen_ssa/src/lib.rs +++ b/compiler/rustc_codegen_ssa/src/lib.rs @@ -216,6 +216,7 @@ impl CodegenResults { sess: &Session, rlink_file: &Path, codegen_results: &CodegenResults, + outputs: &OutputFilenames, ) -> Result { let mut encoder = FileEncoder::new(rlink_file)?; encoder.emit_raw_bytes(RLINK_MAGIC); @@ -224,10 +225,14 @@ impl CodegenResults { encoder.emit_raw_bytes(&RLINK_VERSION.to_be_bytes()); encoder.emit_str(sess.cfg_version); Encodable::encode(codegen_results, &mut encoder); + Encodable::encode(outputs, &mut encoder); encoder.finish().map_err(|(_path, err)| err) } - pub fn deserialize_rlink(sess: &Session, data: Vec) -> Result { + pub fn deserialize_rlink( + sess: &Session, + data: Vec, + ) -> Result<(Self, OutputFilenames), CodegenErrors> { // The Decodable machinery is not used here because it panics if the input data is invalid // and because its internal representation may change. if !data.starts_with(RLINK_MAGIC) { @@ -256,6 +261,7 @@ impl CodegenResults { } let codegen_results = CodegenResults::decode(&mut decoder); - Ok(codegen_results) + let outputs = OutputFilenames::decode(&mut decoder); + Ok((codegen_results, outputs)) } } diff --git a/compiler/rustc_driver_impl/src/lib.rs b/compiler/rustc_driver_impl/src/lib.rs index a2a11ef5ac2..a0794daa23c 100644 --- a/compiler/rustc_driver_impl/src/lib.rs +++ b/compiler/rustc_driver_impl/src/lib.rs @@ -648,12 +648,11 @@ fn show_md_content_with_pager(content: &str, color: ColorConfig) { fn process_rlink(sess: &Session, compiler: &interface::Compiler) { assert!(sess.opts.unstable_opts.link_only); if let Input::File(file) = &sess.io.input { - let outputs = compiler.build_output_filenames(sess, &[]); let rlink_data = fs::read(file).unwrap_or_else(|err| { sess.emit_fatal(RlinkUnableToRead { err }); }); - let codegen_results = match CodegenResults::deserialize_rlink(sess, rlink_data) { - Ok(codegen) => codegen, + let (codegen_results, outputs) = match CodegenResults::deserialize_rlink(sess, rlink_data) { + Ok((codegen, outputs)) => (codegen, outputs), Err(err) => { match err { CodegenErrors::WrongFileType => sess.emit_fatal(RLinkWrongFileType), diff --git a/compiler/rustc_interface/src/interface.rs b/compiler/rustc_interface/src/interface.rs index a55cffbf6df..8a6d8d3d42e 100644 --- a/compiler/rustc_interface/src/interface.rs +++ b/compiler/rustc_interface/src/interface.rs @@ -1,7 +1,7 @@ use crate::util; use rustc_ast::token; -use rustc_ast::{self as ast, LitKind, MetaItemKind}; +use rustc_ast::{LitKind, MetaItemKind}; use rustc_codegen_ssa::traits::CodegenBackend; use rustc_data_structures::defer; use rustc_data_structures::fx::{FxHashMap, FxHashSet}; @@ -15,9 +15,7 @@ use rustc_middle::{bug, ty}; use rustc_parse::maybe_new_parser_from_source_str; use rustc_query_impl::QueryCtxt; use rustc_query_system::query::print_query_stack; -use rustc_session::config::{ - self, Cfg, CheckCfg, ExpectedValues, Input, OutFileName, OutputFilenames, -}; +use rustc_session::config::{self, Cfg, CheckCfg, ExpectedValues, Input, OutFileName}; use rustc_session::filesearch::sysroot_candidates; use rustc_session::parse::ParseSess; use rustc_session::{lint, CompilerIO, EarlyErrorHandler, Session}; @@ -43,19 +41,6 @@ pub struct Compiler { pub(crate) override_queries: Option, } -impl Compiler { - pub fn build_output_filenames( - &self, - sess: &Session, - attrs: &[ast::Attribute], - ) -> OutputFilenames { - util::build_output_filenames( - sess, - rustc_session::output::find_crate_name(sess, attrs).to_string(), - ) - } -} - /// Converts strings provided as `--cfg [cfgspec]` into a `Cfg`. pub(crate) fn parse_cfg(handler: &EarlyErrorHandler, cfgs: Vec) -> Cfg { cfgs.into_iter() diff --git a/compiler/rustc_interface/src/queries.rs b/compiler/rustc_interface/src/queries.rs index 3d65c03309d..95ad6f22b43 100644 --- a/compiler/rustc_interface/src/queries.rs +++ b/compiler/rustc_interface/src/queries.rs @@ -273,8 +273,13 @@ impl Linker { if sess.opts.unstable_opts.no_link { let rlink_file = self.output_filenames.with_extension(config::RLINK_EXT); - CodegenResults::serialize_rlink(sess, &rlink_file, &codegen_results) - .map_err(|error| sess.emit_fatal(FailedWritingFile { path: &rlink_file, error }))?; + CodegenResults::serialize_rlink( + sess, + &rlink_file, + &codegen_results, + &*self.output_filenames, + ) + .map_err(|error| sess.emit_fatal(FailedWritingFile { path: &rlink_file, error }))?; return Ok(()); } diff --git a/compiler/rustc_session/src/config.rs b/compiler/rustc_session/src/config.rs index 2d2a3c3d665..d6e7afb7d09 100644 --- a/compiler/rustc_session/src/config.rs +++ b/compiler/rustc_session/src/config.rs @@ -580,7 +580,7 @@ pub enum ResolveDocLinks { /// *Do not* switch `BTreeMap` out for an unsorted container type! That would break /// dependency tracking for command-line arguments. Also only hash keys, since tracking /// should only depend on the output types, not the paths they're written to. -#[derive(Clone, Debug, Hash, HashStable_Generic)] +#[derive(Clone, Debug, Hash, HashStable_Generic, Encodable, Decodable)] pub struct OutputTypes(BTreeMap>); impl OutputTypes { @@ -818,7 +818,7 @@ impl Input { } } -#[derive(Clone, Hash, Debug, HashStable_Generic, PartialEq)] +#[derive(Clone, Hash, Debug, HashStable_Generic, PartialEq, Encodable, Decodable)] pub enum OutFileName { Real(PathBuf), Stdout, @@ -890,7 +890,7 @@ impl OutFileName { } } -#[derive(Clone, Hash, Debug, HashStable_Generic)] +#[derive(Clone, Hash, Debug, HashStable_Generic, Encodable, Decodable)] pub struct OutputFilenames { pub out_directory: PathBuf, /// Crate name. Never contains '-'. From 4acaa0284ef1354f3310d7de0147ea4a371b4389 Mon Sep 17 00:00:00 2001 From: bjorn3 <17426603+bjorn3@users.noreply.github.com> Date: Sat, 4 Nov 2023 15:59:29 +0000 Subject: [PATCH 4/7] Feed the output filenames into the TyCtxt Since the introduction of the crate attribute pre-expansion pass we don't need access to the TyCtxt to compute it. --- compiler/rustc_driver_impl/src/lib.rs | 8 ++++---- compiler/rustc_interface/src/passes.rs | 12 +++++------- compiler/rustc_interface/src/queries.rs | 2 ++ compiler/rustc_middle/src/query/mod.rs | 5 +++++ 4 files changed, 16 insertions(+), 11 deletions(-) diff --git a/compiler/rustc_driver_impl/src/lib.rs b/compiler/rustc_driver_impl/src/lib.rs index a0794daa23c..7dba0b24637 100644 --- a/compiler/rustc_driver_impl/src/lib.rs +++ b/compiler/rustc_driver_impl/src/lib.rs @@ -401,9 +401,9 @@ fn run_compiler( Ok(()) })?; - // Make sure the `output_filenames` query is run for its side + // Make sure the `write_dep_info` query is run for its side // effects of writing the dep-info and reporting errors. - queries.global_ctxt()?.enter(|tcx| tcx.output_filenames(())); + queries.global_ctxt()?.enter(|tcx| tcx.write_dep_info(())); } else { let krate = queries.parse()?; pretty::print( @@ -431,9 +431,9 @@ fn run_compiler( return early_exit(); } - // Make sure the `output_filenames` query is run for its side + // Make sure the `write_dep_info` query is run for its side // effects of writing the dep-info and reporting errors. - queries.global_ctxt()?.enter(|tcx| tcx.output_filenames(())); + queries.global_ctxt()?.enter(|tcx| tcx.write_dep_info(())); if sess.opts.output_types.contains_key(&OutputType::DepInfo) && sess.opts.output_types.len() == 1 diff --git a/compiler/rustc_interface/src/passes.rs b/compiler/rustc_interface/src/passes.rs index cd8c475c94d..d1270427967 100644 --- a/compiler/rustc_interface/src/passes.rs +++ b/compiler/rustc_interface/src/passes.rs @@ -39,7 +39,7 @@ use std::any::Any; use std::ffi::OsString; use std::io::{self, BufWriter, Write}; use std::path::{Path, PathBuf}; -use std::sync::{Arc, LazyLock}; +use std::sync::LazyLock; use std::{env, fs, iter}; pub fn parse<'a>(sess: &'a Session) -> PResult<'a, ast::Crate> { @@ -553,12 +553,12 @@ fn resolver_for_lowering<'tcx>( tcx.arena.alloc(Steal::new((untracked_resolver_for_lowering, Lrc::new(krate)))) } -fn output_filenames(tcx: TyCtxt<'_>, (): ()) -> Arc { +fn write_dep_info(tcx: TyCtxt<'_>, (): ()) { let sess = tcx.sess; - let _timer = sess.timer("prepare_outputs"); + let _timer = sess.timer("write_dep_info"); let crate_name = tcx.crate_name(LOCAL_CRATE); - let outputs = util::build_output_filenames(sess, crate_name.to_string()); + let outputs = tcx.output_filenames(()); let output_paths = generated_output_paths(tcx, &outputs, sess.io.output_file.is_some(), crate_name); @@ -595,15 +595,13 @@ fn output_filenames(tcx: TyCtxt<'_>, (): ()) -> Arc { } } } - - outputs.into() } pub static DEFAULT_QUERY_PROVIDERS: LazyLock = LazyLock::new(|| { let providers = &mut Providers::default(); providers.analysis = analysis; providers.hir_crate = rustc_ast_lowering::lower_to_hir; - providers.output_filenames = output_filenames; + providers.write_dep_info = write_dep_info; providers.resolver_for_lowering = resolver_for_lowering; providers.early_lint_checks = early_lint_checks; proc_macro_decls::provide(providers); diff --git a/compiler/rustc_interface/src/queries.rs b/compiler/rustc_interface/src/queries.rs index 95ad6f22b43..78edd65df23 100644 --- a/compiler/rustc_interface/src/queries.rs +++ b/compiler/rustc_interface/src/queries.rs @@ -135,6 +135,7 @@ impl<'tcx> Queries<'tcx> { sess.opts.cg.metadata.clone(), sess.cfg_version, ); + let outputs = util::build_output_filenames(sess, crate_name.to_string()); let dep_graph = setup_dep_graph(sess, crate_name, stable_crate_id)?; let cstore = FreezeLock::new(Box::new(CStore::new( @@ -169,6 +170,7 @@ impl<'tcx> Queries<'tcx> { crate_name, ))); feed.crate_for_resolver(tcx.arena.alloc(Steal::new((krate, pre_configured_attrs)))); + feed.output_filenames(Arc::new(outputs)); }); Ok(qcx) }) diff --git a/compiler/rustc_middle/src/query/mod.rs b/compiler/rustc_middle/src/query/mod.rs index 29decd0f050..5873aa7a496 100644 --- a/compiler/rustc_middle/src/query/mod.rs +++ b/compiler/rustc_middle/src/query/mod.rs @@ -1916,6 +1916,11 @@ rustc_queries! { arena_cache } + /// Write the dep-info file. + query write_dep_info(_: ()) -> () { + desc { "writing the dep-info file" } + } + /// Do not call this query directly: invoke `normalize` instead. query normalize_projection_ty( goal: CanonicalProjectionGoal<'tcx> From 7ede8e2a5912778ce01b6a520c954c4e105126e6 Mon Sep 17 00:00:00 2001 From: bjorn3 <17426603+bjorn3@users.noreply.github.com> Date: Sat, 4 Nov 2023 16:06:23 +0000 Subject: [PATCH 5/7] Ensure macro expansion runs before writing the dep info --- compiler/rustc_interface/src/passes.rs | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/compiler/rustc_interface/src/passes.rs b/compiler/rustc_interface/src/passes.rs index d1270427967..6af97910b49 100644 --- a/compiler/rustc_interface/src/passes.rs +++ b/compiler/rustc_interface/src/passes.rs @@ -554,6 +554,11 @@ fn resolver_for_lowering<'tcx>( } fn write_dep_info(tcx: TyCtxt<'_>, (): ()) { + // Make sure name resolution and macro expansion is run for + // the side-effect of providing a complete set of all + // accessed files and env vars. + let _ = tcx.resolver_for_lowering(()); + let sess = tcx.sess; let _timer = sess.timer("write_dep_info"); let crate_name = tcx.crate_name(LOCAL_CRATE); From 365a580bc44ea63d28ce10778d73f91e0b0f4c15 Mon Sep 17 00:00:00 2001 From: bjorn3 <17426603+bjorn3@users.noreply.github.com> Date: Sat, 4 Nov 2023 16:26:20 +0000 Subject: [PATCH 6/7] Mostly revert "Accept crate name instead of attributes in build_output_filenames" --- compiler/rustc_attr/src/builtin.rs | 4 ++++ compiler/rustc_driver_impl/src/lib.rs | 2 +- compiler/rustc_interface/src/queries.rs | 2 +- compiler/rustc_interface/src/util.rs | 14 ++++++++++---- 4 files changed, 16 insertions(+), 6 deletions(-) diff --git a/compiler/rustc_attr/src/builtin.rs b/compiler/rustc_attr/src/builtin.rs index e713b403813..7e87d1c3130 100644 --- a/compiler/rustc_attr/src/builtin.rs +++ b/compiler/rustc_attr/src/builtin.rs @@ -492,6 +492,10 @@ fn parse_unstability(sess: &Session, attr: &Attribute) -> Option<(Symbol, Stabil } } +pub fn find_crate_name(attrs: &[Attribute]) -> Option { + attr::first_attr_value_str_by_name(attrs, sym::crate_name) +} + #[derive(Clone, Debug)] pub struct Condition { pub name: Symbol, diff --git a/compiler/rustc_driver_impl/src/lib.rs b/compiler/rustc_driver_impl/src/lib.rs index 7dba0b24637..6c8b0a6e769 100644 --- a/compiler/rustc_driver_impl/src/lib.rs +++ b/compiler/rustc_driver_impl/src/lib.rs @@ -764,8 +764,8 @@ fn print_crate_info( // no crate attributes, print out an error and exit return Compilation::Continue; }; + let t_outputs = rustc_interface::util::build_output_filenames(attrs, sess); let id = rustc_session::output::find_crate_name(sess, attrs); - let t_outputs = rustc_interface::util::build_output_filenames(sess, id.to_string()); let crate_types = collect_crate_types(sess, attrs); for &style in &crate_types { let fname = diff --git a/compiler/rustc_interface/src/queries.rs b/compiler/rustc_interface/src/queries.rs index 78edd65df23..e352547a6c6 100644 --- a/compiler/rustc_interface/src/queries.rs +++ b/compiler/rustc_interface/src/queries.rs @@ -135,7 +135,7 @@ impl<'tcx> Queries<'tcx> { sess.opts.cg.metadata.clone(), sess.cfg_version, ); - let outputs = util::build_output_filenames(sess, crate_name.to_string()); + let outputs = util::build_output_filenames(&pre_configured_attrs, sess); let dep_graph = setup_dep_graph(sess, crate_name, stable_crate_id)?; let cstore = FreezeLock::new(Box::new(CStore::new( diff --git a/compiler/rustc_interface/src/util.rs b/compiler/rustc_interface/src/util.rs index 5266cdcec17..b3ab01a740a 100644 --- a/compiler/rustc_interface/src/util.rs +++ b/compiler/rustc_interface/src/util.rs @@ -471,7 +471,7 @@ fn multiple_output_types_to_stdout( } } -pub fn build_output_filenames(sess: &Session, crate_name: String) -> OutputFilenames { +pub fn build_output_filenames(attrs: &[ast::Attribute], sess: &Session) -> OutputFilenames { if multiple_output_types_to_stdout( &sess.opts.output_types, sess.io.output_file == Some(OutFileName::Stdout), @@ -479,6 +479,12 @@ pub fn build_output_filenames(sess: &Session, crate_name: String) -> OutputFilen sess.emit_fatal(errors::MultipleOutputTypesToStdout); } + let crate_name = sess + .opts + .crate_name + .clone() + .or_else(|| rustc_attr::find_crate_name(attrs).map(|n| n.to_string())); + match sess.io.output_file { None => { // "-" as input file will cause the parser to read from stdin so we @@ -487,11 +493,11 @@ pub fn build_output_filenames(sess: &Session, crate_name: String) -> OutputFilen let dirpath = sess.io.output_dir.clone().unwrap_or_default(); // If a crate name is present, we use it as the link name - let stem = crate_name.clone(); + let stem = crate_name.clone().unwrap_or_else(|| sess.io.input.filestem().to_owned()); OutputFilenames::new( dirpath, - crate_name, + crate_name.unwrap_or_else(|| stem.replace('-', "_")), stem, None, sess.io.temps_dir.clone(), @@ -520,7 +526,7 @@ pub fn build_output_filenames(sess: &Session, crate_name: String) -> OutputFilen out_file.filestem().unwrap_or_default().to_str().unwrap().to_string(); OutputFilenames::new( out_file.parent().unwrap_or_else(|| Path::new("")).to_path_buf(), - crate_name, + crate_name.unwrap_or_else(|| out_filestem.replace('-', "_")), out_filestem, ofile, sess.io.temps_dir.clone(), From d7e9a30941e6591a4671c3df59c815dc984fd786 Mon Sep 17 00:00:00 2001 From: bjorn3 <17426603+bjorn3@users.noreply.github.com> Date: Sat, 4 Nov 2023 17:35:28 +0000 Subject: [PATCH 7/7] Turn write_dep_info into a regular function It has side-effects and as such can't be cached. --- compiler/rustc_driver_impl/src/lib.rs | 8 ++------ compiler/rustc_interface/src/passes.rs | 3 +-- compiler/rustc_interface/src/queries.rs | 7 +++++++ compiler/rustc_middle/src/query/mod.rs | 5 ----- 4 files changed, 10 insertions(+), 13 deletions(-) diff --git a/compiler/rustc_driver_impl/src/lib.rs b/compiler/rustc_driver_impl/src/lib.rs index 6c8b0a6e769..6af11ce8479 100644 --- a/compiler/rustc_driver_impl/src/lib.rs +++ b/compiler/rustc_driver_impl/src/lib.rs @@ -401,9 +401,7 @@ fn run_compiler( Ok(()) })?; - // Make sure the `write_dep_info` query is run for its side - // effects of writing the dep-info and reporting errors. - queries.global_ctxt()?.enter(|tcx| tcx.write_dep_info(())); + queries.write_dep_info()?; } else { let krate = queries.parse()?; pretty::print( @@ -431,9 +429,7 @@ fn run_compiler( return early_exit(); } - // Make sure the `write_dep_info` query is run for its side - // effects of writing the dep-info and reporting errors. - queries.global_ctxt()?.enter(|tcx| tcx.write_dep_info(())); + queries.write_dep_info()?; if sess.opts.output_types.contains_key(&OutputType::DepInfo) && sess.opts.output_types.len() == 1 diff --git a/compiler/rustc_interface/src/passes.rs b/compiler/rustc_interface/src/passes.rs index 6af97910b49..23034113f30 100644 --- a/compiler/rustc_interface/src/passes.rs +++ b/compiler/rustc_interface/src/passes.rs @@ -553,7 +553,7 @@ fn resolver_for_lowering<'tcx>( tcx.arena.alloc(Steal::new((untracked_resolver_for_lowering, Lrc::new(krate)))) } -fn write_dep_info(tcx: TyCtxt<'_>, (): ()) { +pub(crate) fn write_dep_info(tcx: TyCtxt<'_>) { // Make sure name resolution and macro expansion is run for // the side-effect of providing a complete set of all // accessed files and env vars. @@ -606,7 +606,6 @@ pub static DEFAULT_QUERY_PROVIDERS: LazyLock = LazyLock::new(|| { let providers = &mut Providers::default(); providers.analysis = analysis; providers.hir_crate = rustc_ast_lowering::lower_to_hir; - providers.write_dep_info = write_dep_info; providers.resolver_for_lowering = resolver_for_lowering; providers.early_lint_checks = early_lint_checks; proc_macro_decls::provide(providers); diff --git a/compiler/rustc_interface/src/queries.rs b/compiler/rustc_interface/src/queries.rs index e352547a6c6..b7cd5468a00 100644 --- a/compiler/rustc_interface/src/queries.rs +++ b/compiler/rustc_interface/src/queries.rs @@ -176,6 +176,13 @@ impl<'tcx> Queries<'tcx> { }) } + pub fn write_dep_info(&'tcx self) -> Result<()> { + self.global_ctxt()?.enter(|tcx| { + passes::write_dep_info(tcx); + }); + Ok(()) + } + /// Check for the `#[rustc_error]` annotation, which forces an error in codegen. This is used /// to write UI tests that actually test that compilation succeeds without reporting /// an error. diff --git a/compiler/rustc_middle/src/query/mod.rs b/compiler/rustc_middle/src/query/mod.rs index 5873aa7a496..29decd0f050 100644 --- a/compiler/rustc_middle/src/query/mod.rs +++ b/compiler/rustc_middle/src/query/mod.rs @@ -1916,11 +1916,6 @@ rustc_queries! { arena_cache } - /// Write the dep-info file. - query write_dep_info(_: ()) -> () { - desc { "writing the dep-info file" } - } - /// Do not call this query directly: invoke `normalize` instead. query normalize_projection_ty( goal: CanonicalProjectionGoal<'tcx>