Skip to content

Silence signedness change through implicit conversion error #880

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

Open
wants to merge 1 commit into
base: main
Choose a base branch
from

Conversation

AnthonyLatsis
Copy link

The error pops up when using the rebranch Clang (stable/20250402):

/home/build-user/swift-corelibs-libdispatch/src/event/event_epoll.c:92:27: error: implicit conversion changes signedness: 'int' to 'uint32_t' (aka 'unsigned int') [-Werror,-Wsign-conversion]
92 |         return dmn->dmn_events & ~dmn->dmn_disarmed_events;
   |                                ~ ^~~~~~~~~~~~~~~~~~~~~~~~~

@AnthonyLatsis AnthonyLatsis requested a review from bnbarham June 9, 2025 18:51
@@ -89,7 +89,7 @@ DISPATCH_ALWAYS_INLINE
static inline uint32_t
_dispatch_muxnote_armed_events(dispatch_muxnote_t dmn)
{
return dmn->dmn_events & ~dmn->dmn_disarmed_events;
return dmn->dmn_events & ~(uint32_t)(dmn->dmn_disarmed_events);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think that casting to uint16_t is better as that is the declaration. That should avoid any unintentional zexting.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You mean s/uint32_t/uint16_t/? Or (uint16_t)~dmn->dmn_disarmed_events? The former doesn’t help. I don’t really get which part is causing the conversion. Since the latter works, my guess is it started picking a signed negation overload.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why is it thinking that the member is 32 bit? It’s trying to highlight the implicit integer promotion. We just want to ensure the zext instead of a sext.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The error says 'int' to 'uint32_t'. I’m curious where the int comes from. If int is what the selected negation returns, then (uint16_t)~dmn->dmn_disarmed_events seems like it would cause a double conversion, whereas the current change appears to make it select a negation that returns an uint32_t.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It really is the ~:

  uint16_t x = dmn->dmn_disarmed_events;
  uint16_t nx = ~x;
  return dmn->dmn_events & nx;
error: implicit conversion loses integer precision: 'int' to 'uint16_t' (aka 'unsigned short') [-Werror,-Wimplicit-int-conversion]
   93 |   uint16_t nx = ~x;
      |            ~~   ^~

~(uint16_t)x produces int, and ~(uint32_t)x produces uint32_t 🤷🏼‍♂️


Anyway, if you stand by your original suggestion, I would appreciate some clarity in regards to the subexpression to be casted.

I think that casting to uint16_t is better as that is the declaration.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah right ... the result of ~ would be int because it will widen to the 32-bit int. I'd say we can spell this as: ~(uint32_t)(uint16_t)(dmn->dmn_events) then. The first cast is meaningless but is meant to ensure that we are convert an unsigned to unsigned value to ensure the zext over the sext.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Switched to decltype(dmn->dmn_events) after discussing offline with Ben.

@compnerd
Copy link
Member

@swift-ci please test

@bnbarham
Copy link
Contributor

@swift-ci please test Linux platform

@bnbarham
Copy link
Contributor

Oh, hadn't updated yet so this is just going to fail again 😅

The error pops up when using the rebranch Clang (stable/20250402):

```
/home/build-user/swift-corelibs-libdispatch/src/event/event_epoll.c:92:27: error: implicit conversion changes signedness: 'int' to 'uint32_t' (aka 'unsigned int') [-Werror,-Wsign-conversion]
92 |         return dmn->dmn_events & ~dmn->dmn_disarmed_events;
   |                                ~ ^~~~~~~~~~~~~~~~~~~~~~~~~
```
@AnthonyLatsis
Copy link
Author

@swift-ci please test

@bnbarham
Copy link
Contributor

@swift-ci please test

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