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.
Rollup merge of rust-lang#128735 - jieyouxu:pr-120176-revive, r=cjgillot
Add a special case for `CStr`/`CString` in the `improper_ctypes` lint Revives rust-lang#120176. Just needed to bless a test and fix an argument, but seemed reasonable to me otherwise. Instead of saying to "consider adding a `#[repr(C)]` or `#[repr(transparent)]` attribute to this struct", we now tell users to "Use `*const ffi::c_char` instead, and pass the value from `CStr::as_ptr()`" when the type involved is a `CStr` or a `CString`. The suggestion is not made for `&mut CString` or `*mut CString`. r? `````@cjgillot````` (since you were the reviewer of the original PR rust-lang#120176, but feel free to reroll)
- Loading branch information
Showing
7 changed files
with
172 additions
and
21 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
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,36 @@ | ||
#![crate_type = "lib"] | ||
#![deny(improper_ctypes, improper_ctypes_definitions)] | ||
|
||
use std::ffi::{CStr, CString}; | ||
|
||
extern "C" { | ||
fn take_cstr(s: CStr); | ||
//~^ ERROR `extern` block uses type `CStr`, which is not FFI-safe | ||
//~| HELP consider passing a `*const std::ffi::c_char` instead, and use `CStr::as_ptr()` | ||
fn take_cstr_ref(s: &CStr); | ||
//~^ ERROR `extern` block uses type `CStr`, which is not FFI-safe | ||
//~| HELP consider passing a `*const std::ffi::c_char` instead, and use `CStr::as_ptr()` | ||
fn take_cstring(s: CString); | ||
//~^ ERROR `extern` block uses type `CString`, which is not FFI-safe | ||
//~| HELP consider passing a `*const std::ffi::c_char` instead, and use `CStr::as_ptr()` | ||
fn take_cstring_ref(s: &CString); | ||
//~^ ERROR `extern` block uses type `CString`, which is not FFI-safe | ||
//~| HELP consider passing a `*const std::ffi::c_char` instead, and use `CStr::as_ptr()` | ||
|
||
fn no_special_help_for_mut_cstring(s: *mut CString); | ||
//~^ ERROR `extern` block uses type `CString`, which is not FFI-safe | ||
//~| HELP consider adding a `#[repr(C)]` or `#[repr(transparent)]` attribute to this struct | ||
|
||
fn no_special_help_for_mut_cstring_ref(s: &mut CString); | ||
//~^ ERROR `extern` block uses type `CString`, which is not FFI-safe | ||
//~| HELP consider adding a `#[repr(C)]` or `#[repr(transparent)]` attribute to this struct | ||
} | ||
|
||
extern "C" fn rust_take_cstr_ref(s: &CStr) {} | ||
//~^ ERROR `extern` fn uses type `CStr`, which is not FFI-safe | ||
//~| HELP consider passing a `*const std::ffi::c_char` instead, and use `CStr::as_ptr()` | ||
extern "C" fn rust_take_cstring(s: CString) {} | ||
//~^ ERROR `extern` fn uses type `CString`, which is not FFI-safe | ||
//~| HELP consider passing a `*const std::ffi::c_char` instead, and use `CStr::as_ptr()` | ||
extern "C" fn rust_no_special_help_for_mut_cstring(s: *mut CString) {} | ||
extern "C" fn rust_no_special_help_for_mut_cstring_ref(s: &mut CString) {} |
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,84 @@ | ||
error: `extern` block uses type `CStr`, which is not FFI-safe | ||
--> $DIR/lint-ctypes-cstr.rs:7:21 | ||
| | ||
LL | fn take_cstr(s: CStr); | ||
| ^^^^ not FFI-safe | ||
| | ||
= help: consider passing a `*const std::ffi::c_char` instead, and use `CStr::as_ptr()` | ||
= note: `CStr`/`CString` do not have a guaranteed layout | ||
note: the lint level is defined here | ||
--> $DIR/lint-ctypes-cstr.rs:2:9 | ||
| | ||
LL | #![deny(improper_ctypes, improper_ctypes_definitions)] | ||
| ^^^^^^^^^^^^^^^ | ||
|
||
error: `extern` block uses type `CStr`, which is not FFI-safe | ||
--> $DIR/lint-ctypes-cstr.rs:10:25 | ||
| | ||
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: `CStr`/`CString` do not have a guaranteed layout | ||
|
||
error: `extern` block uses type `CString`, which is not FFI-safe | ||
--> $DIR/lint-ctypes-cstr.rs:13:24 | ||
| | ||
LL | fn take_cstring(s: CString); | ||
| ^^^^^^^ not FFI-safe | ||
| | ||
= help: consider passing a `*const std::ffi::c_char` instead, and use `CStr::as_ptr()` | ||
= note: `CStr`/`CString` do not have a guaranteed layout | ||
|
||
error: `extern` block uses type `CString`, which is not FFI-safe | ||
--> $DIR/lint-ctypes-cstr.rs:16:28 | ||
| | ||
LL | fn take_cstring_ref(s: &CString); | ||
| ^^^^^^^^ not FFI-safe | ||
| | ||
= help: consider passing a `*const std::ffi::c_char` instead, and use `CStr::as_ptr()` | ||
= note: `CStr`/`CString` do not have a guaranteed layout | ||
|
||
error: `extern` block uses type `CString`, which is not FFI-safe | ||
--> $DIR/lint-ctypes-cstr.rs:20:43 | ||
| | ||
LL | fn no_special_help_for_mut_cstring(s: *mut CString); | ||
| ^^^^^^^^^^^^ not FFI-safe | ||
| | ||
= help: consider adding a `#[repr(C)]` or `#[repr(transparent)]` attribute to this struct | ||
= note: this struct has unspecified layout | ||
|
||
error: `extern` block uses type `CString`, which is not FFI-safe | ||
--> $DIR/lint-ctypes-cstr.rs:24:47 | ||
| | ||
LL | fn no_special_help_for_mut_cstring_ref(s: &mut CString); | ||
| ^^^^^^^^^^^^ not FFI-safe | ||
| | ||
= help: consider adding a `#[repr(C)]` or `#[repr(transparent)]` attribute to this struct | ||
= note: this struct has unspecified layout | ||
|
||
error: `extern` fn uses type `CStr`, which is not FFI-safe | ||
--> $DIR/lint-ctypes-cstr.rs:29:37 | ||
| | ||
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: `CStr`/`CString` do not have a guaranteed layout | ||
note: the lint level is defined here | ||
--> $DIR/lint-ctypes-cstr.rs:2:26 | ||
| | ||
LL | #![deny(improper_ctypes, improper_ctypes_definitions)] | ||
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^ | ||
|
||
error: `extern` fn uses type `CString`, which is not FFI-safe | ||
--> $DIR/lint-ctypes-cstr.rs:32:36 | ||
| | ||
LL | extern "C" fn rust_take_cstring(s: CString) {} | ||
| ^^^^^^^ not FFI-safe | ||
| | ||
= help: consider passing a `*const std::ffi::c_char` instead, and use `CStr::as_ptr()` | ||
= note: `CStr`/`CString` do not have a guaranteed layout | ||
|
||
error: aborting due to 8 previous errors | ||
|