pybind / pybind/pybind11

[BUG]: Strict aliasing error on MinGW

Open
#5,244 0 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

triage
Dominant language
C++
Stars
18k
Forks
2.3k
Avg merge
5d 17h
Merged PRs (30d)
10

Description

Required prerequisites
What version (or hash if on master) of pybind11 are you using?

2.13.1

Problem description

Compiling code that uses pybind11/eigen.h on MinGW 6.3.0 fails with the following error:

dereferencing type-punned pointer will break strict-aliasing rules [-Werror=strict-aliasing]

I noticed that in gil_safe_call_once.h, there already is a conditional suppression of this warning, but it seems that MinGW is not yet covered by it.
https://github.com/pybind/pybind11/blob/50acb81b0a0f769dedd265727ade44e03778a977/include/pybind11/gil_safe_call_once.h#L70-L75

Relevant compiler log output:

D:\dev\tmp\pybindmingw>C:\tools\mingw-w64\bin\g++.exe -ID:\\.conan\data\pybind11\2.13.1\_\_\package\5ab84d6acfe1f23c4fae0ab88f26e3a396351ac9\include -isystem D://.conan/data/dyna4-python-embedded/3.9.19.20240423/pnd28/testing/package/a57cdd79732bacf1f842fc7d97f3485af7cbc563/include -isystem D://.conan/data/eigen/3.3.8/_/_/package/5ab84d6acfe1f23c4fae0ab88f26e3a396351ac9/include/eigen3 -Wall -O2 -c .\mysource.cpp
In file included from D://.conan/data/pybind11/2.13.1/_/_/package/5ab84d6acfe1f23c4fae0ab88f26e3a396351ac9/include/pybind11/pybind11.h:17:0,
                 from D://.conan/data/pybind11/2.13.1/_/_/package/5ab84d6acfe1f23c4fae0ab88f26e3a396351ac9/include/pybind11/numpy.h:12,
                 from D:\\.conan\data\pybind11\2.13.1\_\_\package\5ab84d6acfe1f23c4fae0ab88f26e3a396351ac9\include/pybind11/eigen/matrix.h:12,
                 from D:\\.conan\data\pybind11\2.13.1\_\_\package\5ab84d6acfe1f23c4fae0ab88f26e3a396351ac9\include/pybind11/eigen.h:12,
                 from .\mysource.cpp:3:
D://.conan/data/pybind11/2.13.1/_/_/package/5ab84d6acfe1f23c4fae0ab88f26e3a396351ac9/include/pybind11/gil_safe_call_once.h: In instantiation of 'T& pybind11::gil_safe_call_once_and_store<T>::get_stored() [with T = pybind11::detail::npy_api]':
D://.conan/data/pybind11/2.13.1/_/_/package/5ab84d6acfe1f23c4fae0ab88f26e3a396351ac9/include/pybind11/numpy.h:272:70:   required from here
D://.conan/data/pybind11/2.13.1/_/_/package/5ab84d6acfe1f23c4fae0ab88f26e3a396351ac9/include/pybind11/gil_safe_call_once.h:75:16: warning: dereferencing type-punned pointer will break strict-aliasing rules [-Wstrict-aliasing]
         return *reinterpret_cast<T *>(storage_);
                ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
D://.conan/data/pybind11/2.13.1/_/_/package/5ab84d6acfe1f23c4fae0ab88f26e3a396351ac9/include/pybind11/gil_safe_call_once.h: In instantiation of 'T& pybind11::gil_safe_call_once_and_store<T>::get_stored() [with T = pybind11::object]':
D://.conan/data/pybind11/2.13.1/_/_/package/5ab84d6acfe1f23c4fae0ab88f26e3a396351ac9/include/pybind11/numpy.h:771:25:   required from here
D://.conan/data/pybind11/2.13.1/_/_/package/5ab84d6acfe1f23c4fae0ab88f26e3a396351ac9/include/pybind11/gil_safe_call_once.h:75:16: warning: dereferencing type-punned pointer will break strict-aliasing rules [-Wstrict-aliasing]
$ C:\tools\mingw-w64\bin\g++.exe --version
g++ (x86_64-posix-seh-rev2, Built by MinGW-W64 project) 6.3.0
Copyright (C) 2016 Free Software Foundation, Inc.

Related to #4877

Reproducible example code
// compile using `-Wall -O2` flags

#include <pybind11/eigen.h>
#include <pybind11/pybind11.h>

int main() {}
Is this a regression? Put the last known working version here if it is.

Not a regression

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Inspect include/pybind11/gil_safe_call_once.h around the existing warning-suppression conditional and compare it with the MinGW 6.3.0 compiler output. Reproduce the issue using the provided pybind11/eigen.h example with -Wall -O2, then verify that the strict-aliasing warning no longer fails compilation on MinGW.

Written by the indexing model from the issue text.

Assessment

Tech stack
cpp, python
Domain
build-system, tooling
Issue type
Bug
Difficulty
2/5
Estimated time
1-3 hours
Activity status
Stale
Clarity
Clearly specified
Newbie friendliness
45/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.