<table border="1" cellspacing="0" cellpadding="8">
    <tr>
        <th>Issue</th>
        <td>
            <a href=https://github.com/llvm/llvm-project/issues/55829>55829</a>
        </td>
    </tr>

    <tr>
        <th>Summary</th>
        <td>
            bogus std::initializer_list contains unexpectedly large amount of items
        </td>
    </tr>

    <tr>
      <th>Labels</th>
      <td>
            new issue
      </td>
    </tr>

    <tr>
      <th>Assignees</th>
      <td>
      </td>
    </tr>

    <tr>
      <th>Reporter</th>
      <td>
          nkolotov
      </td>
    </tr>
</table>

<pre>
    Various clang versions (including 14.0) compile the following potentially invalid (g++ and vc++ reject) code snippet with C++11/14/17/20 standard:
```
#include <initializer_list>
#include <iostream>

struct foo
{
    int x;
    int y;
    char const * psz;
};

void bar(::std::initializer_list<foo> items)
{
    ::std::cout << "items count " << items.size() << ::std::endl;
    for (auto const & item: items)
    {
        ::std::cout << item.x << " "  << item.y << " "
        << (item.psz ? item.psz : "(null)") << ::std::endl;
    }
    ::std::cout << "done";
}

int main()
{
    bar(::std::initializer_list<foo>{1, 2, "asd", 3, 4, "ertw"});
    return 0;
}
```
output:
>items count 6
1 2 asd
3 4 ertw
0 0 (null)
0 0 (null)
0 0 (null)
0 0 (null)
done

