[core] enable -Wshadow for all c++ targets
Why are these changes needed?
Fix c++ variable shadowing Some naming rules for easier review:
- For struct constructor initialize lists, prefix "_" is added to arguments (e.g.
size(_size)). - For class data members, suffix "_" is added to data members (e.g.
size_(size)). - For JNIEnv *env in nested lambdas,
env->_env->e - For c++ lambdas, prefix "_" is added to lambda arguments
- 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.shto 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.rstfile.
- [ ] I've added any new APIs to the API Reference. For example, if I added a
method in Tune, I've added it in
- [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 :(
Hi @dayshah microcheck finds shadowing in msgpack and absl. Would you like to somehow bypass the externals or patch them?
Hi @dayshah microcheck finds shadowing in msgpack and absl. Would you like to somehow bypass the externals or patch them?
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.
Hey so one general comment here regarding point 1 in the description
- 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)
Hey so one general comment here regarding point 1 in the description
- 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
Hey so one general comment here regarding point 1 in the description
- 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.
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?
https://github.com/ray-project/ray/pull/53194#issuecomment-2918102528
cc @dayshah
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.
keep this pr open
looks like gcs_actor_manager_test is failing to build, probably bc of bad merge?
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.
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
@jjyao / @dayshah / @edoakes can you merge after conflicts are resolved?
