Skip to content

Commit ea92207

Browse files
authored
Rollup merge of #142271 - workingjubilee:fn-ptrs-have-two-different-lints, r=RalfJung
compiler: fn ptrs should hit different lints based on ABI I was looking closer at the code for linting on ABIs and realized a mistake was probably made during rebase or review. I think that for function pointers in the HIR, the lint that fires should probably depend on the ABI we encountered, e.g. if it's on the newly-deprecated set of ABIs or not. This will be slightly confusing for a little bit, but I think we can do more to reduce that confusion by switching `unsupported_fn_ptr_calling_conventions` to a hard error. r? `@RalfJung`
2 parents a0f4b13 + 8808a9c commit ea92207

File tree

9 files changed

+208
-182
lines changed

9 files changed

+208
-182
lines changed

compiler/rustc_hir_analysis/src/check/check.rs

Lines changed: 26 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -37,22 +37,23 @@ use {rustc_attr_data_structures as attrs, rustc_hir as hir};
3737
use super::compare_impl_item::check_type_bounds;
3838
use super::*;
3939

40+
fn add_abi_diag_help<T: EmissionGuarantee>(abi: ExternAbi, diag: &mut Diag<'_, T>) {
41+
if let ExternAbi::Cdecl { unwind } = abi {
42+
let c_abi = ExternAbi::C { unwind };
43+
diag.help(format!("use `extern {c_abi}` instead",));
44+
} else if let ExternAbi::Stdcall { unwind } = abi {
45+
let c_abi = ExternAbi::C { unwind };
46+
let system_abi = ExternAbi::System { unwind };
47+
diag.help(format!(
48+
"if you need `extern {abi}` on win32 and `extern {c_abi}` everywhere else, \
49+
use `extern {system_abi}`"
50+
));
51+
}
52+
}
53+
4054
pub fn check_abi(tcx: TyCtxt<'_>, hir_id: hir::HirId, span: Span, abi: ExternAbi) {
4155
// FIXME: this should be checked earlier, e.g. in `rustc_ast_lowering`, to fix
4256
// things like #86232.
43-
fn add_help<T: EmissionGuarantee>(abi: ExternAbi, diag: &mut Diag<'_, T>) {
44-
if let ExternAbi::Cdecl { unwind } = abi {
45-
let c_abi = ExternAbi::C { unwind };
46-
diag.help(format!("use `extern {c_abi}` instead",));
47-
} else if let ExternAbi::Stdcall { unwind } = abi {
48-
let c_abi = ExternAbi::C { unwind };
49-
let system_abi = ExternAbi::System { unwind };
50-
diag.help(format!(
51-
"if you need `extern {abi}` on win32 and `extern {c_abi}` everywhere else, \
52-
use `extern {system_abi}`"
53-
));
54-
}
55-
}
5657

5758
match AbiMap::from_target(&tcx.sess.target).canonize_abi(abi, false) {
5859
AbiMapping::Direct(..) => (),
@@ -63,13 +64,13 @@ pub fn check_abi(tcx: TyCtxt<'_>, hir_id: hir::HirId, span: Span, abi: ExternAbi
6364
E0570,
6465
"`{abi}` is not a supported ABI for the current target",
6566
);
66-
add_help(abi, &mut err);
67+
add_abi_diag_help(abi, &mut err);
6768
err.emit();
6869
}
6970
AbiMapping::Deprecated(..) => {
7071
tcx.node_span_lint(UNSUPPORTED_CALLING_CONVENTIONS, hir_id, span, |lint| {
7172
lint.primary_message("use of calling convention not supported on this target");
72-
add_help(abi, lint);
73+
add_abi_diag_help(abi, lint);
7374
});
7475
}
7576
}
@@ -80,7 +81,16 @@ pub fn check_abi_fn_ptr(tcx: TyCtxt<'_>, hir_id: hir::HirId, span: Span, abi: Ex
8081
// in `check_abi` above.
8182
match AbiMap::from_target(&tcx.sess.target).canonize_abi(abi, false) {
8283
AbiMapping::Direct(..) => (),
83-
AbiMapping::Deprecated(..) | AbiMapping::Invalid => {
84+
// This is not a redundant match arm: these ABIs started linting after introducing
85+
// UNSUPPORTED_FN_PTR_CALLING_CONVENTIONS already existed and we want to
86+
// avoid expanding the scope of that lint so it can move to a hard error sooner.
87+
AbiMapping::Deprecated(..) => {
88+
tcx.node_span_lint(UNSUPPORTED_CALLING_CONVENTIONS, hir_id, span, |lint| {
89+
lint.primary_message("use of calling convention not supported on this target");
90+
add_abi_diag_help(abi, lint);
91+
});
92+
}
93+
AbiMapping::Invalid => {
8494
tcx.node_span_lint(UNSUPPORTED_FN_PTR_CALLING_CONVENTIONS, hir_id, span, |lint| {
8595
lint.primary_message(format!(
8696
"the calling convention {abi} is not supported on this target"

tests/ui/abi/unsupported.aarch64.stderr

Lines changed: 28 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -114,43 +114,44 @@ LL | fn stdcall_ptr(f: extern "stdcall" fn()) {
114114
= note: for more information, see issue #130260 <https://github.com/rust-lang/rust/issues/130260>
115115

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

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

132-
warning: the calling convention "cdecl" is not supported on this target
133-
--> $DIR/unsupported.rs:129:17
132+
warning: use of calling convention not supported on this target
133+
--> $DIR/unsupported.rs:131:17
134134
|
135135
LL | fn cdecl_ptr(f: extern "cdecl" fn()) {
136136
| ^^^^^^^^^^^^^^^^^^^
137137
|
138138
= warning: this was previously accepted by the compiler but is being phased out; it will become a hard error in a future release!
139-
= note: for more information, see issue #130260 <https://github.com/rust-lang/rust/issues/130260>
139+
= note: for more information, see issue #137018 <https://github.com/rust-lang/rust/issues/137018>
140+
= help: use `extern "C"` instead
141+
= note: `#[warn(unsupported_calling_conventions)]` on by default
140142

141143
warning: use of calling convention not supported on this target
142-
--> $DIR/unsupported.rs:134:1
144+
--> $DIR/unsupported.rs:136:1
143145
|
144146
LL | extern "cdecl" {}
145147
| ^^^^^^^^^^^^^^^^^
146148
|
147149
= warning: this was previously accepted by the compiler but is being phased out; it will become a hard error in a future release!
148150
= note: for more information, see issue #137018 <https://github.com/rust-lang/rust/issues/137018>
149151
= help: use `extern "C"` instead
150-
= note: `#[warn(unsupported_calling_conventions)]` on by default
151152

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

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

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

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

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

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

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

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

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

370371
Future breakage diagnostic:
371-
warning: the calling convention "cdecl" is not supported on this target
372-
--> $DIR/unsupported.rs:129:17
372+
warning: use of calling convention not supported on this target
373+
--> $DIR/unsupported.rs:131:17
373374
|
374375
LL | fn cdecl_ptr(f: extern "cdecl" fn()) {
375376
| ^^^^^^^^^^^^^^^^^^^
376377
|
377378
= warning: this was previously accepted by the compiler but is being phased out; it will become a hard error in a future release!
378-
= note: for more information, see issue #130260 <https://github.com/rust-lang/rust/issues/130260>
379-
= note: `#[warn(unsupported_fn_ptr_calling_conventions)]` on by default
379+
= note: for more information, see issue #137018 <https://github.com/rust-lang/rust/issues/137018>
380+
= help: use `extern "C"` instead
381+
= note: `#[warn(unsupported_calling_conventions)]` on by default
380382

381383
Future breakage diagnostic:
382384
warning: use of calling convention not supported on this target
383-
--> $DIR/unsupported.rs:134:1
385+
--> $DIR/unsupported.rs:136:1
384386
|
385387
LL | extern "cdecl" {}
386388
| ^^^^^^^^^^^^^^^^^
@@ -392,7 +394,7 @@ LL | extern "cdecl" {}
392394

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

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

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

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

438440
Future breakage diagnostic:
439441
warning: use of calling convention not supported on this target
440-
--> $DIR/unsupported.rs:126:1
442+
--> $DIR/unsupported.rs:128:1
441443
|
442444
LL | extern "cdecl" fn cdecl() {}
443445
| ^^^^^^^^^^^^^^^^^^^^^^^^^

0 commit comments

Comments
 (0)