================
@@ -222,3 +222,39 @@ struct on_void_ty {
   // expected-error@+1{{field has incomplete type 'void'}}
   void wrong_ty __counted_by_or_null(count);
 };
+
+//==============================================================================
+// __counted_by_or_null on pointer members in unions
+//==============================================================================
+
+// Pointer in anonymous union with count in parent struct - OK
+struct ptr_in_anon_union_count_in_parent {
+  int count;
+  union {
+    int a;
+    struct size_known *buf __counted_by_or_null(count);
+  };
+};
+
+// Pointer in named union - ERROR
+union ptr_in_named_union {
+  int count;
+  struct size_known *buf __counted_by_or_null(count); // expected-error 
{{'counted_by_or_null' cannot be applied to a union member}}
----------------
ojhunt wrote:

I think the issue is that the error being emitted here sounds like the behavior 
being fixed here is not permitted, e,g, (harvesting the original example)

```cpp
struct buffer {
    int count;
    union {
        char *bytes __counted_by(count);  // expected-error 
{{'counted_by_or_null' cannot be applied to a union member}}
        int *ints __counted_by(count);  // expected-error 
{{'counted_by_or_null' cannot be applied to a union member}}
    };
};
```

Seems like a reasonable interpretation of what this error says.

I think I agree with @bwendling - we should have an distinct error message for 
"the size/count cannot be a union member"

https://github.com/llvm/llvm-project/pull/171996
_______________________________________________
cfe-commits mailing list
[email protected]
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits

Reply via email to