Repository navigation
improper_ctypes lint should check types referenced in C ABI function definitions #19834
Description
Activity
- addedA-lintsArea: Lints (warnings about flaws in source code) such as unused_mut.Area: Lints (warnings about flaws in source code) such as unused_mut.
on Dec 14, 2014 To clarify: foreign functions (
fninside anextern block) are checked. Regular functions with a C ABI are not.This is still an issue. It just came up in a Stack Overflow question, with something roughly equivalent to this:
#[no_mangle] pub extern fn print(s: String) { println!("{}", s); }
This produces no warnings. For context, the user was trying to bind to this function from C#, which went about as well as you'd expect.
Its even worse here - this is an
extern "Rust"function, and these have their own ABI.@arielb1 https://github.com/rust-lang/rust/blob/master/src/doc/trpl/ffi.md#calling-rust-code-from-c seems to imply that a
pub extern fn something() {}uses theCcalling convention by default.Edit: and the reference seems to agree: https://github.com/rust-lang/rust/blob/master/src/doc/reference.md#extern-functions
Continues to be a problem for people new to FFI:
#[no_mangle] pub extern "C" fn hello(cs: CString) -> CString { // ... }
The OP states:
Well, my reasoning is that if I can pass
int32back and forth + ifCStringcan easily go in and out (commented code), then it should work.If this generated a warning, there's more of a chance people might not do this.
Should we start some kind of RFC for this?
/cc @nagisa from comment
18 remaining items
I have not... it's just been sitting in a tab, waiting, lonely. If someone wants to steal it, please feel free!
@varkor @shepmaster I had a go at implementing the instructions in issue-19834-improper-ctypes-in-extern-C-fn, but quickly ran into problems:
- I had to
#[allow(improper_ctypes)]on a bunch of places within thelibproc_macro,libstd,libpanic_abortandlibpanic_unwind. - typeck currently only prohibits generics on foreign functions, so by allowing
improper_ctypeson regular functions (that haveextern "C"), you quickly run into something like ICE: src/librustc_lint/types.rs:858: unexpected type in foreign function: T #65035 - I added basic support for generics to theimproper_ctypeslint so I could#[allow(improper_ctypes)]away the remaining errors.
The branch doesn't fail any tests at the moment. Do either of you think it's worth continuing with this and opening a PR?
- I had to
Neither of these problems is a showstopper in my opinion, please continue and open a PR. Also, I have comments about both of the issues you identified and how you addressed them but it would probably best to discuss that on the PR.
Closing,
extern fns are handled in #72700.Reacted by Jake Goulding
For example, this compiles with no warnings or errors:
Even though
Blahlacks the#[repr(C)]attribute.