Skip to content

Fix ICE in improper_ctypes_definitions lint #115698

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 1 commit into from
Sep 9, 2023
Merged
Show file tree
Hide file tree
Changes from all 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
4 changes: 2 additions & 2 deletions compiler/rustc_lint/src/types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -917,8 +917,8 @@ pub(crate) fn repr_nullable_ptr<'tcx>(
// At this point, the field's type is known to be nonnull and the parent enum is Option-like.
// If the computed size for the field and the enum are different, the nonnull optimization isn't
// being applied (and we've got a problem somewhere).
let compute_size_skeleton = |t| SizeSkeleton::compute(t, tcx, param_env).unwrap();
if !compute_size_skeleton(ty).same_size(compute_size_skeleton(field_ty)) {
let compute_size_skeleton = |t| SizeSkeleton::compute(t, tcx, param_env).ok();
if !compute_size_skeleton(ty)?.same_size(compute_size_skeleton(field_ty)?) {
bug!("improper_ctypes: Option nonnull optimization not applied?");
}

Expand Down
7 changes: 7 additions & 0 deletions tests/ui/lint/lint-ctypes-94223.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,13 @@ enum BadUnion {
type Foo = extern "C" fn([u8]);
//~^ ERROR `extern` fn uses type `[u8]`, which is not FFI-safe

pub trait FooTrait {
type FooType;
}

pub type Foo2<T> = extern "C" fn(Option<&<T as FooTrait>::FooType>);
//~^ ERROR `extern` fn uses type `Option<&<T as FooTrait>::FooType>`, which is not FFI-safe

pub struct FfiUnsafe;

#[allow(improper_ctypes_definitions)]
Expand Down
27 changes: 18 additions & 9 deletions tests/ui/lint/lint-ctypes-94223.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -66,61 +66,70 @@ LL | type Foo = extern "C" fn([u8]);
= help: consider using a raw pointer instead
= note: slices have no C equivalent

error: `extern` fn uses type `Option<&<T as FooTrait>::FooType>`, which is not FFI-safe
--> $DIR/lint-ctypes-94223.rs:31:20
|
LL | pub type Foo2<T> = extern "C" fn(Option<&<T as FooTrait>::FooType>);
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ not FFI-safe
|
= help: consider adding a `#[repr(C)]`, `#[repr(transparent)]`, or integer `#[repr(...)]` attribute to this enum
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is help message isn't super useful, but that's not your fault 😆

= note: enum has no representation hint

error: `extern` fn uses type `FfiUnsafe`, which is not FFI-safe
--> $DIR/lint-ctypes-94223.rs:34:17
--> $DIR/lint-ctypes-94223.rs:41:17
|
LL | pub static BAD: extern "C" fn(FfiUnsafe) = f;
| ^^^^^^^^^^^^^^^^^^^^^^^^ not FFI-safe
|
= 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
--> $DIR/lint-ctypes-94223.rs:27:1
--> $DIR/lint-ctypes-94223.rs:34:1
|
LL | pub struct FfiUnsafe;
| ^^^^^^^^^^^^^^^^^^^^

error: `extern` fn uses type `FfiUnsafe`, which is not FFI-safe
--> $DIR/lint-ctypes-94223.rs:37:30
--> $DIR/lint-ctypes-94223.rs:44:30
|
LL | pub static BAD_TWICE: Result<extern "C" fn(FfiUnsafe), extern "C" fn(FfiUnsafe)> = Ok(f);
| ^^^^^^^^^^^^^^^^^^^^^^^^ not FFI-safe
|
= 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
--> $DIR/lint-ctypes-94223.rs:27:1
--> $DIR/lint-ctypes-94223.rs:34:1
|
LL | pub struct FfiUnsafe;
| ^^^^^^^^^^^^^^^^^^^^

error: `extern` fn uses type `FfiUnsafe`, which is not FFI-safe
--> $DIR/lint-ctypes-94223.rs:37:56
--> $DIR/lint-ctypes-94223.rs:44:56
|
LL | pub static BAD_TWICE: Result<extern "C" fn(FfiUnsafe), extern "C" fn(FfiUnsafe)> = Ok(f);
| ^^^^^^^^^^^^^^^^^^^^^^^^ not FFI-safe
|
= 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
--> $DIR/lint-ctypes-94223.rs:27:1
--> $DIR/lint-ctypes-94223.rs:34:1
|
LL | pub struct FfiUnsafe;
| ^^^^^^^^^^^^^^^^^^^^

error: `extern` fn uses type `FfiUnsafe`, which is not FFI-safe
--> $DIR/lint-ctypes-94223.rs:41:22
--> $DIR/lint-ctypes-94223.rs:48:22
|
LL | pub const BAD_CONST: extern "C" fn(FfiUnsafe) = f;
| ^^^^^^^^^^^^^^^^^^^^^^^^ not FFI-safe
|
= 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
--> $DIR/lint-ctypes-94223.rs:27:1
--> $DIR/lint-ctypes-94223.rs:34:1
|
LL | pub struct FfiUnsafe;
| ^^^^^^^^^^^^^^^^^^^^

error: aborting due to 11 previous errors
error: aborting due to 12 previous errors