Repository navigation
Avoid MSVC C4551 on unused-helper "touch" statements - #7911
Conversation
|
I'm not really convinced by the test (in that we don't really care what the code looks like here, just that it avoids MSVC warnings). Given that it's failing in the shared-utility build I'd propose deleting it rather than fixing it. The change itself looks fine (personally I think this is a common pattern that MSVC should accept, but we also can't change MSVC so...) |
Or if we did want a test, we'd probably want a C lint of some sort to check that we don't add this pattern elsewhere rather than a check for the specific case. But that probably isn't worthwhile given we don't have anything similar right now. |
|
I can see: which you've missed |
Cython marks conditionally-unused static helper functions as "used" to
silence -Wunused-function by referencing them in a discarded expression.
Several sites did this with a bare function name, e.g.
(void)__Pyx_PyObject_CallMethod0;
MSVC reads a bare function name as a call that is missing its argument
list and emits warning C4551. Take the function's address instead:
(void)&__Pyx_PyObject_CallMethod0;
This is unambiguous (it cannot be parsed as a call), is valid C and C++
on every compiler, still marks the symbol as used, and matches the
(void)&func idiom already used elsewhere in CythonFunction.c.
Converted every bare-function "touch" site: ExtensionTypes.c (the
__Pyx_PyType_Ready helper pulled in by every extension type),
MatchCase.c, ObjectHandling.c, Optimize.c, TString.c, ModuleSetupCode.c,
FunctionArguments.c, StringTools.c, and the __Pyx_TraceReturnCValue
macro in Profile.c (whose convert_function argument is always a bare
to_py_function name). Genuine unused-variable touches such as (void)ptr;
are left untouched, since C4551 only applies to functions.
Assisted-by: Claude Code (Opus 4.8), pair-programming with the author
Signed-off-by: Andrew V. Teylu <andrew.teylu@vector.com>
0ac81e6 to
af9a841
Compare
|
Thanks for the review!
Force-pushed with all of the above squashed into the one commit. |
|
Thanks - I'll merge this. I think it'll just put it in the next 3.3.1 release. It'd be fine to backport, but also doesn't seem like it's hugely important to. |
3.3.1 is fine; do you know when that's due? |
|
Depends how broken 3.3.0 turns out to be :)? If we get lots of important-looking bug reports then it could be in the next week. If we don't it might be ~1 months. |
Thanks, all good |
Problem
Cython marks conditionally-unused static helper functions as "used" (to silence
-Wunused-function) by referencing them in a discarded expression. Several sites do this with a bare function name, e.g. in the__Pyx_PyType_Readyhelper that every extension type pulls in:MSVC reads a bare function name as a call that is missing its argument list and emits warning C4551 (
function call missing argument list). Because__Pyx_PyType_Readyis generated for everycdef class, this shows up in essentially any MSVC build.Fix
Take the function's address instead:
&funcis unambiguous — it cannot be parsed as a call — so C4551 does not fire. It is valid C and C++ on every compiler, still marks the symbol as used, and matches the(void)&funcidiom already used elsewhere inCythonFunction.c.All bare-function
(void)func;"touch" sites were converted:ExtensionTypes.c__Pyx_PyObject_CallMethod0(×2),__Pyx_validate_bases_tupleMatchCase.c__Pyx_MatchCase_OtherSequenceSliceToList,__Pyx_MatchCase_TupleSliceToListObjectHandling.c__Pyx_GetBuiltinNameOptimize.c__Pyx_PyObject_CallOneArgTString.c__Pyx_GetObjectFromTemplateLibEach identifier was checked to be addressable (plain functions, or the one object-like macro aliasing a function); none is a function-like macro, so
&is safe at every site.Test
Adds
tests/run/unused_utility_func_no_c4551.pyx(mode: compile). Acdef classpulls in__Pyx_PyType_Ready→__Pyx_PyObject_CallMethod0, and the test uses the existing generated-C directives to assert the safe form is emitted and the bare form never appears:Verification
cdef class: 2×(void)&__Pyx_PyObject_CallMethod0;, 0 bare occurrences.clang -Wall -Wextra -Wunused-function: exit 0, zero warnings — confirming the address-of form is valid C and still suppresses the unused-function warning it was there for.