ray icon indicating copy to clipboard operation
ray copied to clipboard

[core] enable -Wshadow for all c++ targets

Open tianyi-ge opened this issue 1 year ago • 7 comments

Why are these changes needed?

Fix c++ variable shadowing Some naming rules for easier review:

  1. For struct constructor initialize lists, prefix "_" is added to arguments (e.g. size(_size) ).
  2. For class data members, suffix "_" is added to data members (e.g. size_(size) ).
  3. For JNIEnv *env in nested lambdas, env -> _env -> e
  4. For c++ lambdas, prefix "_" is added to lambda arguments
  5. For constants (e.g. actor_id ), I capture them by value.

Related issue number

Closes #51803

Checks

  • [x] I've signed off every commit(by using the -s flag, i.e., git commit -s) in this PR.
  • [x] I've run scripts/format.sh to lint the changes in this PR.
  • [x] I've included any doc changes needed for https://docs.ray.io/en/master/.
    • [ ] I've added any new APIs to the API Reference. For example, if I added a method in Tune, I've added it in doc/source/tune/api/ under the corresponding .rst file.
  • [x] I've made sure the tests are passing. Note that there might be a few flaky tests, see the recent failures at https://flakey-tests.ray.io/
  • Testing Strategy
    • [ ] Unit tests
    • [ ] Release tests
    • [ ] This PR is not tested :(

tianyi-ge avatar May 21 '25 03:05 tianyi-ge

Hi @dayshah microcheck finds shadowing in msgpack and absl. Would you like to somehow bypass the externals or patch them? image

tianyi-ge avatar May 21 '25 07:05 tianyi-ge

Hi @dayshah microcheck finds shadowing in msgpack and absl. Would you like to somehow bypass the externals or patch them? image

for msgpack you can set the copts like this (I think it needs to be on the _msgpack above too) https://github.com/ray-project/ray/blob/b230e61ae0ed2f2c13856c197e5a7fbeb421f1df/bazel/msgpack.BUILD#L33

absl shouldn't have the issue... if it does it's a little more difficult because you'll have to patch the build file to have the copt.

dayshah avatar May 21 '25 15:05 dayshah

Hey so one general comment here regarding point 1 in the description

  1. For constructor initialize lists, prefix "_" is added to arguments (e.g. size(_size) )

This should be the other way around. The arguments should keep their names, and the actual member variables should be suffixed with _ to be consistent with the rest of the codebase. E.g. size_(size)

dayshah avatar May 24 '25 19:05 dayshah

Hey so one general comment here regarding point 1 in the description

  1. For constructor initialize lists, prefix "_" is added to arguments (e.g. size(_size) )

This should be the other way around. The arguments should keep their names, and the actual member variables should be suffixed with _ to be consistent with the rest of the codebase. E.g. size_(size)

Hi @dayshah, thanks for your comments. Based on google c++ style, class data member has a trailing "_", but struct data member does not. https://google.github.io/styleguide/cppguide.html

I have added trailing "_" to classes data members, and leave struct member ordinary nonmember name. could you review it again? thanks

tianyi-ge avatar May 25 '25 10:05 tianyi-ge

Hey so one general comment here regarding point 1 in the description

  1. For constructor initialize lists, prefix "_" is added to arguments (e.g. size(_size) )

This should be the other way around. The arguments should keep their names, and the actual member variables should be suffixed with _ to be consistent with the rest of the codebase. E.g. size_(size)

Hi @dayshah, thanks for your comments. Based on google c++ style, class data member has a trailing "_", but struct data member does not. https://google.github.io/styleguide/cppguide.html

I have added trailing "_" to classes data members, and leave struct member ordinary nonmember name. could you review it again? thanks

I would still either prefer the data members to be suffixed with '' if you have an user-defined constructor for the struct. I think the reasoning for only class data members to have '' is that structs will usually not have user-defined constructors. Maybe some of the struct constructors can be completely removed in this case. If so, they don't have to be suffixed. If they do, the data members should be suffixed to stay consistent with the rest of the codebase.

dayshah avatar May 28 '25 16:05 dayshah

Hi @dayshah, I viewed many of the structs in ray codebase. most of the structs in ray do not have trailing underscore, so without underscore is actually consistent with the rest of the codebase. Renaming those data members would require a ton of changes.

Also, naming data members is probably not the key point of this issue @dentiny. I believe it better to start a new issue to get those data member names fixed. How do you like this idea?

tianyi-ge avatar May 29 '25 03:05 tianyi-ge

https://github.com/ray-project/ray/pull/53194#issuecomment-2918102528

cc @dayshah

kevin85421 avatar Jun 10 '25 16:06 kevin85421

This pull request has been automatically marked as stale because it has not had any activity for 14 days. It will be closed in another 14 days if no further activity occurs. Thank you for your contributions.

You can always ask for help on our discussion forum or Ray's public slack channel.

If you'd like to keep this open, just leave any comment, and the stale label will be removed.

github-actions[bot] avatar Jul 01 '25 12:07 github-actions[bot]

keep this pr open

tianyi-ge avatar Jul 05 '25 02:07 tianyi-ge

looks like gcs_actor_manager_test is failing to build, probably bc of bad merge?

dayshah avatar Jul 07 '25 02:07 dayshah

While I agree having this on is a definite improvement and will prevent us from shadowing in the future, I'm unsure if just prefixing with _ is the right way to do it in most cases.

I'm kind of ok with prefixing struct construction parameters because I understand that there may be a major amount of changes if we go with what the codebase does with classes in suffixing the members with _ instead.

But for other cases, ideally it should be possible to give either the outer or inner variable more meaningful names instead of just prefixing them?

@edoakes any ideas / opinions

Hi @dayshah, for local variables, I tried to rename them with specific names (e.g. status -> register_status/create_status, send_reply_callback -> accepted_send_reply_callback). I hope now it's easier to read.

tianyi-ge avatar Jul 15 '25 09:07 tianyi-ge

I also talked to the team, and we're thinking that we'd prefer the member variable followed by the _ in structs rather than having the parameters prefixed with _

yeah that's the neatest way. It's gonna be a much larger pr. I plan to only modify those structs with shadowing param problem

tianyi-ge avatar Jul 30 '25 01:07 tianyi-ge

@jjyao / @dayshah / @edoakes can you merge after conflicts are resolved?

israbbani avatar Aug 13 '25 00:08 israbbani