Skip to content

Commit 98a6eaa

Browse files
committed
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.
1 parent eacbe65 commit 98a6eaa

File tree

5 files changed

+22
-27
lines changed

5 files changed

+22
-27
lines changed

compiler/rustc_codegen_ssa/src/lib.rs

+8-2
Original file line numberDiff line numberDiff line change
@@ -216,6 +216,7 @@ impl CodegenResults {
216216
sess: &Session,
217217
rlink_file: &Path,
218218
codegen_results: &CodegenResults,
219+
outputs: &OutputFilenames,
219220
) -> Result<usize, io::Error> {
220221
let mut encoder = FileEncoder::new(rlink_file)?;
221222
encoder.emit_raw_bytes(RLINK_MAGIC);
@@ -224,10 +225,14 @@ impl CodegenResults {
224225
encoder.emit_raw_bytes(&RLINK_VERSION.to_be_bytes());
225226
encoder.emit_str(sess.cfg_version);
226227
Encodable::encode(codegen_results, &mut encoder);
228+
Encodable::encode(outputs, &mut encoder);
227229
encoder.finish().map_err(|(_path, err)| err)
228230
}
229231

230-
pub fn deserialize_rlink(sess: &Session, data: Vec<u8>) -> Result<Self, CodegenErrors> {
232+
pub fn deserialize_rlink(
233+
sess: &Session,
234+
data: Vec<u8>,
235+
) -> Result<(Self, OutputFilenames), CodegenErrors> {
231236
// The Decodable machinery is not used here because it panics if the input data is invalid
232237
// and because its internal representation may change.
233238
if !data.starts_with(RLINK_MAGIC) {
@@ -256,6 +261,7 @@ impl CodegenResults {
256261
}
257262

258263
let codegen_results = CodegenResults::decode(&mut decoder);
259-
Ok(codegen_results)
264+
let outputs = OutputFilenames::decode(&mut decoder);
265+
Ok((codegen_results, outputs))
260266
}
261267
}

compiler/rustc_driver_impl/src/lib.rs

+2-3
Original file line numberDiff line numberDiff line change
@@ -648,12 +648,11 @@ fn show_md_content_with_pager(content: &str, color: ColorConfig) {
648648
fn process_rlink(sess: &Session, compiler: &interface::Compiler) {
649649
assert!(sess.opts.unstable_opts.link_only);
650650
if let Input::File(file) = &sess.io.input {
651-
let outputs = compiler.build_output_filenames(sess, &[]);
652651
let rlink_data = fs::read(file).unwrap_or_else(|err| {
653652
sess.emit_fatal(RlinkUnableToRead { err });
654653
});
655-
let codegen_results = match CodegenResults::deserialize_rlink(sess, rlink_data) {
656-
Ok(codegen) => codegen,
654+
let (codegen_results, outputs) = match CodegenResults::deserialize_rlink(sess, rlink_data) {
655+
Ok((codegen, outputs)) => (codegen, outputs),
657656
Err(err) => {
658657
match err {
659658
CodegenErrors::WrongFileType => sess.emit_fatal(RLinkWrongFileType),

compiler/rustc_interface/src/interface.rs

+2-17
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};
@@ -43,19 +41,6 @@ pub struct Compiler {
4341
pub(crate) override_queries: Option<fn(&Session, &mut Providers)>,
4442
}
4543

46-
impl Compiler {
47-
pub fn build_output_filenames(
48-
&self,
49-
sess: &Session,
50-
attrs: &[ast::Attribute],
51-
) -> OutputFilenames {
52-
util::build_output_filenames(
53-
sess,
54-
rustc_session::output::find_crate_name(sess, attrs).to_string(),
55-
)
56-
}
57-
}
58-
5944
/// Converts strings provided as `--cfg [cfgspec]` into a `Cfg`.
6045
pub(crate) fn parse_cfg(handler: &EarlyErrorHandler, cfgs: Vec<String>) -> Cfg {
6146
cfgs.into_iter()

compiler/rustc_interface/src/queries.rs

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

274274
if sess.opts.unstable_opts.no_link {
275275
let rlink_file = self.output_filenames.with_extension(config::RLINK_EXT);
276-
CodegenResults::serialize_rlink(sess, &rlink_file, &codegen_results)
277-
.map_err(|error| sess.emit_fatal(FailedWritingFile { path: &rlink_file, error }))?;
276+
CodegenResults::serialize_rlink(
277+
sess,
278+
&rlink_file,
279+
&codegen_results,
280+
&*self.output_filenames,
281+
)
282+
.map_err(|error| sess.emit_fatal(FailedWritingFile { path: &rlink_file, error }))?;
278283
return Ok(());
279284
}
280285

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)