Bug 214224 - Make hasher work with tuple-like classes
Summary: Make hasher work with tuple-like classes
Status: RESOLVED FIXED
Alias: None
Product: WebKit
Classification: Unclassified
Component: New Bugs (show other bugs)
Version: WebKit Nightly Build
Hardware: Unspecified Unspecified
: P2 Normal
Assignee: Sam Weinig
URL:
Keywords: InRadar
Depends on: 214228
Blocks:
  Show dependency treegraph
 
Reported: 2020-07-11 12:13 PDT by Sam Weinig
Modified: 2020-07-12 15:09 PDT (History)
7 users (show)

See Also:


Attachments
Patch (11.45 KB, patch)
2020-07-11 12:32 PDT, Sam Weinig
no flags Details | Formatted Diff | Diff
Patch (11.40 KB, patch)
2020-07-11 12:35 PDT, Sam Weinig
darin: review+
Details | Formatted Diff | Diff
Patch + `constexpr` => `inline constexpr` template variable fix (11.96 KB, patch)
2020-07-12 01:32 PDT, Yusuke Suzuki
no flags Details | Formatted Diff | Diff

Note You need to log in before you can comment on or make changes to this bug.
Description Sam Weinig 2020-07-11 12:13:13 PDT
Make hasher work with tuple-like classes
Comment 1 Sam Weinig 2020-07-11 12:32:10 PDT Comment hidden (obsolete)
Comment 2 Sam Weinig 2020-07-11 12:35:38 PDT
Created attachment 404065 [details]
Patch
Comment 3 Darin Adler 2020-07-11 13:44:08 PDT
Comment on attachment 404065 [details]
Patch

View in context: https://bugs.webkit.org/attachment.cgi?id=404065&action=review

Thanks for finishing the job. You can tell from all the FIXME that I tried this and couldn’t figure it out.

Where’s the part of this patch where you use this in Color?

> Source/WTF/ChangeLog:25
> +        adhear to WebKit style as IsTypeComplete<>.

adhere

> Source/WTF/wtf/StdLibExtras.h:424
> +// Based on 'Detecting in C++ whether a type is defined, part 3: SFINAE and incomplete types'
> +// <https://devblogs.microsoft.com/oldnewthing/20190710-00/?p=102678>
> +template<typename, typename = void> constexpr bool IsTypeComplete = false;
> +template<typename T> constexpr bool IsTypeComplete<T, std::void_t<decltype(sizeof(T))>> = true;

