Skip to content

Commit

Permalink
[WIP COMMIT, EXPECT OVERWRITING] lint: change help for pointers to dy…
Browse files Browse the repository at this point in the history
…n types in FFI
  • Loading branch information
niacdoial committed Oct 17, 2024
1 parent 2aa26d8 commit 78fb000
Show file tree
Hide file tree
Showing 4 changed files with 36 additions and 13 deletions.
6 changes: 5 additions & 1 deletion compiler/rustc_lint/messages.ftl
Original file line number Diff line number Diff line change
Expand Up @@ -360,7 +360,6 @@ lint_improper_ctypes_128bit = 128-bit integers don't currently have a known stab
lint_improper_ctypes_array_help = consider passing a pointer to the array
lint_improper_ctypes_array_reason = passing raw arrays by value is not FFI-safe
lint_improper_ctypes_box = box cannot be represented as a single pointer
lint_improper_ctypes_char_help = consider using `u32` or `libc::wchar_t` instead
Expand Down Expand Up @@ -389,6 +388,7 @@ lint_improper_ctypes_opaque = opaque types have no C equivalent
lint_improper_ctypes_pat_help = consider using the base type instead
lint_improper_ctypes_pat_reason = pattern types have no C equivalent
lint_improper_ctypes_slice_help = consider using a raw pointer instead
lint_improper_ctypes_slice_reason = slices have no C equivalent
Expand All @@ -415,6 +415,10 @@ lint_improper_ctypes_union_layout_help = consider adding a `#[repr(C)]` or `#[re
lint_improper_ctypes_union_layout_reason = this union has unspecified layout
lint_improper_ctypes_union_non_exhaustive = this union is non-exhaustive
lint_improper_ctypes_unsized_box = this box for an unsized rust type contains metadata, which makes it incompatible with a C pointer
lint_improper_ctypes_unsized_ptr = this pointer to an unsized rust type contains metadata, which makes it incompatible with a C pointer
lint_improper_ctypes_unsized_ref = this reference to an unsized rust type contains metadata, which makes it incompatible with a C pointer
lint_incomplete_include =
include macro expected single expression in source
Expand Down
37 changes: 28 additions & 9 deletions compiler/rustc_lint/src/types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -901,15 +901,24 @@ impl<'a, 'tcx> ImproperCTypesVisitor<'a, 'tcx> {

match *ty.kind() {
ty::Adt(def, args) => {
if let Some(boxed) = ty.boxed_ty()
if let Some(inner_ty) = ty.boxed_ty()
&& matches!(self.mode, CItemKind::Definition)
{
if boxed.is_sized(tcx, self.cx.param_env) {
if inner_ty.is_sized(tcx, self.cx.param_env)
|| matches!(inner_ty.kind(), ty::Foreign(..))
{
let _probe = self.check_type_for_ffi(acc, inner_ty);
return FfiSafe;
} else {
//let help = match inner_ty.kind() {
// ty::Dynamic(..) => Some(fluent::lint_improper_ctypes_unsized_help_dyn),
// ty::Closure(..) => Some(fluent::lint_improper_ctypes_unsized_help_closure),
// ty::Slice(..) => Some(fluent::lint_improper_ctypes_unsized_help_slice),
// _ => None,
//};
return FfiUnsafe {
ty,
reason: fluent::lint_improper_ctypes_box,
reason: fluent::lint_improper_ctypes_unsized_box,
help: None,
};
}
Expand Down Expand Up @@ -1057,6 +1066,7 @@ impl<'a, 'tcx> ImproperCTypesVisitor<'a, 'tcx> {
help: Some(fluent::lint_improper_ctypes_slice_help),
},


ty::Dynamic(..) => {
FfiUnsafe { ty, reason: fluent::lint_improper_ctypes_dyn, help: None }
}
Expand All @@ -1073,13 +1083,22 @@ impl<'a, 'tcx> ImproperCTypesVisitor<'a, 'tcx> {
help: Some(fluent::lint_improper_ctypes_tuple_help),
},

ty::RawPtr(ty, _) | ty::Ref(_, ty, _)
if {
matches!(self.mode, CItemKind::Definition)
&& ty.is_sized(self.cx.tcx, self.cx.param_env)
} =>
ty::RawPtr(inner_ty, _) | ty::Ref(_, inner_ty, _)
if matches!(self.mode, CItemKind::Definition) =>
{
FfiSafe
if inner_ty.is_sized(tcx, self.cx.param_env)
|| matches!(inner_ty.kind(), ty::Foreign(..))
{
let _probe = self.check_type_for_ffi(acc, inner_ty);
FfiSafe
} else {
let reason = match ty.kind() {
ty::RawPtr(..) => fluent::lint_improper_ctypes_unsized_ptr,
ty::Ref(..) => fluent::lint_improper_ctypes_unsized_ref,
_ => unreachable!(),
};
FfiUnsafe { ty, reason, help: None }
}
}

ty::RawPtr(ty, _)
Expand Down
2 changes: 1 addition & 1 deletion tests/ui/lint/extern-C-fnptr-lints-slices.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
// It's an improper ctype (a slice) arg in an extern "C" fnptr.

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


fn main() {}
4 changes: 2 additions & 2 deletions tests/ui/lint/extern-C-fnptr-lints-slices.stderr
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
error: `extern` fn uses type `[u8]`, which is not FFI-safe
error: `extern` fn uses type `&[u8]`, which is not FFI-safe
--> $DIR/extern-C-fnptr-lints-slices.rs:5:14
|
LL | pub type F = extern "C" fn(&[u8]);
| ^^^^^^^^^^^^^^^^^^^^ not FFI-safe
|
= help: consider using a raw pointer instead
= note: slices have no C equivalent
= note: this reference to an unsized rust type contains metadata, which makes it incompatible with a C pointer
note: the lint level is defined here
--> $DIR/extern-C-fnptr-lints-slices.rs:1:8
|
Expand Down

0 comments on commit 78fb000

Please sign in to comment.