Skip to content

Commit 9b47a43

Browse files
committed
Serialize OutputFilenames into rmeta file
This ensures that linking will use the correct crate name even when
1 parent 6687f13 commit 9b47a43

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
@@ -666,33 +666,33 @@ fn show_md_content_with_pager(content: &str, color: ColorConfig) {
666666
fn try_process_rlink(sess: &Session, compiler: &interface::Compiler) -> Compilation {
667667
if sess.opts.unstable_opts.link_only {
668668
if let Input::File(file) = &sess.io.input {
669-
let outputs = compiler.build_output_filenames(sess, &[]);
670669
let rlink_data = fs::read(file).unwrap_or_else(|err| {
671670
sess.emit_fatal(RlinkUnableToRead { err });
672671
});
673-
let codegen_results = match CodegenResults::deserialize_rlink(sess, rlink_data) {
674-
Ok(codegen) => codegen,
675-
Err(err) => {
676-
match err {
677-
CodegenErrors::WrongFileType => sess.emit_fatal(RLinkWrongFileType),
678-
CodegenErrors::EmptyVersionNumber => {
679-
sess.emit_fatal(RLinkEmptyVersionNumber)
680-
}
681-
CodegenErrors::EncodingVersionMismatch { version_array, rlink_version } => {
682-
sess.emit_fatal(RLinkEncodingVersionMismatch {
672+
let (codegen_results, outputs) =
673+
match CodegenResults::deserialize_rlink(sess, rlink_data) {
674+
Ok((codegen, outputs)) => (codegen, outputs),
675+
Err(err) => {
676+
match err {
677+
CodegenErrors::WrongFileType => sess.emit_fatal(RLinkWrongFileType),
678+
CodegenErrors::EmptyVersionNumber => {
679+
sess.emit_fatal(RLinkEmptyVersionNumber)
680+
}
681+
CodegenErrors::EncodingVersionMismatch {
683682
version_array,
684683
rlink_version,
685-
})
686-
}
687-
CodegenErrors::RustcVersionMismatch { rustc_version } => {
688-
sess.emit_fatal(RLinkRustcVersionMismatch {
689-
rustc_version,
690-
current_version: sess.cfg_version,
691-
})
692-
}
693-
};
694-
}
695-
};
684+
} => sess.emit_fatal(RLinkEncodingVersionMismatch {
685+
version_array,
686+
rlink_version,
687+
}),
688+
CodegenErrors::RustcVersionMismatch { rustc_version } => sess
689+
.emit_fatal(RLinkRustcVersionMismatch {
690+
rustc_version,
691+
current_version: sess.cfg_version,
692+
}),
693+
};
694+
}
695+
};
696696
let result = compiler.codegen_backend().link(sess, codegen_results, &outputs);
697697
abort_on_err(result, sess);
698698
} 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};
@@ -50,16 +48,6 @@ impl Compiler {
5048
pub fn codegen_backend(&self) -> &dyn CodegenBackend {
5149
&*self.codegen_backend
5250
}
53-
pub fn build_output_filenames(
54-
&self,
55-
sess: &Session,
56-
attrs: &[ast::Attribute],
57-
) -> OutputFilenames {
58-
util::build_output_filenames(
59-
sess,
60-
rustc_session::output::find_crate_name(sess, attrs).to_string(),
61-
)
62-
}
6351
}
6452

6553
/// 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
@@ -278,8 +278,13 @@ impl Linker {
278278

279279
if sess.opts.unstable_opts.no_link {
280280
let rlink_file = self.output_filenames.with_extension(config::RLINK_EXT);
281-
CodegenResults::serialize_rlink(sess, &rlink_file, &codegen_results)
282-
.map_err(|error| sess.emit_fatal(FailedWritingFile { path: &rlink_file, error }))?;
281+
CodegenResults::serialize_rlink(
282+
sess,
283+
&rlink_file,
284+
&codegen_results,
285+
&*self.output_filenames,
286+
)
287+
.map_err(|error| sess.emit_fatal(FailedWritingFile { path: &rlink_file, error }))?;
283288
return Ok(());
284289
}
285290

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)