pybind11 icon indicating copy to clipboard operation
pybind11 copied to clipboard

[BUG]: Strict aliasing error on MinGW

Open markuspi opened this issue 7 months ago • 0 comments

Required prerequisites

  • [X] Make sure you've read the documentation. Your issue may be addressed there.
  • [X] Search the issue tracker and Discussions to verify that this hasn't already been reported. +1 or comment there if it has.
  • [ ] Consider asking first in the Gitter chat room or in a Discussion.

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

markuspi avatar Jul 15 '24 12:07 markuspi