From 511ae0da0061be338f8af9e58d675b4bb5b97e06 Mon Sep 17 00:00:00 2001 From: Christoffer Lerno Date: Wed, 14 May 2025 23:22:34 +0200 Subject: [PATCH] Contract on trying to use Object without initializing it. --- lib/std/collections/object.c3 | 5 ++++- releasenotes.md | 1 + 2 files changed, 5 insertions(+), 1 deletion(-) diff --git a/lib/std/collections/object.c3 b/lib/std/collections/object.c3 index a3e610d68..c44a7765b 100644 --- a/lib/std/collections/object.c3 +++ b/lib/std/collections/object.c3 @@ -186,6 +186,9 @@ fn void Object.set_object(&self, String key, Object* new_object) @private self.map.set(key, new_object); } +<* + @require self.allocator != null : "This object is not properly initialized, was it really created using 'new'" +*> macro Object* Object.object_from_value(&self, value) @private { var $Type = $typeof(value); @@ -201,7 +204,7 @@ macro Object* Object.object_from_value(&self, value) @private $case $Type.typeid == Object*.typeid: return value; $case $Type.typeid == void*.typeid: - if (value != null) return TYPE_MISMATCH?; + if (value != null) TYPE_MISMATCH?!; return &NULL_OBJECT; $case $assignable(value, String): return new_string(value, self.allocator); diff --git a/releasenotes.md b/releasenotes.md index 37be342be..81b50bef1 100644 --- a/releasenotes.md +++ b/releasenotes.md @@ -16,6 +16,7 @@ - Fix regression in Time diff due to operator overloading #2124. - attrdef with any invalid name causes compiler assert #2128. - Correctly error on `@attrdef Foo = ;`. +- Contract on trying to use Object without initializing it. ### Stdlib changes - Added `String.quick_ztr` and `String.is_zstr`