ShiftHandler: QSet → std::array [1/4], fixing UB (ODR violation)


This module does not own the type QLocale::Language, so it MUST NOT
define a qHash() for it, but this is what
2204663adad9d372197398cbde242d1b698096a1 did.

Unfortunately, the commit message of the reverted change gives no
futher details on the change: not the compile error purportedly fixed
by this local qHash() function, nor a discussion about why it was
believed this change was BC.

This is not theoretical: QLocale::Language is an enum, so is hashable
using the generic enum qHash() function outside the shifthandler.cpp
TU, yielding some hash value, and hashable _differently_ inside the
shifthandler.cpp TU. If a library is linked statically, it's undefined
which QSet/QHash instantiation with Language as a key "wins". One
does, and the insertion/extraction functions of the other will use a
different hash value for equal Languages. Boom.

Instead of just removing the qHash() overload and hoping that the
unknown compiler bug has vanished by now, port completely away from
QSet<Language>:

The QSet<Language>, along with the other three QSets, were never
modified once constructed, and they were constructed with a statically
known number of entries, so they violated the Golden Rule of Container
Selection:

  Never use a dynamically-sized container for statically-sized data.

In addition, the entries were always the same, so there's no need for
them to be instance variables, they can be class variables^Wconstants
instead, and, since we're in a .cpp file, they can even be
file-static/constexpr.

Finally, there are a maximum of eight entries in these QSets, making
the use of a hashed container overkill. Linear search in a std::array
will be just as fast, if not faster.

So write a tiny wrapper around std::array that adds the missing
contains() call and use it to turn the per-object QSet into a
namespace-level constexpr object. Take some pain in enabling CTAD to
avoid having to specify the number of elements manually.

As a drive-by, reorder the entries so they are sorted numerically, in
case we ever want to use binary search on them.

As a consequence, we no longer need the qHash(QLocale::Language) that
could cause ODR violations.

Amends a3e56451e63a1f39ba089c1a422c3dda72557e5a (5.6/"1.3"), which
added the QSet, and 2204663adad9d372197398cbde242d1b698096a1 (6.1),
which added the local qHash().

Pick-to: 6.11 6.8 6.5
Change-Id: I5cf82e723ba3ed2012de1c9494cd39e6e9ac9590
Reviewed-by: Volker Hilsheimer <volker.hilsheimer@qt.io>
commit9b5720967e44600d8213c0666978c7c5e0a23c0f
parent0c6991b2d9c58b003b8787894769e101aea25e96
authorMarc Mutz <marc.mutz@qt.io>
committerMarc Mutz <marc.mutz@qt.io>
commit timeWed, 6 May 2026 15:04:49 +0000