⚠ Archived content — this site is no longer maintained.   Current WebKit documentation is at docs.webkit.org.

Changeset 242778 in webkit


Ignore:
Timestamp:
Mar 11, 2019, 11:04:22 PM (7 years ago)
Author:
commit-queue@webkit.org
Message:

WTF::Expected should use std::addressof instead of operator&
https://bugs.webkit.org/show_bug.cgi?id=195604

Patch by Alex Christensen <achristensen@webkit.org> on 2019-03-11
Reviewed by Myles Maxfield.

Source/WTF:

The latter was causing problems with types that do tricky things with constructors and operator&,
specifically UniqueRef but I made a reduced test case. When it used operator&, it would get the contained
type and call the constructor that takes a contained type instead of the move constructor.

  • wtf/Expected.h:

(std::experimental::fundamentals_v3::expected_detail::base::base):
(std::experimental::fundamentals_v3::expected::swap):

Tools:

  • TestWebKitAPI/Tests/WTF/Expected.cpp:

(TestWebKitAPI::Unique::Unique):
(TestWebKitAPI::Unique::operator&):
(TestWebKitAPI::TEST):

Location:
trunk
Files:
4 edited

Legend:

Unmodified
Added
Removed
  • trunk/Source/WTF/ChangeLog

    r242776 r242778  
     12019-03-11  Alex Christensen  <achristensen@webkit.org>
     2
     3        WTF::Expected should use std::addressof instead of operator&
     4        https://bugs.webkit.org/show_bug.cgi?id=195604
     5
     6        Reviewed by Myles Maxfield.
     7
     8        The latter was causing problems with types that do tricky things with constructors and operator&,
     9        specifically UniqueRef but I made a reduced test case.  When it used operator&, it would get the contained
     10        type and call the constructor that takes a contained type instead of the move constructor.
     11
     12        * wtf/Expected.h:
     13        (std::experimental::fundamentals_v3::__expected_detail::base::base):
     14        (std::experimental::fundamentals_v3::expected::swap):
     15
    1162019-03-11  Ross Kirsling  <ross.kirsling@sony.com>
    217
  • trunk/Source/WTF/wtf/Expected.h

    r242776 r242778  
    335335    {
    336336        if (has)
    337             ::new (&s.val) value_type(o.s.val);
     337            ::new (std::addressof(s.val)) value_type(o.s.val);
    338338        else
    339             ::new (&s.err) error_type(o.s.err);
     339            ::new (std::addressof(s.err)) error_type(o.s.err);
    340340    }
    341341    base(base&& o)
     
    343343    {
    344344        if (has)
    345             ::new (&s.val) value_type(std::move(o.s.val));
     345            ::new (std::addressof(s.val)) value_type(std::move(o.s.val));
    346346        else
    347             ::new (&s.err) error_type(std::move(o.s.err));
     347            ::new (std::addressof(s.err)) error_type(std::move(o.s.err));
    348348    }
    349349    ~base()
     
    387387    {
    388388        if (!has)
    389             ::new (&s.err) error_type(o.s.err);
     389            ::new (std::addressof(s.err)) error_type(o.s.err);
    390390    }
    391391    base(base&& o)
     
    393393    {
    394394        if (!has)
    395             ::new (&s.err) error_type(std::move(o.s.err));
     395            ::new (std::addressof(s.err)) error_type(std::move(o.s.err));
    396396    }
    397397    ~base()
     
    462462            error_type e(std::move(o.s.err));
    463463            __expected_detail::destroy(o.s.err);
    464             ::new (&o.s.val) value_type(std::move(base::s.val));
     464            ::new (std::addressof(o.s.val)) value_type(std::move(base::s.val));
    465465            __expected_detail::destroy(base::s.val);
    466             ::new (&base::s.err) error_type(std::move(e));
     466            ::new (std::addressof(base::s.err)) error_type(std::move(e));
    467467            swap(base::has, o.has);
    468468        } else if (!base::has && o.has) {
    469469            value_type v(std::move(o.s.val));
    470470            __expected_detail::destroy(o.s.val);
    471             ::new (&o.s.err) error_type(std::move(base::s.err));
     471            ::new (std::addressof(o.s.err)) error_type(std::move(base::s.err));
    472472            __expected_detail::destroy(base::s.err);
    473             ::new (&base::s.val) value_type(std::move(v));
     473            ::new (std::addressof(base::s.val)) value_type(std::move(v));
    474474            swap(base::has, o.has);
    475475        } else
     
    537537        } else if (base::has && !o.has) {
    538538            error_type e(std::move(o.s.err));
    539             ::new (&base::s.err) error_type(e);
     539            ::new (std::addressof(base::s.err)) error_type(e);
    540540            swap(base::has, o.has);
    541541        } else if (!base::has && o.has) {
    542             ::new (&o.s.err) error_type(std::move(base::s.err));
     542            ::new (std::addressof(o.s.err)) error_type(std::move(base::s.err));
    543543            swap(base::has, o.has);
    544544        } else
  • trunk/Tools/ChangeLog

    r242776 r242778  
     12019-03-11  Alex Christensen  <achristensen@webkit.org>
     2
     3        WTF::Expected should use std::addressof instead of operator&
     4        https://bugs.webkit.org/show_bug.cgi?id=195604
     5
     6        Reviewed by Myles Maxfield.
     7
     8        * TestWebKitAPI/Tests/WTF/Expected.cpp:
     9        (TestWebKitAPI::Unique::Unique):
     10        (TestWebKitAPI::Unique::operator&):
     11        (TestWebKitAPI::TEST):
     12
    1132019-03-11  Ross Kirsling  <ross.kirsling@sony.com>
    214
  • trunk/Tools/TestWebKitAPI/Tests/WTF/Expected.cpp

    r225499 r242778  
    473473}
    474474
     475class NeedsStdAddress {
     476public:
     477    NeedsStdAddress(NeedsStdAddress&& other)
     478        : m_ptr(WTFMove(other.m_ptr)) { }
     479    NeedsStdAddress(int& other)
     480        : m_ptr(&other) { }
     481    int* operator&() { ASSERT_NOT_REACHED(); return nullptr; }
     482private:
     483    std::unique_ptr<int> m_ptr;
     484};
     485
     486TEST(WTF_Expected, Address)
     487{
     488    NeedsStdAddress a(*new int(3));
     489    Expected<NeedsStdAddress, float> b(WTFMove(a));
     490    Expected<NeedsStdAddress, float> c(WTFMove(b));
     491    (void)c;
     492}
     493
    475494} // namespace TestWebkitAPI
Note: See TracChangeset for help on using the changeset viewer.