Skip to content

Commit 7e44df9

Browse files
XrXrk0kubun
authored andcommitted
Fix C23 (GCC 15) WIN32 compatibility for rb_define_* functions
Backport [Bug #21286]
1 parent 4aac150 commit 7e44df9

File tree

1 file changed

+22
-0
lines changed

1 file changed

+22
-0
lines changed

include/ruby/internal/anyargs.h

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -84,12 +84,15 @@
8484

8585
#elif defined(_WIN32) || defined(__CYGWIN__)
8686
# /* Skip due to [Bug #16134] */
87+
# define RBIMPL_CAST_FN_PTR 1
8788

8889
#elif ! RBIMPL_HAS_ATTRIBUTE(transparent_union)
8990
# /* :TODO: improve here, please find a way to support. */
91+
# define RBIMPL_CAST_FN_PTR 1
9092

9193
#elif ! defined(HAVE_VA_ARGS_MACRO)
9294
# /* :TODO: improve here, please find a way to support. */
95+
# define RBIMPL_CAST_FN_PTR 1
9396

9497
#else
9598
# /** @cond INTERNAL_MACRO */
@@ -348,6 +351,25 @@ RBIMPL_ANYARGS_DECL(rb_define_method, VALUE, const char *)
348351

349352
#endif /* __cplusplus */
350353

354+
#if defined(RBIMPL_CAST_FN_PTR) && !defined(__cplusplus)
355+
/* In C23, K&R style prototypes are gone and so `void foo(ANYARGS)` became
356+
* equivalent to `void foo(void)` unlike in earlier versions. This is a problem
357+
* for rb_define_* functions since that makes all valid functions one can pass
358+
* trip -Wincompatible-pointer-types, which we treat as errors. This is mostly
359+
* not a problem for the __builtin_choose_expr path, but outside of that we
360+
* need to add a cast for compatibility.
361+
*/
362+
#define rb_define_method(klass, mid, func, arity) rb_define_method((klass), (mid), (VALUE (*)(ANYARGS))(func), (arity))
363+
#define rb_define_method_id(klass, mid, func, arity) rb_define_method_id((klass), (mid), (VALUE (*)(ANYARGS))(func), (arity))
364+
#define rb_define_singleton_method(obj, mid, func, arity) rb_define_singleton_method((obj), (mid), (VALUE (*)(ANYARGS))(func), (arity))
365+
#define rb_define_protected_method(klass, mid, func, arity) rb_define_protected_method((klass), (mid), (VALUE (*)(ANYARGS))(func), (arity))
366+
#define rb_define_private_method(klass, mid, func, arity) rb_define_private_method((klass), (mid), (VALUE (*)(ANYARGS))(func), (arity))
367+
#define rb_define_module_function(mod, mid, func, arity) rb_define_module_function((mod), (mid), (VALUE (*)(ANYARGS))(func), (arity))
368+
#define rb_define_global_function(mid, func, arity) rb_define_global_function((mid), (VALUE (*)(ANYARGS))(func), (arity))
369+
370+
#undef RBIMPL_CAST_FN_PTR
371+
#endif /* defined(RBIMPL_CAST_FN_PTR) && !defined(__cplusplus) */
372+
351373
/**
352374
* This macro is to properly cast a function parameter of *_define_method
353375
* family. It has been around since 1.x era so you can maximise backwards

0 commit comments

Comments
 (0)