Bug #21659
closedrstring.h error: missing initializer for field ‘len’ of ‘struct RString’ [-Werror=missing-field-initializers] starting in ruby-3.3.10
Description
All extensions now have this error when using -Werror -Wmissing-field-initializers:
In file included from /home/runner/.rvm/rubies/ruby-3.3.10/include/ruby-3.3.0/ruby/internal/arithmetic/char.h:29,
from /home/runner/.rvm/rubies/ruby-3.3.10/include/ruby-3.3.0/ruby/internal/arithmetic.h:24,
from /home/runner/.rvm/rubies/ruby-3.3.10/include/ruby-3.3.0/ruby/ruby.h:28,
from /home/runner/.rvm/rubies/ruby-3.3.10/include/ruby-3.3.0/ruby.h:38,
from example_wrap.c:1008:
/home/runner/.rvm/rubies/ruby-3.3.10/include/ruby-3.3.0/ruby/internal/core/rstring.h: In function ‘rbimpl_rstring_getmem’:
/home/runner/.rvm/rubies/ruby-3.3.10/include/ruby-3.3.0/ruby/internal/core/rstring.h:398:16: error: missing initializer for field ‘len’ of ‘struct RString’ [-Werror=missing-field-initializers]
398 | struct RString retval = {RBASIC_INIT};
| ^~~~~~~
/home/runner/.rvm/rubies/ruby-3.3.10/include/ruby-3.3.0/ruby/internal/core/rstring.h:206:10: note: ‘len’ declared here
206 | long len;
| ^~~
Can you please test with warnings turned on as this now breaks all extensions that rely on Ruby.h being warning free.
Looks like it is due to commit https://github.com/ruby/ruby/commit/5a8d7642168f4ea0d9331fded3033c225bbc36c5 and expect that
struct RString retval = {RBASIC_INIT};
should have been changed to:
struct RString retval = {RBASIC_INIT, 0};
in rstring.h as well as all in the actual Ruby code base.
Updated by nobu (Nobuyoshi Nakada) 10 days ago
- Status changed from Open to Feedback
In configure.ac, we add -Wno-missing-field-initializers explicitly if available.
Are you adding that -Werror -Wmissing-field-initializer by yourself?
Updated by wsfulton (William Fulton) 10 days ago
This happens when compiling an EXTENSION. We have always used -Wmissing-field-initializers in our extensions and now they are broken. New Ruby releases really MUST NOT introduce new warnings for extensions which have to include Ruby.h.
Updated by jeremyevans0 (Jeremy Evans) 10 days ago
wsfulton (William Fulton) wrote in #note-2:
This happens when compiling an EXTENSION. We have always used -Wmissing-field-initializers in our extensions and now they are broken. New Ruby releases really MUST NOT introduce new warnings for extensions which have to include Ruby.h.
New Ruby releases should not introduce errors in extensions. I think it is unreasonable to expect that they will not introduce warnings if you are explicitly configuring non-default warning flags. If you are combining non-default warning flags with -Werror, I think you are responsible for handling the issues that result from that, since it is your actions that are causing the errors. I realize this may have worked in the past for you, but that was by chance, it is not something that Ruby has officially or deliberately supported.
Updated by wsfulton (William Fulton) 9 days ago
Keep code warning free improves the code quality. Fixing this warning, in particular, removes undefined behaviour. Are the core Ruby developers supportive of these goals and consider a patch to fix this warning and possibly even making it a supported warning flag? It will lead to an improved Ruby ecosystem by using the C++ tools available to automatically find time wasting bugs, not just in Ruby itself, but in all the Ruby extensions.
Updated by jeremyevans0 (Jeremy Evans) 8 days ago
wsfulton (William Fulton) wrote in #note-4:
Fixing this warning, in particular, removes undefined behaviour.
Can you explain what the undefined behavior is? My understanding is that in both C and C++, the behavior is defined to zero-initialize all uninitialized fields.
Updated by ufuk (Ufuk Kayserilioglu) 8 days ago
Isn't this related to this problem with GCC? https://bugs.ruby-lang.org/issues/21655#change-114967
Updated by alanwu (Alan Wu) 8 days ago
- Status changed from Feedback to Rejected
-Wmissing-field-initializers is in -Wextra, not even in -Wall. From the manual (emphasis mine)
Warn if a structure’s initializer has some fields missing. For example, the following code causes such a warning, because x.h is implicitly zero:
struct s { int f, g, h; }; struct s x = { 3, 4 };
All fields are initialized, just some are done implicitly. In fact, before the patch you point to, all fields were actually uninitialized; there is strictly more initialization happening now.
The warning acts more like a lint if you want to conform to a certain coding style, and isn't a flag for some material problem that ends up in the compiled artifact. It's fine to use this warning in your code, but it's unreasonable to expect ruby to use the same style as you. You can toss in a #pragma around #include "ruby.h" to fix your build.
In general, -Werror makes your build forward incompatible with future compilers because they release with new warnings all the time.
Updated by wsfulton (William Fulton) 6 days ago
I thought it was UB in c++98, but have just checked the standard and I have this wrong as the missing members are indeed default initialized.
A more conventional way for default aggregate initialisation is
struct RString retval = {};
This has the advantages of being compatible with C and C++, so does not need the RBASIC_INIT macro. It won't require specifying adding additional initializers either should RString change and also does not issue the warning -Wmissing-field-initializer making it more extension writer friendly.
But, the message is clear, at least some of the Ruby developers are not interested in reducing warnings in the code that some find useful and we will have to work around this warning.
Updated by mame (Yusuke Endoh) 6 days ago
Fun C topic!
wsfulton (William Fulton) wrote in #note-8:
A more conventional way for default aggregate initialisation is
struct RString retval = {};
Unfortunately, that empty initializer is a violation of the C99 language specification. You can confirm that with -pedantic option:
$ echo 'struct { int i; } retval = {};' | gcc -pedantic -std=c99 -xc -
<stdin>:1:28: warning: ISO C forbids empty initializer braces before C23 [-Wpedantic]
The current code isn't unnecessarily complicated for no reason.