Skip to content

Commit 0237899

Browse files
committed
bootstrap naked_asm! for compiler-builtins
in this commit, `naked_asm!` is an alias for `asm!` with one difference: `options(noreturn)` is always enabled by `naked_asm!`. That makes it future-compatible for when `naked_asm!` starts disallowing `options(noreturn)` later.
1 parent d678b81 commit 0237899

File tree

5 files changed

+60
-6
lines changed

5 files changed

+60
-6
lines changed

compiler/rustc_builtin_macros/src/asm.rs

+38
Original file line numberDiff line numberDiff line change
@@ -812,6 +812,44 @@ pub(super) fn expand_asm<'cx>(
812812
})
813813
}
814814

815+
pub(super) fn expand_naked_asm<'cx>(
816+
ecx: &'cx mut ExtCtxt<'_>,
817+
sp: Span,
818+
tts: TokenStream,
819+
) -> MacroExpanderResult<'cx> {
820+
ExpandResult::Ready(match parse_args(ecx, sp, tts, false) {
821+
Ok(args) => {
822+
let ExpandResult::Ready(mac) = expand_preparsed_asm(ecx, args) else {
823+
return ExpandResult::Retry(());
824+
};
825+
let expr = match mac {
826+
Ok(mut inline_asm) => {
827+
// for future compatibility, we always set the NORETURN option.
828+
//
829+
// When we turn `asm!` into `naked_asm!` with this implementation, we can drop
830+
// the `options(noreturn)`, which makes the upgrade smooth when `naked_asm!`
831+
// starts disallowing the `noreturn` option in the future
832+
inline_asm.options |= ast::InlineAsmOptions::NORETURN;
833+
834+
P(ast::Expr {
835+
id: ast::DUMMY_NODE_ID,
836+
kind: ast::ExprKind::InlineAsm(P(inline_asm)),
837+
span: sp,
838+
attrs: ast::AttrVec::new(),
839+
tokens: None,
840+
})
841+
}
842+
Err(guar) => DummyResult::raw_expr(sp, Some(guar)),
843+
};
844+
MacEager::expr(expr)
845+
}
846+
Err(err) => {
847+
let guar = err.emit();
848+
DummyResult::any(sp, guar)
849+
}
850+
})
851+
}
852+
815853
pub(super) fn expand_global_asm<'cx>(
816854
ecx: &'cx mut ExtCtxt<'_>,
817855
sp: Span,

compiler/rustc_builtin_macros/src/lib.rs

+1
Original file line numberDiff line numberDiff line change
@@ -94,6 +94,7 @@ pub fn register_builtin_macros(resolver: &mut dyn ResolverExpand) {
9494
line: source_util::expand_line,
9595
log_syntax: log_syntax::expand_log_syntax,
9696
module_path: source_util::expand_mod,
97+
naked_asm: asm::expand_naked_asm,
9798
option_env: env::expand_option_env,
9899
pattern_type: pattern_type::expand,
99100
std_panic: edition_panic::expand_panic,

compiler/rustc_span/src/symbol.rs

+1
Original file line numberDiff line numberDiff line change
@@ -1254,6 +1254,7 @@ symbols! {
12541254
mut_preserve_binding_mode_2024,
12551255
mut_ref,
12561256
naked,
1257+
naked_asm,
12571258
naked_functions,
12581259
name,
12591260
names,

library/core/src/arch.rs

+14
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,20 @@ pub macro asm("assembly template", $(operands,)* $(options($(option),*))?) {
2626
/* compiler built-in */
2727
}
2828

29+
/// Inline assembly used in combination with `#[naked]` functions.
30+
///
31+
/// Refer to [Rust By Example] for a usage guide and the [reference] for
32+
/// detailed information about the syntax and available options.
33+
///
34+
/// [Rust By Example]: https://doc.rust-lang.org/nightly/rust-by-example/unsafe/asm.html
35+
/// [reference]: https://doc.rust-lang.org/nightly/reference/inline-assembly.html
36+
#[unstable(feature = "naked_functions", issue = "90957")]
37+
#[rustc_builtin_macro]
38+
#[cfg(not(bootstrap))]
39+
pub macro naked_asm("assembly template", $(operands,)* $(options($(option),*))?) {
40+
/* compiler built-in */
41+
}
42+
2943
/// Module-level inline assembly.
3044
///
3145
/// Refer to [Rust By Example] for a usage guide and the [reference] for

tests/ui/asm/naked-functions-inline.rs

+6-6
Original file line numberDiff line numberDiff line change
@@ -2,37 +2,37 @@
22
#![feature(naked_functions)]
33
#![crate_type = "lib"]
44

5-
use std::arch::asm;
5+
use std::arch::naked_asm;
66

77
#[naked]
88
pub unsafe extern "C" fn inline_none() {
9-
asm!("", options(noreturn));
9+
naked_asm!("");
1010
}
1111

1212
#[naked]
1313
#[inline]
1414
//~^ ERROR [E0736]
1515
pub unsafe extern "C" fn inline_hint() {
16-
asm!("", options(noreturn));
16+
naked_asm!("");
1717
}
1818

1919
#[naked]
2020
#[inline(always)]
2121
//~^ ERROR [E0736]
2222
pub unsafe extern "C" fn inline_always() {
23-
asm!("", options(noreturn));
23+
naked_asm!("");
2424
}
2525

2626
#[naked]
2727
#[inline(never)]
2828
//~^ ERROR [E0736]
2929
pub unsafe extern "C" fn inline_never() {
30-
asm!("", options(noreturn));
30+
naked_asm!("");
3131
}
3232

3333
#[naked]
3434
#[cfg_attr(all(), inline(never))]
3535
//~^ ERROR [E0736]
3636
pub unsafe extern "C" fn conditional_inline_never() {
37-
asm!("", options(noreturn));
37+
naked_asm!("");
3838
}

0 commit comments

Comments
 (0)