From c068f5c310370bb47e1ec870b1382b87d20f3c3b Mon Sep 17 00:00:00 2001 From: Arthur Augusto <59454155+KitsuneSemCalda@users.noreply.github.com> Date: Fri, 30 Oct 2026 18:17:40 -0300 Subject: [PATCH] Fix Objective-C ARC assertion values and add standalone regressions --- .github/workflows/ci.yml | 23 ++++++++++++++++++++ cest.h | 32 +++++++++++++++++++++------- docs/en-us/api.md | 10 ++++++++- tests/objc_arc.m | 45 ++++++++++++++++++++++++++++++++++++++++ tests/objc_arc_probe.h | 20 ++++++++++++++++++ tests/objc_arc_probe.m | 24 +++++++++++++++++++++ 6 files changed, 146 insertions(+), 8 deletions(-) create mode 100644 tests/objc_arc.m create mode 100644 tests/objc_arc_probe.h create mode 100644 tests/objc_arc_probe.m diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index c84d96a..937ba19 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -71,6 +71,29 @@ jobs: - name: make run run: make run + objc-arc: + name: Objective-C ARC and MRC regression (native Apple runtime) + runs-on: macos-latest + steps: + - uses: actions/checkout@v4 + - name: Compile runtime fixture without ARC + run: clang -Wall -Wextra -Werror -fno-objc-arc -c tests/objc_arc_probe.m -o /tmp/cest_probe.o + - name: Run Objective-C and Objective-C++ in both ownership modes + run: | + for mode in -fobjc-arc -fno-objc-arc; do + for language in objective-c objective-c++; do + compiler=clang + if [ "$language" = objective-c++ ]; then compiler=clang++; fi + "$compiler" -Wall -Wextra -Werror "$mode" -x "$language" tests/objc_arc.m \ + -x none /tmp/cest_probe.o -framework Foundation -o /tmp/cest_arc + /tmp/cest_arc + code=0 + /tmp/cest_arc --negative > /tmp/cest_arc_negative.log 2>&1 || code=$? + test "$code" -eq 1 + grep 'Received (id:' /tmp/cest_arc_negative.log + done + done + msvc: name: msvc (cl.exe) runs-on: windows-latest diff --git a/cest.h b/cest.h index dfe89af..64c3c4e 100644 --- a/cest.h +++ b/cest.h @@ -1,8 +1,12 @@ #ifndef CEST_H_INCLUDED #define CEST_H_INCLUDED -#define _POSIX_C_SOURCE 200809L -#define _DEFAULT_SOURCE +#ifndef _POSIX_C_SOURCE +# define _POSIX_C_SOURCE 200809L +#endif +#ifndef _DEFAULT_SOURCE +# define _DEFAULT_SOURCE +#endif #include #include @@ -19,6 +23,16 @@ #ifdef __OBJC__ #import +# if defined(__clang__) +# if __has_feature(objc_arc) +# define _CEST_OBJC_BORROWED __unsafe_unretained +# define _CEST_OBJC_POINTER(value) ((__bridge const void *)(value)) +# endif +# endif +# ifndef _CEST_OBJC_BORROWED +# define _CEST_OBJC_BORROWED +# define _CEST_OBJC_POINTER(value) ((const void *)(value)) +# endif #endif #ifdef __cplusplus @@ -274,7 +288,9 @@ typedef struct { bool b; unsigned char u8; #ifdef __OBJC__ - id obj; + // Assertion values are synchronous borrows, not object owners. + // A strong id would make this union non-trivial under ARC. + id _CEST_OBJC_BORROWED obj; #endif #ifdef __cplusplus std::regex* re; @@ -541,6 +557,8 @@ static inline cest_value_t cest_value(T* v) { return cest_ptr((const void*)v); } #define cest_value(x) \ _Generic((x), \ _Bool: cest_bool, \ + signed char: cest_int, \ + unsigned char: cest_int, \ int: cest_int, \ long: cest_int, \ long long: cest_int, \ @@ -593,7 +611,7 @@ static inline void _cest_print_value(cest_value_t v) { case CEST_TYPE_BOOL: printf("(bool: %s)", v.as.b ? "true" : "false"); break; case CEST_TYPE_ARRAY: printf("(array[%d]: %p)", v.len, v.as.p); break; #ifdef __OBJC__ - case CEST_TYPE_OBJC_ID: printf("(id: %p)", (void*)v.as.obj); break; + case CEST_TYPE_OBJC_ID: printf("(id: %p)", _CEST_OBJC_POINTER(v.as.obj)); break; #else case CEST_TYPE_OBJC_ID: printf("(id: %p)", v.as.p); break; #endif @@ -655,12 +673,12 @@ static inline int match_eq(cest_value_t a, cest_value_t b, int* diff_pos) { (a.type == CEST_TYPE_OBJC_ID && b.type == CEST_TYPE_PTR)) { const void *p = a.type == CEST_TYPE_PTR ? a.as.p : b.as.p; id obj = a.type == CEST_TYPE_OBJC_ID ? a.as.obj : b.as.obj; - return p == (const void*)obj; + return p == _CEST_OBJC_POINTER(obj); } if ((a.type == CEST_TYPE_PTR || a.type == CEST_TYPE_OBJC_ID) && (b.type == CEST_TYPE_PTR || b.type == CEST_TYPE_OBJC_ID)) { - const void *p1 = a.type == CEST_TYPE_PTR ? a.as.p : (const void*)a.as.obj; - const void *p2 = b.type == CEST_TYPE_PTR ? b.as.p : (const void*)b.as.obj; + const void *p1 = a.type == CEST_TYPE_PTR ? a.as.p : _CEST_OBJC_POINTER(a.as.obj); + const void *p2 = b.type == CEST_TYPE_PTR ? b.as.p : _CEST_OBJC_POINTER(b.as.obj); return p1 == p2; } #endif diff --git a/docs/en-us/api.md b/docs/en-us/api.md index b35456f..db2c2d4 100644 --- a/docs/en-us/api.md +++ b/docs/en-us/api.md @@ -405,4 +405,12 @@ Output: ● test_string.c ● String ✓ compares -``` \ No newline at end of file +``` + +### Objective-C ownership in assertions + +Object assertion values borrow the object for the synchronous assertion; Cest +does not retain the last object in its global assertion context. Keep the object +alive through the assertion expression. ARC and manual-reference-counting builds +are supported, including Objective-C++. The regression sources in `tests/objc_arc*` +exercise nil, BOOL, pointer comparison, failure formatting and ARC deallocation. diff --git a/tests/objc_arc.m b/tests/objc_arc.m new file mode 100644 index 0000000..29eef17 --- /dev/null +++ b/tests/objc_arc.m @@ -0,0 +1,45 @@ +/* Standalone regression: this includes no FoxStep headers or implementation. + * Compile this same source as Objective-C and Objective-C++, with and without + * ARC, against libobjc2 (Linux) or the native Apple runtime. */ +#import "objc_arc_probe.h" +#include "../cest.h" +int main(int argc, char **argv) { + @autoreleasepool { + if (argc == 2 && strcmp(argv[1], "--negative") == 0) { + /* Exercises failure formatting (the id-to-void* diagnostic cast). */ + expect((id)nil).toBeTruthy(); + return cest_result(); + } + describe("Objective-C object assertions", { + it("supports nil, BOOL and object/pointer comparisons", { + id object = [CestARCProbe new]; + BOOL yes = YES; + BOOL no = NO; + expect(object).toBeTruthy(); + expect((id)nil).toBeNull(); + expect(yes).toBeTruthy(); + expect(no).toBeFalsy(); + expect((id)nil).toEqual((const void *)NULL); + expect((const void *)NULL).toEqual((id)nil); +#if !__has_feature(objc_arc) + [object release]; +#endif + }); +#if __has_feature(objc_arc) + it("does not retain an object in the last assertion's global context", { + __weak id weak; + int before = cest_arc_probe_deallocations; + @autoreleasepool { + id object = [CestARCProbe new]; + weak = object; + expect(object).toBeTruthy(); + object = nil; + } + expect(weak == nil).toBeTruthy(); + expect(cest_arc_probe_deallocations == before + 1).toBeTruthy(); + }); +#endif + }); + return cest_result(); + } +} diff --git a/tests/objc_arc_probe.h b/tests/objc_arc_probe.h new file mode 100644 index 0000000..5d98ea4 --- /dev/null +++ b/tests/objc_arc_probe.h @@ -0,0 +1,20 @@ +#ifndef CEST_ARC_PROBE_H +#define CEST_ARC_PROBE_H +#ifdef __APPLE__ +#import +@interface CestARCProbe : NSObject @end +#else +#import +__attribute__((objc_root_class)) +@interface CestARCProbe { Class isa; } ++ (id)new; +#if !__has_feature(objc_arc) +- (id)retain; +- (void)release; +- (id)autorelease; +- (void)dealloc; +#endif +@end +#endif +extern int cest_arc_probe_deallocations; +#endif diff --git a/tests/objc_arc_probe.m b/tests/objc_arc_probe.m new file mode 100644 index 0000000..48d32a8 --- /dev/null +++ b/tests/objc_arc_probe.m @@ -0,0 +1,24 @@ +/* Compile this runtime-only fixture without ARC. No application framework is + * required on Linux/libobjc2; Apple builds use the system NSObject. */ +#import "objc_arc_probe.h" +#ifndef __APPLE__ +#import +#endif +int cest_arc_probe_deallocations; +@implementation CestARCProbe +#ifndef __APPLE__ ++ (id)new { return class_createInstance(self, 0); } +- (id)retain { return objc_retain_fast_np(self); } +- (void)release { objc_release_fast_np(self); } +- (id)autorelease { return objc_autorelease(self); } +- (void)_ARCCompliantRetainRelease {} +#endif +- (void)dealloc { + ++cest_arc_probe_deallocations; +#ifdef __APPLE__ + [super dealloc]; +#else + object_dispose(self); +#endif +} +@end