Uh oh!
There was an error while loading. Please reload this page.
- Notifications
You must be signed in to change notification settings - Fork 34k
gh-120593: Fix const qualifier in pyatomic.h#121052
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
Uh oh!
There was an error while loading. Please reload this page.
Conversation
vstinner commented Jun 26, 2024 • edited by bedevere-app bot
Loading Uh oh!
There was an error while loading. Please reload this page.
edited by bedevere-app bot
Uh oh!
There was an error while loading. Please reload this page.
Remove the 'const' qualifier of the argument of functions: * _Py_atomic_load_ptr() * _Py_atomic_load_ptr_relaxed() * _Py_atomic_load_ptr_acquire()
picnixz left a comment
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm not sure if you like when we comment on a draft so feel free to ignore my comment!
| static inline uintptr_t | ||
| _Py_atomic_load_uintptr_acquire(const uintptr_t *obj) | ||
| {return (uintptr_t)__atomic_load_n((uintptr_t *)obj, __ATOMIC_ACQUIRE)} | ||
| {return (uintptr_t)__atomic_load_n(obj, __ATOMIC_ACQUIRE)} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I don't understand this change. Shouldn't you rather remove the const from the signature?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Without this change, the compiler emits a warning.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes, but isn't it because the object being passed to _Py_atomic_load_uintptr_acquire is a const uintptr_t *?
Now that I look a bit around, all the signatures take a const T * but I'm not sure if it's better to remove the inner cast or to fix the cast itself.
For instance,
staticinlinevoid*_Py_atomic_load_ptr_acquire(constvoid*obj){return (void*)__atomic_load_n((void**)obj, __ATOMIC_ACQUIRE)}could be fixed as
staticinlinevoid*_Py_atomic_load_ptr_acquire(constvoid*obj){return (void*)__atomic_load_n((void*const*)obj, __ATOMIC_ACQUIRE)}instead of changing the signature from const void * to void * (also, the cast on the return type should not needed I think). What do you think of that? at least, we would keep the same logic for signatures.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes, but isn't it because the object being passed to _Py_atomic_load_uintptr_acquire is a const uintptr_t *?
The compiler warns if you cast from "const TYPE" to "TYPE". My change removes the cast. It just works.
instead of changing the signature from const void * to void *
void* is special. I gave up trying to fix the warning, so I changed the parameters type of the 3 "pointer" functions instead.
Did you try your proposed fix? When I tried, it didn't work for me.
picnixzJun 26, 2024 • edited
Loading Uh oh!
There was an error while loading. Please reload this page.
edited
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Well yes, it worked for me actually.. that's why I suggested it. I changed this:
// L491 in pyatomic_gcc.hstaticinlinevoid*_Py_atomic_load_ptr_acquire(constvoid*obj){return (void*)__atomic_load_n((void**)obj, __ATOMIC_ACQUIRE)}into
staticinlinevoid*_Py_atomic_load_ptr_acquire(constvoid*obj){return__atomic_load_n((void*const*)obj, __ATOMIC_ACQUIRE)}and I did not have any warning for this specific function (there were more but not for this one) (I also tried with a cast on the return and the warning still did not appear).
My GCC version is 7.5.0 by the way.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Maybe I tried const void * const *, I don't recall.
Anyway, I wrote a new PR instead: PR gh-121055.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Maybe I tried
const void * const *,
It's highly possible because the first fix I suggested on the issue was actually with a return type of const void * and not void * (hence the const void * const *). So it's partly my fault!
vstinnerJun 27, 2024 • edited
Loading Uh oh!
There was an error while loading. Please reload this page.
edited
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thank you for your nice advice, it worked well at the end 😉
vstinner commented Jun 26, 2024
@colesbury: Would you be ok with these changes to avoid compiler warnings with cc @pitrou |
pitrou commented Jun 26, 2024
Why not use something like this instead of removing the |
colesbury commented Jun 26, 2024 • edited
Loading Uh oh!
There was an error while loading. Please reload this page.
edited
Uh oh!
There was an error while loading. Please reload this page.
I don't think we should remove the const qualifiers. They are useful for the load functions both to signify to callers that we don't modify the value and to avoid extra casts at the call sites. My preference in rough order is:
|
vstinner commented Jun 26, 2024
Ok, I abandon this PR in favor of a different approach: PR gh-121055. |
Remove the 'const' qualifier of the argument of functions: