protobuf icon indicating copy to clipboard operation
protobuf copied to clipboard

Add static_casts to silence msvc compiler warning C4267

Open chrisse74 opened this issue 10 months ago • 2 comments

Fix conversion warnings (MSVC C4267) from size_t to int / map_index_t

chrisse74 avatar Apr 02 '25 11:04 chrisse74

This PR changes protoc thus some checked-in proto files had to be regenerated.

chrisse74 avatar Apr 03 '25 11:04 chrisse74

@chrisse74 Thank you for sending this cleanup. I'm conflicted on this, because I like complying with compiler warnings but I also find the static_casts to be kind of distracting. Would it work for you to instead add C4267 to the list of disabled warnings here?

acozzette avatar Apr 07 '25 23:04 acozzette

Hi @acozzette thanks for your reply.

  • The static_casts suppress the warning locally.
  • Disabling the warning in the port_def.inc suppresses it globally and maybe hides (more problematic?) cases in future implementations. I do not like it, but I can live with it. ;)

Maybe the better way would be to switch the receiving types (e.g. here) to size_t, but I am unsure about the consequences.

chrisse74 avatar Apr 15 '25 07:04 chrisse74

Part of the problem is that we don't currently have a good way to enforce compliance with this warning, so even if we fix a few things, they are likely to break again. We do most of our development on Linux with Clang or GCC, so I tried adding -Werror=sign-conversion and found that we got a lot of errors in Abseil code. We would probably need to fix up Abseil and get them to enforce compliance before we could start doing the same. That would be a big project, so unfortunately for now I think the best solution is probably to disable the warning in port_def.inc.

acozzette avatar Apr 15 '25 16:04 acozzette

Okay, I will remove the PR.

chrisse74 avatar Apr 25 '25 07:04 chrisse74

The way to go forward is by adding a suppression instead. See discussion for details, closing the PR.

chrisse74 avatar Apr 25 '25 07:04 chrisse74