Ruby 4.1.0dev (2026-09-27 revision f6ff9e7d02e46360f8930b280a3dd921cccbda29)
error.h
1#ifndef INTERNAL_ERROR_H /*-*-C-*-vi:se ft=c:*/
2#define INTERNAL_ERROR_H
11#include "ruby/internal/config.h"
12#include <stdarg.h> /* for va_list */
13#include "internal/string.h" /* for rb_fstring_cstr */
14#include "ruby/internal/stdbool.h" /* for bool */
15#include "ruby/encoding.h" /* for rb_encoding */
16#include "ruby/intern.h" /* for rb_exc_raise */
17#include "ruby/ruby.h" /* for enum ruby_value_type */
18
19#ifdef Check_Type
20# undef Check_Type /* in ruby/ruby.h */
21#endif
22
23#ifdef rb_raise_static
24# undef rb_raise_static
25# undef rb_sys_fail_path
26# undef rb_syserr_fail_path
27#endif
28
29#define rb_raise_static(e, m) \
30 rb_raise_cstr_i((e), rb_str_new_static((m), rb_strlen_lit(m)))
31#ifdef RUBY_FUNCTION_NAME_STRING
32# define rb_syserr_fail_path(err, path) rb_syserr_fail_path_in(RUBY_FUNCTION_NAME_STRING, (err), (path))
33# define rb_syserr_new_path(err, path) rb_syserr_new_path_in(RUBY_FUNCTION_NAME_STRING, (err), (path))
34#else
35# define rb_syserr_fail_path(err, path) rb_syserr_fail_str((err), (path))
36# define rb_syserr_new_path(err, path) rb_syserr_new_str((err), (path))
37#endif
38
39#define rb_sys_fail(mesg) \
40do { \
41 int errno_to_fail = errno; \
42 rb_syserr_fail(errno_to_fail, (mesg)); \
43} while (0)
44
45#define rb_sys_fail_str(mesg) \
46do { \
47 int errno_to_fail = errno; \
48 rb_syserr_fail_str(errno_to_fail, (mesg)); \
49} while (0)
50
51#define rb_sys_fail_path(path) \
52do { \
53 int errno_to_fail = errno; \
54 rb_syserr_fail_path(errno_to_fail, (path)); \
55} while (0)
56
57#define rb_sys_fail_sprintf(...) \
58do { \
59 int errno_to_fail = errno; \
60 rb_syserr_fail_str(errno_to_fail, rb_sprintf("" __VA_ARGS__)); \
61} while (0)
62
63/* error.c */
64extern long rb_backtrace_length_limit;
65extern VALUE rb_eEAGAIN;
66extern VALUE rb_eEWOULDBLOCK;
67extern VALUE rb_eEINPROGRESS;
68RBIMPL_ATTR_FORMAT(RBIMPL_PRINTF_FORMAT, 3, 0)
69void rb_report_bug_valist(VALUE file, int line, const char *fmt, va_list args);
70NORETURN(void rb_async_bug_errno(const char *,int));
71const char *rb_builtin_type_name(int t);
72const char *rb_builtin_class_name(VALUE x);
73PRINTF_ARGS(void rb_warn_deprecated(const char *fmt, const char *suggest, ...), 1, 3);
74PRINTF_ARGS(void rb_warn_reserved_name(const char *removal, const char *fmt, ...), 2, 3);
75#include "ruby/version.h"
76#define RUBY_VERSION_SINCE(major, minor) (RUBY_API_VERSION_CODE >= (major) * 10000 + (minor) * 100)
77#define RUBY_VERSION_BEFORE(major, minor) (RUBY_API_VERSION_CODE < (major) * 10000 + (minor) * 100)
78
79#define RUBY_VERSION_isdigit(c) ('0'<=(c)&&(c)<='9')
80// upto 99
81#define RUBY_VERSION__number_len(v, ofs) \
82 (!RUBY_VERSION_isdigit((v)[ofs]) ? \
83 0 : !RUBY_VERSION_isdigit((v)[(ofs) + 1]) ? 1 : 2)
84#define RUBY_VERSION__to_number(v, ofs) \
85 (!RUBY_VERSION_isdigit((v)[ofs]) ? \
86 0 : !RUBY_VERSION_isdigit((v)[(ofs) + 1]) ? \
87 ((v)[ofs]-'0') : \
88 (((v)[ofs]-'0')*10+(v)[(ofs)+1]-'0'))
89
90#define RUBY_VERSION_CODE_FROM_MAJOR_MINOR_STRING(v) \
91 (RUBY_VERSION__to_number(v, 0) * 10000 + \
92 ((v)[RUBY_VERSION__number_len(v, 0)] == '.' ? \
93 RUBY_VERSION__to_number(v, RUBY_VERSION__number_len(v, 0)+1) * 100 : 0))
94#define RUBY_VERSION_STRING_SINCE(v) (RUBY_API_VERSION_CODE >= RUBY_VERSION_CODE_FROM_MAJOR_MINOR_STRING(v))
95#define RUBY_VERSION_STRING_BEFORE(v) (RUBY_API_VERSION_CODE < RUBY_VERSION_CODE_FROM_MAJOR_MINOR_STRING(v))
96
97#if RUBY_DEBUG
98# if defined(RBIMPL_WARNING_PRAGMA0)
99# define RBIMPL_TODO0(x) RBIMPL_WARNING_PRAGMA0(message(x))
100# elif RBIMPL_COMPILER_IS(MSVC)
101# define RBIMPL_TODO0(x) __pragma(message(x))
102# endif
103
104# if RBIMPL_HAS_ATTRIBUTE(diagnose_if) || defined(__OPTIMIZE__)
105
106# if RBIMPL_HAS_ATTRIBUTE(diagnose_if)
108static void
109rb_deprecated_method_to_be_removed(const char *removal)
110 RBIMPL_ATTR_DIAGNOSE_IF(!RUBY_VERSION_isdigit(removal[0]), "malformed version number", "error")
111 RBIMPL_ATTR_DIAGNOSE_IF(RUBY_VERSION_STRING_SINCE(removal), "deprecated method to be removed", "error")
112{
113}
114
116static void
117rb_diagnose_scheduled_deprecation(const char *unconditional)
118 RBIMPL_ATTR_DIAGNOSE_IF(!RUBY_VERSION_isdigit(unconditional[0]), "malformed version number", "error")
119{
120}
121
123static void
124rb_diagnose_reserved_name_at(const char *coming)
125 RBIMPL_ATTR_DIAGNOSE_IF(!RUBY_VERSION_isdigit(coming[0]), "malformed version number", "error")
126 RBIMPL_ATTR_DIAGNOSE_IF(RUBY_VERSION_STRING_SINCE(coming), "reserved name already in use", "error")
127{
128}
129# else
130RBIMPL_ATTR_ERROR(("deprecated"))
131void rb_deprecated_method_to_be_removed(const char *);
132# define rb_deprecated_method_to_be_removed(removal) \
133 (sizeof(char[1-2*(!RUBY_VERSION_isdigit(removal[0]) || RUBY_VERSION_STRING_SINCE(removal))])!=1 ? \
134 rb_deprecated_method_to_be_removed(removal) : \
135 RBIMPL_ASSERT_NOTHING)
136
137RBIMPL_ATTR_ERROR(("malformed version number"))
138void rb_diagnose_scheduled_deprecation(const char *);
139# define rb_diagnose_scheduled_deprecation(unconditional) \
140 (sizeof(char[1-2*!RUBY_VERSION_isdigit(unconditional[0])])!=1 ? \
141 rb_diagnose_scheduled_deprecation(unconditional) : \
142 RBIMPL_ASSERT_NOTHING)
143
144RBIMPL_ATTR_ERROR(("deprecated"))
145void rb_diagnose_reserved_name_at(const char *);
146# define rb_diagnose_reserved_name_at(coming) \
147 (sizeof(char[1-2*(!RUBY_VERSION_isdigit(coming[0]) || RUBY_VERSION_STRING_SINCE(coming))])!=1 ? \
148 rb_diagnose_reserved_name_at(coming) : \
149 RBIMPL_ASSERT_NOTHING)
150
151# endif
152# define rb_warn_deprecated_to_remove_at(removal, ...) \
153 (rb_deprecated_method_to_be_removed(#removal), \
154 rb_warn_deprecated_to_remove(#removal, __VA_ARGS__))
155
156# define rb_warn_to_remove_at(removal, ...) \
157 (rb_deprecated_method_to_be_removed(#removal), \
158 rb_warn_to_remove(#removal, __VA_ARGS__))
159
160/* Warn only with Warning[:deprecated] before `unconditional`, always from then on. */
161# define rb_warn_scheduled_deprecation(unconditional, removal, ...) \
162 (rb_diagnose_scheduled_deprecation(#unconditional), \
163 RUBY_VERSION_STRING_SINCE(#unconditional) ? \
164 rb_warn_to_remove_at(removal, __VA_ARGS__) : \
165 rb_warn_deprecated_to_remove_at(removal, __VA_ARGS__))
166
167# define rb_warn_reserved_name_at(coming, ...) \
168 (rb_diagnose_reserved_name_at(#coming), \
169 rb_warn_reserved_name(#coming, __VA_ARGS__))
170# endif
171#endif
172#ifndef rb_warn_deprecated_to_remove_at
173# define rb_warn_deprecated_to_remove_at(removal, ...) \
174 rb_warn_deprecated_to_remove(#removal, __VA_ARGS__)
175#endif
176#ifndef rb_warn_to_remove_at
177# define rb_warn_to_remove_at(removal, ...) \
178 rb_warn_to_remove(#removal, __VA_ARGS__)
179#endif
180#ifndef rb_warn_scheduled_deprecation
181# define rb_warn_scheduled_deprecation(unconditional, removal, ...) \
182 (RUBY_VERSION_STRING_SINCE(#unconditional) ? \
183 rb_warn_to_remove_at(removal, __VA_ARGS__) : \
184 rb_warn_deprecated_to_remove_at(removal, __VA_ARGS__))
185#endif
186#ifndef rb_warn_reserved_name_at
187# define rb_warn_reserved_name_at(removal, ...) \
188 rb_warn_reserved_name(#removal, __VA_ARGS__)
189#endif
190#ifndef RBIMPL_TODO0
191# define RBIMPL_TODO0(x)
192#endif
193#define RBIMPL_TODO(message) RBIMPL_TODO0("TODO: " message)
194RBIMPL_ATTR_FORMAT(RBIMPL_PRINTF_FORMAT, 6, 0)
195VALUE rb_syntax_error_append(VALUE, VALUE, int, int, rb_encoding*, const char*, va_list);
196PRINTF_ARGS(void rb_enc_warn(rb_encoding *enc, const char *fmt, ...), 2, 3);
197PRINTF_ARGS(void rb_sys_enc_warning(rb_encoding *enc, const char *fmt, ...), 2, 3);
198PRINTF_ARGS(void rb_syserr_enc_warning(int err, rb_encoding *enc, const char *fmt, ...), 3, 4);
199PRINTF_ARGS(void rb_enc_compile_warning(rb_encoding *enc, const char *file, int line, const char *fmt, ...), 4, 5);
200PRINTF_ARGS(void rb_enc_compile_warn(rb_encoding *enc, const char *file, int line, const char *fmt, ...), 4, 5);
201rb_warning_category_t rb_warning_category_from_name(VALUE category);
202bool rb_warning_category_enabled_p(rb_warning_category_t category);
203VALUE rb_name_err_new(VALUE mesg, VALUE recv, VALUE method);
204VALUE rb_nomethod_err_new(VALUE mesg, VALUE recv, VALUE method, VALUE args, int priv);
205VALUE rb_key_err_new(VALUE mesg, VALUE recv, VALUE name);
206PRINTF_ARGS(VALUE rb_warning_string(const char *fmt, ...), 1, 2);
207RBIMPL_ATTR_FORMAT(RBIMPL_PRINTF_FORMAT, 2, 0)
208NORETURN(void rb_vraise(VALUE, const char *, va_list));
209NORETURN(static inline void rb_raise_cstr(VALUE etype, const char *mesg));
210NORETURN(static inline void rb_raise_cstr_i(VALUE etype, VALUE mesg));
211NORETURN(static inline void rb_name_err_raise_str(VALUE mesg, VALUE recv, VALUE name));
212NORETURN(static inline void rb_name_err_raise(const char *mesg, VALUE recv, VALUE name));
213NORETURN(static inline void rb_key_err_raise(VALUE mesg, VALUE recv, VALUE name));
214static inline void Check_Type(VALUE v, enum ruby_value_type t);
215static inline bool rb_typeddata_is_instance_of_inline(VALUE obj, const rb_data_type_t *data_type);
216#define rb_typeddata_is_instance_of rb_typeddata_is_instance_of_inline
217void rb_bug_without_die(const char *fmt, ...);
218NORETURN(void rb_no_implicit_conversion(VALUE val, const char *tname));
219NORETURN(void rb_cant_convert(VALUE val, const char *tname));
220NORETURN(void rb_cant_convert_invalid_return(VALUE val, const char *tname, const char *method_name, VALUE ret));
221void rb_eager_load_detailed_message_extension(void);
222
223RUBY_SYMBOL_EXPORT_BEGIN
224/* error.c (export) */
225PRINTF_ARGS(void rb_warn_deprecated_to_remove(const char *removal, const char *fmt, const char *suggest, ...), 2, 4);
226PRINTF_ARGS(void rb_warn_to_remove(const char *removal, const char *fmt, const char *suggest, ...), 2, 4);
227int rb_bug_reporter_add(void (*func)(FILE *, void *), void *data);
228#ifdef RUBY_FUNCTION_NAME_STRING
229NORETURN(void rb_sys_fail_path_in(const char *func_name, VALUE path));
230NORETURN(void rb_syserr_fail_path_in(const char *func_name, int err, VALUE path));
231VALUE rb_syserr_new_path_in(const char *func_name, int n, VALUE path);
232#endif
233RUBY_SYMBOL_EXPORT_END
234
235/* vm.c */
236void rb_free_warning(void);
237
238static inline void
239rb_raise_cstr_i(VALUE etype, VALUE mesg)
240{
241 VALUE exc = rb_exc_new_str(etype, mesg);
242 rb_exc_raise(exc);
243}
244
245static inline void
246rb_raise_cstr(VALUE etype, const char *mesg)
247{
248 VALUE str = rb_str_new_cstr(mesg);
249 rb_raise_cstr_i(etype, str);
250}
251
252static inline void
253rb_name_err_raise_str(VALUE mesg, VALUE recv, VALUE name)
254{
255 VALUE exc = rb_name_err_new(mesg, recv, name);
256 rb_exc_raise(exc);
257}
258
259static inline void
260rb_name_err_raise(const char *mesg, VALUE recv, VALUE name)
261{
262 VALUE str = rb_fstring_cstr(mesg);
263 rb_name_err_raise_str(str, recv, name);
264}
265
266static inline void
267rb_key_err_raise(VALUE mesg, VALUE recv, VALUE name)
268{
269 VALUE exc = rb_key_err_new(mesg, recv, name);
270 rb_exc_raise(exc);
271}
272
274static inline bool
275rb_typeddata_is_instance_of_inline(VALUE obj, const rb_data_type_t *data_type)
276{
277 return rbimpl_obj_typeddata_p(obj) && (RTYPEDDATA_TYPE(obj) == data_type);
278}
279
280typedef enum {
281 rb_stack_overflow_prevention = 0, // VM stack overflow or about to machine stack overflow
282 rb_stack_overflow_signal = 1, // machine stack overflow but may be recoverable
283 rb_stack_overflow_fatal = 2, // fatal machine stack overflow
284} ruby_stack_overflow_critical_level;
285NORETURN(void rb_ec_stack_overflow(struct rb_execution_context_struct *ec, ruby_stack_overflow_critical_level crit));
286
287#endif /* INTERNAL_ERROR_H */
#define RBIMPL_ATTR_DIAGNOSE_IF(_, __, ___)
Wraps (or simulates) __attribute__((diagnose_if))
Definition diagnose_if.h:39
#define RBIMPL_ATTR_FORCEINLINE()
Wraps (or simulates) __forceinline.
Definition forceinline.h:37
rb_warning_category_t
Warning categories.
Definition error.h:43
void rb_exc_raise(VALUE mesg)
Raises an exception in the current thread.
Definition eval.c:678
VALUE rb_exc_new_str(VALUE etype, VALUE str)
Identical to rb_exc_new_cstr(), except it takes a Ruby's string instead of C's.
Definition error.c:1524
Encoding relates APIs.
#define RBIMPL_ATTR_ERROR(msg)
Wraps (or simulates) __attribute__((error))
Definition error.h:29
#define RBIMPL_ATTR_FORMAT(x, y, z)
Wraps (or simulates) __attribute__((format))
Definition format.h:33
#define RBIMPL_ATTR_NONNULL(list)
Wraps (or simulates) __attribute__((nonnull))
Definition nonnull.h:30
#define rb_str_new_cstr(str)
Identical to rb_str_new, except it assumes the passed pointer is a pointer to a C string.
Definition string.h:1515
static const rb_data_type_t * RTYPEDDATA_TYPE(VALUE obj)
Queries for the type of given object.
Definition rtypeddata.h:692
Defines old _.
C99 shim for <stdbool.h>
This is the struct that holds necessary info for a struct.
Definition rtypeddata.h:242
uintptr_t VALUE
Type that represents a Ruby object.
Definition value.h:40
ruby_value_type
C-level type of an object.
Definition value_type.h:113