-
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
Specify that bool
is compatible with the _Bool
C type.
#954
Conversation
+1 from me, seems like a simple and straightforward thing for us to guarantee. |
|
||
# Drawbacks | ||
|
||
None right now. This should be a no-op on all supported platforms. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
What about arbitrary unsupported platforms? This proposal has the ability to box us into a suboptimal corner, by being beholden to whatever quirks C might have.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Unsupported platforms are not supported, hence we're not beholden to do anything on those platforms, unless I misunderstand your concern.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
They are only unsupported now. If/when we gain support for them we will have to guarantee that bool
acts like _Bool
on those platforms too, so we need to plan ahead.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I see. It seems unlikely to me that there will be any platforms where _Bool
is different than our bool
(especially considering that we will only ever target platforms LLVM supports, which means there is some degree of sanity), but I guess it is possible.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
What's the plan for platforms that don't use two's complement? If there is no such plan, then why would you worry more about the representation of bool
than the representation of u8
?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
(Also, I have no idea if _Bool
is one byte on all supported platforms, so bool
still may not match _Bool
even with that property.)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The premise of this argument is pretty ridiculous: "We know better than the hardware developers how to represent a boolean value on their hardware."
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It's not ridiculous: the C _Bool
type on some platforms may have historical constraints that don't apply to Rust. In any case, the precise representation of C types is usually chosen by the compiler, not the hardware.
A minimum improvement that would help assuage my concerns would be quoting the C standard's definition of _Bool
in the RFC.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
In any case, the precise representation of C types is usually chosen by the compiler, not the hardware.
That's wrong. The representation of _Bool is part of the ABI and should be (for all hardware developed after C99) defined by the hardware vendor. E.g.
http://www.x86-64.org/documentation/abi.pdf page 12
http://infocenter.arm.com/help/topic/com.arm.doc.ihi0042e/IHI0042E_aapcs.pdf page 26
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
That information should be in the RFC.
Personally I think we should have a libc definition for |
A comprehensive review of sizes/alignments of C |
That would not be helpful. In C, bool is an alias for _Bool and _Bool is almost never used directly. |
So name it |
Sure, as long as it's typedef'd to bool and not an integer type there is no problem. |
👍 for adding FYI, the embedded world is chock-full of conflicting |
I don't see much sense in |
Actually, I agree with @petrochenkov with the However, although rare, the Back to topic: by the c99 standard, it is not clear what underlying type maps to |
We discussed this at triage today, and the general feeling was that we probably don't want to commit to the representation of Rust's As you've pointed out, just using a type alias probably won't work so well here, so we will likely need a new primitive type for this purpose if we are to add it. There are a number of possible routes here (such as a newtype scalar which canonicalizes on casts), but at this time though this is believe to be a backwards compatible change and we've decided to close this as postponed for now. Thanks for the RFC regardless, though! |
Setting aside the question of whether we should have I and others have claimed that C In summary, Rust and C have identical representations for Again, this only bears on whether it would be correct to equate Rust's and C's LinuxThe x86 and x86-64 ABIs that GCC uses for Linux are community-maintained forks of the original System V Application Binary Interface: Intel386 Architecture Processor Supplement, Fourth Edition. They both specify that C's
The Linux Standard Base had intended to be the authority that specifies the ABI to use on Linux, but it seems to be waning; Debian dropped LSB support in September 2015. For what it's worth, the LSB fails to specify the representation of Mac OSXFor the x86-64 architecture, Apple's ABI page cites what appears to be a copy of the GCC x86-64 ABI. This document contains the same language as quoted above for Linux: a Apple's page on its x86 ABI says that
Then I get the following x86 machine code:
This loads the pointer However, note that the C language specification requires So although it's not written out, the OSX compiler behaves as if the x86 ABI places the same restrictions on the representation of WindowsThe MSDN page on Visual Studio's C++ ABI says that I asked a friend to compile the following function with MSVC, without optimization:
This produced the following machine code:
The code for x86_64 is essentially the same. As is the case with OSX above, the MSVC compiler feels free to assume that a value stored in a bool about which it has no other information is either zero or one. We can conclude that the values 0 and 1 are the only permitted representations for |
There is no need to search for the ABIs. Simply look at the llvm source and you will find that i1 is one byte everywhere but on one odd platform where it is 4 bytes. |
@mahkoh: That's not the question I was trying to answer. I wanted to answer the question: "Is it true that Rust's in-memory representation for |
Both rust and clang use i1 to represent bools in scalar context and whatever LLVM uses as the memory representation (mostly i8) for the memory representation. Hence the question is reduced to whether clang is compatible with other C compilers. This is the case. |
It appears that rust unconditionally uses i8 for the memory representation of bool. Nevertheless, since it's i8 everywhere but on one unsupported platform, the result is the same. |
+1, If GCC/Clang sources show that "1 byte/0/1"
I'm curious what platforms supported by GCC or Clang don't use "1 byte/0/1" |
The ABI of bool is reserved, see these links: * rust-lang/rfcs#954 (comment) * rust-lang#46156 (comment) * rust-lang/rfcs#992 Currently the improper_ctypes lint is inconsistent with that position by treating bools as if they had a specified ABI. To fix this inconsistency, this changes treatment of bools by the lint. This might break some code, so possibly it has to be phased in slowly...
Specify that
bool
is compatible with the_Bool
C type.Rendered