Skip to content

Commit 8ed1d4a

Browse files
committed
Auto merge of rust-lang#114750 - Enselic:metadata-dep-info, r=compiler-errors
Make `.rmeta` file in `dep-info` have correct name (`lib` prefix) Since `filename_for_metadata()` and `OutputFilenames::path(OutputType::Metadata)` had different logic for the name of the metadata file, the `.d` file contained a file name different from the actual name used. Share the logic to fix the out-of-sync name. Without this fix, the `.d` file contained dash-separated_something-extra.rmeta: dash-separated.rs instead of libdash_separated_something-extra.rmeta: dash-separated.rs which is the name of the file that is actually written by the compiler. Worth noting: It took me several iterations to get all tests to pass, so I am relatively confident that this PR does not break anything. Closes rust-lang#68839
2 parents 327e6cf + 04d81ba commit 8ed1d4a

File tree

7 files changed

+49
-28
lines changed

7 files changed

+49
-28
lines changed

compiler/rustc_interface/src/util.rs

+13-7
Original file line numberDiff line numberDiff line change
@@ -568,6 +568,13 @@ pub fn build_output_filenames(attrs: &[ast::Attribute], sess: &Session) -> Outpu
568568
) {
569569
sess.emit_fatal(errors::MultipleOutputTypesToStdout);
570570
}
571+
572+
let crate_name = sess
573+
.opts
574+
.crate_name
575+
.clone()
576+
.or_else(|| rustc_attr::find_crate_name(attrs).map(|n| n.to_string()));
577+
571578
match sess.io.output_file {
572579
None => {
573580
// "-" as input file will cause the parser to read from stdin so we
@@ -576,15 +583,11 @@ pub fn build_output_filenames(attrs: &[ast::Attribute], sess: &Session) -> Outpu
576583
let dirpath = sess.io.output_dir.clone().unwrap_or_default();
577584

578585
// If a crate name is present, we use it as the link name
579-
let stem = sess
580-
.opts
581-
.crate_name
582-
.clone()
583-
.or_else(|| rustc_attr::find_crate_name(attrs).map(|n| n.to_string()))
584-
.unwrap_or_else(|| sess.io.input.filestem().to_owned());
586+
let stem = crate_name.clone().unwrap_or_else(|| sess.io.input.filestem().to_owned());
585587

586588
OutputFilenames::new(
587589
dirpath,
590+
crate_name.unwrap_or_else(|| stem.replace('-', "_")),
588591
stem,
589592
None,
590593
sess.io.temps_dir.clone(),
@@ -609,9 +612,12 @@ pub fn build_output_filenames(attrs: &[ast::Attribute], sess: &Session) -> Outpu
609612
sess.emit_warning(errors::IgnoringOutDir);
610613
}
611614

615+
let out_filestem =
616+
out_file.filestem().unwrap_or_default().to_str().unwrap().to_string();
612617
OutputFilenames::new(
613618
out_file.parent().unwrap_or_else(|| Path::new("")).to_path_buf(),
614-
out_file.filestem().unwrap_or_default().to_str().unwrap().to_string(),
619+
crate_name.unwrap_or_else(|| out_filestem.replace('-', "_")),
620+
out_filestem,
615621
ofile,
616622
sess.io.temps_dir.clone(),
617623
sess.opts.cg.extra_filename.clone(),

compiler/rustc_metadata/src/fs.rs

+1-3
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@ use crate::errors::{
55
use crate::{encode_metadata, EncodedMetadata};
66

77
use rustc_data_structures::temp_dir::MaybeTempDir;
8-
use rustc_hir::def_id::LOCAL_CRATE;
98
use rustc_middle::ty::TyCtxt;
109
use rustc_session::config::{OutFileName, OutputType};
1110
use rustc_session::output::filename_for_metadata;
@@ -40,8 +39,7 @@ pub fn emit_wrapper_file(
4039
}
4140

4241
pub fn encode_and_write_metadata(tcx: TyCtxt<'_>) -> (EncodedMetadata, bool) {
43-
let crate_name = tcx.crate_name(LOCAL_CRATE);
44-
let out_filename = filename_for_metadata(tcx.sess, crate_name, tcx.output_filenames(()));
42+
let out_filename = filename_for_metadata(tcx.sess, tcx.output_filenames(()));
4543
// To avoid races with another rustc process scanning the output directory,
4644
// we need to write the file somewhere else and atomically move it to its
4745
// final destination, with an `fs::rename` call. In order for the rename to

compiler/rustc_session/src/config.rs

+11-1
Original file line numberDiff line numberDiff line change
@@ -898,6 +898,9 @@ impl OutFileName {
898898
#[derive(Clone, Hash, Debug, HashStable_Generic)]
899899
pub struct OutputFilenames {
900900
pub out_directory: PathBuf,
901+
/// Crate name. Never contains '-'.
902+
crate_stem: String,
903+
/// Typically based on `.rs` input file name. Any '-' is preserved.
901904
filestem: String,
902905
pub single_output_file: Option<OutFileName>,
903906
pub temps_directory: Option<PathBuf>,
@@ -911,6 +914,7 @@ pub const DWARF_OBJECT_EXT: &str = "dwo";
911914
impl OutputFilenames {
912915
pub fn new(
913916
out_directory: PathBuf,
917+
out_crate_name: String,
914918
out_filestem: String,
915919
single_output_file: Option<OutFileName>,
916920
temps_directory: Option<PathBuf>,
@@ -922,6 +926,7 @@ impl OutputFilenames {
922926
single_output_file,
923927
temps_directory,
924928
outputs,
929+
crate_stem: format!("{out_crate_name}{extra}"),
925930
filestem: format!("{out_filestem}{extra}"),
926931
}
927932
}
@@ -938,7 +943,12 @@ impl OutputFilenames {
938943
/// should be placed on disk.
939944
pub fn output_path(&self, flavor: OutputType) -> PathBuf {
940945
let extension = flavor.extension();
941-
self.with_directory_and_extension(&self.out_directory, extension)
946+
match flavor {
947+
OutputType::Metadata => {
948+
self.out_directory.join(format!("lib{}.{}", self.crate_stem, extension))
949+
}
950+
_ => self.with_directory_and_extension(&self.out_directory, extension),
951+
}
942952
}
943953

944954
/// Gets the path where a compilation artifact of the given type for the

compiler/rustc_session/src/output.rs

+2-17
Original file line numberDiff line numberDiff line change
@@ -119,26 +119,11 @@ pub fn validate_crate_name(sess: &Session, s: Symbol, sp: Option<Span>) {
119119
}
120120
}
121121

122-
pub fn filename_for_metadata(
123-
sess: &Session,
124-
crate_name: Symbol,
125-
outputs: &OutputFilenames,
126-
) -> OutFileName {
127-
// If the command-line specified the path, use that directly.
128-
if let Some(Some(out_filename)) = sess.opts.output_types.get(&OutputType::Metadata) {
129-
return out_filename.clone();
130-
}
131-
132-
let libname = format!("{}{}", crate_name, sess.opts.cg.extra_filename);
133-
134-
let out_filename = outputs.single_output_file.clone().unwrap_or_else(|| {
135-
OutFileName::Real(outputs.out_directory.join(&format!("lib{libname}.rmeta")))
136-
});
137-
122+
pub fn filename_for_metadata(sess: &Session, outputs: &OutputFilenames) -> OutFileName {
123+
let out_filename = outputs.path(OutputType::Metadata);
138124
if let OutFileName::Real(ref path) = out_filename {
139125
check_file_is_writeable(path, sess);
140126
}
141-
142127
out_filename
143128
}
144129

+13
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
include ../tools.mk
2+
3+
ifdef RUSTC_BLESS_TEST
4+
RUSTC_TEST_OP = cp
5+
else
6+
RUSTC_TEST_OP = $(DIFF)
7+
endif
8+
9+
all:
10+
$(RUSTC) --emit=metadata,dep-info --crate-type lib dash-separated.rs -C extra-filename=_something-extra
11+
# Strip TMPDIR since it is a machine specific absolute path
12+
sed "s%.*[/\\]%%" "$(TMPDIR)"/dash-separated_something-extra.d > "$(TMPDIR)"/dash-separated_something-extra.normalized.d
13+
$(RUSTC_TEST_OP) "$(TMPDIR)"/dash-separated_something-extra.normalized.d dash-separated_something-extra.normalized.d
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
//! It is important that this file has at least one `-` in the file name since
2+
//! we want to test that it becomes a `_` when appropriate.
3+
4+
pub struct Foo;
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
libdash_separated_something-extra.rmeta: dash-separated.rs
2+
3+
dash-separated_something-extra.d: dash-separated.rs
4+
5+
dash-separated.rs:

0 commit comments

Comments
 (0)