Skip to content

Commit 28c689b

Browse files
champoeliasnaur
authored andcommitted
bind: annotate all remaining ObjC types for nullability
From Xcode 10.2 onwards, these annotations are required in all interface declarations. The behaviour can be disabled, but since we were already annotated most types it made sense to just annotate the rest. Change-Id: Iacd09a2fea4dfb3e22fec97cf4ca22966fc783bf GitHub-Last-Rev: a431572 GitHub-Pull-Request: #29 Reviewed-on: https://go-review.googlesource.com/c/mobile/+/171957 Reviewed-by: Elias Naur <[email protected]>
1 parent 167ebed commit 28c689b

25 files changed

+122
-122
lines changed

bind/genobjc.go

+10-10
Original file line numberDiff line numberDiff line change
@@ -198,7 +198,7 @@ func (g *ObjcGen) GenH() error {
198198
g.objcdoc(g.docs[obj.Name()].Doc())
199199
switch b := obj.Type().(*types.Basic); b.Kind() {
200200
case types.String, types.UntypedString:
201-
g.Printf("FOUNDATION_EXPORT NSString* const %s%s;\n", g.namePrefix, obj.Name())
201+
g.Printf("FOUNDATION_EXPORT NSString* _Nonnull const %s%s;\n", g.namePrefix, obj.Name())
202202
default:
203203
g.Printf("FOUNDATION_EXPORT const %s %s%s;\n", g.objcType(obj.Type()), g.namePrefix, obj.Name())
204204
}
@@ -545,7 +545,7 @@ func (s *funcSummary) asFunc(g *ObjcGen) string {
545545
skip = 1
546546
}
547547
for _, p := range s.retParams[skip:] {
548-
params = append(params, g.objcType(p.typ)+"* "+p.name)
548+
params = append(params, g.objcType(p.typ)+"* _Nullable "+p.name)
549549
}
550550
paramContents := "void"
551551
if len(params) > 0 {
@@ -580,7 +580,7 @@ func (s *funcSummary) asSignature(g *ObjcGen) string {
580580
if len(params) > 0 {
581581
key = p.name
582582
}
583-
params = append(params, fmt.Sprintf("%s:(%s)%s", key, g.objcType(p.typ)+"*", p.name))
583+
params = append(params, fmt.Sprintf("%s:(%s)%s", key, g.objcType(p.typ)+"* _Nullable", p.name))
584584
}
585585
return strings.Join(params, " ")
586586
}
@@ -878,9 +878,9 @@ func (g *ObjcGen) genInterfaceInterface(obj *types.TypeName, summary ifaceSummar
878878
}
879879
g.Printf(" <%s>", strings.Join(prots, ", "))
880880
g.Printf(" {\n}\n")
881-
g.Printf("@property(strong, readonly) id _ref;\n")
881+
g.Printf("@property(strong, readonly) _Nonnull id _ref;\n")
882882
g.Printf("\n")
883-
g.Printf("- (instancetype)initWithRef:(id)ref;\n")
883+
g.Printf("- (nonnull instancetype)initWithRef:(_Nonnull id)ref;\n")
884884
for _, m := range summary.callable {
885885
if !g.isSigSupported(m.Type()) {
886886
g.Printf("// skipped method %s.%s with unsupported parameter or return types\n\n", obj.Name(), m.Name())
@@ -918,7 +918,7 @@ func (g *ObjcGen) genInterfaceM(obj *types.TypeName, t *types.Interface) bool {
918918
g.Printf("@implementation %s%s {\n", g.namePrefix, obj.Name())
919919
g.Printf("}\n")
920920
g.Printf("\n")
921-
g.Printf("- (instancetype)initWithRef:(id)ref {\n")
921+
g.Printf("- (nonnull instancetype)initWithRef:(id)ref {\n")
922922
g.Indent()
923923
if isErrorType(obj.Type()) {
924924
g.Printf("if (self) {\n")
@@ -1097,9 +1097,9 @@ func (g *ObjcGen) genStructH(obj *types.TypeName, t *types.Struct) {
10971097
}
10981098
g.Printf(" {\n")
10991099
g.Printf("}\n")
1100-
g.Printf("@property(strong, readonly) id _ref;\n")
1100+
g.Printf("@property(strong, readonly) _Nonnull id _ref;\n")
11011101
g.Printf("\n")
1102-
g.Printf("- (nonnull instancetype)initWithRef:(id)ref;\n")
1102+
g.Printf("- (nonnull instancetype)initWithRef:(_Nonnull id)ref;\n")
11031103
cons := g.constructors[obj]
11041104
if oinf == nil {
11051105
for _, f := range cons {
@@ -1156,7 +1156,7 @@ func (g *ObjcGen) genStructM(obj *types.TypeName, t *types.Struct) {
11561156
oinf := g.ostructs[obj]
11571157
g.Printf("@implementation %s%s {\n", g.namePrefix, obj.Name())
11581158
g.Printf("}\n\n")
1159-
g.Printf("- (instancetype)initWithRef:(id)ref {\n")
1159+
g.Printf("- (nonnull instancetype)initWithRef:(_Nonnull id)ref {\n")
11601160
g.Indent()
11611161
g.Printf("self = [super init];\n")
11621162
g.Printf("if (self) { __ref = ref; }\n")
@@ -1174,7 +1174,7 @@ func (g *ObjcGen) genStructM(obj *types.TypeName, t *types.Struct) {
11741174
}
11751175
}
11761176
if oinf != nil || len(cons) == 0 {
1177-
g.Printf("- (instancetype)init {\n")
1177+
g.Printf("- (nonnull instancetype)init {\n")
11781178
g.Indent()
11791179
g.Printf("self = [super init];\n")
11801180
g.Printf("if (self) {\n")

bind/testdata/basictypes.objc.h.golden

+4-4
Original file line numberDiff line numberDiff line change
@@ -13,19 +13,19 @@
1313

1414
FOUNDATION_EXPORT const BOOL BasictypesABool;
1515
FOUNDATION_EXPORT const double BasictypesAFloat;
16-
FOUNDATION_EXPORT NSString* const BasictypesALongString;
16+
FOUNDATION_EXPORT NSString* _Nonnull const BasictypesALongString;
1717
FOUNDATION_EXPORT const int32_t BasictypesARune;
18-
FOUNDATION_EXPORT NSString* const BasictypesAString;
18+
FOUNDATION_EXPORT NSString* _Nonnull const BasictypesAString;
1919
FOUNDATION_EXPORT const int64_t BasictypesAnInt;
2020
FOUNDATION_EXPORT const int64_t BasictypesAnInt2;
2121

2222
FOUNDATION_EXPORT BOOL BasictypesBool(BOOL p0);
2323

2424
FOUNDATION_EXPORT NSData* _Nullable BasictypesByteArrays(NSData* _Nullable x);
2525

26-
FOUNDATION_EXPORT BOOL BasictypesError(NSError* _Nullable* error);
26+
FOUNDATION_EXPORT BOOL BasictypesError(NSError* _Nullable* _Nullable error);
2727

28-
FOUNDATION_EXPORT BOOL BasictypesErrorPair(long* ret0_, NSError* _Nullable* error);
28+
FOUNDATION_EXPORT BOOL BasictypesErrorPair(long* _Nullable ret0_, NSError* _Nullable* _Nullable error);
2929

3030
FOUNDATION_EXPORT void BasictypesInts(int8_t x, int16_t y, int32_t z, int64_t t, long u);
3131

bind/testdata/basictypes.objc.m.golden

+2-2
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ NSData* _Nullable BasictypesByteArrays(NSData* _Nullable x) {
3434
return _ret0_;
3535
}
3636

37-
BOOL BasictypesError(NSError* _Nullable* error) {
37+
BOOL BasictypesError(NSError* _Nullable* _Nullable error) {
3838
int32_t r0 = proxybasictypes__Error();
3939
Universeerror* _error = nil;
4040
GoSeqRef* _error_ref = go_seq_from_refnum(r0);
@@ -50,7 +50,7 @@ BOOL BasictypesError(NSError* _Nullable* error) {
5050
return (_error == nil);
5151
}
5252

53-
BOOL BasictypesErrorPair(long* ret0_, NSError* _Nullable* error) {
53+
BOOL BasictypesErrorPair(long* _Nullable ret0_, NSError* _Nullable* _Nullable error) {
5454
struct proxybasictypes__ErrorPair_return res = proxybasictypes__ErrorPair();
5555
long _ret0_ = (long)res.r0;
5656
Universeerror* _error = nil;

bind/testdata/doc.objc.h.golden

+8-8
Original file line numberDiff line numberDiff line change
@@ -26,9 +26,9 @@
2626
*/
2727
@interface DocNoDoc : NSObject <goSeqRefInterface> {
2828
}
29-
@property(strong, readonly) id _ref;
29+
@property(strong, readonly) _Nonnull id _ref;
3030

31-
- (nonnull instancetype)initWithRef:(id)ref;
31+
- (nonnull instancetype)initWithRef:(_Nonnull id)ref;
3232
- (nonnull instancetype)init;
3333
@end
3434

@@ -37,9 +37,9 @@
3737
*/
3838
@interface DocS : NSObject <goSeqRefInterface> {
3939
}
40-
@property(strong, readonly) id _ref;
40+
@property(strong, readonly) _Nonnull id _ref;
4141

42-
- (nonnull instancetype)initWithRef:(id)ref;
42+
- (nonnull instancetype)initWithRef:(_Nonnull id)ref;
4343
/**
4444
* NewS is a constructor.
4545
*/
@@ -72,9 +72,9 @@
7272
*/
7373
@interface DocS2 : NSObject <goSeqRefInterface> {
7474
}
75-
@property(strong, readonly) id _ref;
75+
@property(strong, readonly) _Nonnull id _ref;
7676

77-
- (nonnull instancetype)initWithRef:(id)ref;
77+
- (nonnull instancetype)initWithRef:(_Nonnull id)ref;
7878
- (nonnull instancetype)init;
7979
@end
8080

@@ -121,9 +121,9 @@ FOUNDATION_EXPORT DocS* _Nullable DocNewS(void);
121121
*/
122122
@interface DocI : NSObject <goSeqRefInterface, DocI> {
123123
}
124-
@property(strong, readonly) id _ref;
124+
@property(strong, readonly) _Nonnull id _ref;
125125

126-
- (instancetype)initWithRef:(id)ref;
126+
- (nonnull instancetype)initWithRef:(_Nonnull id)ref;
127127
/**
128128
* IM is a method.
129129
*/

bind/testdata/doc.objc.m.golden

+6-6
Original file line numberDiff line numberDiff line change
@@ -12,13 +12,13 @@
1212
@implementation DocNoDoc {
1313
}
1414

15-
- (instancetype)initWithRef:(id)ref {
15+
- (nonnull instancetype)initWithRef:(_Nonnull id)ref {
1616
self = [super init];
1717
if (self) { __ref = ref; }
1818
return self;
1919
}
2020

21-
- (instancetype)init {
21+
- (nonnull instancetype)init {
2222
self = [super init];
2323
if (self) {
2424
__ref = go_seq_from_refnum(new_doc_NoDoc());
@@ -33,7 +33,7 @@
3333
@implementation DocS {
3434
}
3535

36-
- (instancetype)initWithRef:(id)ref {
36+
- (nonnull instancetype)initWithRef:(_Nonnull id)ref {
3737
self = [super init];
3838
if (self) { __ref = ref; }
3939
return self;
@@ -129,13 +129,13 @@
129129
@implementation DocS2 {
130130
}
131131

132-
- (instancetype)initWithRef:(id)ref {
132+
- (nonnull instancetype)initWithRef:(_Nonnull id)ref {
133133
self = [super init];
134134
if (self) { __ref = ref; }
135135
return self;
136136
}
137137

138-
- (instancetype)init {
138+
- (nonnull instancetype)init {
139139
self = [super init];
140140
if (self) {
141141
__ref = go_seq_from_refnum(new_doc_S2());
@@ -149,7 +149,7 @@
149149
@implementation DocI {
150150
}
151151

152-
- (instancetype)initWithRef:(id)ref {
152+
- (nonnull instancetype)initWithRef:(id)ref {
153153
self = [super init];
154154
if (self) { __ref = ref; }
155155
return self;

bind/testdata/ignore.objc.h.golden

+4-4
Original file line numberDiff line numberDiff line change
@@ -24,9 +24,9 @@
2424

2525
@interface IgnoreS : NSObject <goSeqRefInterface, IgnoreI> {
2626
}
27-
@property(strong, readonly) id _ref;
27+
@property(strong, readonly) _Nonnull id _ref;
2828

29-
- (nonnull instancetype)initWithRef:(id)ref;
29+
- (nonnull instancetype)initWithRef:(_Nonnull id)ref;
3030
- (nonnull instancetype)init;
3131
// skipped field S.F with unsupported type: interface{}
3232

@@ -72,9 +72,9 @@
7272

7373
@interface IgnoreI : NSObject <goSeqRefInterface, IgnoreI> {
7474
}
75-
@property(strong, readonly) id _ref;
75+
@property(strong, readonly) _Nonnull id _ref;
7676

77-
- (instancetype)initWithRef:(id)ref;
77+
- (nonnull instancetype)initWithRef:(_Nonnull id)ref;
7878
// skipped method I.Argument with unsupported parameter or return types
7979

8080
// skipped method I.Result with unsupported parameter or return types

bind/testdata/ignore.objc.m.golden

+3-3
Original file line numberDiff line numberDiff line change
@@ -12,13 +12,13 @@
1212
@implementation IgnoreS {
1313
}
1414

15-
- (instancetype)initWithRef:(id)ref {
15+
- (nonnull instancetype)initWithRef:(_Nonnull id)ref {
1616
self = [super init];
1717
if (self) { __ref = ref; }
1818
return self;
1919
}
2020

21-
- (instancetype)init {
21+
- (nonnull instancetype)init {
2222
self = [super init];
2323
if (self) {
2424
__ref = go_seq_from_refnum(new_ignore_S());
@@ -38,7 +38,7 @@
3838
@implementation IgnoreI {
3939
}
4040

41-
- (instancetype)initWithRef:(id)ref {
41+
- (nonnull instancetype)initWithRef:(id)ref {
4242
self = [super init];
4343
if (self) { __ref = ref; }
4444
return self;

bind/testdata/interfaces.objc.h.golden

+21-21
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@
2929
@class InterfacesWithParam;
3030

3131
@protocol InterfacesError <NSObject>
32-
- (BOOL)err:(NSError* _Nullable*)error;
32+
- (BOOL)err:(NSError* _Nullable* _Nullable)error;
3333
@end
3434

3535
@protocol InterfacesI <NSObject>
@@ -41,9 +41,9 @@
4141
*/
4242
@interface InterfacesI1 : NSObject <goSeqRefInterface> {
4343
}
44-
@property(strong, readonly) id _ref;
44+
@property(strong, readonly) _Nonnull id _ref;
4545

46-
- (instancetype)initWithRef:(id)ref;
46+
- (nonnull instancetype)initWithRef:(_Nonnull id)ref;
4747
- (void)j;
4848
@end
4949

@@ -52,9 +52,9 @@
5252
*/
5353
@interface InterfacesI2 : NSObject <goSeqRefInterface> {
5454
}
55-
@property(strong, readonly) id _ref;
55+
@property(strong, readonly) _Nonnull id _ref;
5656

57-
- (instancetype)initWithRef:(id)ref;
57+
- (nonnull instancetype)initWithRef:(_Nonnull id)ref;
5858
- (void)g;
5959
@end
6060

@@ -81,7 +81,7 @@
8181

8282
FOUNDATION_EXPORT int32_t InterfacesAdd3(id<InterfacesI> _Nullable r);
8383

84-
FOUNDATION_EXPORT BOOL InterfacesCallErr(id<InterfacesError> _Nullable e, NSError* _Nullable* error);
84+
FOUNDATION_EXPORT BOOL InterfacesCallErr(id<InterfacesError> _Nullable e, NSError* _Nullable* _Nullable error);
8585

8686
FOUNDATION_EXPORT id<InterfacesI> _Nullable InterfacesSeven(void);
8787

@@ -101,17 +101,17 @@ FOUNDATION_EXPORT id<InterfacesI> _Nullable InterfacesSeven(void);
101101

102102
@interface InterfacesError : NSObject <goSeqRefInterface, InterfacesError> {
103103
}
104-
@property(strong, readonly) id _ref;
104+
@property(strong, readonly) _Nonnull id _ref;
105105

106-
- (instancetype)initWithRef:(id)ref;
107-
- (BOOL)err:(NSError* _Nullable*)error;
106+
- (nonnull instancetype)initWithRef:(_Nonnull id)ref;
107+
- (BOOL)err:(NSError* _Nullable* _Nullable)error;
108108
@end
109109

110110
@interface InterfacesI : NSObject <goSeqRefInterface, InterfacesI> {
111111
}
112-
@property(strong, readonly) id _ref;
112+
@property(strong, readonly) _Nonnull id _ref;
113113

114-
- (instancetype)initWithRef:(id)ref;
114+
- (nonnull instancetype)initWithRef:(_Nonnull id)ref;
115115
- (int32_t)rand;
116116
@end
117117

@@ -121,9 +121,9 @@ FOUNDATION_EXPORT id<InterfacesI> _Nullable InterfacesSeven(void);
121121
*/
122122
@interface InterfacesI3 : NSObject <goSeqRefInterface, InterfacesI3> {
123123
}
124-
@property(strong, readonly) id _ref;
124+
@property(strong, readonly) _Nonnull id _ref;
125125

126-
- (instancetype)initWithRef:(id)ref;
126+
- (nonnull instancetype)initWithRef:(_Nonnull id)ref;
127127
- (InterfacesI1* _Nullable)f;
128128
@end
129129

@@ -132,34 +132,34 @@ FOUNDATION_EXPORT id<InterfacesI> _Nullable InterfacesSeven(void);
132132
*/
133133
@interface InterfacesInterfaces : NSObject <goSeqRefInterface, InterfacesInterfaces> {
134134
}
135-
@property(strong, readonly) id _ref;
135+
@property(strong, readonly) _Nonnull id _ref;
136136

137-
- (instancetype)initWithRef:(id)ref;
137+
- (nonnull instancetype)initWithRef:(_Nonnull id)ref;
138138
- (void)someMethod;
139139
@end
140140

141141
@interface InterfacesLargerI : NSObject <goSeqRefInterface, InterfacesLargerI> {
142142
}
143-
@property(strong, readonly) id _ref;
143+
@property(strong, readonly) _Nonnull id _ref;
144144

145-
- (instancetype)initWithRef:(id)ref;
145+
- (nonnull instancetype)initWithRef:(_Nonnull id)ref;
146146
- (void)anotherFunc;
147147
- (int32_t)rand;
148148
@end
149149

150150
@interface InterfacesSameI : NSObject <goSeqRefInterface, InterfacesSameI> {
151151
}
152-
@property(strong, readonly) id _ref;
152+
@property(strong, readonly) _Nonnull id _ref;
153153

154-
- (instancetype)initWithRef:(id)ref;
154+
- (nonnull instancetype)initWithRef:(_Nonnull id)ref;
155155
- (int32_t)rand;
156156
@end
157157

158158
@interface InterfacesWithParam : NSObject <goSeqRefInterface, InterfacesWithParam> {
159159
}
160-
@property(strong, readonly) id _ref;
160+
@property(strong, readonly) _Nonnull id _ref;
161161

162-
- (instancetype)initWithRef:(id)ref;
162+
- (nonnull instancetype)initWithRef:(_Nonnull id)ref;
163163
- (void)hasParam:(BOOL)p0;
164164
@end
165165

0 commit comments

Comments
 (0)