Skip to content

[MSYS2] Ordered dynamic initialization is not sequenced #55938

Closed
@silverqx

Description

@silverqx

Following simple code example crashes or doesn't work correctly on MSYS2 UCRT64 clang++ and on MSYS2 CLANG64 clang++. All other compilers don't have problem with this, gcc/clang on linux, msvc or clang-cl msvc and also MSYS2 UCRT64 g++.

It crashes because ConstAppInline::LEFT is uninitialized during the Model::m_fillable initialization.

I think that this is happening, here should apply Ordered dynamic initialization , so the initialization should be sequenced:

within a single translation unit, initialization of these variables is always sequenced in exact order their definitions appear in the source code.

The consequence is that you can not initialize inline static data member with Inline const variables at namespace scope variable with external linkage. ☝️

Click to expand!
#include <iostream>
#include <string>
#include <vector>

//#include <QDebug>
//#include <QStringLiteral>

namespace ConstAppInline
{

   // Common chars
//    inline const QChar DOT = QChar('.');

   // Common strings
   inline const std::string LEFT = "left";
//    inline const QString LEFT = "left";

} // namespace ConstAppInline


class Model
{
   inline static std::vector<std::string> m_fillable {ConstAppInline::LEFT};
//    inline static QStringList m_fillable {ConstAppInline::LEFT};

public:
   inline void run() const
   {
       std::cout << m_fillable.size() << "\n";

       for (const auto &s : m_fillable)
           std::cout << s << "\n";

//        qDebug() << m_fillable.size();

//        qDebug() << m_fillable;
   }
};

int main(int /*unused*/, char */*unused*/[])
{
   Model m;
   m.run();

   return 0;
}

The example contains two types, one types from stl library and other for Qt types, in both versions the ConstAppInline::LEFT is uninitialized.

Qt version crashes and the output with stl types is like following:

1

Of course, it should look like this with stl types:

1
left

And like this with Qt types:

1
QList("left")

This is the long-running bug that I'm experiencing with during the development of one of my libraries and hoped that it will be fixed, but it wasn't. The problem exists as long as I remember, from Clang 10/11.

Metadata

Metadata

Assignees

No one assigned

    Labels

    clang:driver'clang' and 'clang++' user-facing binaries. Not 'clang-cl'platform:windows

    Type

    No type

    Projects

    Status

    Done

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions