Skip to content

Commit 487427f

Browse files
committed
Better error messages
1 parent b65a92f commit 487427f

File tree

1 file changed

+14
-8
lines changed

1 file changed

+14
-8
lines changed

compiler/rustc_codegen_ssa/src/back/metadata.rs

+14-8
Original file line numberDiff line numberDiff line change
@@ -28,8 +28,10 @@ fn load_metadata_with(
2828
path: &Path,
2929
f: impl for<'a> FnOnce(&'a [u8]) -> Result<&'a [u8], String>,
3030
) -> Result<MetadataRef, String> {
31-
let file = File::open(path).map_err(|e| format!("{:?}", e))?;
32-
let data = unsafe { Mmap::map(file) }.map_err(|e| format!("{:?}", e))?;
31+
let file =
32+
File::open(path).map_err(|e| format!("failed to open file '{}': {}", path.display(), e))?;
33+
let data = unsafe { Mmap::map(file) }
34+
.map_err(|e| format!("failed to mmap file '{}': {}", path.display(), e))?;
3335
let metadata = OwningRef::new(data).try_map(f)?;
3436
return Ok(rustc_erase_owner!(metadata.map_owner_box()));
3537
}
@@ -38,28 +40,32 @@ impl MetadataLoader for DefaultMetadataLoader {
3840
fn get_rlib_metadata(&self, _target: &Target, path: &Path) -> Result<MetadataRef, String> {
3941
load_metadata_with(path, |data| {
4042
let archive = object::read::archive::ArchiveFile::parse(&*data)
41-
.map_err(|e| format!("{:?}", e))?;
43+
.map_err(|e| format!("failed to parse rlib '{}': {}", path.display(), e))?;
4244

4345
for entry_result in archive.members() {
44-
let entry = entry_result.map_err(|e| format!("{:?}", e))?;
46+
let entry = entry_result
47+
.map_err(|e| format!("failed to parse rlib '{}': {}", path.display(), e))?;
4548
if entry.name() == METADATA_FILENAME.as_bytes() {
4649
return Ok(entry.data());
4750
}
4851
}
4952

50-
Err("couldn't find metadata entry".to_string())
53+
Err(format!("metadata not found in rlib '{}'", path.display()))
5154
})
5255
}
5356

5457
fn get_dylib_metadata(&self, _target: &Target, path: &Path) -> Result<MetadataRef, String> {
5558
use object::{Object, ObjectSection};
5659

5760
load_metadata_with(path, |data| {
58-
let file = object::File::parse(&data).map_err(|e| format!("parse: {:?}", e))?;
61+
let file = object::File::parse(&data)
62+
.map_err(|e| format!("failed to parse dylib '{}': {}", path.display(), e))?;
5963
file.section_by_name(".rustc")
60-
.ok_or("no .rustc section")?
64+
.ok_or_else(|| format!("no .rustc section in '{}'", path.display()))?
6165
.data()
62-
.map_err(|e| format!("failed to read .rustc section: {:?}", e))
66+
.map_err(|e| {
67+
format!("failed to read .rustc section in '{}': {}", path.display(), e)
68+
})
6369
})
6470
}
6571
}

0 commit comments

Comments
 (0)