Skip to content

Commit 911ec3b

Browse files
committed
Serialize OutputFilenames into rmeta file
This ensures that linking will use the correct crate name even when #![crate_name] is used.
1 parent f4e3ad0 commit 911ec3b

File tree

5 files changed

+42
-43
lines changed

5 files changed

+42
-43
lines changed

compiler/rustc_codegen_ssa/src/lib.rs

+8-2
Original file line numberDiff line numberDiff line change
@@ -218,6 +218,7 @@ impl CodegenResults {
218218
sess: &Session,
219219
rlink_file: &Path,
220220
codegen_results: &CodegenResults,
221+
outputs: &OutputFilenames,
221222
) -> Result<usize, io::Error> {
222223
let mut encoder = FileEncoder::new(rlink_file)?;
223224
encoder.emit_raw_bytes(RLINK_MAGIC);
@@ -226,10 +227,14 @@ impl CodegenResults {
226227
encoder.emit_raw_bytes(&RLINK_VERSION.to_be_bytes());
227228
encoder.emit_str(sess.cfg_version);
228229
Encodable::encode(codegen_results, &mut encoder);
230+
Encodable::encode(outputs, &mut encoder);
229231
encoder.finish()
230232
}
231233

232-
pub fn deserialize_rlink(sess: &Session, data: Vec<u8>) -> Result<Self, CodegenErrors> {
234+
pub fn deserialize_rlink(
235+
sess: &Session,
236+
data: Vec<u8>,
237+
) -> Result<(Self, OutputFilenames), CodegenErrors> {
233238
// The Decodable machinery is not used here because it panics if the input data is invalid
234239
// and because its internal representation may change.
235240
if !data.starts_with(RLINK_MAGIC) {
@@ -258,6 +263,7 @@ impl CodegenResults {
258263
}
259264

260265
let codegen_results = CodegenResults::decode(&mut decoder);
261-
Ok(codegen_results)
266+
let outputs = OutputFilenames::decode(&mut decoder);
267+
Ok((codegen_results, outputs))
262268
}
263269
}

compiler/rustc_driver_impl/src/lib.rs

+22-22
Original file line numberDiff line numberDiff line change
@@ -678,33 +678,33 @@ fn show_md_content_with_pager(content: &str, color: ColorConfig) {
678678
fn try_process_rlink(sess: &Session, compiler: &interface::Compiler) -> Compilation {
679679
if sess.opts.unstable_opts.link_only {
680680
if let Input::File(file) = &sess.io.input {
681-
let outputs = compiler.build_output_filenames(sess, &[]);
682681
let rlink_data = fs::read(file).unwrap_or_else(|err| {
683682
sess.emit_fatal(RlinkUnableToRead { err });
684683
});
685-
let codegen_results = match CodegenResults::deserialize_rlink(sess, rlink_data) {
686-
Ok(codegen) => codegen,
687-
Err(err) => {
688-
match err {
689-
CodegenErrors::WrongFileType => sess.emit_fatal(RLinkWrongFileType),
690-
CodegenErrors::EmptyVersionNumber => {
691-
sess.emit_fatal(RLinkEmptyVersionNumber)
692-
}
693-
CodegenErrors::EncodingVersionMismatch { version_array, rlink_version } => {
694-
sess.emit_fatal(RLinkEncodingVersionMismatch {
684+
let (codegen_results, outputs) =
685+
match CodegenResults::deserialize_rlink(sess, rlink_data) {
686+
Ok((codegen, outputs)) => (codegen, outputs),
687+
Err(err) => {
688+
match err {
689+
CodegenErrors::WrongFileType => sess.emit_fatal(RLinkWrongFileType),
690+
CodegenErrors::EmptyVersionNumber => {
691+
sess.emit_fatal(RLinkEmptyVersionNumber)
692+
}
693+
CodegenErrors::EncodingVersionMismatch {
695694
version_array,
696695
rlink_version,
697-
})
698-
}
699-
CodegenErrors::RustcVersionMismatch { rustc_version } => {
700-
sess.emit_fatal(RLinkRustcVersionMismatch {
701-
rustc_version,
702-
current_version: sess.cfg_version,
703-
})
704-
}
705-
};
706-
}
707-
};
696+
} => sess.emit_fatal(RLinkEncodingVersionMismatch {
697+
version_array,
698+
rlink_version,
699+
}),
700+
CodegenErrors::RustcVersionMismatch { rustc_version } => sess
701+
.emit_fatal(RLinkRustcVersionMismatch {
702+
rustc_version,
703+
current_version: sess.cfg_version,
704+
}),
705+
};
706+
}
707+
};
708708
let result = compiler.codegen_backend().link(sess, codegen_results, &outputs);
709709
abort_on_err(result, sess);
710710
} else {

compiler/rustc_interface/src/interface.rs

+2-14
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
use crate::util;
22

33
use rustc_ast::token;
4-
use rustc_ast::{self as ast, LitKind, MetaItemKind};
4+
use rustc_ast::{LitKind, MetaItemKind};
55
use rustc_codegen_ssa::traits::CodegenBackend;
66
use rustc_data_structures::defer;
77
use rustc_data_structures::fx::{FxHashMap, FxHashSet};
@@ -15,9 +15,7 @@ use rustc_middle::{bug, ty};
1515
use rustc_parse::maybe_new_parser_from_source_str;
1616
use rustc_query_impl::QueryCtxt;
1717
use rustc_query_system::query::print_query_stack;
18-
use rustc_session::config::{
19-
self, Cfg, CheckCfg, ExpectedValues, Input, OutFileName, OutputFilenames,
20-
};
18+
use rustc_session::config::{self, Cfg, CheckCfg, ExpectedValues, Input, OutFileName};
2119
use rustc_session::filesearch::sysroot_candidates;
2220
use rustc_session::parse::ParseSess;
2321
use rustc_session::{lint, CompilerIO, EarlyErrorHandler, Session};
@@ -54,16 +52,6 @@ impl Compiler {
5452
pub fn register_lints(&self) -> &Option<Box<dyn Fn(&Session, &mut LintStore) + Send + Sync>> {
5553
&self.register_lints
5654
}
57-
pub fn build_output_filenames(
58-
&self,
59-
sess: &Session,
60-
attrs: &[ast::Attribute],
61-
) -> OutputFilenames {
62-
util::build_output_filenames(
63-
sess,
64-
rustc_session::output::find_crate_name(sess, attrs).to_string(),
65-
)
66-
}
6755
}
6856

6957
/// Converts strings provided as `--cfg [cfgspec]` into a `Cfg`.

compiler/rustc_interface/src/queries.rs

+7-2
Original file line numberDiff line numberDiff line change
@@ -299,8 +299,13 @@ impl Linker {
299299

300300
if sess.opts.unstable_opts.no_link {
301301
let rlink_file = self.prepare_outputs.with_extension(config::RLINK_EXT);
302-
CodegenResults::serialize_rlink(sess, &rlink_file, &codegen_results)
303-
.map_err(|error| sess.emit_fatal(FailedWritingFile { path: &rlink_file, error }))?;
302+
CodegenResults::serialize_rlink(
303+
sess,
304+
&rlink_file,
305+
&codegen_results,
306+
&*self.prepare_outputs,
307+
)
308+
.map_err(|error| sess.emit_fatal(FailedWritingFile { path: &rlink_file, error }))?;
304309
return Ok(());
305310
}
306311

compiler/rustc_session/src/config.rs

+3-3
Original file line numberDiff line numberDiff line change
@@ -580,7 +580,7 @@ pub enum ResolveDocLinks {
580580
/// *Do not* switch `BTreeMap` out for an unsorted container type! That would break
581581
/// dependency tracking for command-line arguments. Also only hash keys, since tracking
582582
/// should only depend on the output types, not the paths they're written to.
583-
#[derive(Clone, Debug, Hash, HashStable_Generic)]
583+
#[derive(Clone, Debug, Hash, HashStable_Generic, Encodable, Decodable)]
584584
pub struct OutputTypes(BTreeMap<OutputType, Option<OutFileName>>);
585585

586586
impl OutputTypes {
@@ -818,7 +818,7 @@ impl Input {
818818
}
819819
}
820820

821-
#[derive(Clone, Hash, Debug, HashStable_Generic, PartialEq)]
821+
#[derive(Clone, Hash, Debug, HashStable_Generic, PartialEq, Encodable, Decodable)]
822822
pub enum OutFileName {
823823
Real(PathBuf),
824824
Stdout,
@@ -890,7 +890,7 @@ impl OutFileName {
890890
}
891891
}
892892

893-
#[derive(Clone, Hash, Debug, HashStable_Generic)]
893+
#[derive(Clone, Hash, Debug, HashStable_Generic, Encodable, Decodable)]
894894
pub struct OutputFilenames {
895895
pub out_directory: PathBuf,
896896
/// Crate name. Never contains '-'.

0 commit comments

Comments
 (0)