Join GitHub today
GitHub is home to over 31 million developers working together to host and review code, manage projects, and build software together.
Sign upSpecify that `bool` is compatible with the `_Bool` C type. #954
Conversation
This comment has been minimized.
This comment has been minimized.
reem
commented
Mar 8, 2015
|
+1 from me, seems like a simple and straightforward thing for us to guarantee. |
huonw
reviewed
Mar 8, 2015
|
|
||
| # Drawbacks | ||
|
|
||
| None right now. This should be a no-op on all supported platforms. |
This comment has been minimized.
This comment has been minimized.
huonw
Mar 8, 2015
Member
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.
This comment has been minimized.
This comment has been minimized.
reem
Mar 8, 2015
Unsupported platforms are not supported, hence we're not beholden to do anything on those platforms, unless I misunderstand your concern.
This comment has been minimized.
This comment has been minimized.
huonw
Mar 8, 2015
Member
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.
This comment has been minimized.
This comment has been minimized.
reem
Mar 8, 2015
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.
This comment has been minimized.
This comment has been minimized.
mahkoh
Mar 8, 2015
Author
Contributor
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?
This comment has been minimized.
This comment has been minimized.
huonw
Mar 9, 2015
Member
(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.)
This comment has been minimized.
This comment has been minimized.
mahkoh
Mar 9, 2015
Author
Contributor
The premise of this argument is pretty ridiculous: "We know better than the hardware developers how to represent a boolean value on their hardware."
This comment has been minimized.
This comment has been minimized.
huonw
Mar 9, 2015
Member
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.
This comment has been minimized.
This comment has been minimized.
mahkoh
Mar 9, 2015
Author
Contributor
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
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
|
Personally I think we should have a libc definition for |
This comment has been minimized.
This comment has been minimized.
|
A comprehensive review of sizes/alignments of C |
This comment has been minimized.
This comment has been minimized.
That would not be helpful. In C, bool is an alias for _Bool and _Bool is almost never used directly. |
This comment has been minimized.
This comment has been minimized.
So name it |
This comment has been minimized.
This comment has been minimized.
|
Sure, as long as it's typedef'd to bool and not an integer type there is no problem. |
This comment has been minimized.
This comment has been minimized.
adfernandes
commented
Mar 12, 2015
|
FYI, the embedded world is chock-full of conflicting |
This comment has been minimized.
This comment has been minimized.
|
I don't see much sense in |
This comment has been minimized.
This comment has been minimized.
adfernandes
commented
Mar 12, 2015
|
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 |
alexcrichton
referenced this pull request
Mar 19, 2015
Closed
Add a type which is equivalent to _Bool in C99 #992
This comment has been minimized.
This comment has been minimized.
|
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! |
alexcrichton
closed this
Mar 19, 2015
alexcrichton
added
the
postponed
label
Mar 19, 2015
This comment has been minimized.
This comment has been minimized.
jimblandy
commented
Jan 7, 2016
|
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 |
This comment has been minimized.
This comment has been minimized.
|
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. |
This comment has been minimized.
This comment has been minimized.
jimblandy
commented
Jan 7, 2016
|
@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 |
This comment has been minimized.
This comment has been minimized.
|
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. |
This comment has been minimized.
This comment has been minimized.
|
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. |
This comment has been minimized.
This comment has been minimized.
+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" |
mahkoh commentedMar 8, 2015
Specify that
boolis compatible with the_BoolC type.Rendered