Skip to content
This repository was archived by the owner on May 28, 2025. It is now read-only.

Commit 066f844

Browse files
committed
Move some sess.fatal calls out of compile_global_asm
1 parent 7cc97eb commit 066f844

File tree

3 files changed

+35
-19
lines changed

3 files changed

+35
-19
lines changed

src/driver/aot.rs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -198,7 +198,10 @@ fn module_codegen(
198198
)
199199
});
200200

201-
crate::global_asm::compile_global_asm(tcx, cgu.name().as_str(), &cx.global_asm);
201+
match crate::global_asm::compile_global_asm(tcx, cgu.name().as_str(), &cx.global_asm) {
202+
Ok(()) => {}
203+
Err(err) => tcx.sess.fatal(&err.to_string()),
204+
}
202205

203206
codegen_result
204207
}

src/global_asm.rs

Lines changed: 29 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
11
//! The AOT driver uses [`cranelift_object`] to write object files suitable for linking into a
22
//! standalone executable.
33
4+
use std::io::{self, Write};
45
use std::path::PathBuf;
6+
use std::process::{Command, Stdio};
57

68
use rustc_ast::{InlineAsmOptions, InlineAsmTemplatePiece};
79
use rustc_hir::ItemId;
@@ -29,29 +31,34 @@ pub(crate) fn codegen_global_asm_item(tcx: TyCtxt<'_>, global_asm: &mut String,
2931
}
3032
}
3133

32-
pub(crate) fn compile_global_asm(tcx: TyCtxt<'_>, cgu_name: &str, global_asm: &str) {
33-
use std::io::Write;
34-
use std::process::{Command, Stdio};
35-
34+
pub(crate) fn compile_global_asm(
35+
tcx: TyCtxt<'_>,
36+
cgu_name: &str,
37+
global_asm: &str,
38+
) -> io::Result<()> {
3639
if global_asm.is_empty() {
37-
return;
40+
return Ok(());
3841
}
3942

4043
if cfg!(not(feature = "inline_asm"))
4144
|| tcx.sess.target.is_like_osx
4245
|| tcx.sess.target.is_like_windows
4346
{
4447
if global_asm.contains("__rust_probestack") {
45-
return;
48+
return Ok(());
4649
}
4750

4851
// FIXME fix linker error on macOS
4952
if cfg!(not(feature = "inline_asm")) {
50-
tcx.sess.fatal(
53+
return Err(io::Error::new(
54+
io::ErrorKind::Unsupported,
5155
"asm! and global_asm! support is disabled while compiling rustc_codegen_cranelift",
52-
);
56+
));
5357
} else {
54-
tcx.sess.fatal("asm! and global_asm! are not yet supported on macOS and Windows");
58+
return Err(io::Error::new(
59+
io::ErrorKind::Unsupported,
60+
"asm! and global_asm! are not yet supported on macOS and Windows",
61+
));
5562
}
5663
}
5764

@@ -78,7 +85,10 @@ pub(crate) fn compile_global_asm(tcx: TyCtxt<'_>, cgu_name: &str, global_asm: &s
7885
child.stdin.take().unwrap().write_all(global_asm.as_bytes()).unwrap();
7986
let status = child.wait().expect("Failed to wait for `as`.");
8087
if !status.success() {
81-
tcx.sess.fatal(&format!("Failed to assemble `{}`", global_asm));
88+
return Err(io::Error::new(
89+
io::ErrorKind::Other,
90+
format!("Failed to assemble `{}`", global_asm),
91+
));
8292
}
8393

8494
// 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
93103
.status()
94104
.unwrap();
95105
if !status.success() {
96-
tcx.sess.fatal(&format!(
97-
"Failed to link `{}` and `{}` together",
98-
main_object_file.display(),
99-
global_asm_object_file.display(),
106+
return Err(io::Error::new(
107+
io::ErrorKind::Other,
108+
format!(
109+
"Failed to link `{}` and `{}` together",
110+
main_object_file.display(),
111+
global_asm_object_file.display(),
112+
),
100113
));
101114
}
102115

103116
std::fs::remove_file(global_asm_object_file).unwrap();
104117
std::fs::remove_file(main_object_file).unwrap();
118+
119+
Ok(())
105120
}
106121

107122
fn add_file_stem_postfix(mut path: PathBuf, postfix: &str) -> PathBuf {

src/toolchain.rs

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,10 +8,8 @@ use rustc_session::Session;
88
/// Tries to infer the path of a binary for the target toolchain from the linker name.
99
pub(crate) fn get_toolchain_binary(sess: &Session, tool: &str) -> PathBuf {
1010
let (mut linker, _linker_flavor) = linker_and_flavor(sess);
11-
let linker_file_name = linker
12-
.file_name()
13-
.and_then(|name| name.to_str())
14-
.unwrap_or_else(|| sess.fatal("couldn't extract file name from specified linker"));
11+
let linker_file_name =
12+
linker.file_name().unwrap().to_str().expect("linker filename should be valid UTF-8");
1513

1614
if linker_file_name == "ld.lld" {
1715
if tool != "ld" {

0 commit comments

Comments
 (0)