Skip to content

Reset kj::Maybes with kj::none instead of {} - #2071

Merged
justin-mp merged 1 commit into
mainfrom
justin-mp/fix-ambiguous-maybe-assignments
Apr 30, 2024
Merged

Reset kj::Maybes with kj::none instead of {}#2071
justin-mp merged 1 commit into
mainfrom
justin-mp/fix-ambiguous-maybe-assignments

Conversation

@justin-mp

Copy link
Copy Markdown
Contributor

The capnproto pull request
capnproto/capnproto#2014 adds another assignment operator for Maybes. With a version of capnproto including that PR, we get compilation errors of the form:

external/workerd/src/workerd/io/worker.c++:2524:15: error: use of overloaded operator '=' is ambiguous (with operand types 'kj::Maybe<InspectorChannelImpl &>' and 'void')
      channel = {};
      ~~~~~~~ ^ ~~
bazel-out/k8-dbg/bin/external/capnp-cpp/src/kj/_virtual_includes/kj/kj/common.h:1679:17: note: candidate function
  inline Maybe& operator=(decltype(nullptr)) { ptr = nullptr; return *this; }
                ^
bazel-out/k8-dbg/bin/external/capnp-cpp/src/kj/_virtual_includes/kj/kj/common.h:1682:17: note: candidate function
  inline Maybe& operator=(T* other) { ptr = other; return *this; }
                ^

The Maybes that I fixed in this PR are all reference types (Maybe<T&>), so I’m not sure what assigning {} would do, since if it’s assigning a temporary, the reference will be garbage.

However, in the three cases here, we seem to be resetting the objects, so assigning kj::none is correct.

The capnproto pull request
capnproto/capnproto#2014 adds another
assignment operator for Maybes.  With a version of capnproto including
that PR, we get compilation errors of the form:

```
external/workerd/src/workerd/io/worker.c++:2524:15: error: use of overloaded operator '=' is ambiguous (with operand types 'kj::Maybe<InspectorChannelImpl &>' and 'void')
      channel = {};
      ~~~~~~~ ^ ~~
bazel-out/k8-dbg/bin/external/capnp-cpp/src/kj/_virtual_includes/kj/kj/common.h:1679:17: note: candidate function
  inline Maybe& operator=(decltype(nullptr)) { ptr = nullptr; return *this; }
                ^
bazel-out/k8-dbg/bin/external/capnp-cpp/src/kj/_virtual_includes/kj/kj/common.h:1682:17: note: candidate function
  inline Maybe& operator=(T* other) { ptr = other; return *this; }
                ^
```

The Maybes that I fixed in this PR are all reference
types (`Maybe<T&>`), so I’m not sure what assigning `{}` would do,
since if it’s assigning a temporary, the reference will be garbage.

However, in the three cases here, we seem to be resetting the objects,
so assigning `kj::none` is correct.
@justin-mp
justin-mp requested review from a team as code owners April 30, 2024 13:00
@justin-mp
justin-mp merged commit 4f6f9fe into main Apr 30, 2024
@justin-mp
justin-mp deleted the justin-mp/fix-ambiguous-maybe-assignments branch April 30, 2024 15:01
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