about summary refs log tree commit diff
diff options
context:
space:
mode:
authorbjorn3 <17426603+bjorn3@users.noreply.github.com>2022-08-12 09:28:41 +0000
committerbjorn3 <17426603+bjorn3@users.noreply.github.com>2022-08-12 09:28:41 +0000
commit066f844fff7b6bf227c375a293fe15af88cf85ac (patch)
tree1702be1c46bbffca395d14b724c5243f54c41839
parent7cc97ebcbb3fe637e2467f2e4fc45d94dae013e8 (diff)
downloadrust-066f844fff7b6bf227c375a293fe15af88cf85ac.tar.gz
rust-066f844fff7b6bf227c375a293fe15af88cf85ac.zip
Move some sess.fatal calls out of compile_global_asm
-rw-r--r--src/driver/aot.rs5
-rw-r--r--src/global_asm.rs43
-rw-r--r--src/toolchain.rs6
3 files changed, 35 insertions, 19 deletions
diff --git a/src/driver/aot.rs b/src/driver/aot.rs
index 6482dce2746..0816ebc4599 100644
--- a/src/driver/aot.rs
+++ b/src/driver/aot.rs
@@ -198,7 +198,10 @@ fn module_codegen(
         )
     });
 
-    crate::global_asm::compile_global_asm(tcx, cgu.name().as_str(), &cx.global_asm);
+    match crate::global_asm::compile_global_asm(tcx, cgu.name().as_str(), &cx.global_asm) {
+        Ok(()) => {}
+        Err(err) => tcx.sess.fatal(&err.to_string()),
+    }
 
     codegen_result
 }
diff --git a/src/global_asm.rs b/src/global_asm.rs
index 5962a86a686..5cd7abfdfb5 100644
--- a/src/global_asm.rs
+++ b/src/global_asm.rs
@@ -1,7 +1,9 @@
 //! The AOT driver uses [`cranelift_object`] to write object files suitable for linking into a
 //! standalone executable.
 
+use std::io::{self, Write};
 use std::path::PathBuf;
+use std::process::{Command, Stdio};
 
 use rustc_ast::{InlineAsmOptions, InlineAsmTemplatePiece};
 use rustc_hir::ItemId;
@@ -29,12 +31,13 @@ pub(crate) fn codegen_global_asm_item(tcx: TyCtxt<'_>, global_asm: &mut String,
     }
 }
 
-pub(crate) fn compile_global_asm(tcx: TyCtxt<'_>, cgu_name: &str, global_asm: &str) {
-    use std::io::Write;
-    use std::process::{Command, Stdio};
-
+pub(crate) fn compile_global_asm(
+    tcx: TyCtxt<'_>,
+    cgu_name: &str,
+    global_asm: &str,
+) -> io::Result<()> {
     if global_asm.is_empty() {
-        return;
+        return Ok(());
     }
 
     if cfg!(not(feature = "inline_asm"))
@@ -42,16 +45,20 @@ pub(crate) fn compile_global_asm(tcx: TyCtxt<'_>, cgu_name: &str, global_asm: &s
         || tcx.sess.target.is_like_windows
     {
         if global_asm.contains("__rust_probestack") {
-            return;
+            return Ok(());
         }
 
         // FIXME fix linker error on macOS
         if cfg!(not(feature = "inline_asm")) {
-            tcx.sess.fatal(
+            return Err(io::Error::new(
+                io::ErrorKind::Unsupported,
                 "asm! and global_asm! support is disabled while compiling rustc_codegen_cranelift",
-            );
+            ));
         } else {
-            tcx.sess.fatal("asm! and global_asm! are not yet supported on macOS and Windows");
+            return Err(io::Error::new(
+                io::ErrorKind::Unsupported,
+                "asm! and global_asm! are not yet supported on macOS and Windows",
+            ));
         }
     }
 
@@ -78,7 +85,10 @@ pub(crate) fn compile_global_asm(tcx: TyCtxt<'_>, cgu_name: &str, global_asm: &s
     child.stdin.take().unwrap().write_all(global_asm.as_bytes()).unwrap();
     let status = child.wait().expect("Failed to wait for `as`.");
     if !status.success() {
-        tcx.sess.fatal(&format!("Failed to assemble `{}`", global_asm));
+        return Err(io::Error::new(
+            io::ErrorKind::Other,
+            format!("Failed to assemble `{}`", global_asm),
+        ));
     }
 
     // Link the global asm and main object file together
@@ -93,15 +103,20 @@ pub(crate) fn compile_global_asm(tcx: TyCtxt<'_>, cgu_name: &str, global_asm: &s
         .status()
         .unwrap();
     if !status.success() {
-        tcx.sess.fatal(&format!(
-            "Failed to link `{}` and `{}` together",
-            main_object_file.display(),
-            global_asm_object_file.display(),
+        return Err(io::Error::new(
+            io::ErrorKind::Other,
+            format!(
+                "Failed to link `{}` and `{}` together",
+                main_object_file.display(),
+                global_asm_object_file.display(),
+            ),
         ));
     }
 
     std::fs::remove_file(global_asm_object_file).unwrap();
     std::fs::remove_file(main_object_file).unwrap();
+
+    Ok(())
 }
 
 fn add_file_stem_postfix(mut path: PathBuf, postfix: &str) -> PathBuf {
diff --git a/src/toolchain.rs b/src/toolchain.rs
index f86236ef3ea..b6b465e1f4e 100644
--- a/src/toolchain.rs
+++ b/src/toolchain.rs
@@ -8,10 +8,8 @@ use rustc_session::Session;
 /// Tries to infer the path of a binary for the target toolchain from the linker name.
 pub(crate) fn get_toolchain_binary(sess: &Session, tool: &str) -> PathBuf {
     let (mut linker, _linker_flavor) = linker_and_flavor(sess);
-    let linker_file_name = linker
-        .file_name()
-        .and_then(|name| name.to_str())
-        .unwrap_or_else(|| sess.fatal("couldn't extract file name from specified linker"));
+    let linker_file_name =
+        linker.file_name().unwrap().to_str().expect("linker filename should be valid UTF-8");
 
     if linker_file_name == "ld.lld" {
         if tool != "ld" {