-
Notifications
You must be signed in to change notification settings - Fork 1.6k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Meta issue: FFI attributes syntax #2637
Comments
Some notes:
|
Restricting Rust foreign function interface to "standard C" was never a goal AFAIK. So I don't think that this arguments supports giving these different names.
These attributes are very old. As a consequence, a huge amount of C code in the wild uses them, C programmers know them, and most C compilers support them. The name is bad today, but "pure" wasn't really mainstream term back then.
For the user copy-pasting a C function declaration into Rust, giving it the same name has maximum discoverability. Giving them a different name, reduces this discoverability. So it appears to me that giving these good names is at tension with making them discoverable. The PR balances the trade-offs by using
Indeed :/ Underscores create new identifiers, so as a namespacing method, it doesn't have this problem. Alternatively,
Maybe it would be better to handle this at the ABI level? For example, via: |
#[ffi(const)] is ok. With rust-lang/rust#57367 the "no keywords" restriction is only active for built-in attributes due to using |
Oh right; we changed this... I should know this since I reviewed the PR 😅. Still, I think
What notable stable FFI things do we expose that are not standard C? I think it's an important consideration because it matters wrt. universality and because if something is in standard C it is unlikely to ever change and you don't get the potential for dialectal problems between clang, gcc, etc. The examples to come to my mind are
I think it was misleading when it was first introduced in 2000 (which is not that old) and I expect the term "pure function" has been used for some time...
I think it's relatively easy to write a sentence about the correspondence to GCC/clang/etc. and it will quite likely be the first thing you find if you search for "Rust pure". If you search for "Rust const" you'd find something entirely different anyways (at least in the first search results) so that doesn't seem optimal in terms of discoverability.
I'll think of something.
What is that repainting intended to achieve? You still get the multiplicative complexity of all these different toolchains as well as the encouragement of toolchain dependence in code (which is the opposite of what I think we should aim for). |
That's offtopic, we can talk about this in Discord if you want, but let's stay on this issue.
That's mainly why I suggested using
But I have a solution. This issue is about adding a consistent way of mapping C attributes in Rust. Let's assume the syntax is That way we don't have to use C names, and people writing code don't need to remember the mapping.
(note: that's a C++ paper, and there hasn't been a new revision since 2015) |
So I've had more time to think about this, and while this issue is worth resolving, I think #2633 needs extra work for unrelated reasons (will post there in a sec). |
There are some FFI (and C FFI) specific attributes that we might want to add to Rust (e.g. see rust-lang/rust#58327 and rust-lang/rust#58315, amongst others).
We probably want to expose FFI attributes in a clean way.
Prefix vs no prefix
Many FFI attributes don't have a prefix, e.g.,
#[link_name]
, etc. Some like#[returns_twice]
are required for correctness, and some likepure
andconst
are optimization hints that might be ignored by the compiler. These do however have overloaded names that we might want to put into context to avoid confusion with, e.g.,const fn
.Also, some attributes like
pure
andconst
have names with overloaded semantics that might make sense to disambiguate, e.g., instead of#[const]
which is very similar toconst fn
, maybe#[ffi_const]
.FFI can be used to interface with any language, but some attributes might only make sense when interfacing with some particular language.
c_ffi
orffi::c
)ffi
? (independent of language)ffi
andffi_c
vsffi
andc_ffi
, etc.)Option namespacing
Some FFI attributes might need to behave slightly different depending on the toolchain used to compile the code that's being interfaced via FFI.
For example, clang currently exposes both the
[[const]]
and[[gcc::const]]
attributes. These currently are identical, but this separation is forward compatible with diverging semantics in the optimization hints thatconst
has in the different toolchains. This is important both for interfacing clang binaries with gcc compiled libraries and vice-verse, and also, for not performing incorrect optimizations on programs that satisfy e.g.[[gcc::const]]
requirements but not clang's[[const]]
.#[ffi::c::const(clang)]
vs#[ffi::c::const(cranelift)]
?Even if this is not RFC'ed right now, it might be worth it to have some plan for this in the pipe line in case it becomes necessary, and to make sure that merged RFCs are forward compatible with this. Otherwise we might end with
#[c_ffi_const_clang]
and#[gcc_ffi_c_const]
or some similar zoo of attributes.The text was updated successfully, but these errors were encountered: