Skip to content

Commit

Permalink
[ffigen] Fix nullable typealias bug (#1718)
Browse files Browse the repository at this point in the history
  • Loading branch information
liamappelbe authored Nov 13, 2024
1 parent a1f33ee commit 8211198
Show file tree
Hide file tree
Showing 6 changed files with 462 additions and 14 deletions.
2 changes: 2 additions & 0 deletions pkgs/ffigen/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@
- https://github.com/dart-lang/native/issues/1582
- https://github.com/dart-lang/native/issues/1594
- https://github.com/dart-lang/native/issues/1595
- Fix [a bug](https://github.com/dart-lang/native/issues/1701) where nullable
typealiases were treated as non-null.
- Allow static and instance methods to have the same name:
https://github.com/dart-lang/native/issues/1136
- __Breaking change__: Change the way ObjC categories are generated. Instead of
Expand Down
5 changes: 3 additions & 2 deletions pkgs/ffigen/lib/src/code_generator/objc_nullable.dart
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,11 @@ class ObjCNullable extends Type {
Type child;

ObjCNullable(this.child)
: assert(isSupported(child.typealiasType),
: assert(isSupported(child),
'Nullable ${child.typealiasType.runtimeType} is not supported');

static bool isSupported(Type type) =>
static bool isSupported(Type type) => _isSupported(type.typealiasType);
static bool _isSupported(Type type) =>
type is ObjCInterface ||
type is ObjCBlock ||
type is ObjCObjectPointer ||
Expand Down
6 changes: 6 additions & 0 deletions pkgs/ffigen/test/native_objc_test/nullable_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -71,5 +71,11 @@ void main() {
false);
});
});

test('Nullable typealias', () {
// Regression test for https://github.com/dart-lang/native/issues/1701
expect(NullableInterface.returnNullableAlias_(true), isNull);
expect(NullableInterface.returnNullableAlias_(false)?.toString(), "Hi");
});
});
}
11 changes: 11 additions & 0 deletions pkgs/ffigen/test/native_objc_test/nullable_test.m
Original file line number Diff line number Diff line change
@@ -1,12 +1,15 @@
#import <Foundation/NSObject.h>

typedef NSString* MyString;

@interface NullableInterface : NSObject {
}

+(BOOL) isNullWithNullableNSObjectArg:(nullable NSObject *)x;
+(BOOL) isNullWithNotNullableNSObjectPtrArg:(NSObject *)x;
+(BOOL) isNullWithExplicitNonNullableNSObjectPtrArg:(nonnull NSObject *)x;
+(nullable NSObject *) returnNil:(BOOL)r;
+(nullable MyString) returnNullableAlias:(BOOL)r;

@property (nullable, retain) NSObject *nullableObjectProperty;

Expand Down Expand Up @@ -34,4 +37,12 @@ +(nullable NSObject *) returnNil:(BOOL)r {
}
}

+(nullable MyString) returnNullableAlias:(BOOL)r {
if (r) {
return nil;
} else {
return @"Hi";
}
}

@end
Loading

0 comments on commit 8211198

Please sign in to comment.