aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMichael Williamson <mike@zwobble.org>2026-03-29 10:06:09 +0100
committerMichael Williamson <mike@zwobble.org>2026-03-29 10:06:09 +0100
commita1f6380416af3f8b6314d2eda7624faee4b2f446 (patch)
treec3f11a5f91b70e895e7213095354fa0d2b98bf62
parente3b30817b6cd2496df2101843cddc4cf5f04bea2 (diff)
Remove unwraps
-rw-r--r--patio-to-cranelift/Cargo.lock1
-rw-r--r--patio-to-cranelift/Cargo.toml1
-rw-r--r--patio-to-cranelift/src/main.rs35
3 files changed, 29 insertions, 8 deletions
diff --git a/patio-to-cranelift/Cargo.lock b/patio-to-cranelift/Cargo.lock
index 3558cdf0..56e05437 100644
--- a/patio-to-cranelift/Cargo.lock
+++ b/patio-to-cranelift/Cargo.lock
@@ -449,6 +449,7 @@ version = "0.1.0"
dependencies = [
"clap",
"cranelift",
+ "cranelift-codegen",
"cranelift-module",
"cranelift-object",
"flatbuffers",
diff --git a/patio-to-cranelift/Cargo.toml b/patio-to-cranelift/Cargo.toml
index d6d2796e..dca2c3d9 100644
--- a/patio-to-cranelift/Cargo.toml
+++ b/patio-to-cranelift/Cargo.toml
@@ -6,6 +6,7 @@ edition = "2024"
[dependencies]
clap = { version = "4.6.0", features = ["derive"] }
cranelift = "0.130.0"
+cranelift-codegen = "0.130.0"
cranelift-module = "0.130.0"
cranelift-object = "0.130.0"
flatbuffers = "25.12.19"
diff --git a/patio-to-cranelift/src/main.rs b/patio-to-cranelift/src/main.rs
index 5346bcdb..65119c16 100644
--- a/patio-to-cranelift/src/main.rs
+++ b/patio-to-cranelift/src/main.rs
@@ -37,27 +37,28 @@ fn compile(source_path: &Path, target: &str) -> Result<(), CompileError> {
.map_err(CompileError::InvalidFlatbuffer)?;
let object_builder = cranelift_object::ObjectBuilder::new(
- isa(target),
+ isa(target)?,
source_path.as_os_str().as_encoded_bytes(),
cranelift_module::default_libcall_names()
- ).unwrap();
+ ).map_err(CompileError::ObjectBuilderNewFailed)?;
let module = cranelift_object::ObjectModule::new(object_builder);
let object = module.finish();
- let object_bytes = object.emit().unwrap();
+ let object_bytes = object.emit().map_err(CompileError::ObjectEmitFailed)?;
let destination_path = source_path.with_extension("o");
- std::fs::write(destination_path, object_bytes).unwrap();
+ std::fs::write(destination_path, object_bytes).map_err(CompileError::WriteObjectFile)?;
Ok(())
}
-fn isa(target: &str) -> std::sync::Arc<dyn cranelift::prelude::isa::TargetIsa> {
+fn isa(target: &str) -> Result<std::sync::Arc<dyn cranelift::prelude::isa::TargetIsa>, CompileError> {
let mut flags_builder = cranelift::prelude::settings::builder();
- flags_builder.enable("is_pic").unwrap();
- cranelift::prelude::isa::lookup_by_name(target).unwrap()
+ flags_builder.enable("is_pic").map_err(CompileError::EnablePicFailed)?;
+ cranelift::prelude::isa::lookup_by_name(target)
+ .map_err(CompileError::LookupIsa)?
.finish(cranelift::prelude::settings::Flags::new(flags_builder))
- .unwrap()
+ .map_err(CompileError::BuildIsa)
}
#[derive(Debug)]
@@ -67,4 +68,22 @@ enum CompileError {
/// The bytes of the patio source were an invalid flatbuffer.
InvalidFlatbuffer(#[allow(dead_code)] flatbuffers::InvalidFlatbuffer),
+
+ /// Enabling PIC failed.
+ EnablePicFailed(#[allow(dead_code)] cranelift_codegen::settings::SetError),
+
+ /// The ISA for the target triple could not be found.
+ LookupIsa(#[allow(dead_code)] cranelift_codegen::isa::LookupError),
+
+ /// Building the ISA failed.
+ BuildIsa(#[allow(dead_code)] cranelift_codegen::CodegenError),
+
+ /// Create a new object builder failed.
+ ObjectBuilderNewFailed(#[allow(dead_code)] cranelift_module::ModuleError),
+
+ /// Emitting the bytes for an object failed.
+ ObjectEmitFailed(#[allow(dead_code)] cranelift_object::object::write::Error),
+
+ /// Writing the object file to disk failed.
+ WriteObjectFile(#[allow(dead_code)] std::io::Error),
}