Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Move naked function ABI check to its own lint #87772

Merged
merged 2 commits into from
Aug 8, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
34 changes: 33 additions & 1 deletion compiler/rustc_lint_defs/src/builtin.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2691,6 +2691,38 @@ declare_lint! {
"detects deprecation attributes with no effect",
}

declare_lint! {
/// The `undefined_naked_function_abi` lint detects naked function definitions that
/// either do not specify an ABI or specify the Rust ABI.
///
/// ### Example
///
/// ```rust
/// #![feature(naked_functions)]
/// #![feature(asm)]
///
/// #[naked]
/// pub fn default_abi() -> u32 {
/// unsafe { asm!("", options(noreturn)); }
/// }
///
/// #[naked]
/// pub extern "Rust" fn rust_abi() -> u32 {
/// unsafe { asm!("", options(noreturn)); }
/// }
/// ```
///
/// {{produces}}
///
/// ### Explanation
///
/// The Rust ABI is currently undefined. Therefore, naked functions should
/// specify a non-Rust ABI.
pub UNDEFINED_NAKED_FUNCTION_ABI,
Warn,
"undefined naked function ABI"
}

declare_lint! {
/// The `unsupported_naked_functions` lint detects naked function
/// definitions that are unsupported but were previously accepted.
Expand All @@ -2701,7 +2733,7 @@ declare_lint! {
/// #![feature(naked_functions)]
///
/// #[naked]
/// pub fn f() -> u32 {
/// pub extern "C" fn f() -> u32 {
/// 42
/// }
/// ```
Expand Down
3 changes: 2 additions & 1 deletion compiler/rustc_passes/src/naked_functions.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ use rustc_hir::intravisit::{ErasedMap, FnKind, NestedVisitorMap, Visitor};
use rustc_hir::{ExprKind, HirId, InlineAsmOperand, StmtKind};
use rustc_middle::ty::query::Providers;
use rustc_middle::ty::TyCtxt;
use rustc_session::lint::builtin::UNDEFINED_NAKED_FUNCTION_ABI;
use rustc_session::lint::builtin::UNSUPPORTED_NAKED_FUNCTIONS;
use rustc_span::symbol::sym;
use rustc_span::Span;
Expand Down Expand Up @@ -87,7 +88,7 @@ fn check_inline(tcx: TyCtxt<'_>, hir_id: HirId, attrs: &[Attribute]) {
/// Checks that function uses non-Rust ABI.
fn check_abi(tcx: TyCtxt<'_>, hir_id: HirId, abi: Abi, fn_ident_span: Span) {
if abi == Abi::Rust {
tcx.struct_span_lint_hir(UNSUPPORTED_NAKED_FUNCTIONS, hir_id, fn_ident_span, |lint| {
tcx.struct_span_lint_hir(UNDEFINED_NAKED_FUNCTION_ABI, hir_id, fn_ident_span, |lint| {
lint.build("Rust ABI is unsupported in naked functions").emit();
});
}
Expand Down
2 changes: 0 additions & 2 deletions src/test/ui/asm/naked-functions.rs
Original file line number Diff line number Diff line change
Expand Up @@ -134,14 +134,12 @@ unsafe extern "C" fn invalid_options_continued() {
#[naked]
pub unsafe fn default_abi() {
//~^ WARN Rust ABI is unsupported in naked functions
//~| WARN this was previously accepted
asm!("", options(noreturn));
}

#[naked]
pub unsafe extern "Rust" fn rust_abi() {
//~^ WARN Rust ABI is unsupported in naked functions
//~| WARN this was previously accepted
asm!("", options(noreturn));
}

Expand Down
20 changes: 8 additions & 12 deletions src/test/ui/asm/naked-functions.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -284,20 +284,16 @@ warning: Rust ABI is unsupported in naked functions
LL | pub unsafe fn default_abi() {
| ^^^^^^^^^^^
|
= warning: this was previously accepted by the compiler but is being phased out; it will become a hard error in a future release!
= note: for more information, see issue #32408 <https://github.com/rust-lang/rust/issues/32408>
= note: `#[warn(undefined_naked_function_abi)]` on by default

warning: Rust ABI is unsupported in naked functions
--> $DIR/naked-functions.rs:142:29
--> $DIR/naked-functions.rs:141:29
|
LL | pub unsafe extern "Rust" fn rust_abi() {
| ^^^^^^^^
|
= warning: this was previously accepted by the compiler but is being phased out; it will become a hard error in a future release!
= note: for more information, see issue #32408 <https://github.com/rust-lang/rust/issues/32408>

warning: naked functions cannot be inlined
--> $DIR/naked-functions.rs:177:1
--> $DIR/naked-functions.rs:175:1
|
LL | #[inline]
| ^^^^^^^^^
Expand All @@ -306,7 +302,7 @@ LL | #[inline]
= note: for more information, see issue #32408 <https://github.com/rust-lang/rust/issues/32408>

warning: naked functions cannot be inlined
--> $DIR/naked-functions.rs:185:1
--> $DIR/naked-functions.rs:183:1
|
LL | #[inline(always)]
| ^^^^^^^^^^^^^^^^^
Expand All @@ -315,7 +311,7 @@ LL | #[inline(always)]
= note: for more information, see issue #32408 <https://github.com/rust-lang/rust/issues/32408>

warning: naked functions cannot be inlined
--> $DIR/naked-functions.rs:193:1
--> $DIR/naked-functions.rs:191:1
|
LL | #[inline(never)]
| ^^^^^^^^^^^^^^^^
Expand All @@ -324,7 +320,7 @@ LL | #[inline(never)]
= note: for more information, see issue #32408 <https://github.com/rust-lang/rust/issues/32408>

warning: naked functions cannot be inlined
--> $DIR/naked-functions.rs:201:1
--> $DIR/naked-functions.rs:199:1
|
LL | #[inline]
| ^^^^^^^^^
Expand All @@ -333,7 +329,7 @@ LL | #[inline]
= note: for more information, see issue #32408 <https://github.com/rust-lang/rust/issues/32408>

warning: naked functions cannot be inlined
--> $DIR/naked-functions.rs:204:1
--> $DIR/naked-functions.rs:202:1
|
LL | #[inline(always)]
| ^^^^^^^^^^^^^^^^^
Expand All @@ -342,7 +338,7 @@ LL | #[inline(always)]
= note: for more information, see issue #32408 <https://github.com/rust-lang/rust/issues/32408>

warning: naked functions cannot be inlined
--> $DIR/naked-functions.rs:207:1
--> $DIR/naked-functions.rs:205:1
|
LL | #[inline(never)]
| ^^^^^^^^^^^^^^^^
Expand Down