Skip to content

Commit 4b7a87d

Browse files
committed
use an allow list for allowed asm options in naked functions
1 parent c31ff97 commit 4b7a87d

File tree

3 files changed

+11
-14
lines changed

3 files changed

+11
-14
lines changed

compiler/rustc_passes/src/naked_functions.rs

+9-12
Original file line numberDiff line numberDiff line change
@@ -244,22 +244,19 @@ impl<'tcx> CheckInlineAssembly<'tcx> {
244244
self.tcx.dcx().emit_err(NakedFunctionsOperands { unsupported_operands });
245245
}
246246

247-
let unsupported_options: Vec<&'static str> = [
248-
(InlineAsmOptions::MAY_UNWIND, "`may_unwind`"),
249-
(InlineAsmOptions::NOMEM, "`nomem`"),
250-
(InlineAsmOptions::NOSTACK, "`nostack`"),
251-
(InlineAsmOptions::PRESERVES_FLAGS, "`preserves_flags`"),
252-
(InlineAsmOptions::PURE, "`pure`"),
253-
(InlineAsmOptions::READONLY, "`readonly`"),
254-
]
255-
.iter()
256-
.filter_map(|&(option, name)| if asm.options.contains(option) { Some(name) } else { None })
257-
.collect();
247+
let supported_options =
248+
InlineAsmOptions::RAW | InlineAsmOptions::NORETURN | InlineAsmOptions::ATT_SYNTAX;
249+
let unsupported_options = asm.options.difference(supported_options);
258250

259251
if !unsupported_options.is_empty() {
260252
self.tcx.dcx().emit_err(NakedFunctionsAsmOptions {
261253
span,
262-
unsupported_options: unsupported_options.join(", "),
254+
unsupported_options: unsupported_options
255+
.human_readable_names()
256+
.into_iter()
257+
.map(|name| format!("`{name}`"))
258+
.collect::<Vec<_>>()
259+
.join(", "),
263260
});
264261
}
265262

tests/ui/asm/naked-functions.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -111,7 +111,7 @@ unsafe extern "C" fn invalid_options() {
111111
unsafe extern "C" fn invalid_options_continued() {
112112
asm!("", options(readonly, nostack), options(pure));
113113
//~^ ERROR asm with the `pure` option must have at least one output
114-
//~| ERROR asm options unsupported in naked functions: `nostack`, `pure`, `readonly`
114+
//~| ERROR asm options unsupported in naked functions: `pure`, `readonly`, `nostack`
115115
//~| ERROR asm in naked functions must use `noreturn` option
116116
}
117117

tests/ui/asm/naked-functions.stderr

+1-1
Original file line numberDiff line numberDiff line change
@@ -212,7 +212,7 @@ error[E0787]: asm options unsupported in naked functions: `nomem`, `preserves_fl
212212
LL | asm!("", options(nomem, preserves_flags, noreturn));
213213
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
214214

215-
error[E0787]: asm options unsupported in naked functions: `nostack`, `pure`, `readonly`
215+
error[E0787]: asm options unsupported in naked functions: `pure`, `readonly`, `nostack`
216216
--> $DIR/naked-functions.rs:112:5
217217
|
218218
LL | asm!("", options(readonly, nostack), options(pure));

0 commit comments

Comments
 (0)