Why rename IsTypeComplete? If we’re just previewing C++20, could we use the real name and inject in the std namespace? Or maybe this is the way to go.
Comment 4 Sam Weinig 2020-07-11 15:08:16 PDT
(In reply to Darin Adler from comment #3)
> Comment on attachment 404065 [details]
> Patch
> 
> View in context:
> https://bugs.webkit.org/attachment.cgi?id=404065&action=review
> 
> Thanks for finishing the job. You can tell from all the FIXME that I tried
> this and couldn’t figure it out.
> 
> Where’s the part of this patch where you use this in Color?

Since I have not landed that one yet (still needs a final r+), I figured I land this first, and land the fix for WTF first.

> 
> > Source/WTF/ChangeLog:25
> > +        adhear to WebKit style as IsTypeComplete<>.
> 
> adhere
> 
> > Source/WTF/wtf/StdLibExtras.h:424
> > +// Based on 'Detecting in C++ whether a type is defined, part 3: SFINAE and incomplete types'
> > +// <https://devblogs.microsoft.com/oldnewthing/20190710-00/?p=102678>
> > +template<typename, typename = void> constexpr bool IsTypeComplete = false;
> > +template<typename T> constexpr bool IsTypeComplete<T, std::void_t<decltype(sizeof(T))>> = true;
> 
> Why rename IsTypeComplete? If we’re just previewing C++20, could we use the
> real name and inject in the std namespace? Or maybe this is the way to go.

I don't think it's a c++20 thing, just a useful trait that Raymond Chen blogged about.

Thanks.
Comment 5 Sam Weinig 2020-07-11 15:12:46 PDT
Committed r264270: <https://trac.webkit.org/changeset/264270>
Comment 6 Radar WebKit Bug Importer 2020-07-11 15:13:15 PDT
<rdar://problem/65410178>
Comment 7 Yusuke Suzuki 2020-07-11 21:37:51 PDT
Re-opened since this is blocked by bug 214228
Comment 8 Alexey Proskuryakov 2020-07-11 21:38:56 PDT
Reverting due to broken build, failure details in Slack.
Comment 9 Yusuke Suzuki 2020-07-12 01:32:40 PDT
Created attachment 404090 [details]
Patch + `constexpr` => `inline constexpr` template variable fix
Comment 10 Yusuke Suzuki 2020-07-12 02:19:38 PDT
(In reply to Yusuke Suzuki from comment #9)
> Created attachment 404090 [details]
> Patch + `constexpr` => `inline constexpr` template variable fix

Internal one is built with this fix.
Comment 11 Yusuke Suzuki 2020-07-12 02:47:12 PDT
Committed r264277: <https://trac.webkit.org/changeset/264277>
Comment 12 Yusuke Suzuki 2020-07-12 03:39:34 PDT
Comment on attachment 404065 [details]
Patch

View in context: https://bugs.webkit.org/attachment.cgi?id=404065&action=review

I've relanded the patch with `inline constexpr` fix, and note the rationale why this is necessary here :)

> Source/WTF/wtf/Hasher.h:110
> +template<typename, typename = void> constexpr bool HasBeginFunctionMember = false;
> +template<typename T> constexpr bool HasBeginFunctionMember<T, std::void_t<decltype(std::declval<T>().begin())>> = true;

We need `inline constexpr` to make this template-variable inline-variable (C++ is super complicated!)
https://stackoverflow.com/questions/14391272/does-constexpr-imply-inline

> Source/WTF/wtf/Hasher.h:129
> +template<typename T> constexpr bool HasGetFunctionMember<T, std::void_t<decltype(std::declval<T>().template get<0>())>> = true;

Ditto. Work-around with `inline constexpr`.

>> Source/WTF/wtf/StdLibExtras.h:424
>> +template<typename T> constexpr bool IsTypeComplete<T, std::void_t<decltype(sizeof(T))>> = true;
> 
> Why rename IsTypeComplete? If we’re just previewing C++20, could we use the real name and inject in the std namespace? Or maybe this is the way to go.

Ditto :)
Comment 13 Darin Adler 2020-07-12 08:32:06 PDT
Comment on attachment 404065 [details]
Patch

View in context: https://bugs.webkit.org/attachment.cgi?id=404065&action=review

>> Source/WTF/wtf/Hasher.h:110
>> +template<typename T> constexpr bool HasBeginFunctionMember<T, std::void_t<decltype(std::declval<T>().begin())>> = true;
> 
> We need `inline constexpr` to make this template-variable inline-variable (C++ is super complicated!)
> https://stackoverflow.com/questions/14391272/does-constexpr-imply-inline

This is the first time I have ever heard of an inline variable!

>>> Source/WTF/wtf/StdLibExtras.h:424
>>> +template<typename T> constexpr bool IsTypeComplete<T, std::void_t<decltype(sizeof(T))>> = true;
>> 
>> Why rename IsTypeComplete? If we’re just previewing C++20, could we use the real name and inject in the std namespace? Or maybe this is the way to go.
> 
> Ditto :)

Sam clarified that is_type_complete is not in C++20.
Comment 14 Yusuke Suzuki 2020-07-12 15:09:31 PDT
Comment on attachment 404065 [details]
Patch

View in context: https://bugs.webkit.org/attachment.cgi?id=404065&action=review

>>> Source/WTF/wtf/Hasher.h:110
>>> +template<typename T> constexpr bool HasBeginFunctionMember<T, std::void_t<decltype(std::declval<T>().begin())>> = true;
>> 
>> We need `inline constexpr` to make this template-variable inline-variable (C++ is super complicated!)
>> https://stackoverflow.com/questions/14391272/does-constexpr-imply-inline
> 
> This is the first time I have ever heard of an inline variable!

inline variable is one of the best feature in C++17! Sometimes we, C++ developers, saw super weird linker errors about `static XXX::value` variables defined in the header because,

1. One translation unit is accessing it via reference.
2. One translation unit is building it as a constant value and do not generate a data for that.

Then linker causes link failures because (1) cannot find the referenced data. Inline variable solves this problem by ensuring that this variable is not referenced like (1).