]> git.ipfire.org Git - thirdparty/gcc.git/commitdiff
c: Reject vector type bit-fields [PR123018]
authorJakub Jelinek <jakub@redhat.com>
Tue, 9 Dec 2025 09:22:26 +0000 (10:22 +0100)
committerJakub Jelinek <jakub@gcc.gnu.org>
Tue, 9 Dec 2025 09:22:26 +0000 (10:22 +0100)
The following testcase ICEs since checking has been added to TYPE_PRECISION
macro.  check_bitfield_type_and_width is called when attributes haven't
been applied to the bit-field decl yet and so it still has INTEGER_TYPE
type, while at finish_struct time it already has VECTOR_TYPE.

The following patch just repeats the check_bitfield_type_and_width
in there.

Another option would be let handle_vector_size_attribute check for
bit-fields and error out there.

2025-12-09  Jakub Jelinek  <jakub@redhat.com>

PR c/123018
* c-decl.cc (finish_struct): Diagnose bit-fields with vector type.

* gcc.dg/pr123018.c: New test.

gcc/c/c-decl.cc
gcc/testsuite/gcc.dg/pr123018.c [new file with mode: 0644]

index a02fed1cde4dcd72c726100c8feff8fd98601b66..e79f77e688329d431a80f2642eb98da75283ee09 100644 (file)
@@ -9774,6 +9774,13 @@ finish_struct (location_t loc, tree t, tree fieldlist, tree attributes,
          unsigned HOST_WIDE_INT width
            = tree_to_uhwi (DECL_INITIAL (field));
          tree type = TREE_TYPE (field);
+         if (VECTOR_TYPE_P (type))
+           {
+             error_at (DECL_SOURCE_LOCATION (field),
+                       "bit-field %qD has invalid type", field);
+             type = TREE_TYPE (type);
+             TREE_TYPE (field) = type;
+           }
          if (width != TYPE_PRECISION (type))
            {
              if (TREE_CODE (type) == BITINT_TYPE
diff --git a/gcc/testsuite/gcc.dg/pr123018.c b/gcc/testsuite/gcc.dg/pr123018.c
new file mode 100644 (file)
index 0000000..f1f701b
--- /dev/null
@@ -0,0 +1,17 @@
+/* PR c/123018 */
+/* { dg-do compile } */
+
+struct A {
+  int x : 8 __attribute__ ((vector_size (8))); /* { dg-error "bit-field 'x' has invalid type" } */
+};
+struct B {
+  float x : 8;                                 /* { dg-error "bit-field 'x' has invalid type" } */
+};
+struct C {
+  int : 8 __attribute__ ((vector_size (8)));   /* { dg-error "bit-field '\[^\n\r]*anonymous\[^\n\r]*' has invalid type" } */
+  int x;
+};
+struct D {
+  float : 8;                                   /* { dg-error "bit-field '\[^\n\r]*anonymous\[^\n\r]*' has invalid type" } */
+  int x;
+};