Skip to content
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

Compiler: sizeof(Bool) must be 1, not 0 #8273

Merged
merged 1 commit into from Oct 5, 2019

Conversation

asterite
Copy link
Member

@asterite asterite commented Oct 5, 2019

Fixes #8272

I don't know why LLVM returns 0 for bool (i1). It seems that in C++ sizeof(bool) is implementation defined which isn't really good, so maybe LLVM chooses to return 0.

It's also interesting to know that (before this PR):

p sizeof(Bool) # => 0
p sizeof(Bool[1]) # => 1

That is, a static array of just one bool occupies one byte, per a bool occupies 0. In all other cases you have that sizeof(T) == sizeof(T[1]) because a static array has its memory inlined.

There are many ways we can solve this: we could use sizeof(T[1]) in Pointer, when we need to know the size of memory allocated by a pointer. Or we could make sizeof(Bool) be 1. I chose the latter because I think it's more intuitive, but I'm not sure.

@asterite
Copy link
Member Author

asterite commented Oct 5, 2019

I also just checked what Go does, and unsafe.Sizeof(x) with x := true returns 1, so I think this change is fine.

@bcardiff
Copy link
Member

bcardiff commented Oct 5, 2019

So llvm informs size(i1) != size({i1})? That’s weird

@asterite asterite added this to the 0.32.0 milestone Oct 5, 2019
@asterite asterite merged commit 0bb318e into crystal-lang:master Oct 5, 2019
@asterite asterite deleted the bug/sizeof-bool branch October 5, 2019 10:42
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Array(Bool)#[](start, count) always returns an array with all elements false
2 participants