Skip to content

Commit

Permalink
Add error message suggestion for missing noreturn in naked function
Browse files Browse the repository at this point in the history
  • Loading branch information
jam1garner committed Mar 31, 2022
1 parent 0331491 commit b657cb5
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 1 deletion.
15 changes: 14 additions & 1 deletion compiler/rustc_passes/src/naked_functions.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
//! Checks validity of naked functions.

use rustc_ast::{Attribute, InlineAsmOptions};
use rustc_errors::struct_span_err;
use rustc_errors::{struct_span_err, Applicability};
use rustc_hir as hir;
use rustc_hir::def_id::LocalDefId;
use rustc_hir::intravisit::{FnKind, Visitor};
Expand Down Expand Up @@ -274,12 +274,25 @@ impl<'tcx> CheckInlineAssembly<'tcx> {
}

if !asm.options.contains(InlineAsmOptions::NORETURN) {
let last_span = asm
.operands
.last()
.map_or_else(|| asm.template_strs.last().unwrap().2, |op| op.1)
.shrink_to_hi();

struct_span_err!(
self.tcx.sess,
span,
E0787,
"asm in naked functions must use `noreturn` option"
)
.span_suggestion(
last_span,
"consider specifying that the asm block is responsible \
for returning, if desired",
String::from(", options(noreturn)"),
Applicability::MachineApplicable,
)
.emit();
}
}
Expand Down
25 changes: 25 additions & 0 deletions src/test/ui/asm/naked-functions.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,11 @@ LL | |
LL | | sym G,
LL | | );
| |_____^
|
help: consider specifying that the asm block is responsible for returning, if desired
|
LL | sym G, options(noreturn),
| +++++++++++++++++++

error[E0787]: naked functions must contain a single asm block
--> $DIR/naked-functions.rs:53:1
Expand Down Expand Up @@ -131,18 +136,33 @@ error[E0787]: asm in naked functions must use `noreturn` option
|
LL | asm!("");
| ^^^^^^^^
|
help: consider specifying that the asm block is responsible for returning, if desired
|
LL | asm!("", options(noreturn));
| +++++++++++++++++++

error[E0787]: asm in naked functions must use `noreturn` option
--> $DIR/naked-functions.rs:85:5
|
LL | asm!("");
| ^^^^^^^^
|
help: consider specifying that the asm block is responsible for returning, if desired
|
LL | asm!("", options(noreturn));
| +++++++++++++++++++

error[E0787]: asm in naked functions must use `noreturn` option
--> $DIR/naked-functions.rs:87:5
|
LL | asm!("");
| ^^^^^^^^
|
help: consider specifying that the asm block is responsible for returning, if desired
|
LL | asm!("", options(noreturn));
| +++++++++++++++++++

error[E0787]: naked functions must contain a single asm block
--> $DIR/naked-functions.rs:81:1
Expand Down Expand Up @@ -198,6 +218,11 @@ error[E0787]: asm in naked functions must use `noreturn` option
|
LL | asm!("", options(readonly, nostack), options(pure));
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
|
help: consider specifying that the asm block is responsible for returning, if desired
|
LL | asm!("", options(noreturn), options(readonly, nostack), options(pure));
| +++++++++++++++++++

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

0 comments on commit b657cb5

Please sign in to comment.