Bug 156266

Summary: Implement operator== and operator!= for Optional<>
Product: WebKit Reporter: Simon Fraser (smfr) <simon.fraser>
Component: New BugsAssignee: Simon Fraser (smfr) <simon.fraser>
Status: RESOLVED FIXED    
Severity: Normal CC: andersca, benjamin, cdumez, cmarcelo, commit-queue, mmaxfield, sam, simon.fraser
Priority: P2    
Version: WebKit Nightly Build   
Hardware: Unspecified   
OS: Unspecified   
Bug Depends on:    
Bug Blocks: 156274    
Attachments:
Description Flags
Patch andersca: review+

Description Simon Fraser (smfr) 2016-04-05 17:21:44 PDT
Implement operator== and operator!= for Optional<>
Comment 1 Simon Fraser (smfr) 2016-04-05 17:22:48 PDT
Created attachment 275726 [details]
Patch
Comment 2 WebKit Commit Bot 2016-04-05 17:23:33 PDT
Attachment 275726 [details] did not pass style-queue:


ERROR: Tools/TestWebKitAPI/Tests/WTF/Optional.cpp:115:  Consider using EXPECT_EQ instead of EXPECT_TRUE(a == b)  [readability/check] [2]
ERROR: Tools/TestWebKitAPI/Tests/WTF/Optional.cpp:116:  Consider using EXPECT_EQ instead of EXPECT_TRUE(a == b)  [readability/check] [2]
ERROR: Tools/TestWebKitAPI/Tests/WTF/Optional.cpp:117:  Consider using EXPECT_NE instead of EXPECT_FALSE(a == b)  [readability/check] [2]
ERROR: Tools/TestWebKitAPI/Tests/WTF/Optional.cpp:118:  Consider using EXPECT_NE instead of EXPECT_FALSE(a == b)  [readability/check] [2]
ERROR: Tools/TestWebKitAPI/Tests/WTF/Optional.cpp:140:  Consider using EXPECT_EQ instead of EXPECT_FALSE(a != b)  [readability/check] [2]
ERROR: Tools/TestWebKitAPI/Tests/WTF/Optional.cpp:141:  Consider using EXPECT_NE instead of EXPECT_TRUE(a != b)  [readability/check] [2]
ERROR: Tools/TestWebKitAPI/Tests/WTF/Optional.cpp:142:  Consider using EXPECT_EQ instead of EXPECT_FALSE(a != b)  [readability/check] [2]
ERROR: Tools/TestWebKitAPI/Tests/WTF/Optional.cpp:143:  Consider using EXPECT_NE instead of EXPECT_TRUE(a != b)  [readability/check] [2]
ERROR: Tools/TestWebKitAPI/Tests/WTF/Optional.cpp:145:  Consider using EXPECT_NE instead of EXPECT_TRUE(a != b)  [readability/check] [2]
ERROR: Tools/TestWebKitAPI/Tests/WTF/Optional.cpp:146:  Consider using EXPECT_NE instead of EXPECT_TRUE(a != b)  [readability/check] [2]
Total errors found: 10 in 4 files


If any of these errors are false positives, please file a bug against check-webkit-style.
Comment 3 Myles C. Maxfield 2016-04-05 17:28:33 PDT
Comment on attachment 275726 [details]
Patch

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

I think I should wait for Anders to give the r+, but LGTM.

> Source/WTF/wtf/Optional.h:202
> +    return static_cast<bool>(lhs) == static_cast<bool>(rhs) && (!static_cast<bool>(lhs) || lhs.value() == rhs.value());

Wouldn't an if statement be cleaner?

Are these taken directly from C++ docs?

> Source/WTF/wtf/Optional.h:256
> +    return !(value == opt);

Perhaps it would be useful to make one with two template types, and static_cast one to the other, to try to compare things which aren't the same type but are convertible.
Comment 4 Simon Fraser (smfr) 2016-04-05 17:31:30 PDT
(In reply to comment #2)
> Attachment 275726 [details] did not pass style-queue:
> 
> 
> ERROR: Tools/TestWebKitAPI/Tests/WTF/Optional.cpp:115:  Consider using
> EXPECT_EQ instead of EXPECT_TRUE(a == b)  [readability/check] [2]

Using these didn't compile.(In reply to comment #3)

> Comment on attachment 275726 [details]
> Patch
> 
> View in context:
> https://bugs.webkit.org/attachment.cgi?id=275726&action=review
> 
> I think I should wait for Anders to give the r+, but LGTM.
> 
> > Source/WTF/wtf/Optional.h:202
> > +    return static_cast<bool>(lhs) == static_cast<bool>(rhs) && (!static_cast<bool>(lhs) || lhs.value() == rhs.value());
> 
> Wouldn't an if statement be cleaner?

But this is branchless.
> 
> Are these taken directly from C++ docs?

Some are.
Comment 5 Anders Carlsson 2016-04-06 11:35:18 PDT
(In reply to comment #4)
> (In reply to comment #2)
> > Attachment 275726 [details] did not pass style-queue:
> > 
> > 
> > ERROR: Tools/TestWebKitAPI/Tests/WTF/Optional.cpp:115:  Consider using
> > EXPECT_EQ instead of EXPECT_TRUE(a == b)  [readability/check] [2]
> 
> Using these didn't compile.(In reply to comment #3)
> 
> > Comment on attachment 275726 [details]
> > Patch
> > 
> > View in context:
> > https://bugs.webkit.org/attachment.cgi?id=275726&action=review
> > 
> > I think I should wait for Anders to give the r+, but LGTM.
> > 
> > > Source/WTF/wtf/Optional.h:202
> > > +    return static_cast<bool>(lhs) == static_cast<bool>(rhs) && (!static_cast<bool>(lhs) || lhs.value() == rhs.value());
> > 
> > Wouldn't an if statement be cleaner?
> 
> But this is branchless.

Pretty sure it isn't branchless. && and || always form branches. Besides, the compiler will be able to get rid of unnecessary branches - even if you use if statements.

> > 
> > Are these taken directly from C++ docs?
> 
> Some are.
Comment 6 Simon Fraser (smfr) 2016-04-06 12:04:00 PDT
http://trac.webkit.org/changeset/199107