Skip to content

improper_ctypes should not suggest reprs that lead to errors #60757

Open
@hanna-kruppe

Description

@hanna-kruppe

Currently when the lint finds a repr(Rust) struct, enum, or union that is not FFI-safe, it generally suggests some repr attributes that could be applied to a struct/enum/union that would make it FFI-safe, which is nice. However, it bases these suggestions only on the kind of data type, without checking whether the attribute could actually be applied to the specific type.

For example, when compiling this code:

struct Rgb(u8, u8, u8);

extern "C" {
    fn get_color(name: *const u8) -> Rgb;
}

The compiler suggests repr(transparent) alongside repr(C), but applying this suggestion will cause an error because the struct has multiple non-ZST fields.

Arguably the compiler should first check whether the suggestion "makes sense", and not suggest repr(transparent) in cases such as this one. Although it probably should continue to suggesting repr(C) even if that would then lead to another improper_ctypes about a field of the affected type (the user might want to repeatedly apply those suggestions to mark all necessary types as repr(C)).

Metadata

Metadata

Assignees

No one assigned

    Labels

    A-FFIArea: Foreign function interface (FFI)A-lintsArea: Lints (warnings about flaws in source code) such as unused_mut.C-enhancementCategory: An issue proposing an enhancement or a PR with one.L-improper_ctypesLint: improper_ctypesT-compilerRelevant to the compiler team, which will review and decide on the PR/issue.

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions