forked from rust-lang/rust
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
lint/ctypes: stricter
()
return type checks
`()` 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 an 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 a unit return type or a transparent wrapper around a unit is checked for directly for functions and fn-ptrs. Signed-off-by: David Wood <[email protected]>
- Loading branch information
Showing
3 changed files
with
115 additions
and
20 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
#![deny(improper_ctypes_definitions)] | ||
|
||
#[repr(C)] | ||
pub struct Wrap<T>(T); | ||
|
||
#[repr(transparent)] | ||
pub struct TransparentWrap<T>(T); | ||
|
||
pub extern "C" fn f() -> Wrap<()> { | ||
//~^ ERROR `extern` fn uses type `()`, which is not FFI-safe | ||
todo!() | ||
} | ||
|
||
const _: extern "C" fn() -> Wrap<()> = f; | ||
//~^ ERROR `extern` fn uses type `()`, which is not FFI-safe | ||
|
||
pub extern "C" fn ff() -> Wrap<Wrap<()>> { | ||
//~^ ERROR `extern` fn uses type `()`, which is not FFI-safe | ||
todo!() | ||
} | ||
|
||
const _: extern "C" fn() -> Wrap<Wrap<()>> = ff; | ||
//~^ ERROR `extern` fn uses type `()`, which is not FFI-safe | ||
|
||
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() {} |
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,43 @@ | ||
error: `extern` fn uses type `()`, which is not FFI-safe | ||
--> $DIR/lint-ctypes-113436.rs:9:26 | ||
| | ||
LL | pub extern "C" fn f() -> Wrap<()> { | ||
| ^^^^^^^^ not FFI-safe | ||
| | ||
= help: consider using a struct instead | ||
= note: tuples have unspecified layout | ||
note: the lint level is defined here | ||
--> $DIR/lint-ctypes-113436.rs:1:9 | ||
| | ||
LL | #![deny(improper_ctypes_definitions)] | ||
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^ | ||
|
||
error: `extern` fn uses type `()`, which is not FFI-safe | ||
--> $DIR/lint-ctypes-113436.rs:14:10 | ||
| | ||
LL | const _: extern "C" fn() -> Wrap<()> = f; | ||
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^ not FFI-safe | ||
| | ||
= help: consider using a struct instead | ||
= note: tuples have unspecified layout | ||
|
||
error: `extern` fn uses type `()`, which is not FFI-safe | ||
--> $DIR/lint-ctypes-113436.rs:17:27 | ||
| | ||
LL | pub extern "C" fn ff() -> Wrap<Wrap<()>> { | ||
| ^^^^^^^^^^^^^^ not FFI-safe | ||
| | ||
= help: consider using a struct instead | ||
= note: tuples have unspecified layout | ||
|
||
error: `extern` fn uses type `()`, which is not FFI-safe | ||
--> $DIR/lint-ctypes-113436.rs:22:10 | ||
| | ||
LL | const _: extern "C" fn() -> Wrap<Wrap<()>> = ff; | ||
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ not FFI-safe | ||
| | ||
= help: consider using a struct instead | ||
= note: tuples have unspecified layout | ||
|
||
error: aborting due to 4 previous errors | ||
|