Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor(treewide): make some move ctors noexcept where appropriate #11846

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion src/libexpr/value.hh
Original file line number Diff line number Diff line change
Expand Up @@ -141,7 +141,9 @@ public:
Value * * elems;
ListBuilder(EvalState & state, size_t size);

ListBuilder(ListBuilder && x)
// NOTE: Can be noexcept because we are just copying integral values and
// raw pointers.
ListBuilder(ListBuilder && x) noexcept
: size(x.size)
, inlineElems{x.inlineElems[0], x.inlineElems[1]}
, elems(size <= 2 ? inlineElems : x.elems)
Expand Down
2 changes: 1 addition & 1 deletion src/libstore/remote-store-connection.hh
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ struct RemoteStore::ConnectionHandle
: handle(std::move(handle))
{ }

ConnectionHandle(ConnectionHandle && h)
ConnectionHandle(ConnectionHandle && h) noexcept
: handle(std::move(h.handle))
{ }

Expand Down
3 changes: 2 additions & 1 deletion src/libstore/sqlite.hh
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,8 @@ struct SQLite
SQLite(const Path & path, SQLiteOpenMode mode = SQLiteOpenMode::Normal);
SQLite(const SQLite & from) = delete;
SQLite& operator = (const SQLite & from) = delete;
SQLite& operator = (SQLite && from) { db = from.db; from.db = 0; return *this; }
// NOTE: This is noexcept since we are only copying and assigning raw pointers.
SQLite& operator = (SQLite && from) noexcept { db = from.db; from.db = 0; return *this; }
~SQLite();
operator sqlite3 * () { return db; }

Expand Down
4 changes: 3 additions & 1 deletion src/libutil/callback.hh
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,9 @@ public:

Callback(std::function<void(std::future<T>)> fun) : fun(fun) { }

Callback(Callback && callback) : fun(std::move(callback.fun))
// NOTE: std::function is noexcept move-constructible since C++20.
Callback(Callback && callback) noexcept(std::is_nothrow_move_constructible_v<decltype(fun)>)
: fun(std::move(callback.fun))
{
auto prev = callback.done.test_and_set();
if (prev) done.test_and_set();
Expand Down
5 changes: 3 additions & 2 deletions src/libutil/file-descriptor.cc
Original file line number Diff line number Diff line change
Expand Up @@ -45,8 +45,9 @@ AutoCloseFD::AutoCloseFD() : fd{INVALID_DESCRIPTOR} {}

AutoCloseFD::AutoCloseFD(Descriptor fd) : fd{fd} {}


AutoCloseFD::AutoCloseFD(AutoCloseFD && that) : fd{that.fd}
// NOTE: This can be noexcept since we are just copying a value and resetting
// the file descriptor in the rhs.
AutoCloseFD::AutoCloseFD(AutoCloseFD && that) noexcept : fd{that.fd}
{
that.fd = INVALID_DESCRIPTOR;
}
Expand Down
2 changes: 1 addition & 1 deletion src/libutil/file-descriptor.hh
Original file line number Diff line number Diff line change
Expand Up @@ -155,7 +155,7 @@ public:
AutoCloseFD();
AutoCloseFD(Descriptor fd);
AutoCloseFD(const AutoCloseFD & fd) = delete;
AutoCloseFD(AutoCloseFD&& fd);
AutoCloseFD(AutoCloseFD&& fd) noexcept;
~AutoCloseFD();
AutoCloseFD& operator =(const AutoCloseFD & fd) = delete;
AutoCloseFD& operator =(AutoCloseFD&& fd);
Expand Down
6 changes: 5 additions & 1 deletion src/libutil/finally.hh
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,11 @@ public:
// Copying Finallys is definitely not a good idea and will cause them to be
// called twice.
Finally(Finally &other) = delete;
Finally(Finally &&other) : fun(std::move(other.fun)) {
// NOTE: Move constructor can be nothrow if the callable type is itself nothrow
// move-constructible.
Finally(Finally && other) noexcept(std::is_nothrow_move_constructible_v<Fn>)
: fun(std::move(other.fun))
{
other.movedFrom = true;
}
~Finally() noexcept(false)
Expand Down
10 changes: 9 additions & 1 deletion src/libutil/pool.hh
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,15 @@ public:
Handle(Pool & pool, std::shared_ptr<R> r) : pool(pool), r(r) { }

public:
Handle(Handle && h) : pool(h.pool), r(h.r) { h.r.reset(); }
// NOTE: Copying std::shared_ptr and calling a .reset() on it is always noexcept.
Handle(Handle && h) noexcept
: pool(h.pool)
, r(h.r)
{
static_assert(noexcept(h.r.reset()));
static_assert(noexcept(std::shared_ptr(h.r)));
h.r.reset();
}

Handle(const Handle & l) = delete;

Expand Down
Loading