Bug 141935

Summary: WTF::WeakPtr should rename 'forget' to 'clear', and support assignment from nullptr
Product: WebKit Reporter: Brent Fulgham <bfulgham>
Component: WebCore Misc.Assignee: Brent Fulgham <bfulgham>
Status: RESOLVED FIXED    
Severity: Normal CC: andersca, ap, benjamin, bfulgham, cdumez, cmarcelo, commit-queue, darin, rniwa
Priority: P2    
Version: 528+ (Nightly build)   
Hardware: All   
OS: All   
Bug Depends on: 141923    
Bug Blocks:    
Attachments:
Description Flags
Patch mmaxfield: review+

Brent Fulgham
Reported 2015-02-23 16:47:39 PST
After some bike shedding, we decided that: 1. The new "forget" method added in Bug 141923 should be renamed "clear". 2. We really should support assignment from nullptr.
Attachments
Patch (4.67 KB, patch)
2015-02-23 16:56 PST, Brent Fulgham
mmaxfield: review+
Brent Fulgham
Comment 1 2015-02-23 16:56:21 PST
Myles C. Maxfield
Comment 2 2015-02-23 16:59:42 PST
Comment on attachment 247168 [details] Patch View in context: https://bugs.webkit.org/attachment.cgi?id=247168&action=review > Source/WTF/wtf/WeakPtr.h:103 > + WeakPtr& operator=(std::nullptr_t) { m_ref = WeakReference<T>::create(nullptr); return *this; } Is it valuable to use the argument here? (for performance, obviously correctness is right) > Tools/TestWebKitAPI/Tests/WTF/WeakPtr.cpp:183 > + weakPtr7 = nullptr; I wish there was a way we could test compilation failures, so we could make sure that operator=() never works with raw pointers :(
Brent Fulgham
Comment 3 2015-02-23 17:02:46 PST
(In reply to comment #2) > Comment on attachment 247168 [details] > Patch > > View in context: > https://bugs.webkit.org/attachment.cgi?id=247168&action=review > > > Source/WTF/wtf/WeakPtr.h:103 > > + WeakPtr& operator=(std::nullptr_t) { m_ref = WeakReference<T>::create(nullptr); return *this; } > > Is it valuable to use the argument here? (for performance, obviously > correctness is right) I'm not sure. Maybe Anders can tell us... But since he suggested the implementation I used, I sort of doubt that it would be better ;-) > > Tools/TestWebKitAPI/Tests/WTF/WeakPtr.cpp:183 > > + weakPtr7 = nullptr; > > I wish there was a way we could test compilation failures, so we could make > sure that operator=() never works with raw pointers :( I did try adding this to the end of the WeakPtr.cpp test file: int doNotCopyMe = 152; weakPtr7 = &doNotCopyMe; The compiler properly complained: /Volumes/Data/Projects/WebKit/OpenSource/Tools/TestWebKitAPI/Tests/WTF/WeakPtr.cpp:187:14: error: no viable overloaded '=' weakPtr7 = &doNotCopyMe; ~~~~~~~~ ^ ~~~~~~~~~~~~ In file included from /Volumes/Data/Projects/WebKit/OpenSource/Tools/TestWebKitAPI/Tests/WTF/WeakPtr.cpp:29: /Volumes/Data/Projects/WebKit/OpenSource/WebKitBuild/Debug/usr/local/include/wtf/WeakPtr.h:101:14: note: candidate function not viable: no known conversion from 'int *' to 'const WTF::WeakPtr<int>' for 1st argument WeakPtr& operator=(const WeakPtr& o) { m_ref = o.m_ref.copyRef(); return *this; } ^ /Volumes/Data/Projects/WebKit/OpenSource/WebKitBuild/Debug/usr/local/include/wtf/WeakPtr.h:103:14: note: candidate function not viable: no known conversion from 'int *' to 'std::nullptr_t' (aka 'nullptr_t') for 1st argument WeakPtr& operator=(std::nullptr_t) { m_ref = WeakReference<T>::create(nullptr); return *this; } ^ /Volumes/Data/Projects/WebKit/OpenSource/WebKitBuild/Debug/usr/local/include/wtf/WeakPtr.h:102:35: note: candidate template ignored: could not match 'WeakPtr<type-parameter-0-0>' against 'int *' template<typename U> WeakPtr& operator=(const WeakPtr<U>& o) { m_ref = o.m_ref.copyRef(); return *this; } ^ 1 error generated. ** BUILD FAILED **
Brent Fulgham
Comment 4 2015-02-23 17:03:49 PST
Comment on attachment 247168 [details] Patch View in context: https://bugs.webkit.org/attachment.cgi?id=247168&action=review >>> Source/WTF/wtf/WeakPtr.h:103 >>> + WeakPtr& operator=(std::nullptr_t) { m_ref = WeakReference<T>::create(nullptr); return *this; } >> >> Is it valuable to use the argument here? (for performance, obviously correctness is right) > > I'm not sure. Maybe Anders can tell us... But since he suggested the implementation I used, I sort of doubt that it would be better ;-) I caught Anders in the hallway, and he say NO NEED!
Brent Fulgham
Comment 5 2015-02-23 17:04:11 PST
Ryosuke Niwa
Comment 7 2015-02-23 18:34:46 PST
(In reply to comment #6) > This broke the build: > https://build.webkit.org/builders/Apple%20Yosemite%20Release%20%28Build%29/ > builds/3236/steps/compile-webkit/logs/stdio Fixed in https://trac.webkit.org/changeset/180540.
Anders Carlsson
Comment 8 2015-02-24 08:51:24 PST
(In reply to comment #4) > Comment on attachment 247168 [details] > Patch > > View in context: > https://bugs.webkit.org/attachment.cgi?id=247168&action=review > > >>> Source/WTF/wtf/WeakPtr.h:103 > >>> + WeakPtr& operator=(std::nullptr_t) { m_ref = WeakReference<T>::create(nullptr); return *this; } > >> > >> Is it valuable to use the argument here? (for performance, obviously correctness is right) > > > > I'm not sure. Maybe Anders can tell us... But since he suggested the implementation I used, I sort of doubt that it would be better ;-) > > I caught Anders in the hallway, and he say NO NEED! To clarify, since nullptr_t is an "empty type", meaning that it has no members etc, it won't even be passed in a register or on the stack! It's only used for overload resolution.
Darin Adler
Comment 9 2015-02-24 19:51:18 PST
Can we just dump clear?
Darin Adler
Comment 10 2015-02-24 19:51:31 PST
Seems like "= nullptr" is sufficient.
Brent Fulgham
Comment 11 2015-02-24 19:59:00 PST
(In reply to comment #10) > Seems like "= nullptr" is sufficient. Yes, but the other smart pointer implementations do provide a 'clear' method.
Chris Dumez
Comment 12 2015-02-24 20:02:55 PST
(In reply to comment #11) > (In reply to comment #10) > > Seems like "= nullptr" is sufficient. > > Yes, but the other smart pointer implementations do provide a 'clear' method. But we prefer "= nullptr;" in new code I believe, for consistency with std::unique_ptr.
Darin Adler
Comment 13 2015-02-25 15:28:18 PST
(In reply to comment #11) > Yes, but the other smart pointer implementations do provide a 'clear' method. Ours do, but std::unique_ptr does not. I think eventually we will want to remove our clear functions. We made those back when there was no nullptr in the language.
Note You need to log in before you can comment on or make changes to this bug.