diff --git a/include/rice/rice.hpp b/include/rice/rice.hpp index 13fb2c89..87bd59dd 100644 --- a/include/rice/rice.hpp +++ b/include/rice/rice.hpp @@ -1169,7 +1169,12 @@ namespace Rice::detail if (k == klass && m == method_id) { - return std::any_cast(d); + auto* ptr = std::any_cast(&d); + if (!ptr) + { + rb_raise(rb_eRuntimeError, "Unexpected return type for %s#%s", rb_class2name(klass), rb_id2name(method_id)); + } + return *ptr; } } diff --git a/rice/detail/NativeRegistry.ipp b/rice/detail/NativeRegistry.ipp index d325f81b..78ba58b8 100644 --- a/rice/detail/NativeRegistry.ipp +++ b/rice/detail/NativeRegistry.ipp @@ -68,7 +68,12 @@ namespace Rice::detail if (k == klass && m == method_id) { - return std::any_cast(d); + auto* ptr = std::any_cast(&d); + if (!ptr) + { + rb_raise(rb_eRuntimeError, "Unexpected return type for %s#%s", rb_class2name(klass), rb_id2name(method_id)); + } + return *ptr; } }