Skip to content

Commit 783da0a

Browse files
Disable constinit on MSVC for static library builds
38927bf cleaned up the constinit suppressions based on our understanding of the Windows/MSVC limitations at the time. It turns out this was incomplete and, by restoring constinit on static library MSVC builds, we broke something. On MSVC, but not clang-cl, the workaround must extend to static library builds too. Although pointers to same-dll symbols are fine and MSVC, with optimizations, will avoid a static initializer, it doesn't seem to consistently recognize it in the type systems, so we can't enforce constinit. See https://godbolt.org/z/cYeYaaWq5 On MSVC, but not clang-cl, the above workaround must extend to static library builds too. MSVC can avoid a global constructor when initializing structures containing pointers to same-dll symbols, it relies on the optimizer for this, so we can't enforce constinit. This limitation does not apply to constexpr. See https://godbolt.org/z/hsT9e3zs4 Add a Windows Bazel C++20 build configuration, which would have caught this. PiperOrigin-RevId: 858801389
1 parent 6ba0e6f commit 783da0a

File tree

2 files changed

+11
-0
lines changed

2 files changed

+11
-0
lines changed

.github/workflows/test_cpp.yml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -431,6 +431,10 @@ jobs:
431431
os: windows-2022
432432
cache_key: windows-2022-msvc-cl
433433
bazel: test //src/... --config=msvc-cl --test_tag_filters=-conformance --build_tag_filters=-conformance
434+
- name: Windows Bazel C++20
435+
os: windows-2022
436+
cache_key: windows-2022-msvc-cl
437+
bazel: test //src/... --config=msvc-cl --cxxopt="-std:c++20" --test_tag_filters=-conformance --build_tag_filters=-conformance
434438
- name: Windows Bazel clang-cl
435439
os: windows-2022
436440
cache_key: windows-2022-clang-cl

src/google/protobuf/port_def.inc

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -424,6 +424,13 @@ static_assert(PROTOBUF_ABSL_MIN(20230125, 3),
424424
// https://github.com/protocolbuffers/protobuf/pull/13240. Work around this by
425425
// suppressing `constinit`.
426426
#define PROTOBUF_CONSTINIT
427+
#elif defined(_MSC_VER) && !defined(__clang__)
428+
// On MSVC, but not clang-cl, the above workaround must extend to static library
429+
// builds too. MSVC can avoid a global constructor when initializing structures
430+
// containing pointers to same-dll symbols, it relies on the optimizer for this,
431+
// so we can't enforce constinit. This limitation does not apply to constexpr.
432+
// See https://godbolt.org/z/hsT9e3zs4
433+
#define PROTOBUF_CONSTINIT
427434
#elif defined(__GNUC__) && !defined(__clang__)
428435
// GCC doesn't support constinit aggregate initialization of absl::Cord.
429436
#define PROTOBUF_CONSTINIT

0 commit comments

Comments
 (0)