Skip to content

Commit 28491a7

Browse files
committed
UPDATE - address PR Comments
FIX - StrippingDebugInfoFailed typo DELETE - unneeded FIXME comment UPDATE - only declare the error with ExtractBundledLibsError as an enum and use the Diagnostic derive macro
1 parent 2678765 commit 28491a7

File tree

3 files changed

+40
-83
lines changed

3 files changed

+40
-83
lines changed

compiler/rustc_codegen_ssa/src/back/archive.rs

+19-37
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ use std::fs::File;
1010
use std::io;
1111
use std::path::{Path, PathBuf};
1212

13-
use crate::errors::{ExtractBundledLibsError, ExtractBundledLibsErrorKind::*};
13+
use crate::errors::ExtractBundledLibsError;
1414

1515
pub trait ArchiveBuilderBuilder {
1616
fn new_archive_builder<'a>(&self, sess: &'a Session) -> Box<dyn ArchiveBuilder<'a> + 'a>;
@@ -35,48 +35,30 @@ pub trait ArchiveBuilderBuilder {
3535
outdir: &Path,
3636
bundled_lib_file_names: &FxHashSet<Symbol>,
3737
) -> Result<(), ExtractBundledLibsError<'_>> {
38-
let archive_map = unsafe {
39-
Mmap::map(File::open(rlib).map_err(|e| ExtractBundledLibsError {
40-
kind: OpenFile,
41-
rlib,
42-
error: e.to_string(),
43-
})?)
44-
.map_err(|e| ExtractBundledLibsError {
45-
kind: MmapFile,
46-
rlib,
47-
error: e.to_string(),
48-
})?
49-
};
50-
let archive = ArchiveFile::parse(&*archive_map).map_err(|e| ExtractBundledLibsError {
51-
kind: ParseArchive,
52-
rlib,
53-
error: e.to_string(),
54-
})?;
38+
let archive_map =
39+
unsafe {
40+
Mmap::map(File::open(rlib).map_err(|e| ExtractBundledLibsError::OpenFile {
41+
rlib,
42+
error: e.to_string(),
43+
})?)
44+
.map_err(|e| ExtractBundledLibsError::MmapFile { rlib, error: e.to_string() })?
45+
};
46+
let archive = ArchiveFile::parse(&*archive_map)
47+
.map_err(|e| ExtractBundledLibsError::ParseArchive { rlib, error: e.to_string() })?;
5548

5649
for entry in archive.members() {
57-
let entry = entry.map_err(|e| ExtractBundledLibsError {
58-
kind: ReadEntry,
59-
rlib,
60-
error: e.to_string(),
61-
})?;
62-
let data = entry.data(&*archive_map).map_err(|e| ExtractBundledLibsError {
63-
kind: ArchiveMember,
64-
rlib,
65-
error: e.to_string(),
66-
})?;
67-
let name = std::str::from_utf8(entry.name()).map_err(|e| ExtractBundledLibsError {
68-
kind: ConvertName,
69-
rlib,
70-
error: e.to_string(),
50+
let entry = entry
51+
.map_err(|e| ExtractBundledLibsError::ReadEntry { rlib, error: e.to_string() })?;
52+
let data = entry.data(&*archive_map).map_err(|e| {
53+
ExtractBundledLibsError::ArchiveMember { rlib, error: e.to_string() }
7154
})?;
55+
let name = std::str::from_utf8(entry.name())
56+
.map_err(|e| ExtractBundledLibsError::ConvertName { rlib, error: e.to_string() })?;
7257
if !bundled_lib_file_names.contains(&Symbol::intern(name)) {
7358
continue; // We need to extract only native libraries.
7459
}
75-
std::fs::write(&outdir.join(&name), data).map_err(|e| ExtractBundledLibsError {
76-
kind: WriteFile,
77-
rlib,
78-
error: e.to_string(),
79-
})?;
60+
std::fs::write(&outdir.join(&name), data)
61+
.map_err(|e| ExtractBundledLibsError::WriteFile { rlib, error: e.to_string() })?;
8062
}
8163
Ok(())
8264
}

compiler/rustc_codegen_ssa/src/back/link.rs

+1-2
Original file line numberDiff line numberDiff line change
@@ -1063,7 +1063,7 @@ fn strip_symbols_with_external_utility<'a>(
10631063
if !prog.status.success() {
10641064
let mut output = prog.stderr.clone();
10651065
output.extend_from_slice(&prog.stdout);
1066-
sess.emit_warning(errors::StrippingDebuInfoFailed {
1066+
sess.emit_warning(errors::StrippingDebugInfoFailed {
10671067
util,
10681068
status: prog.status,
10691069
output: escape_string(&output),
@@ -1077,7 +1077,6 @@ fn strip_symbols_with_external_utility<'a>(
10771077
fn escape_string(s: &[u8]) -> String {
10781078
match str::from_utf8(s) {
10791079
Ok(s) => s.to_owned(),
1080-
// FIXME: return a type that can conform to IntoDiagnosticArg
10811080
Err(_) => format!("Non-UTF-8 output: {}", s.escape_ascii()),
10821081
}
10831082
}

compiler/rustc_codegen_ssa/src/errors.rs

+20-44
Original file line numberDiff line numberDiff line change
@@ -423,7 +423,7 @@ pub struct UnableToRunDsymutil {
423423
#[derive(Diagnostic)]
424424
#[diag(codegen_ssa_stripping_debu_info_failed)]
425425
#[note]
426-
pub struct StrippingDebuInfoFailed<'a> {
426+
pub struct StrippingDebugInfoFailed<'a> {
427427
pub util: &'a str,
428428
pub status: ExitStatus,
429429
pub output: String,
@@ -485,52 +485,28 @@ pub struct RlibArchiveBuildFailure {
485485
#[diag(codegen_ssa_option_gcc_only)]
486486
pub struct OptionGccOnly;
487487

488-
pub struct ExtractBundledLibsError<'a> {
489-
pub kind: ExtractBundledLibsErrorKind,
490-
pub rlib: &'a Path,
491-
pub error: String,
492-
}
488+
#[derive(Diagnostic)]
489+
pub enum ExtractBundledLibsError<'a> {
490+
#[diag(codegen_ssa_extract_bundled_libs_open_file)]
491+
OpenFile { rlib: &'a Path, error: String },
493492

494-
pub enum ExtractBundledLibsErrorKind {
495-
OpenFile,
496-
MmapFile,
497-
ParseArchive,
498-
ReadEntry,
499-
ArchiveMember,
500-
ConvertName,
501-
WriteFile,
502-
}
493+
#[diag(codegen_ssa_extract_bundled_libs_mmap_file)]
494+
MmapFile { rlib: &'a Path, error: String },
503495

504-
impl IntoDiagnostic<'_, !> for ExtractBundledLibsError<'_> {
505-
fn into_diagnostic(self, handler: &'_ Handler) -> DiagnosticBuilder<'_, !> {
506-
let mut diag = match self.kind {
507-
ExtractBundledLibsErrorKind::OpenFile => {
508-
handler.struct_fatal(fluent::codegen_ssa_extract_bundled_libs_open_file)
509-
}
510-
ExtractBundledLibsErrorKind::MmapFile => {
511-
handler.struct_fatal(fluent::codegen_ssa_extract_bundled_libs_mmap_file)
512-
}
513-
ExtractBundledLibsErrorKind::ParseArchive => {
514-
handler.struct_fatal(fluent::codegen_ssa_extract_bundled_libs_parse_archive)
515-
}
516-
ExtractBundledLibsErrorKind::ReadEntry => {
517-
handler.struct_fatal(fluent::codegen_ssa_extract_bundled_libs_read_entry)
518-
}
519-
ExtractBundledLibsErrorKind::ArchiveMember => {
520-
handler.struct_fatal(fluent::codegen_ssa_extract_bundled_libs_archive_member)
521-
}
522-
ExtractBundledLibsErrorKind::ConvertName => {
523-
handler.struct_fatal(fluent::codegen_ssa_extract_bundled_libs_convert_name)
524-
}
525-
ExtractBundledLibsErrorKind::WriteFile => {
526-
handler.struct_fatal(fluent::codegen_ssa_extract_bundled_libs_write_file)
527-
}
528-
};
496+
#[diag(codegen_ssa_extract_bundled_libs_parse_archive)]
497+
ParseArchive { rlib: &'a Path, error: String },
529498

530-
diag.set_arg("rlib", self.rlib);
531-
diag.set_arg("error", self.error);
532-
diag
533-
}
499+
#[diag(codegen_ssa_extract_bundled_libs_read_entry)]
500+
ReadEntry { rlib: &'a Path, error: String },
501+
502+
#[diag(codegen_ssa_extract_bundled_libs_archive_member)]
503+
ArchiveMember { rlib: &'a Path, error: String },
504+
505+
#[diag(codegen_ssa_extract_bundled_libs_convert_name)]
506+
ConvertName { rlib: &'a Path, error: String },
507+
508+
#[diag(codegen_ssa_extract_bundled_libs_write_file)]
509+
WriteFile { rlib: &'a Path, error: String },
534510
}
535511

536512
#[derive(Diagnostic)]

0 commit comments

Comments
 (0)