Skip to content

Commit 78fb000

Browse files
committed
[WIP COMMIT, EXPECT OVERWRITING] lint: change help for pointers to dyn types in FFI
1 parent 2aa26d8 commit 78fb000

File tree

4 files changed

+36
-13
lines changed

4 files changed

+36
-13
lines changed

compiler/rustc_lint/messages.ftl

+5-1
Original file line numberDiff line numberDiff line change
@@ -360,7 +360,6 @@ lint_improper_ctypes_128bit = 128-bit integers don't currently have a known stab
360360
lint_improper_ctypes_array_help = consider passing a pointer to the array
361361
362362
lint_improper_ctypes_array_reason = passing raw arrays by value is not FFI-safe
363-
lint_improper_ctypes_box = box cannot be represented as a single pointer
364363
365364
lint_improper_ctypes_char_help = consider using `u32` or `libc::wchar_t` instead
366365
@@ -389,6 +388,7 @@ lint_improper_ctypes_opaque = opaque types have no C equivalent
389388
lint_improper_ctypes_pat_help = consider using the base type instead
390389
391390
lint_improper_ctypes_pat_reason = pattern types have no C equivalent
391+
392392
lint_improper_ctypes_slice_help = consider using a raw pointer instead
393393
394394
lint_improper_ctypes_slice_reason = slices have no C equivalent
@@ -415,6 +415,10 @@ lint_improper_ctypes_union_layout_help = consider adding a `#[repr(C)]` or `#[re
415415
lint_improper_ctypes_union_layout_reason = this union has unspecified layout
416416
lint_improper_ctypes_union_non_exhaustive = this union is non-exhaustive
417417
418+
lint_improper_ctypes_unsized_box = this box for an unsized rust type contains metadata, which makes it incompatible with a C pointer
419+
lint_improper_ctypes_unsized_ptr = this pointer to an unsized rust type contains metadata, which makes it incompatible with a C pointer
420+
lint_improper_ctypes_unsized_ref = this reference to an unsized rust type contains metadata, which makes it incompatible with a C pointer
421+
418422
lint_incomplete_include =
419423
include macro expected single expression in source
420424

compiler/rustc_lint/src/types.rs

+28-9
Original file line numberDiff line numberDiff line change
@@ -901,15 +901,24 @@ impl<'a, 'tcx> ImproperCTypesVisitor<'a, 'tcx> {
901901

902902
match *ty.kind() {
903903
ty::Adt(def, args) => {
904-
if let Some(boxed) = ty.boxed_ty()
904+
if let Some(inner_ty) = ty.boxed_ty()
905905
&& matches!(self.mode, CItemKind::Definition)
906906
{
907-
if boxed.is_sized(tcx, self.cx.param_env) {
907+
if inner_ty.is_sized(tcx, self.cx.param_env)
908+
|| matches!(inner_ty.kind(), ty::Foreign(..))
909+
{
910+
let _probe = self.check_type_for_ffi(acc, inner_ty);
908911
return FfiSafe;
909912
} else {
913+
//let help = match inner_ty.kind() {
914+
// ty::Dynamic(..) => Some(fluent::lint_improper_ctypes_unsized_help_dyn),
915+
// ty::Closure(..) => Some(fluent::lint_improper_ctypes_unsized_help_closure),
916+
// ty::Slice(..) => Some(fluent::lint_improper_ctypes_unsized_help_slice),
917+
// _ => None,
918+
//};
910919
return FfiUnsafe {
911920
ty,
912-
reason: fluent::lint_improper_ctypes_box,
921+
reason: fluent::lint_improper_ctypes_unsized_box,
913922
help: None,
914923
};
915924
}
@@ -1057,6 +1066,7 @@ impl<'a, 'tcx> ImproperCTypesVisitor<'a, 'tcx> {
10571066
help: Some(fluent::lint_improper_ctypes_slice_help),
10581067
},
10591068

1069+
10601070
ty::Dynamic(..) => {
10611071
FfiUnsafe { ty, reason: fluent::lint_improper_ctypes_dyn, help: None }
10621072
}
@@ -1073,13 +1083,22 @@ impl<'a, 'tcx> ImproperCTypesVisitor<'a, 'tcx> {
10731083
help: Some(fluent::lint_improper_ctypes_tuple_help),
10741084
},
10751085

1076-
ty::RawPtr(ty, _) | ty::Ref(_, ty, _)
1077-
if {
1078-
matches!(self.mode, CItemKind::Definition)
1079-
&& ty.is_sized(self.cx.tcx, self.cx.param_env)
1080-
} =>
1086+
ty::RawPtr(inner_ty, _) | ty::Ref(_, inner_ty, _)
1087+
if matches!(self.mode, CItemKind::Definition) =>
10811088
{
1082-
FfiSafe
1089+
if inner_ty.is_sized(tcx, self.cx.param_env)
1090+
|| matches!(inner_ty.kind(), ty::Foreign(..))
1091+
{
1092+
let _probe = self.check_type_for_ffi(acc, inner_ty);
1093+
FfiSafe
1094+
} else {
1095+
let reason = match ty.kind() {
1096+
ty::RawPtr(..) => fluent::lint_improper_ctypes_unsized_ptr,
1097+
ty::Ref(..) => fluent::lint_improper_ctypes_unsized_ref,
1098+
_ => unreachable!(),
1099+
};
1100+
FfiUnsafe { ty, reason, help: None }
1101+
}
10831102
}
10841103

10851104
ty::RawPtr(ty, _)

tests/ui/lint/extern-C-fnptr-lints-slices.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
// It's an improper ctype (a slice) arg in an extern "C" fnptr.
44

55
pub type F = extern "C" fn(&[u8]);
6-
//~^ ERROR: `extern` fn uses type `[u8]`, which is not FFI-safe
6+
//~^ ERROR: `extern` fn uses type `&[u8]`, which is not FFI-safe
77

88

99
fn main() {}

tests/ui/lint/extern-C-fnptr-lints-slices.stderr

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
1-
error: `extern` fn uses type `[u8]`, which is not FFI-safe
1+
error: `extern` fn uses type `&[u8]`, which is not FFI-safe
22
--> $DIR/extern-C-fnptr-lints-slices.rs:5:14
33
|
44
LL | pub type F = extern "C" fn(&[u8]);
55
| ^^^^^^^^^^^^^^^^^^^^ not FFI-safe
66
|
77
= help: consider using a raw pointer instead
8-
= note: slices have no C equivalent
8+
= note: this reference to an unsized rust type contains metadata, which makes it incompatible with a C pointer
99
note: the lint level is defined here
1010
--> $DIR/extern-C-fnptr-lints-slices.rs:1:8
1111
|

0 commit comments

Comments
 (0)