[online compiler](https://godbolt.org/#g:!((g:!((g:!((h:codeEditor,i:(filename:'1',fontScale:14,fontUsePx:'0',j:1,lang:c%2B%2B,selection:(endColumn:63,endLineNumber:24,positionColumn:63,positionLineNumber:24,selectionStartColumn:63,selectionStartLineNumber:24,startColumn:63,startLineNumber:24),source:'%23include+%3Cinitializer_list%3E%0A%23include+%3Ciostream%3E%0A%0Astruct+foo%0A%7B%0A++++int+x%3B%0A++++int+y%3B%0A++++char+const+*+psz%3B%0A%7D%3B%0A%0Avoid+bar(::std::initializer_list%3Cfoo%3E+items)%0A%7B%0A++++::std::cout+%3C%3C+%22items+count+%22+%3C%3C+items.size()+%3C%3C+::std::endl%3B%0A++++for+(auto+const+%26+item:+items)%0A++++%7B%0A++++++++::std::cout+%3C%3C+item.x+%3C%3C+%22+%22++%3C%3C+item.y+%3C%3C+%22+%22%0A++++++++%3C%3C+(item.psz+%3F+item.psz+:+%22(null)%22)+%3C%3C+::std::endl%3B%0A++++%7D%0A++++::std::cout+%3C%3C+%22done%22%3B%0A%7D%0A%0Aint+main()%0A%7B%0A++++bar(::std::initializer_list%3Cfoo%3E%7B1,+2,+%22asd%22,+3,+4,+%22ertw%22%7D)%3B%0A++++return+0%3B%0A%7D%0A'),l:'5',n:'0',o:'C%2B%2B+source+%231',t:'0')),k:50.784929356357935,l:'4',n:'0',o:'',s:0,t:'0'),(g:!((g:!((h:compiler,i:(compiler:clang1400,filters:(b:'0',binary:'1',commentOnly:'0',demangle:'0',directives:'0',execute:'0',intel:'0',libraryCode:'0',trim:'1'),flagsViewOpen:'1',fontScale:14,fontUsePx:'0',j:1,lang:c%2B%2B,libs:!(),options:'--std%3Dc%2B%2B20',selection:(endColumn:1,endLineNumber:1,positionColumn:1,positionLineNumber:1,selectionStartColumn:1,selectionStartLineNumber:1,startColumn:1,startLineNumber:1),source:1,tree:'1'),l:'5',n:'0',o:'x86-64+clang+14.0.0+(C%2B%2B,+Editor+%231,+Compiler+%231)',t:'0')),k:50,l:'4',m:54.98575498575499,n:'0',o:'',s:0,t:'0'),(g:!((h:output,i:(compilerName:'x86-64+gcc+12.1',editorid:1,fontScale:14,fontUsePx:'0',j:1,wrap:'1'),l:'5',n:'0',o:'Output+of+x86-64+clang+14.0.0+(Compiler+%231)',t:'0')),header:(),l:'4',m:45.014245014245006,n:'0',o:'',s:0,t:'0')),k:49.21507064364208,l:'3',n:'0',o:'',t:'0')),l:'2',n:'0',o:'',t:'0')),version:4)

Surprisingly, amount of items in the resulting object seems to be equal to the amount of initializers inside of curly braces and since there are insufficient initializers only the first couple of objects are actually properly initialized while the rest are left zero-initialized. Since both g++ and vc++ reject this code i suspect that it may be indeed invalid and clang should reject it as well rather than produce bogus initializer_list content.

Workaround: Enabling warnings (-Wall) causes [infamous `suggest braces around initialization of subobject [-Wmissing-braces]` warning](https://stackoverflow.com/search?q=suggest+braces+around+initialization) to be emitted. Adding suggested braces does help to eliminate the problem (and makes g++ and vc++ compile code as well) and produces list with 2 items:
```
bar(::std::initializer_list<foo>{{1, 2, "asd"}, {3, 4, "ertw"}});
```

Additional context: At the codebase where i encountered this problem `::std::initializer_list<foo>` is used as a type alias replacing a couple of `::std::array<foo, 2>`, `::std::array<foo, 4>` types. It was working fine while `array` was used and it seemed to be a good idea to get rid of unnecessary overloads taking `array` types of different sizes and to use a single overload taking `initializer_list` as a lightweight array proxy instead.
</pre>
<img width="1px" height="1px" alt="" src="http://email.email.llvm.org/o/eJytGNtu6zjua9IXoYGvuTzkoUlbYIHFnAUOds7jQLGVRFPF8khy08zXL0nJiZ04ve0UqWxRIsU7La51eVz8zo3UjWWF4tWWvQpjpa4sGyUzWRWqKSVA42wcjZI5K_S-lkowtxNso5XSB1yttROVk1ypI5PVK1eyRPTtKFnCj_GqZK9FmBjxpyicp1UKZitZ18Kxg3Q7tvJ74niUPMcZDlMYkohZBzS4KUfpwyh6HEUPo0kUfn6apJ5XwUbpSlYSmZF_C_OHktaN0qfBbdo6I_j-vEwjAJvCgXQ6QKdL_8LgT1aOvY3SC8ixByl23IBwlXWghAdW279Py6Pp4_mdxlcNulpzA-pC4VI4vvQv11KskKf0iUkn9hY0OMDfBY1CNw5FhR_wkhAisNZUyFrSrhB4bOEk5AIs02L0iYmqVD1BN9qgmXnj9EneCVGD3RdcEnNdTj_gFrHHbx3e6b-3erxYvaQd1ma0GawAoGfWmTwQUjKrGqWQT5x8Wna05Ce1XupKIPGuE3Q8AB1oz2XldT9g0695B6BC-KxYggMcy21Jkq1YikMWwMK4A8KBFzi0K5kRrjEViwb47QcdSFk37hyS6VPXvyYeGrOEIQs0SVnG6GCaRSxiHfX_3zDSc0exo3ypKyUr0WYtM8pB2tnOudoi28kz_La6XGvlxtpsCZBuaSkmc8xuT3Zk6FI8ldJpMNBK0upsAwdVfC9oNoXNkMLANJX7WXCFUExsB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