Skip to content

Conversation

@rwgk
Copy link
Collaborator

@rwgk rwgk commented Mar 31, 2025

Description

This PR refines and extends the use of std::launder for callable captures stored in function_record::data. It generalizes the approach first introduced in PR #2816 by applying it more consistently and defensively.

Key Improvements:

  • 🔁 Consistent use of std::launder
    All reinterpretations of the data field into the internal capture type now go through a centralized capture::from_data(void**) helper, which applies std::launder where supported. This eliminates ad hoc casting and ensures correct behavior under the C++ object model.

  • 🛡️ Precondition enforcement
    Adds a static_assert on std::is_standard_layout<capture> at critical optimization points. This ensures that reinterpret_casts and in-place storage remain well-defined.

  • 🧼 Macro centralization
    Moves the PYBIND11_STD_LAUNDER and PYBIND11_HAS_STD_LAUNDER macro definitions to common.h, enabling their reuse across headers (e.g., functional.h in addition to pybind11.h).

  • 📉 Reduced reliance on compiler-specific warnings
    The change removes special-case #pragma use around -Wstrict-aliasing by ensuring that laundering is properly applied across the board.

Motivation:

The original std::launder usage in PR #2816 was narrow in scope and only applied during destruction of non-trivially destructible callables. This patch addresses potential undefined behavior more broadly and makes the low-level implementation safer and easier to reason about, while maintaining performance characteristics.

ABI Note:

No changes to the ABI are introduced. The layout of function_record and the capture storage remains unchanged.

Changelog: Not needed.

Upgrade guide: Not needed.


This PR is a by-product of work under PR #5585 (which we will not merge, most likely).


Browse function_record_std_launder/manuscript tree

Browse function_record_std_launder/manuscript commits

@rwgk rwgk changed the title Squashed function_record_std_launder/manuscript — 57b9a0af815d19b236b… function_record code health enhancements (std::launder, std::is_standard_layout) Mar 31, 2025
@rwgk rwgk force-pushed the function_record_std_launder/review branch from 9d0bdc6 to 0e61ae9 Compare March 31, 2025 03:23
@rwgk rwgk marked this pull request as ready for review March 31, 2025 04:36
@rwgk rwgk requested a review from henryiii March 31, 2025 04:37
@rwgk
Copy link
Collaborator Author

rwgk commented Mar 31, 2025

Thanks @henryiii!

@rwgk rwgk merged commit e03ec30 into pybind:master Mar 31, 2025
74 checks passed
@github-actions github-actions bot added the needs changelog Possibly needs a changelog entry label Mar 31, 2025
@rwgk rwgk removed the needs changelog Possibly needs a changelog entry label Mar 31, 2025
@rwgk rwgk deleted the function_record_std_launder/review branch March 31, 2025 22:55
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants