Skip to content

Ruby: suppress -Wdefault-const-init-field-unsafe for clang 21+#3419

Merged
kpumuk merged 1 commit into
apache:masterfrom
masak1yu:fix/ruby-clang21-default-const-init-warning
Apr 26, 2026
Merged

Ruby: suppress -Wdefault-const-init-field-unsafe for clang 21+#3419
kpumuk merged 1 commit into
apache:masterfrom
masak1yu:fix/ruby-clang21-default-const-init-warning

Conversation

@masak1yu
Copy link
Copy Markdown
Contributor

Problem

clang 21 introduced -Wdefault-const-init-field-unsafe, which fires on Ruby 3.2's rstring.h:465:

struct RString retval;  // has const field (klass) via RBasic → triggers warning

Because extconf.rb unconditionally adds -Werror, this new warning causes the native extension build to fail with:

error: default initialization of an object of type 'struct RString' with const member
       leaves the object uninitialized [-Werror,-Wdefault-const-init-field-unsafe]

This affects macOS users on Xcode 26 / Nix with LLVM 21 when building against Ruby 3.2.

Root cause

The warning is in Ruby's own headers (rstring.h), not in Thrift code. Ruby 3.2 did not anticipate this warning being added to clang.

Fix

Add append_cflags("-Wno-default-const-init-field-unsafe") after the existing flags. Since append_cflags silently ignores flags that the compiler doesn't recognize, this is safe on all older clang versions.

Testing

gem build thrift.gemspec
gem install thrift-*.gem

clang 21 introduced -Wdefault-const-init-field-unsafe, which triggers on
Ruby 3.2's rstring.h because struct RString contains a const field (klass)
via RBasic. Since extconf.rb unconditionally adds -Werror, this new warning
causes the native extension build to fail.

The warning originates in Ruby headers, not Thrift code. Use append_cflags
to suppress it; append_cflags silently ignores unknown flags, so this is
safe on all older compiler versions.

Client: rb
@masak1yu masak1yu requested a review from kpumuk as a code owner April 26, 2026 02:23
@mergeable mergeable Bot added the ruby Pull requests that update Ruby code label Apr 26, 2026
Copy link
Copy Markdown
Member

@kpumuk kpumuk left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@kpumuk kpumuk merged commit d47d1b9 into apache:master Apr 26, 2026
62 of 63 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

ruby Pull requests that update Ruby code

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants