Skip to content

Invalid type for bit-field smaller than 32 bits #42784

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

Closed
llvmbot opened this issue Sep 25, 2019 · 1 comment
Closed

Invalid type for bit-field smaller than 32 bits #42784

llvmbot opened this issue Sep 25, 2019 · 1 comment
Labels
bugzilla Issues migrated from bugzilla c11 invalid Resolved as invalid, i.e. not a bug

Comments

@llvmbot
Copy link
Member

llvmbot commented Sep 25, 2019

Bugzilla Link 43439
Resolution INVALID
Resolved on Sep 25, 2019 23:21
Version 9.0
OS All
Reporter LLVM Bugzilla Contributor
CC @DougGregor,@zygoloid

Extended Description

Here is the reproduction of the bug: https://godbolt.org/z/_IWIqC

The compilation fails on the static_assert, while it shouldn't based on the C11 standard (see quoting below in this post).

The code example below was compiled with the following options: -O3 -std=c11 -Wall -Wextra -v

#include <stdint.h>
#include <assert.h>

#define CHECK_NUM(num) _Generic((num),
uint64_t : 0,
int64_t : 0,
default : 1)
int main(void)
{
struct _s
{
uint64_t val : 1;
} s = {0};

static_assert(CHECK_NUM(s.val), "Variable too large #&#8203;1"); // <strong>Should not fail here</strong>

return 0;

}

The compilation output:

clang version 9.0.0 (tags/RELEASE_900/final 372344)
Target: x86_64-unknown-linux-gnu
Thread model: posix
InstalledDir: /opt/compiler-explorer/clang-9.0.0/bin
Found candidate GCC installation: /opt/compiler-explorer/gcc-9.2.0/lib/gcc/x86_64-linux-gnu/9.2.0
Selected GCC installation: /opt/compiler-explorer/gcc-9.2.0/lib/gcc/x86_64-linux-gnu/9.2.0
Candidate multilib: .;@m64
Candidate multilib: 32;@m32
Candidate multilib: x32;@MX32
Selected multilib: .;@m64

"/opt/compiler-explorer/clang-9.0.0/bin/clang-9" -cc1 -triple x86_64-unknown-linux-gnu -S -disable-free -disable-llvm-verifier -discard-value-names -main-file-name example.c -mrelocation-model static -mthread-model posix -fmath-errno -masm-verbose -mconstructor-aliases -munwind-tables -fuse-init-array -target-cpu x86-64 -dwarf-column-info -debug-info-kind=limited -dwarf-version=4 -debugger-tuning=gdb -momit-leaf-frame-pointer -v -coverage-notes-file /home/ubuntu/./output.gcno -resource-dir /opt/compiler-explorer/clang-9.0.0/lib/clang/9.0.0 -internal-isystem /usr/local/include -internal-isystem /opt/compiler-explorer/clang-9.0.0/lib/clang/9.0.0/include -internal-externc-isystem /usr/include/x86_64-linux-gnu -internal-externc-isystem /include -internal-externc-isystem /usr/include -O3 -Wall -Wextra -std=c11 -fdebug-compilation-dir /home/ubuntu -ferror-limit 19 -fmessage-length 0 -fobjc-runtime=gcc -fdiagnostics-show-option -fcolor-diagnostics -vectorize-loops -vectorize-slp -mllvm --x86-asm-syntax=intel -faddrsig -o ./output.s -x c

clang -cc1 version 9.0.0 based upon LLVM 9.0.0 default target x86_64-unknown-linux-gnu

ignoring nonexistent directory "/include"
#include "..." search starts here:
#include <...> search starts here:
/usr/local/include
/opt/compiler-explorer/clang-9.0.0/lib/clang/9.0.0/include
/usr/include/x86_64-linux-gnu
/usr/include

End of search list.

:15:5: error: static_assert failed "Variable too large #​1"
static_assert(CHECK_NUM(s.val), "Variable too large #&#8203;1"); // Should not fail here

^             ~~~~~~~~~~~~~~~~

/usr/include/assert.h:143:24: note: expanded from macro 'static_assert'

define static_assert _Static_assert

                   ^

1 error generated.
Compiler returned: 1


Based on the C11 standard section 6.3.1.1 Boolean, characters and integers :

If an int can represent all values of the original type (as restricted by the width, for a bit-field), the value is converted to an int; otherwise, it is converted to an unsigned int. These are called the integer promotions.) All other types are unchanged by the integer promotions.

I would expect _Generic((s.val), ...) to return 1 for int in this case.

@llvmbot
Copy link
Member Author

llvmbot commented Sep 26, 2019

Not related to integer promotion.

This issue was closed.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bugzilla Issues migrated from bugzilla c11 invalid Resolved as invalid, i.e. not a bug
Projects
None yet
Development

No branches or pull requests

1 participant