Skip to content

Commit c9b41af

Browse files
authored
Rollup merge of rust-lang#95544 - jam1garner:improve-naked-noreturn-diagnostic, r=tmiasko
Add error message suggestion for missing noreturn in naked function I had to google the syntax for inline asm's `noreturn` option when I got this error earlier today, so I figured I'd save others the trouble and add the syntax/fix as a suggestion in the error.
2 parents e76a694 + f793b69 commit c9b41af

File tree

2 files changed

+39
-1
lines changed

2 files changed

+39
-1
lines changed

compiler/rustc_passes/src/naked_functions.rs

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
//! Checks validity of naked functions.
22
33
use rustc_ast::{Attribute, InlineAsmOptions};
4-
use rustc_errors::struct_span_err;
4+
use rustc_errors::{struct_span_err, Applicability};
55
use rustc_hir as hir;
66
use rustc_hir::def_id::LocalDefId;
77
use rustc_hir::intravisit::{FnKind, Visitor};
@@ -274,12 +274,25 @@ impl<'tcx> CheckInlineAssembly<'tcx> {
274274
}
275275

276276
if !asm.options.contains(InlineAsmOptions::NORETURN) {
277+
let last_span = asm
278+
.operands
279+
.last()
280+
.map_or_else(|| asm.template_strs.last().unwrap().2, |op| op.1)
281+
.shrink_to_hi();
282+
277283
struct_span_err!(
278284
self.tcx.sess,
279285
span,
280286
E0787,
281287
"asm in naked functions must use `noreturn` option"
282288
)
289+
.span_suggestion(
290+
last_span,
291+
"consider specifying that the asm block is responsible \
292+
for returning from the function",
293+
String::from(", options(noreturn)"),
294+
Applicability::MachineApplicable,
295+
)
283296
.emit();
284297
}
285298
}

src/test/ui/asm/naked-functions.stderr

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -97,6 +97,11 @@ LL | |
9797
LL | | sym G,
9898
LL | | );
9999
| |_____^
100+
|
101+
help: consider specifying that the asm block is responsible for returning from the function
102+
|
103+
LL | sym G, options(noreturn),
104+
| +++++++++++++++++++
100105

101106
error[E0787]: naked functions must contain a single asm block
102107
--> $DIR/naked-functions.rs:53:1
@@ -131,18 +136,33 @@ error[E0787]: asm in naked functions must use `noreturn` option
131136
|
132137
LL | asm!("");
133138
| ^^^^^^^^
139+
|
140+
help: consider specifying that the asm block is responsible for returning from the function
141+
|
142+
LL | asm!("", options(noreturn));
143+
| +++++++++++++++++++
134144

135145
error[E0787]: asm in naked functions must use `noreturn` option
136146
--> $DIR/naked-functions.rs:85:5
137147
|
138148
LL | asm!("");
139149
| ^^^^^^^^
150+
|
151+
help: consider specifying that the asm block is responsible for returning from the function
152+
|
153+
LL | asm!("", options(noreturn));
154+
| +++++++++++++++++++
140155

141156
error[E0787]: asm in naked functions must use `noreturn` option
142157
--> $DIR/naked-functions.rs:87:5
143158
|
144159
LL | asm!("");
145160
| ^^^^^^^^
161+
|
162+
help: consider specifying that the asm block is responsible for returning from the function
163+
|
164+
LL | asm!("", options(noreturn));
165+
| +++++++++++++++++++
146166

147167
error[E0787]: naked functions must contain a single asm block
148168
--> $DIR/naked-functions.rs:81:1
@@ -198,6 +218,11 @@ error[E0787]: asm in naked functions must use `noreturn` option
198218
|
199219
LL | asm!("", options(readonly, nostack), options(pure));
200220
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
221+
|
222+
help: consider specifying that the asm block is responsible for returning from the function
223+
|
224+
LL | asm!("", options(noreturn), options(readonly, nostack), options(pure));
225+
| +++++++++++++++++++
201226

202227
error[E0787]: asm options unsupported in naked functions: `may_unwind`
203228
--> $DIR/naked-functions.rs:118:5

0 commit comments

Comments
 (0)