1
Fork 0

lint ImproperCTypes: message tweaks and refactoring from code review

This commit is contained in:
niacdoial 2024-12-03 23:57:56 +01:00
parent 9b59dd8178
commit 8b6289f6ae
7 changed files with 52 additions and 49 deletions

View file

@ -391,7 +391,7 @@ 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_sized_ptr_to_unsafe_type =
this reference (`{$ty}`) is can safely be translated into a C pointer, but `{$inner_ty}` itself is not FFI-safe
this reference (`{$ty}`) is ABI-compatible with a C pointer, but `{$inner_ty}` itself does not have a C layout
lint_improper_ctypes_slice_help = consider using a raw pointer to the slice's first element (and a length) instead
@ -419,9 +419,9 @@ 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_improper_ctypes_unsized_box = this box for an unsized type contains metadata, which makes it incompatible with a C pointer
lint_improper_ctypes_unsized_ptr = this pointer to an unsized type contains metadata, which makes it incompatible with a C pointer
lint_improper_ctypes_unsized_ref = this reference to an unsized type contains metadata, which makes it incompatible with a C pointer
lint_incomplete_include =
include macro expected single expression in source

View file

@ -743,10 +743,10 @@ enum FfiResult<'tcx> {
/// Determine if a type is sized or not, and wether it affects references/pointers/boxes to it
#[derive(Clone, Copy)]
enum TypeSizedness {
/// sized type (pointers are C-compatible)
Sized,
/// type of definite size (pointers are C-compatible)
Definite,
/// unsized type because it includes an opaque/foreign type (pointers are C-compatible)
UnsizedBecauseForeign,
UnsizedWithExternType,
/// unsized type for other reasons (slice, string, dyn Trait, closure, ...) (pointers are not C-compatible)
UnsizedWithMetadata,
}
@ -757,13 +757,13 @@ fn get_type_sizedness<'tcx, 'a>(cx: &'a LateContext<'tcx>, ty: Ty<'tcx>) -> Type
let tcx = cx.tcx;
if ty.is_sized(tcx, cx.typing_env()) {
TypeSizedness::Sized
TypeSizedness::Definite
} else {
match ty.kind() {
ty::Slice(_) => TypeSizedness::UnsizedWithMetadata,
ty::Str => TypeSizedness::UnsizedWithMetadata,
ty::Dynamic(..) => TypeSizedness::UnsizedWithMetadata,
ty::Foreign(..) => TypeSizedness::UnsizedBecauseForeign,
ty::Foreign(..) => TypeSizedness::UnsizedWithExternType,
// While opaque types are checked for earlier, if a projection in a struct field
// normalizes to an opaque type, then it will reach this branch.
ty::Alias(ty::Opaque, ..) => todo!("We... don't know enough about this type yet?"),
@ -797,8 +797,8 @@ fn get_type_sizedness<'tcx, 'a>(cx: &'a LateContext<'tcx>, ty: Ty<'tcx>) -> Type
.unwrap_or(field_ty);
match get_type_sizedness(cx, field_ty) {
s @ (TypeSizedness::UnsizedWithMetadata
| TypeSizedness::UnsizedBecauseForeign) => s,
TypeSizedness::Sized => {
| TypeSizedness::UnsizedWithExternType) => s,
TypeSizedness::Definite => {
bug!("failed to find the reason why struct `{:?}` is unsized", ty)
}
}
@ -816,16 +816,16 @@ fn get_type_sizedness<'tcx, 'a>(cx: &'a LateContext<'tcx>, ty: Ty<'tcx>) -> Type
.unwrap_or(field_ty);
match get_type_sizedness(cx, field_ty) {
s @ (TypeSizedness::UnsizedWithMetadata
| TypeSizedness::UnsizedBecauseForeign) => s,
TypeSizedness::Sized => {
| TypeSizedness::UnsizedWithExternType) => s,
TypeSizedness::Definite => {
bug!("failed to find the reason why tuple `{:?}` is unsized", ty)
}
}
}
t => {
ty => {
bug!(
"we shouldn't be trying to determine if this is unsized for a reason or another: `{:?}`",
t
ty
)
}
}
@ -1135,7 +1135,7 @@ impl<'a, 'tcx> ImproperCTypesVisitor<'a, 'tcx> {
match *ty.kind() {
ty::Adt(def, args) => {
if let Some(inner_ty) = ty.boxed_ty() {
if let TypeSizedness::UnsizedBecauseForeign | TypeSizedness::Sized =
if let TypeSizedness::UnsizedWithExternType | TypeSizedness::Definite =
get_type_sizedness(self.cx, inner_ty)
{
// discussion on declaration vs definition:
@ -1340,24 +1340,27 @@ impl<'a, 'tcx> ImproperCTypesVisitor<'a, 'tcx> {
}
ty::RawPtr(inner_ty, _) | ty::Ref(_, inner_ty, _) => {
if let TypeSizedness::UnsizedBecauseForeign | TypeSizedness::Sized =
if let TypeSizedness::UnsizedWithExternType | TypeSizedness::Definite =
get_type_sizedness(self.cx, inner_ty)
{
// there's a nuance on what this lint should do for function definitions
// (`extern "C" fn fn_name(...) {...}`) versus declarations (`extern "C" {fn fn_name(...);}`).
// (this is touched upon in https://github.com/rust-lang/rust/issues/66220
// and https://github.com/rust-lang/rust/pull/72700)
// there's a nuance on what this lint should do for
// function definitions (`extern "C" fn fn_name(...) {...}`)
// versus declarations (`unsafe extern "C" {fn fn_name(...);}`).
// This is touched upon in https://github.com/rust-lang/rust/issues/66220
// and https://github.com/rust-lang/rust/pull/72700
//
// The big question is: what does "ABI safety" mean? if you have something translated to a C pointer
// (which has a stable layout) but points to FFI-unsafe type, is it safe?
// on one hand, the function's ABI will match that of a similar C-declared function API,
// on the other, dereferencing the pointer in not-rust will be painful.
// In this code, the opinion is split between function declarations and function definitions.
// On one hand, the function's ABI will match that of a similar C-declared function API,
// on the other, dereferencing the pointer on the other side of the FFI boundary will be painful.
// In this code, the opinion on is split between function declarations and function definitions,
// with the idea that at least one side of the FFI boundary needs to treat the pointee as an opaque type.
// For declarations, we see this as unsafe, but for definitions, we see this as safe.
// This is mostly because, for extern function declarations, the actual definition of the function is written somewhere else,
// so the fact that a pointer's pointee should be treated as opaque to one side or the other can be explicitely written out.
// For extern function definitions, however, both callee and some callers can be written in rust,
// so developers need to keep as much typing information as possible.
//
// For extern function declarations, the actual definition of the function is written somewhere else,
// meaning the declaration is free to express this opaqueness with an extern type (opaque caller-side) or a std::ffi::c_void (opaque callee-side)
// For extern function definitions, however, in the case where the type is opaque caller-side, it is not opaque callee-side,
// and having the full type information is necessary to compile the function.
if matches!(self.mode, CItemKind::Definition) {
return FfiSafe;
} else if matches!(ty.kind(), ty::RawPtr(..))

View file

@ -6,7 +6,7 @@ LL | pub type F = extern "C" fn(&[u8]);
|
= note: the function pointer to `for<'a> extern "C" fn(&'a [u8])` is FFI-unsafe due to `&[u8]`
= help: consider using a raw pointer to the slice's first element (and a length) instead
= note: this reference to an unsized rust type contains metadata, which makes it incompatible with a C pointer
= note: this reference to an unsized 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
|

View file

@ -4,7 +4,7 @@ error: `extern` block uses type `Qux`, which is not FFI-safe
LL | fn lint_me() -> A<()>;
| ^^^^^ not FFI-safe
|
= note: this reference (`&Qux`) is can safely be translated into a C pointer, but `Qux` itself is not FFI-safe
= note: this reference (`&Qux`) is ABI-compatible with a C pointer, but `Qux` itself does not have a C layout
= note: opaque types have no C equivalent
note: the lint level is defined here
--> $DIR/lint-ctypes-73249-2.rs:2:9

View file

@ -19,7 +19,7 @@ LL | fn take_cstr_ref(s: &CStr);
| ^^^^^ not FFI-safe
|
= help: consider passing a `*const std::ffi::c_char` instead, and use `CStr::as_ptr()`
= note: this reference to an unsized rust type contains metadata, which makes it incompatible with a C pointer
= note: this reference to an unsized type contains metadata, which makes it incompatible with a C pointer
error: `extern` block uses type `CString`, which is not FFI-safe
--> $DIR/lint-ctypes-cstr.rs:13:24
@ -36,7 +36,7 @@ error: `extern` block uses type `CString`, which is not FFI-safe
LL | fn take_cstring_ref(s: &CString);
| ^^^^^^^^ not FFI-safe
|
= note: this reference (`&CString`) is can safely be translated into a C pointer, but `CString` itself is not FFI-safe
= note: this reference (`&CString`) is ABI-compatible with a C pointer, but `CString` itself does not have a C layout
= help: consider passing a `*const std::ffi::c_char` instead, and use `CStr::as_ptr()`
= note: `CStr`/`CString` do not have a guaranteed layout
@ -46,7 +46,7 @@ error: `extern` block uses type `CString`, which is not FFI-safe
LL | fn no_special_help_for_mut_cstring(s: *mut CString);
| ^^^^^^^^^^^^ not FFI-safe
|
= note: this reference (`*mut CString`) is can safely be translated into a C pointer, but `CString` itself is not FFI-safe
= note: this reference (`*mut CString`) is ABI-compatible with a C pointer, but `CString` itself does not have a C layout
= help: consider adding a `#[repr(C)]` or `#[repr(transparent)]` attribute to this struct
= note: this struct has unspecified layout
@ -56,7 +56,7 @@ error: `extern` block uses type `CString`, which is not FFI-safe
LL | fn no_special_help_for_mut_cstring_ref(s: &mut CString);
| ^^^^^^^^^^^^ not FFI-safe
|
= note: this reference (`&mut CString`) is can safely be translated into a C pointer, but `CString` itself is not FFI-safe
= note: this reference (`&mut CString`) is ABI-compatible with a C pointer, but `CString` itself does not have a C layout
= help: consider adding a `#[repr(C)]` or `#[repr(transparent)]` attribute to this struct
= note: this struct has unspecified layout
@ -67,7 +67,7 @@ LL | extern "C" fn rust_take_cstr_ref(s: &CStr) {}
| ^^^^^ not FFI-safe
|
= help: consider passing a `*const std::ffi::c_char` instead, and use `CStr::as_ptr()`
= note: this reference to an unsized rust type contains metadata, which makes it incompatible with a C pointer
= note: this reference to an unsized type contains metadata, which makes it incompatible with a C pointer
note: the lint level is defined here
--> $DIR/lint-ctypes-cstr.rs:2:26
|

View file

@ -5,7 +5,7 @@ LL | pub extern "C" fn slice_type(p: &[u32]) { }
| ^^^^^^ not FFI-safe
|
= help: consider using a raw pointer to the slice's first element (and a length) instead
= note: this reference to an unsized rust type contains metadata, which makes it incompatible with a C pointer
= note: this reference to an unsized type contains metadata, which makes it incompatible with a C pointer
note: the lint level is defined here
--> $DIR/lint-ctypes-fn.rs:2:9
|
@ -19,7 +19,7 @@ LL | pub extern "C" fn str_type(p: &str) { }
| ^^^^ not FFI-safe
|
= help: consider using `*const u8` and a length instead
= note: this reference to an unsized rust type contains metadata, which makes it incompatible with a C pointer
= note: this reference to an unsized type contains metadata, which makes it incompatible with a C pointer
error: `extern` fn uses type `Box<[u8]>`, which is not FFI-safe
--> $DIR/lint-ctypes-fn.rs:80:34
@ -28,7 +28,7 @@ LL | pub extern "C" fn boxed_slice(p: Box<[u8]>) { }
| ^^^^^^^^^ not FFI-safe
|
= help: consider using a raw pointer to the slice's first element (and a length) instead
= note: this box for an unsized rust type contains metadata, which makes it incompatible with a C pointer
= note: this box for an unsized type contains metadata, which makes it incompatible with a C pointer
error: `extern` fn uses type `Box<str>`, which is not FFI-safe
--> $DIR/lint-ctypes-fn.rs:83:35
@ -37,7 +37,7 @@ LL | pub extern "C" fn boxed_string(p: Box<str>) { }
| ^^^^^^^^ not FFI-safe
|
= help: consider using `*const u8` and a length instead
= note: this box for an unsized rust type contains metadata, which makes it incompatible with a C pointer
= note: this box for an unsized type contains metadata, which makes it incompatible with a C pointer
error: `extern` fn uses type `Box<dyn Trait>`, which is not FFI-safe
--> $DIR/lint-ctypes-fn.rs:86:34
@ -45,7 +45,7 @@ error: `extern` fn uses type `Box<dyn Trait>`, which is not FFI-safe
LL | pub extern "C" fn boxed_trait(p: Box<dyn Trait>) { }
| ^^^^^^^^^^^^^^ not FFI-safe
|
= note: this box for an unsized rust type contains metadata, which makes it incompatible with a C pointer
= note: this box for an unsized type contains metadata, which makes it incompatible with a C pointer
error: `extern` fn uses type `char`, which is not FFI-safe
--> $DIR/lint-ctypes-fn.rs:89:32
@ -158,7 +158,7 @@ LL | pub extern "C" fn transparent_str(p: TransparentStr) { }
| ^^^^^^^^^^^^^^ not FFI-safe
|
= help: consider using `*const u8` and a length instead
= note: this reference to an unsized rust type contains metadata, which makes it incompatible with a C pointer
= note: this reference to an unsized type contains metadata, which makes it incompatible with a C pointer
error: `extern` fn uses type `PhantomData<bool>`, which is not FFI-safe
--> $DIR/lint-ctypes-fn.rs:172:43

View file

@ -4,7 +4,7 @@ error: `extern` block uses type `Foo`, which is not FFI-safe
LL | pub fn ptr_type1(size: *const Foo);
| ^^^^^^^^^^ not FFI-safe
|
= note: this reference (`*const Foo`) is can safely be translated into a C pointer, but `Foo` itself is not FFI-safe
= note: this reference (`*const Foo`) is ABI-compatible with a C pointer, but `Foo` itself does not have a C layout
= help: consider adding a `#[repr(C)]` or `#[repr(transparent)]` attribute to this struct
= note: this struct has unspecified layout
note: the type is defined here
@ -24,7 +24,7 @@ error: `extern` block uses type `Foo`, which is not FFI-safe
LL | pub fn ptr_type2(size: *const Foo);
| ^^^^^^^^^^ not FFI-safe
|
= note: this reference (`*const Foo`) is can safely be translated into a C pointer, but `Foo` itself is not FFI-safe
= note: this reference (`*const Foo`) is ABI-compatible with a C pointer, but `Foo` itself does not have a C layout
= help: consider adding a `#[repr(C)]` or `#[repr(transparent)]` attribute to this struct
= note: this struct has unspecified layout
note: the type is defined here
@ -39,7 +39,7 @@ error: `extern` block uses type `((),)`, which is not FFI-safe
LL | pub fn ptr_tuple(p: *const ((),));
| ^^^^^^^^^^^^ not FFI-safe
|
= note: this reference (`*const ((),)`) is can safely be translated into a C pointer, but `((),)` itself is not FFI-safe
= note: this reference (`*const ((),)`) is ABI-compatible with a C pointer, but `((),)` itself does not have a C layout
= help: consider using a struct instead
= note: tuples have unspecified layout
@ -50,7 +50,7 @@ LL | pub fn slice_type(p: &[u32]);
| ^^^^^^ not FFI-safe
|
= help: consider using a raw pointer to the slice's first element (and a length) instead
= note: this reference to an unsized rust type contains metadata, which makes it incompatible with a C pointer
= note: this reference to an unsized type contains metadata, which makes it incompatible with a C pointer
error: `extern` block uses type `&str`, which is not FFI-safe
--> $DIR/lint-ctypes.rs:65:24
@ -59,7 +59,7 @@ LL | pub fn str_type(p: &str);
| ^^^^ not FFI-safe
|
= help: consider using `*const u8` and a length instead
= note: this reference to an unsized rust type contains metadata, which makes it incompatible with a C pointer
= note: this reference to an unsized type contains metadata, which makes it incompatible with a C pointer
error: `extern` block uses type `char`, which is not FFI-safe
--> $DIR/lint-ctypes.rs:68:25
@ -92,7 +92,7 @@ error: `extern` block uses type `&dyn Bar`, which is not FFI-safe
LL | pub fn trait_type(p: &dyn Bar);
| ^^^^^^^^ not FFI-safe
|
= note: this reference to an unsized rust type contains metadata, which makes it incompatible with a C pointer
= note: this reference to an unsized type contains metadata, which makes it incompatible with a C pointer
error: `extern` block uses type `(i32, i32)`, which is not FFI-safe
--> $DIR/lint-ctypes.rs:72:26
@ -180,7 +180,7 @@ LL | pub fn transparent_str(p: TransparentStr);
| ^^^^^^^^^^^^^^ not FFI-safe
|
= help: consider using `*const u8` and a length instead
= note: this reference to an unsized rust type contains metadata, which makes it incompatible with a C pointer
= note: this reference to an unsized type contains metadata, which makes it incompatible with a C pointer
error: `extern` block uses type `[u8; 8]`, which is not FFI-safe
--> $DIR/lint-ctypes.rs:85:27
@ -197,7 +197,7 @@ error: `extern` block uses type `&UnsizedStructBecauseDyn`, which is not FFI-saf
LL | pub fn struct_unsized_ptr_has_metadata(p: &UnsizedStructBecauseDyn);
| ^^^^^^^^^^^^^^^^^^^^^^^^ not FFI-safe
|
= note: this reference to an unsized rust type contains metadata, which makes it incompatible with a C pointer
= note: this reference to an unsized type contains metadata, which makes it incompatible with a C pointer
error: `extern` block uses type `Option<UnsafeCell<extern "C" fn()>>`, which is not FFI-safe
--> $DIR/lint-ctypes.rs:90:26