From 63d550938ba2a3690be987e884fe95e384708a82 Mon Sep 17 00:00:00 2001 From: eldritch horrors Date: Sat, 5 Apr 2025 21:57:20 +0200 Subject: [PATCH] treewide: derive all lix exceptions from BaseError even the non-errors. we should probably insert a BaseException here. Change-Id: I1b1af8ba0bf49251fe9d1a24c6559db3ef4a59d2 --- lix/libstore/gc.cc | 3 +- lix/libutil/checked-arithmetic.hh | 3 +- lix/libutil/exit.cc | 7 ----- lix/libutil/exit.hh | 5 ++- lix/libutil/meson.build | 1 - lix/nix/why-depends.cc | 3 +- tests/unit/libstore/filetransfer.cc | 3 +- .../tests/cli-literate-parser.hh | 3 +- tests/unit/libutil/closure.cc | 3 +- tests/unit/libutil/generator.cc | 31 ++++++++++--------- tests/unit/libutil/thread-pool.cc | 9 +++--- 11 files changed, 35 insertions(+), 36 deletions(-) delete mode 100644 lix/libutil/exit.cc diff --git a/lix/libstore/gc.cc b/lix/libstore/gc.cc index 6d3ab73c7..6cc78a527 100644 --- a/lix/libstore/gc.cc +++ b/lix/libstore/gc.cc @@ -2,6 +2,7 @@ #include "lix/libstore/local-store.hh" #include "lix/libstore/pathlocks.hh" #include "lix/libutil/async.hh" +#include "lix/libutil/error.hh" #include "lix/libutil/processes.hh" #include "lix/libutil/result.hh" #include "lix/libutil/signals.hh" @@ -399,7 +400,7 @@ try { } -struct GCLimitReached : std::exception { }; +struct GCLimitReached : BaseException { }; /** diff --git a/lix/libutil/checked-arithmetic.hh b/lix/libutil/checked-arithmetic.hh index c0c63feff..630092b5a 100644 --- a/lix/libutil/checked-arithmetic.hh +++ b/lix/libutil/checked-arithmetic.hh @@ -3,6 +3,7 @@ * @file Checked arithmetic with classes that make it hard to accidentally make something an unchecked operation. */ +#include "lix/libutil/error.hh" #include #include // IWYU pragma: keep #include @@ -13,7 +14,7 @@ namespace nix::checked { -class DivideByZero : std::exception +struct DivideByZero : BaseException {}; /** diff --git a/lix/libutil/exit.cc b/lix/libutil/exit.cc deleted file mode 100644 index 9a1781ed9..000000000 --- a/lix/libutil/exit.cc +++ /dev/null @@ -1,7 +0,0 @@ -#include "lix/libutil/exit.hh" - -namespace nix { - -Exit::~Exit() {} - -} diff --git a/lix/libutil/exit.hh b/lix/libutil/exit.hh index 27abc6a89..677f1c230 100644 --- a/lix/libutil/exit.hh +++ b/lix/libutil/exit.hh @@ -1,20 +1,19 @@ #pragma once ///@file -#include +#include "lix/libutil/error.hh" namespace nix { /** * Exit the program with a given exit code. */ -class Exit : public std::exception +class Exit : public BaseException { public: int status; Exit() : status(0) { } explicit Exit(int status) : status(status) { } - virtual ~Exit(); }; } diff --git a/lix/libutil/meson.build b/lix/libutil/meson.build index bf6c66da8..2b7214734 100644 --- a/lix/libutil/meson.build +++ b/lix/libutil/meson.build @@ -15,7 +15,6 @@ libutil_sources = files( 'error.cc', 'escape-char.cc', 'escape-string.cc', - 'exit.cc', 'experimental-features.cc', 'file-descriptor.cc', 'file-system.cc', diff --git a/lix/nix/why-depends.cc b/lix/nix/why-depends.cc index b699da4db..43fa0a4c6 100644 --- a/lix/nix/why-depends.cc +++ b/lix/nix/why-depends.cc @@ -2,6 +2,7 @@ #include "lix/libstore/store-api.hh" #include "lix/libstore/fs-accessor.hh" #include "lix/libmain/shared.hh" +#include "lix/libutil/error.hh" #include "why-depends.hh" #include @@ -173,7 +174,7 @@ struct CmdWhyDepends : SourceExprCommand, MixOperateOnOptions and `dependency`. */ std::function printNode; - struct BailOut : std::exception { }; + struct BailOut : BaseException { }; printNode = [&](Node & node, const std::string & firstPad, const std::string & tailPad) { auto pathS = store->printStorePath(node.path); diff --git a/tests/unit/libstore/filetransfer.cc b/tests/unit/libstore/filetransfer.cc index a44a7b892..5d523e37e 100644 --- a/tests/unit/libstore/filetransfer.cc +++ b/tests/unit/libstore/filetransfer.cc @@ -1,5 +1,6 @@ #include "lix/libstore/filetransfer.hh" #include "lix/libutil/compression.hh" +#include "lix/libutil/error.hh" #include "lix/libutil/signals.hh" #include "lix/libutil/thread-name.hh" @@ -203,7 +204,7 @@ serveHTTP(std::string status, std::string headers, std::function TEST(FileTransfer, exceptionAbortsDownload) { - struct Done : std::exception + struct Done : BaseException {}; auto ft = makeFileTransfer(); diff --git a/tests/unit/libutil-support/tests/cli-literate-parser.hh b/tests/unit/libutil-support/tests/cli-literate-parser.hh index 2ff9348ef..2eace1e3d 100644 --- a/tests/unit/libutil-support/tests/cli-literate-parser.hh +++ b/tests/unit/libutil-support/tests/cli-literate-parser.hh @@ -1,6 +1,7 @@ #pragma once ///@file +#include "lix/libutil/error.hh" #include #include #include @@ -127,7 +128,7 @@ struct ParseResult { /** * A parse error. */ -struct ParseError : std::exception { +struct ParseError : BaseException { std::string expected; std::string rest; diff --git a/tests/unit/libutil/closure.cc b/tests/unit/libutil/closure.cc index 08ae650cb..bbbe1c3ac 100644 --- a/tests/unit/libutil/closure.cc +++ b/tests/unit/libutil/closure.cc @@ -1,4 +1,5 @@ #include "lix/libutil/closure.hh" +#include "lix/libutil/error.hh" #include namespace nix { @@ -28,7 +29,7 @@ TEST(closure, correctClosure) { } TEST(closure, properlyHandlesDirectExceptions) { - struct TestExn : std::exception {}; + struct TestExn : BaseException {}; EXPECT_THROW( computeClosure( {"A"}, diff --git a/tests/unit/libutil/generator.cc b/tests/unit/libutil/generator.cc index 5b5622a70..ce9588b12 100644 --- a/tests/unit/libutil/generator.cc +++ b/tests/unit/libutil/generator.cc @@ -1,4 +1,5 @@ #include "lix/libutil/generator.hh" +#include "lix/libutil/error.hh" #include #include @@ -6,6 +7,11 @@ namespace nix { +namespace { +MakeError(TestError, BaseError); +MakeError(TestError2, BaseError); +} + TEST(Generator, yields) { auto g = []() -> Generator { @@ -85,8 +91,7 @@ TEST(Generator, nestsExceptions) co_yield 1; co_yield []() -> Generator { co_yield 9; - // NOLINTNEXTLINE(hicpp-exception-baseclass) - throw 1; + throw TestError(""); co_yield 10; }(); co_yield 2; @@ -94,7 +99,7 @@ TEST(Generator, nestsExceptions) ASSERT_EQ(g.next(), 1); ASSERT_EQ(g.next(), 9); - ASSERT_THROW(g.next(), int); + ASSERT_THROW(g.next(), TestError); } TEST(Generator, exception) @@ -102,22 +107,20 @@ TEST(Generator, exception) { auto g = []() -> Generator { co_yield 1; - // NOLINTNEXTLINE(hicpp-exception-baseclass) - throw 1; + throw TestError(""); }(); ASSERT_EQ(g.next(), 1); - ASSERT_THROW(g.next(), int); + ASSERT_THROW(g.next(), TestError); ASSERT_FALSE(g.next().has_value()); } { auto g = []() -> Generator { - // NOLINTNEXTLINE(hicpp-exception-baseclass) - throw 1; + throw TestError(""); co_return; }(); - ASSERT_THROW(g.next(), int); + ASSERT_THROW(g.next(), TestError); ASSERT_FALSE(g.next().has_value()); } } @@ -176,14 +179,12 @@ struct ThrowTransform int operator()(bool) { - // NOLINTNEXTLINE(hicpp-exception-baseclass) - throw 2; + throw TestError(""); } Generator operator()(Generator && inner) { - // NOLINTNEXTLINE(hicpp-exception-baseclass) - throw false; + throw TestError2(""); } }; } @@ -198,7 +199,7 @@ TEST(Generator, transformThrows) }(); ASSERT_EQ(g.next(), 1); - ASSERT_THROW(g.next(), int); + ASSERT_THROW(g.next(), TestError); ASSERT_FALSE(g.next().has_value()); } { @@ -211,7 +212,7 @@ TEST(Generator, transformThrows) }(); ASSERT_EQ(g.next(), 1); - ASSERT_THROW(g.next(), bool); + ASSERT_THROW(g.next(), TestError2); ASSERT_FALSE(g.next().has_value()); } } diff --git a/tests/unit/libutil/thread-pool.cc b/tests/unit/libutil/thread-pool.cc index 34c234d03..8ab673854 100644 --- a/tests/unit/libutil/thread-pool.cc +++ b/tests/unit/libutil/thread-pool.cc @@ -1,5 +1,6 @@ #include "lix/libutil/thread-pool.hh" #include "lix/libutil/async.hh" +#include "lix/libutil/error.hh" #include #include #include @@ -51,7 +52,7 @@ TEST(ThreadPool, early_quit) ThreadPool t{"test", 2}; bool ran_anyway = false; - struct Dead : std::exception {}; + struct Dead : BaseException {}; std::atomic_bool unblockA{false}, unblockB{false}; std::atomic_bool started{false}; @@ -92,7 +93,7 @@ TEST(ThreadPool, early_quit_async) ThreadPool t{"test", 2}; bool ran_anyway = false; - struct Dead : std::exception {}; + struct Dead : BaseException {}; std::atomic_bool unblockA{false}, unblockB{false}; std::atomic_bool started{false}; @@ -133,7 +134,7 @@ TEST(ThreadPool, always_rethrows) { ThreadPool t{"test"}; - struct Dead : std::exception {}; + struct Dead : BaseException {}; std::atomic_bool flag{false}; @@ -155,7 +156,7 @@ TEST(ThreadPool, always_rethrows_async) { ThreadPool t{"test"}; - struct Dead : std::exception {}; + struct Dead : BaseException {}; std::atomic_bool flag{false};