Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[ffigen] Fix nullable typealias bug #1718

Merged
merged 3 commits into from
Nov 13, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading