Title: [225727] trunk
Revision
225727
Author
[email protected]
Date
2017-12-09 12:34:45 -0800 (Sat, 09 Dec 2017)

Log Message

Fix WTF::Hasher tuple expansion with variadic args
https://bugs.webkit.org/show_bug.cgi?id=180623

Reviewed by JF Bastien.

Source/WTF:

We expand a tuple with `...`, and use this in a function's argument list.
And in this argument list, we call `add()` for each value. This will be
expanded as follows.

    [] (...) { }((add(hasher, std::get<i>(values)), 0)...);

will become,

    [] (...) { }((add(hasher, std::get<0>(values)), 0), (add(hasher, std::get<1>(values)), 0), ...);

However, the evaluation order of the C++ argument is unspecified. Actually,
in GCC environment, this `add()` is not called in our expected order. As a
result, currently we have test failures in TestWTF.

This patch just uses variadic templates to expand tuples, and call add() in
our expected order. This patch fixes an existing failure of TestWTF in GCC environment.

* wtf/Hasher.h:
(WTF::Hasher::computeHash):
(WTF::addArgs):
(WTF::addTupleHelper):
(WTF::add):

Tools:

* TestWebKitAPI/Tests/WTF/Hasher.cpp:
(TestWebKitAPI::TEST):

Modified Paths

Diff

Modified: trunk/Source/WTF/ChangeLog (225726 => 225727)


--- trunk/Source/WTF/ChangeLog	2017-12-09 20:11:56 UTC (rev 225726)
+++ trunk/Source/WTF/ChangeLog	2017-12-09 20:34:45 UTC (rev 225727)
@@ -1,5 +1,35 @@
 2017-12-09  Yusuke Suzuki  <[email protected]>
 
+        Fix WTF::Hasher tuple expansion with variadic args
+        https://bugs.webkit.org/show_bug.cgi?id=180623
+
+        Reviewed by JF Bastien.
+
+        We expand a tuple with `...`, and use this in a function's argument list.
+        And in this argument list, we call `add()` for each value. This will be
+        expanded as follows.
+
+            [] (...) { }((add(hasher, std::get<i>(values)), 0)...);
+
+        will become,
+
+            [] (...) { }((add(hasher, std::get<0>(values)), 0), (add(hasher, std::get<1>(values)), 0), ...);
+
+        However, the evaluation order of the C++ argument is unspecified. Actually,
+        in GCC environment, this `add()` is not called in our expected order. As a
+        result, currently we have test failures in TestWTF.
+
+        This patch just uses variadic templates to expand tuples, and call add() in
+        our expected order. This patch fixes an existing failure of TestWTF in GCC environment.
+
+        * wtf/Hasher.h:
+        (WTF::Hasher::computeHash):
+        (WTF::addArgs):
+        (WTF::addTupleHelper):
+        (WTF::add):
+
+2017-12-09  Yusuke Suzuki  <[email protected]>
+
         Use relaxed constexpr for StringHasher
         https://bugs.webkit.org/show_bug.cgi?id=180622
 

Modified: trunk/Source/WTF/wtf/Hasher.h (225726 => 225727)


--- trunk/Source/WTF/wtf/Hasher.h	2017-12-09 20:11:56 UTC (rev 225726)
+++ trunk/Source/WTF/wtf/Hasher.h	2017-12-09 20:34:45 UTC (rev 225727)
@@ -51,7 +51,7 @@
     template<typename... Types> friend uint32_t computeHash(const Types&... values)
     {
         Hasher hasher;
-        add(hasher, std::forward_as_tuple(values...));
+        addArgs(hasher, values...);
         return hasher.m_underlyingHasher.hash();
     }
 
@@ -59,7 +59,7 @@
     {
         Hasher hasher;
         add(hasher, list);
-        add(hasher, std::forward_as_tuple(otherLists...));
+        addArgs(hasher, otherLists...);
         return hasher.m_underlyingHasher.hash();
     }
 
@@ -113,9 +113,19 @@
         add(hasher, value);
 }
 
+inline void addArgs(Hasher&)
+{
+}
+
+template<typename Arg, typename ...Args> void addArgs(Hasher& hasher, const Arg& arg, const Args&... args)
+{
+    add(hasher, arg);
+    addArgs(hasher, args...);
+}
+
 template<typename Tuple, std::size_t ...i> void addTupleHelper(Hasher& hasher, const Tuple& values, std::index_sequence<i...>)
 {
-    [] (...) { }((add(hasher, std::get<i>(values)), 0)...);
+    addArgs(hasher, std::get<i>(values)...);
 }
 
 template<typename... Types> void add(Hasher& hasher, const std::tuple<Types...>& tuple)
@@ -148,7 +158,7 @@
 {
     add(hasher, value1);
     add(hasher, value2);
-    add(hasher, std::forward_as_tuple(otherValues...));
+    addArgs(hasher, otherValues...);
 }
 
 template<typename T> void add(Hasher& hasher, std::initializer_list<T> values)

Modified: trunk/Tools/ChangeLog (225726 => 225727)


--- trunk/Tools/ChangeLog	2017-12-09 20:11:56 UTC (rev 225726)
+++ trunk/Tools/ChangeLog	2017-12-09 20:34:45 UTC (rev 225727)
@@ -1,3 +1,13 @@
+2017-12-09  Yusuke Suzuki  <[email protected]>
+
+        Fix WTF::Hasher tuple expansion with variadic args
+        https://bugs.webkit.org/show_bug.cgi?id=180623
+
+        Reviewed by JF Bastien.
+
+        * TestWebKitAPI/Tests/WTF/Hasher.cpp:
+        (TestWebKitAPI::TEST):
+
 2017-12-09  Konstantin Tokarev  <[email protected]>
 
         [python] Replace print >> operator with print() function for python3 compatibility

Modified: trunk/Tools/TestWebKitAPI/Tests/WTF/Hasher.cpp (225726 => 225727)


--- trunk/Tools/TestWebKitAPI/Tests/WTF/Hasher.cpp	2017-12-09 20:11:56 UTC (rev 225726)
+++ trunk/Tools/TestWebKitAPI/Tests/WTF/Hasher.cpp	2017-12-09 20:34:45 UTC (rev 225727)
@@ -169,6 +169,7 @@
     EXPECT_EQ(zero32BitHash, computeHash(Vector<int> { 0 }));
     EXPECT_EQ(zero32BitHash, computeHash(Vector<int, 1> { 0 }));
     EXPECT_EQ(zero32BitHash, computeHash(std::optional<int> { std::nullopt }));
+    EXPECT_EQ(zero32BitHash, computeHash(std::make_tuple(0)));
 
     EXPECT_EQ(one64BitHash, computeHash(1, 0));
     EXPECT_EQ(one64BitHash, computeHash(std::make_tuple(1, 0)));
_______________________________________________
webkit-changes mailing list
[email protected]
https://lists.webkit.org/mailman/listinfo/webkit-changes

Reply via email to