steveire / grantlee

Libraries for text templating with Qt
Other
141 stars 50 forks source link

Enums cannot be compared in Qt 5 #89

Open awilfox opened 1 year ago

awilfox commented 1 year ago

3b1931e30a23 added operator< to MetaEnumVariable. However, contrary to the commit message, no call to QMetaType::registerComparators is made.

In Qt 6, this works because it uses introspection to determine if operator< is implemented in the class. However, in Qt 5 the call must be explicit.

Oddly, it seems to work on most platforms anyway, somehow… except 64-bit PowerPC. On this platform, using Qt 5.15 + KDE Patch Collection, and GCC 8.5.0, with -O2, or -O0 -fstack-protector -fschedule-insns -fmove-loop-invariants -finline-functions-called-once -fguess-branch-probability (minimum optimisation flags needed to trigger):

FAIL!  : TestBuiltinSyntax::testEnums(gadget-enums-compare05) Compared values are not the same                                          
   Actual   (result): "true"                                 
   Expected (output): "false"                                                                                                           
   Loc: [/usr/src/packages/user/grantlee/src/grantlee-5.3.1/templates/tests/testbuiltins.cpp(482)]
PASS   : TestBuiltinSyntax::testEnums(gadget-enums-compare06)                                                                           
FAIL!  : TestBuiltinSyntax::testEnums(gadget-enums-compare07) Compared values are not the same
   Actual   (result): "false"                                                                                                           
   Expected (output): "true"                                                                                                            
   Loc: [/usr/src/packages/user/grantlee/src/grantlee-5.3.1/templates/tests/testbuiltins.cpp(482)]                                      

Adding a call to registerComparators in testbuiltins makes it pass:

diff --git a/templates/tests/testbuiltins.cpp b/templates/tests/testbuiltins.cpp
index fe7e4ed..82a065d 100644
--- a/templates/tests/testbuiltins.cpp
+++ b/templates/tests/testbuiltins.cpp
@@ -298,6 +298,7 @@ void TestBuiltinSyntax::testObjects()
   Q_UNUSED(s3);

   QMetaType{qMetaTypeId<MetaEnumVariable>()}.create(nullptr);
+  QMetaType::registerComparators<MetaEnumVariable>();
 }

 void TestBuiltinSyntax::testTruthiness_data()
    Start 2: testbuiltins
1/1 Test #2: testbuiltins .....................   Passed    0.25 sec

100% tests passed, 0 tests failed out of 1
pinotree commented 1 year ago

Changing the test only fixes the test itself, and not comparisons done when using grantlee in some other application/library.

I submitted #91 for this.

awilfox commented 1 year ago

Thanks. I figured this would need to be called somewhere, but wasn't sure the best place for it.