diff --git a/lib/std/core/builtin.c3 b/lib/std/core/builtin.c3 index ef4dd38f7..171f228d1 100644 --- a/lib/std/core/builtin.c3 +++ b/lib/std/core/builtin.c3 @@ -317,6 +317,9 @@ macro any.retype_to(&self, typeid type) return $$any_make(self.ptr, type); } +<* + @require (bool)self.type : "A typed any was expected" +*> macro any.as_inner(&self) { return $$any_make(self.ptr, self.type.inner); diff --git a/lib/std/core/types.c3 b/lib/std/core/types.c3 index c097c846d..44572c2a9 100644 --- a/lib/std/core/types.c3 +++ b/lib/std/core/types.c3 @@ -7,6 +7,7 @@ faultdef VALUE_OUT_OF_RANGE, VALUE_OUT_OF_UNSIGNED_RANGE; <* @require $Type.kindof.is_int() : "Type was not an integer" + @require (bool)v.type : "The value was empty" @require v.type.kindof == ENUM : "Value was not an enum" *> macro any_to_enum_ordinal(any v, $Type) @@ -16,6 +17,7 @@ macro any_to_enum_ordinal(any v, $Type) <* @require $Type.kindof.is_int() : "Type was not an integer" + @require (bool)v.type : "The value was empty" @require v.type.kindof.is_int() : "Value was not an integer" *> macro any_to_int(any v, $Type) diff --git a/releasenotes.md b/releasenotes.md index 4a405a64a..a8011a854 100644 --- a/releasenotes.md +++ b/releasenotes.md @@ -6,7 +6,7 @@ - Removed support for LLVM 17, 18. ### Stdlib changes -None +- Add contract on `any_to_enum_ordinal` and `any_to_int` to improve error when passed an empty any. #2977 ### Fixes None