Skip to content

Don't allow array types in extern "C" functions. #24578

New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

Closed
pnkfelix opened this issue Apr 18, 2015 · 4 comments · Fixed by #66305
Closed

Don't allow array types in extern "C" functions. #24578

pnkfelix opened this issue Apr 18, 2015 · 4 comments · Fixed by #66305
Labels
A-diagnostics Area: Messages for errors, warnings, and lints A-FFI Area: Foreign function interface (FFI) A-lints Area: Lints (warnings about flaws in source code) such as unused_mut. C-bug Category: This is a bug. E-easy Call for participation: Easy difficulty. Experience needed to fix: Not much. Good first issue. P-low Low priority T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.

Comments

@pnkfelix
Copy link
Member

(imported from improperly closed bug #16221)

You cannot pass an array to or return an array from a C function.
The signature f(char x[N]) is equivalent to f(char *x) for every N in C. Therefore it is meaningless to write this in Rust, (or rather, it does not mean what a C programmer and/or FFI generation tool likely thinks it means):

    extern "C" fn f(x: [i32, ..4]); // void f(int x[4])

Option for dealing with this:

  • We can extend our existing lint to detect cases like this and warn about them.
  • We could make the extern "C" fn form reject this signature, since the expected correspondence between Rust and C does not hold here.
  • If the Rust FFI binding generation scripts/programs out there are generating this sort of code, they need to be fixed.
@pnkfelix
Copy link
Member Author

triage: P-low

@rust-highfive rust-highfive added the P-low Low priority label Apr 18, 2015
@steveklabnik
Copy link
Member

Triage: array syntax has changed, here's the correct syntax:

extern {
    fn f(x: [i32; 4]);
}

fn main() {}

no warning or anything.

@Mark-Simulacrum Mark-Simulacrum added A-diagnostics Area: Messages for errors, warnings, and lints A-FFI Area: Foreign function interface (FFI) C-bug Category: This is a bug. labels Jul 22, 2017
@steveklabnik
Copy link
Member

Triage: no change

@estebank estebank added T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. A-lints Area: Lints (warnings about flaws in source code) such as unused_mut. labels Oct 11, 2019
@estebank
Copy link
Contributor

This would have to be implemented as a lint now due to the potential of breaking existing builds. It should be easy enough to implement.

@estebank estebank added the E-easy Call for participation: Easy difficulty. Experience needed to fix: Not much. Good first issue. label Oct 11, 2019
@bors bors closed this as completed in 2f1a4b3 Nov 28, 2019
@fmease fmease added A-lints Area: Lints (warnings about flaws in source code) such as unused_mut. and removed A-lints Area: Lints (warnings about flaws in source code) such as unused_mut. labels Dec 21, 2024
# for free to join this conversation on GitHub. Already have an account? # to comment
Labels
A-diagnostics Area: Messages for errors, warnings, and lints A-FFI Area: Foreign function interface (FFI) A-lints Area: Lints (warnings about flaws in source code) such as unused_mut. C-bug Category: This is a bug. E-easy Call for participation: Easy difficulty. Experience needed to fix: Not much. Good first issue. P-low Low priority T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.
Projects
None yet
Development

Successfully merging a pull request may close this issue.

6 participants