Skip to content

make exception copy explicit or completely avoid it#6478

Merged
mikea merged 1 commit intomainfrom
maizatskyi/2026-04-01-exception-copy
Apr 1, 2026
Merged

make exception copy explicit or completely avoid it#6478
mikea merged 1 commit intomainfrom
maizatskyi/2026-04-01-exception-copy

Conversation

@mikea
Copy link
Copy Markdown
Contributor

@mikea mikea commented Apr 1, 2026

I am making Exception copy constructor explicit in capnp and it uncovered these cases

@mikea mikea requested review from a team as code owners April 1, 2026 17:33
@ask-bonk
Copy link
Copy Markdown
Contributor

ask-bonk bot commented Apr 1, 2026

All the changes look correct. Let me review all five files one more time systematically:

  1. identity-transform-stream.c++: cancel() takes by value, but exception is used after the cancel() call (via kj::mv(exception)). Adding kj::cp() makes the copy explicit. Correct.

  2. writable-sink-adapter-test.c++: isErrored() returns kj::Maybe<const kj::Exception&>, so KJ_ASSERT_NONNULL returns const kj::Exception&. Using auto& instead of auto avoids an implicit copy. Correct.

  3. exception.h: The three macros catch by non-const ref (was const kj::Exception& e) and now use throw kj::mv(e) instead of throw e. This avoids a copy on rethrow. The & e spacing (with space before &) is just clang-format style. Correct.

  4. test-fixture.c++: Already catching by non-const ref (kj::Exception& e); throw kj::mv(e) avoids a copy on rethrow. Correct.

  5. util/test.h: kj::cp(expException) makes the copy explicit. The NOLINTNEXTLINE(performance-unnecessary-copy-initialization) comment was already there acknowledging the copy, and now the copy is explicit. Correct.

All changes are straightforward, mechanically correct, and well-motivated by making the upstream kj::Exception copy constructor explicit. No behavioral changes, no backward compatibility concerns.

LGTM

github run

I am making Exception copy constructor explicit in capnp and it
uncovered these cases
@mikea mikea force-pushed the maizatskyi/2026-04-01-exception-copy branch from b245242 to 927d7f5 Compare April 1, 2026 17:43
@mikea mikea enabled auto-merge April 1, 2026 17:47
@mikea mikea merged commit 33c7d11 into main Apr 1, 2026
22 of 23 checks passed
@mikea mikea deleted the maizatskyi/2026-04-01-exception-copy branch April 1, 2026 18:22
@kentonv
Copy link
Copy Markdown
Member

kentonv commented Apr 1, 2026

FWIW I would be in favor of adding a .clone() method to exceptions and then removing the copy constructor entirely.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants