Skip to content

Avoid MSVC C4551 on unused-helper "touch" statements - #7911

Merged
da-woods merged 1 commit into
cython:masterfrom
aytey:fix-msvc-c4551-unused-helper-funcs
Aug 23, 2026
Merged

da-woods merged 1 commit into
cython:masterfrom
aytey:fix-msvc-c4551-unused-helper-funcs

Conversation

@aytey

@aytey aytey commented Aug 23, 2026

Copy link
Copy Markdown
Contributor

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_Ready helper that every extension type pulls in:

(void)__Pyx_PyObject_CallMethod0;

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_Ready is generated for every cdef class, this shows up in essentially any MSVC build.

Fix

Take the function's address instead:

(void)&__Pyx_PyObject_CallMethod0;

&func is 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)&func idiom already used elsewhere in CythonFunction.c.

All bare-function (void)func; "touch" sites were converted:

File Helper(s)
ExtensionTypes.c __Pyx_PyObject_CallMethod0 (×2), __Pyx_validate_bases_tuple
MatchCase.c __Pyx_MatchCase_OtherSequenceSliceToList, __Pyx_MatchCase_TupleSliceToList
ObjectHandling.c __Pyx_GetBuiltinName
Optimize.c __Pyx_PyObject_CallOneArg
TString.c __Pyx_GetObjectFromTemplateLib

Each 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). A cdef class pulls 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:

# cython: test_assert_c_code_has = \(void\)&__Pyx_PyObject_CallMethod0;
# cython: test_fail_if_c_code_has = \(void\)__Pyx_PyObject_CallMethod0;

Verification

  • Regenerated C for a cdef class: 2× (void)&__Pyx_PyObject_CallMethod0;, 0 bare occurrences.
  • Compiled the generated C with 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.

@da-woods

Copy link
Copy Markdown
Collaborator

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...)

@da-woods

Copy link
Copy Markdown
Collaborator

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.

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.

@da-woods

Copy link
Copy Markdown
Collaborator

I can see:

Cython/Utility/ModuleSetupCode.c:    (void)__pyx__find_code_object;
Cython/Utility/ModuleSetupCode.c:    (void)__pyx__insert_code_object;

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>
@aytey
aytey force-pushed the fix-msvc-c4551-unused-helper-funcs branch from 0ac81e6 to af9a841 Compare August 23, 2026 07:44
@aytey

aytey commented Aug 23, 2026 •

Copy link
Copy Markdown
Contributor Author

Thanks for the review!

  • Dropped the test

  • I've fixed __pyx__find_code_object / __pyx__insert_code_object and:

    • FunctionArguments.c: __Pyx_PyDict_NextRef (×2)
    • StringTools.c: __Pyx_PyObject_CallMethod1
    • Profile.c: the __Pyx_TraceReturnCValue macro, whose convert_function argument is always a bare to_py_function name

    Left the genuine unused-variable touches ((void)ptr;, (void)wraparound;, (void)__pyx_exception_already_reported;, etc.) alone, since C4551 only applies to functions.

Force-pushed with all of the above squashed into the one commit.

@da-woods

Copy link
Copy Markdown
Collaborator

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.

@da-woods
da-woods enabled auto-merge (squash) August 23, 2026 13:20
@aytey

aytey commented Aug 23, 2026

Copy link
Copy Markdown
Contributor Author

I think it'll just put it in the next 3.3.1 release.

3.3.1 is fine; do you know when that's due?

@da-woods

Copy link
Copy Markdown
Collaborator

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.

@aytey

aytey commented Aug 23, 2026

Copy link
Copy Markdown
Contributor Author

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

@da-woods
da-woods merged commit 148938f into cython:master Aug 23, 2026
120 of 124 checks passed
Comment thread Cython/Utility/ExtensionTypes.c Resolved
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants