Skip to content

compiler: fn ptrs should hit different lints based on ABI #142271

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

Merged
Merged
Show file tree
Hide file tree
Changes from 2 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
40 changes: 24 additions & 16 deletions compiler/rustc_hir_analysis/src/check/check.rs
Original file line number Diff line number Diff line change
Expand Up @@ -37,22 +37,23 @@ use {rustc_attr_data_structures as attrs, rustc_hir as hir};
use super::compare_impl_item::check_type_bounds;
use super::*;

fn add_abi_diag_help<T: EmissionGuarantee>(abi: ExternAbi, diag: &mut Diag<'_, T>) {
if let ExternAbi::Cdecl { unwind } = abi {
let c_abi = ExternAbi::C { unwind };
diag.help(format!("use `extern {c_abi}` instead",));
} else if let ExternAbi::Stdcall { unwind } = abi {
let c_abi = ExternAbi::C { unwind };
let system_abi = ExternAbi::System { unwind };
diag.help(format!(
"if you need `extern {abi}` on win32 and `extern {c_abi}` everywhere else, \
use `extern {system_abi}`"
));
}
}

pub fn check_abi(tcx: TyCtxt<'_>, hir_id: hir::HirId, span: Span, abi: ExternAbi) {
// FIXME: this should be checked earlier, e.g. in `rustc_ast_lowering`, to fix
// things like #86232.
fn add_help<T: EmissionGuarantee>(abi: ExternAbi, diag: &mut Diag<'_, T>) {
if let ExternAbi::Cdecl { unwind } = abi {
let c_abi = ExternAbi::C { unwind };
diag.help(format!("use `extern {c_abi}` instead",));
} else if let ExternAbi::Stdcall { unwind } = abi {
let c_abi = ExternAbi::C { unwind };
let system_abi = ExternAbi::System { unwind };
diag.help(format!(
"if you need `extern {abi}` on win32 and `extern {c_abi}` everywhere else, \
use `extern {system_abi}`"
));
}
}

match AbiMap::from_target(&tcx.sess.target).canonize_abi(abi, false) {
AbiMapping::Direct(..) => (),
Expand All @@ -63,13 +64,13 @@ pub fn check_abi(tcx: TyCtxt<'_>, hir_id: hir::HirId, span: Span, abi: ExternAbi
E0570,
"`{abi}` is not a supported ABI for the current target",
);
add_help(abi, &mut err);
add_abi_diag_help(abi, &mut err);
err.emit();
}
AbiMapping::Deprecated(..) => {
tcx.node_span_lint(UNSUPPORTED_CALLING_CONVENTIONS, hir_id, span, |lint| {
lint.primary_message("use of calling convention not supported on this target");
add_help(abi, lint);
add_abi_diag_help(abi, lint);
});
}
}
Expand All @@ -80,7 +81,14 @@ pub fn check_abi_fn_ptr(tcx: TyCtxt<'_>, hir_id: hir::HirId, span: Span, abi: Ex
// in `check_abi` above.
match AbiMap::from_target(&tcx.sess.target).canonize_abi(abi, false) {
AbiMapping::Direct(..) => (),
AbiMapping::Deprecated(..) | AbiMapping::Invalid => {
// this is not a redundant match arm: these ABIs started linting after reviving this lint
AbiMapping::Deprecated(..) => {
tcx.node_span_lint(UNSUPPORTED_CALLING_CONVENTIONS, hir_id, span, |lint| {
lint.primary_message("use of calling convention not supported on this target");
add_abi_diag_help(abi, lint);
});
}
AbiMapping::Invalid => {
tcx.node_span_lint(UNSUPPORTED_FN_PTR_CALLING_CONVENTIONS, hir_id, span, |lint| {
lint.primary_message(format!(
"the calling convention {abi} is not supported on this target"
Expand Down
54 changes: 28 additions & 26 deletions tests/ui/abi/unsupported.aarch64.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -114,43 +114,44 @@ LL | fn stdcall_ptr(f: extern "stdcall" fn()) {
= note: for more information, see issue #130260 <https://github.com/rust-lang/rust/issues/130260>

error[E0570]: `"stdcall"` is not a supported ABI for the current target
--> $DIR/unsupported.rs:117:1
--> $DIR/unsupported.rs:119:1
|
LL | extern "stdcall" {}
| ^^^^^^^^^^^^^^^^^^^
|
= help: if you need `extern "stdcall"` on win32 and `extern "C"` everywhere else, use `extern "system"`

error[E0570]: `"stdcall-unwind"` is not a supported ABI for the current target
--> $DIR/unsupported.rs:121:1
--> $DIR/unsupported.rs:123:1
|
LL | extern "stdcall-unwind" {}
| ^^^^^^^^^^^^^^^^^^^^^^^^^^
|
= help: if you need `extern "stdcall-unwind"` on win32 and `extern "C-unwind"` everywhere else, use `extern "system-unwind"`

warning: the calling convention "cdecl" is not supported on this target
--> $DIR/unsupported.rs:129:17
warning: use of calling convention not supported on this target
--> $DIR/unsupported.rs:131:17
|
LL | fn cdecl_ptr(f: extern "cdecl" fn()) {
| ^^^^^^^^^^^^^^^^^^^
|
= 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 #130260 <https://github.com/rust-lang/rust/issues/130260>
= note: for more information, see issue #137018 <https://github.com/rust-lang/rust/issues/137018>
= help: use `extern "C"` instead
= note: `#[warn(unsupported_calling_conventions)]` on by default

warning: use of calling convention not supported on this target
--> $DIR/unsupported.rs:134:1
--> $DIR/unsupported.rs:136:1
|
LL | extern "cdecl" {}
| ^^^^^^^^^^^^^^^^^
|
= 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 #137018 <https://github.com/rust-lang/rust/issues/137018>
= help: use `extern "C"` instead
= note: `#[warn(unsupported_calling_conventions)]` on by default

warning: use of calling convention not supported on this target
--> $DIR/unsupported.rs:137:1
--> $DIR/unsupported.rs:139:1
|
LL | extern "cdecl-unwind" {}
| ^^^^^^^^^^^^^^^^^^^^^^^^
Expand All @@ -160,7 +161,7 @@ LL | extern "cdecl-unwind" {}
= help: use `extern "C-unwind"` instead

warning: the calling convention "vectorcall" is not supported on this target
--> $DIR/unsupported.rs:143:22
--> $DIR/unsupported.rs:145:22
|
LL | fn vectorcall_ptr(f: extern "vectorcall" fn()) {
| ^^^^^^^^^^^^^^^^^^^^^^^^
Expand All @@ -169,13 +170,13 @@ LL | fn vectorcall_ptr(f: extern "vectorcall" fn()) {
= note: for more information, see issue #130260 <https://github.com/rust-lang/rust/issues/130260>

error[E0570]: `"vectorcall"` is not a supported ABI for the current target
--> $DIR/unsupported.rs:148:1
--> $DIR/unsupported.rs:150:1
|
LL | extern "vectorcall" {}
| ^^^^^^^^^^^^^^^^^^^^^^

warning: the calling convention "C-cmse-nonsecure-call" is not supported on this target
--> $DIR/unsupported.rs:151:21
--> $DIR/unsupported.rs:153:21
|
LL | fn cmse_call_ptr(f: extern "C-cmse-nonsecure-call" fn()) {
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
Expand All @@ -184,7 +185,7 @@ LL | fn cmse_call_ptr(f: extern "C-cmse-nonsecure-call" fn()) {
= note: for more information, see issue #130260 <https://github.com/rust-lang/rust/issues/130260>

warning: the calling convention "C-cmse-nonsecure-entry" is not supported on this target
--> $DIR/unsupported.rs:159:22
--> $DIR/unsupported.rs:161:22
|
LL | fn cmse_entry_ptr(f: extern "C-cmse-nonsecure-entry" fn()) {
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
Expand All @@ -193,7 +194,7 @@ LL | fn cmse_entry_ptr(f: extern "C-cmse-nonsecure-entry" fn()) {
= note: for more information, see issue #130260 <https://github.com/rust-lang/rust/issues/130260>

error[E0570]: `"C-cmse-nonsecure-entry"` is not a supported ABI for the current target
--> $DIR/unsupported.rs:164:1
--> $DIR/unsupported.rs:166:1
|
LL | extern "C-cmse-nonsecure-entry" {}
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
Expand Down Expand Up @@ -255,7 +256,7 @@ LL | extern "stdcall" fn stdcall() {}
= help: if you need `extern "stdcall"` on win32 and `extern "C"` everywhere else, use `extern "system"`

warning: use of calling convention not supported on this target
--> $DIR/unsupported.rs:126:1
--> $DIR/unsupported.rs:128:1
|
LL | extern "cdecl" fn cdecl() {}
| ^^^^^^^^^^^^^^^^^^^^^^^^^
Expand All @@ -265,13 +266,13 @@ LL | extern "cdecl" fn cdecl() {}
= help: use `extern "C"` instead

error[E0570]: `"vectorcall"` is not a supported ABI for the current target
--> $DIR/unsupported.rs:141:1
--> $DIR/unsupported.rs:143:1
|
LL | extern "vectorcall" fn vectorcall() {}
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

error[E0570]: `"C-cmse-nonsecure-entry"` is not a supported ABI for the current target
--> $DIR/unsupported.rs:157:1
--> $DIR/unsupported.rs:159:1
|
LL | extern "C-cmse-nonsecure-entry" fn cmse_entry() {}
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
Expand Down Expand Up @@ -368,19 +369,20 @@ LL | fn stdcall_ptr(f: extern "stdcall" fn()) {
= note: `#[warn(unsupported_fn_ptr_calling_conventions)]` on by default

Future breakage diagnostic:
warning: the calling convention "cdecl" is not supported on this target
--> $DIR/unsupported.rs:129:17
warning: use of calling convention not supported on this target
--> $DIR/unsupported.rs:131:17
|
LL | fn cdecl_ptr(f: extern "cdecl" fn()) {
| ^^^^^^^^^^^^^^^^^^^
|
= 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 #130260 <https://github.com/rust-lang/rust/issues/130260>
= note: `#[warn(unsupported_fn_ptr_calling_conventions)]` on by default
= note: for more information, see issue #137018 <https://github.com/rust-lang/rust/issues/137018>
= help: use `extern "C"` instead
= note: `#[warn(unsupported_calling_conventions)]` on by default

Future breakage diagnostic:
warning: use of calling convention not supported on this target
--> $DIR/unsupported.rs:134:1
--> $DIR/unsupported.rs:136:1
|
LL | extern "cdecl" {}
| ^^^^^^^^^^^^^^^^^
Expand All @@ -392,7 +394,7 @@ LL | extern "cdecl" {}

Future breakage diagnostic:
warning: use of calling convention not supported on this target
--> $DIR/unsupported.rs:137:1
--> $DIR/unsupported.rs:139:1
|
LL | extern "cdecl-unwind" {}
| ^^^^^^^^^^^^^^^^^^^^^^^^
Expand All @@ -404,7 +406,7 @@ LL | extern "cdecl-unwind" {}

Future breakage diagnostic:
warning: the calling convention "vectorcall" is not supported on this target
--> $DIR/unsupported.rs:143:22
--> $DIR/unsupported.rs:145:22
|
LL | fn vectorcall_ptr(f: extern "vectorcall" fn()) {
| ^^^^^^^^^^^^^^^^^^^^^^^^
Expand All @@ -415,7 +417,7 @@ LL | fn vectorcall_ptr(f: extern "vectorcall" fn()) {

Future breakage diagnostic:
warning: the calling convention "C-cmse-nonsecure-call" is not supported on this target
--> $DIR/unsupported.rs:151:21
--> $DIR/unsupported.rs:153:21
|
LL | fn cmse_call_ptr(f: extern "C-cmse-nonsecure-call" fn()) {
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
Expand All @@ -426,7 +428,7 @@ LL | fn cmse_call_ptr(f: extern "C-cmse-nonsecure-call" fn()) {

Future breakage diagnostic:
warning: the calling convention "C-cmse-nonsecure-entry" is not supported on this target
--> $DIR/unsupported.rs:159:22
--> $DIR/unsupported.rs:161:22
|
LL | fn cmse_entry_ptr(f: extern "C-cmse-nonsecure-entry" fn()) {
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
Expand All @@ -437,7 +439,7 @@ LL | fn cmse_entry_ptr(f: extern "C-cmse-nonsecure-entry" fn()) {

Future breakage diagnostic:
warning: use of calling convention not supported on this target
--> $DIR/unsupported.rs:126:1
--> $DIR/unsupported.rs:128:1
|
LL | extern "cdecl" fn cdecl() {}
| ^^^^^^^^^^^^^^^^^^^^^^^^^
Expand Down
Loading
Loading