[PATCH] D129531: [clang][C++20] P0960R3: Allow initializing aggregates from a parenthesized list of values

Alan Zhao via Phabricator via cfe-commits cfe-commits at lists.llvm.org
Wed Oct 5 14:06:41 PDT 2022


ayzhao added inline comments.


================
Comment at: clang/lib/Sema/SemaInit.cpp:4151
+      QualType T = Entity.getType();
+      // FIXME: Union is unsupported.
+      int InitKind = T->isArrayType() ? 0 : 4;
----------------
ayzhao wrote:
> ayzhao wrote:
> > Hmm...it seems like as is this patch generates the correct code for parentheses-initialized `union`s:
> > 
> > ```
> > % cat ~/src/tests/test-union.cc
> > union C {
> >   float a;
> >   double b;
> > };
> > 
> > 
> > C foo() {
> >   return C(1);
> > }
> > 
> > % bin/clang++ -std=c++20 -emit-llvm -S -c -o - ~/src/tests/test-union.cc
> > ; ModuleID = '/usr/local/google/home/ayzhao/src/tests/test-union.cc'
> > source_filename = "/usr/local/google/home/ayzhao/src/tests/test-union.cc"
> > target datalayout = "e-m:e-p270:32:32-p271:32:32-p272:64:64-i64:64-f80:128-n8:16:32:64-S128"
> > target triple = "x86_64-unknown-linux-gnu"
> > 
> > %union.C = type { double }
> > 
> > ; Function Attrs: mustprogress noinline nounwind optnone uwtable
> > define dso_local double @_Z3foov() #0 {
> > entry:
> >   %retval = alloca %union.C, align 8
> >   store float 1.000000e+00, ptr %retval, align 8
> >   %coerce.dive = getelementptr inbounds %union.C, ptr %retval, i32 0, i32 0
> >   %0 = load double, ptr %coerce.dive, align 8
> >   ret double %0
> > }
> > 
> > attributes #0 = { mustprogress noinline nounwind optnone uwtable "frame-pointer"="all" "min-legal-vector-width"="0" "no-trapping-math"="true" "stack-protector-buffer-size"="8" "target-cpu"="x86-64" "target-features"="+cx8,+fxsr,+mmx,+sse,+sse2,+x87" "tune-cpu"="generic" }
> > 
> > !llvm.linker.options = !{}
> > !llvm.module.flags = !{!0, !1, !2, !3, !4}
> > !llvm.ident = !{!5}
> > 
> > !0 = !{i32 1, !"wchar_size", i32 4}
> > !1 = !{i32 8, !"PIC Level", i32 2}
> > !2 = !{i32 7, !"PIE Level", i32 2}
> > !3 = !{i32 7, !"uwtable", i32 2}
> > !4 = !{i32 7, !"frame-pointer", i32 2}
> > !5 = !{!"clang version 16.0.0"}
> > ```
> > 
> > I'm guessing this comment is specific to error messages.
> OK, I see what's going on. The following program fails to build:
> 
> ```
> #include <string>
> 
> union C {
>   int a;
>   std::string b;
> };
> 
> 
> C foo() {
>   return C("Hello, World!");
> }
> ```
> 
Actually, it turns out that the above example program is ill-formed because in a non-designated brace initialization, the parameter initializes the first element.

OTOH, there is a bug where

```
C(1, "Hello, World!");
```

compiles without errors because we check that # of parameters <= # of fields.

There may also be issues with anonymous `union`s in `struct`s - I'll have to investigate this further.


Repository:
  rG LLVM Github Monorepo

CHANGES SINCE LAST ACTION
  https://reviews.llvm.org/D129531/new/

https://reviews.llvm.org/D129531



More information about the cfe-commits mailing list