-
Notifications
You must be signed in to change notification settings - Fork 12.7k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Auto merge of #113457 - davidtwco:lint-ctypes-issue-113436, r=oli-obk
lint/ctypes: fix `()` return type checks Fixes #113436. `()` is normally FFI-unsafe, but is FFI-safe when used as a return type. It is also desirable that a transparent newtype for `()` is FFI-safe when used as a return type. In order to support this, when a type was deemed FFI-unsafe, because of a `()` type, and was used in return type - then the type was considered FFI-safe. However, this was the wrong approach - it didn't check that the `()` was part of a transparent newtype! The consequence of this is that the presence of a `()` type in a more complex return type would make it the entire type be considered safe (as long as the `()` type was the first that the lint found) - which is obviously incorrect. Instead, this logic is removed, and after [consultation with t-lang](#113436 (comment)), I've fixed the bugs and inconsistencies and made `()` FFI-safe within types. I also refactor a function, but that's not too exciting.
- Loading branch information
Showing
5 changed files
with
136 additions
and
46 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
#![deny(improper_ctypes_definitions)] | ||
|
||
#[repr(C)] | ||
pub struct Foo { | ||
a: u8, | ||
b: (), | ||
} | ||
|
||
extern "C" fn foo(x: Foo) -> Foo { | ||
todo!() | ||
} | ||
|
||
struct NotSafe(u32); | ||
|
||
#[repr(C)] | ||
pub struct Bar { | ||
a: u8, | ||
b: (), | ||
c: NotSafe, | ||
} | ||
|
||
extern "C" fn bar(x: Bar) -> Bar { | ||
//~^ ERROR `extern` fn uses type `NotSafe`, which is not FFI-safe | ||
//~^^ ERROR `extern` fn uses type `NotSafe`, which is not FFI-safe | ||
todo!() | ||
} | ||
|
||
fn main() {} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
error: `extern` fn uses type `NotSafe`, which is not FFI-safe | ||
--> $DIR/lint-ctypes-113436-1.rs:22:22 | ||
| | ||
LL | extern "C" fn bar(x: Bar) -> Bar { | ||
| ^^^ 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-113436-1.rs:13:1 | ||
| | ||
LL | struct NotSafe(u32); | ||
| ^^^^^^^^^^^^^^ | ||
note: the lint level is defined here | ||
--> $DIR/lint-ctypes-113436-1.rs:1:9 | ||
| | ||
LL | #![deny(improper_ctypes_definitions)] | ||
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^ | ||
|
||
error: `extern` fn uses type `NotSafe`, which is not FFI-safe | ||
--> $DIR/lint-ctypes-113436-1.rs:22:30 | ||
| | ||
LL | extern "C" fn bar(x: Bar) -> Bar { | ||
| ^^^ 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-113436-1.rs:13:1 | ||
| | ||
LL | struct NotSafe(u32); | ||
| ^^^^^^^^^^^^^^ | ||
|
||
error: aborting due to 2 previous errors | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
// check-pass | ||
#![deny(improper_ctypes_definitions)] | ||
|
||
#[repr(C)] | ||
pub struct Wrap<T>(T); | ||
|
||
#[repr(transparent)] | ||
pub struct TransparentWrap<T>(T); | ||
|
||
pub extern "C" fn f() -> Wrap<()> { | ||
todo!() | ||
} | ||
|
||
const _: extern "C" fn() -> Wrap<()> = f; | ||
|
||
pub extern "C" fn ff() -> Wrap<Wrap<()>> { | ||
todo!() | ||
} | ||
|
||
const _: extern "C" fn() -> Wrap<Wrap<()>> = ff; | ||
|
||
pub extern "C" fn g() -> TransparentWrap<()> { | ||
todo!() | ||
} | ||
|
||
const _: extern "C" fn() -> TransparentWrap<()> = g; | ||
|
||
pub extern "C" fn gg() -> TransparentWrap<TransparentWrap<()>> { | ||
todo!() | ||
} | ||
|
||
const _: extern "C" fn() -> TransparentWrap<TransparentWrap<()>> = gg; | ||
|
||
fn main() {} |