[swift-evolution] [proposal]Decouple definition of Int8 from target char type
Dmitri Gribenko
gribozavr at gmail.com
Thu Feb 25 22:24:26 CST 2016
On Thu, Feb 25, 2016 at 7:47 PM, William Dillon via swift-evolution
<swift-evolution at swift.org> wrote:
> Hi All,
>
> (For best viewing, use a fixed-width font with this email)
>
> While porting Swift to ARM/Linux (and while observing other ports, such as SwiftAndroid) several of my collaborators and I have noticed inconsistencies in the handling of char. I’ve taken the liberty of producing a table to summarize these differences that will hopefully illuminate the problem.
>
> char ARM mips ppc ppc64 i386 x86_64
> Linux/ELF unsigned[1] unsigned[2] unsigned[3] unsigned[4] signed[5] signed[6]
> Mach-O signed[7] N/A signed[7] signed[7] signed[7] signed[7]
> Windows —=-=-=-=-=-=-=-=-=-=-=-=-=-=- signed —=-=-=-=-=-=-=-=-=-=-=-=-=-=-[8]
>
> Swift currently maps the Int8 type to be equal to the char type of the target platform. On targets where char is unsigned by default, Int8 becomes an unsigned 8-bit integer, which is a clear violation of the Principle of Least Astonishment. Furthermore, it is impossible to specify a signed 8-bit integer type on platforms with unsigned chars.
I'm probably misunderstanding you, but are you sure that's what is
happening? I can't imagine how the standard library would just
silently make Int8 unsigned on Linux arm.
What I would expect to happen is that on Linux arm the Clang importer
would map 'char' to UInt8, instead of mapping it to Int8 like it does
on x86_64.
> C type | Swift type
> -=-=-=-=-=-=-=-=-=-=-=-=-=-=-
> char | CChar
> unsigned char | UInt8
> signed char | Int8
This brings in the notion of the CChar type, and requires us to define
(hopefully!) some rules for type-based aliasing, since you want to be
able to freely cast UnsafePointer<CChar> to UnsafePointer<UInt8> or
UnsafePointer<Int8>.
What about a proposal where we would always map 'char' to Int8,
regardless of the C's idea of signedness?
Dmitri
--
main(i,j){for(i=2;;i++){for(j=2;j<i;j++){if(!(i%j)){j=0;break;}}if
(j){printf("%d\n",i);}}} /*Dmitri Gribenko <gribozavr at gmail.com>*/
More information about the swift-evolution
mailing list