Skip to content
This repository was archived by the owner on Dec 12, 2024. It is now read-only.

Commit b2bafcd

Browse files
Merge pull request #828 from xamarin/bad-warnings
removed a bunch of nullable warnings
2 parents 5f79ca4 + 61ae5fc commit b2bafcd

File tree

4 files changed

+36
-20
lines changed

4 files changed

+36
-20
lines changed

Dynamo/Dynamo.CSLang/CSType.cs

Lines changed: 20 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -88,6 +88,7 @@ public string Name {
8888
public CSType [] GenericTypes { get; private set; }
8989
public bool IsArray { get; private set; }
9090
public bool IsPointer { get; private set; }
91+
public bool IsNullable { get; private set; }
9192

9293
string GenerateName ()
9394
{
@@ -156,14 +157,26 @@ static CSSimpleType
156157

157158
public CSSimpleType Star {
158159
get {
159-
if (Name.EndsWith ("[]")) {
160-
throw new NotImplementedException ("Blindly making an array a pointer doesn't do what you think.");
161-
} else {
162-
var ptrType = new CSSimpleType (Name + " *", false);
163-
ptrType.IsPointer = true;
164-
return ptrType;
165-
}
160+
var ptrType = TypeWithSuffix (" *");
161+
ptrType.IsPointer = true;
162+
return ptrType;
163+
}
164+
}
165+
166+
public CSSimpleType Nullable {
167+
get {
168+
var nullableType = TypeWithSuffix ("?");
169+
nullableType.IsNullable = true;
170+
return nullableType;
171+
}
172+
}
173+
174+
CSSimpleType TypeWithSuffix (string suffix)
175+
{
176+
if (IsArray || Name.EndsWith ("[]")) {
177+
throw new NotImplementedException ($"Blindly making an array a {suffix} doesn't do what you think.");
166178
}
179+
return new CSSimpleType (Name + suffix, false);
167180
}
168181

169182
public static CSSimpleType Bool { get { return tBool; } }

SwiftReflector/MarshalEngine.cs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -327,7 +327,7 @@ public IEnumerable<ICodeElement> MarshalFunctionCall (FunctionDeclaration wrappe
327327
returnIntPtr = new CSIdentifier (Uniqueify ("retvalIntPtr", identifiersUsed));
328328
identifiersUsed.Add (returnIntPtr.Name);
329329
if (!returnsIntPtrForReal)
330-
preMarshalCode.Add (CSVariableDeclaration.VarLine (returnType, returnIdent, CSConstant.Null));
330+
preMarshalCode.Add (CSVariableDeclaration.VarLine (returnType, returnIdent, new CSIdentifier ("null!")));
331331

332332
preMarshalCode.Add (CSVariableDeclaration.VarLine (CSSimpleType.IntPtr, returnIntPtr, new CSIdentifier ("IntPtr.Zero")));
333333
use.AddIfNotPresent (typeof (SwiftObjectRegistry));
@@ -1327,7 +1327,7 @@ CSBaseExpression MarshalProtocolConstrained (BaseDeclaration typeContext, Functi
13271327
var pNameIsSwiftable = new CSIdentifier (Uniqueify (p.Name.Name + "IsSwiftable", identifiersUsed));
13281328
identifiersUsed.Add (pNameIsSwiftable.Name);
13291329
preMarshalCode.Add (CSVariableDeclaration.VarLine (CSSimpleType.IntPtr, pNameIntPtr));
1330-
preMarshalCode.Add (CSVariableDeclaration.VarLine (new CSSimpleType ("ISwiftObject"), pNameAssocProxy, CSConstant.Null));
1330+
preMarshalCode.Add (CSVariableDeclaration.VarLine (new CSSimpleType ("ISwiftObject?"), pNameAssocProxy, CSConstant.Null));
13311331
preMarshalCode.Add (CSVariableDeclaration.VarLine (
13321332
CSSimpleType.Bool, pNameIsSwiftable, new CSFunctionCall (
13331333
"StructMarshal.Marshaler.IsSwiftRepresentable", false, p.CSType.Typeof ())));
@@ -1822,13 +1822,13 @@ CSBaseExpression MarshalObjCProtocol (CSParameter p, NamedTypeSpec proto)
18221822
if (proto.IsInOut || p.ParameterKind == CSParameterKind.Out || p.ParameterKind == CSParameterKind.Ref) {
18231823
// var localHandle = handleExpr
18241824
// call ( out localHandle )
1825-
// p = ObjCRuntime.Runtime.GetINativeObject<p.CSType>(localHandle, false)
1825+
// p = ObjCRuntime.Runtime.GetINativeObject<p.CSType>(localHandle, false)!
18261826
var localHandle = new CSIdentifier (Uniqueify (p.Name + "Handle", identifiersUsed));
18271827
identifiersUsed.Add (localHandle.Name);
18281828
preMarshalCode.Add (CSVariableDeclaration.VarLine (CSSimpleType.IntPtr, localHandle, handleExpr));
18291829
var accessCallName = $"ObjCRuntime.Runtime.GetINativeObject<{p.CSType.ToString ()}>";
18301830
var accessCall = new CSFunctionCall (accessCallName, false, localHandle, CSConstant.Val (false));
1831-
postMarshalCode.Add (CSAssignment.Assign (p.Name, accessCall));
1831+
postMarshalCode.Add (CSAssignment.Assign (p.Name, CSUnaryExpression.PostBang (accessCall)));
18321832
return ParmName (localHandle.Name, p.ParameterKind);
18331833
} else {
18341834
return handleExpr;

SwiftReflector/NewClassCompiler.cs

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3349,7 +3349,7 @@ char PropPrefix (PropertyType propType)
33493349
void ImplementProxyConstructorAndFields (CSClass cl, CSUsingPackages use, bool hasVtable, CSInterface iface, bool hasAssociatedTypes)
33503350
{
33513351
if (hasVtable || hasAssociatedTypes)
3352-
cl.Fields.Add (CSFieldDeclaration.FieldLine (iface.ToCSType (), kInterfaceImpl));
3352+
cl.Fields.Add (CSFieldDeclaration.FieldLine (iface.ToCSType (), kInterfaceImpl, new CSIdentifier ("null!")));
33533353
if (!hasAssociatedTypes) {
33543354
cl.Fields.Add (CSFieldDeclaration.FieldLine (new CSSimpleType (typeof (SwiftExistentialContainer1)), kContainer));
33553355
var prop = CSProperty.PublicGetBacking (new CSSimpleType (typeof (ISwiftExistentialContainer)), new CSIdentifier ("ProxyExistentialContainer"), kContainer, false, CSMethodKind.Override);
@@ -3862,9 +3862,9 @@ CSMethod MakePublicFactory (TypeDeclaration classDecl, string className, CSUsing
38623862
// public static object XamarinFactory(IntPtr p, Type[] genericTypes)
38633863
// {
38643864
// Type t = typeof(className<,,,>).MakeGenericType(genericTypes);
3865-
// ConstructorInfo ci = t.GetConstructor(BindingFlags.Instance | BindingFlags.NonPublic,
3865+
// ConstructorInfo? ci = t.GetConstructor(BindingFlags.Instance | BindingFlags.NonPublic,
38663866
// null, new Type[] { typeof(IntPtr), typeof(SwiftObjectRegistry), null);
3867-
// return ci.Invoke(new object[] { p, SwiftObjectRegistry.Registry }, null);
3867+
// return ci!.Invoke(new object[] { p, SwiftObjectRegistry.Registry }, null);
38683868
// }
38693869

38703870
var extraFactoryParam = classDecl.IsObjCOrInheritsObjC (TypeMapper) ? null :
@@ -3885,15 +3885,15 @@ CSMethod MakePublicFactory (TypeDeclaration classDecl, string className, CSUsing
38853885
new CSFunctionCall (String.Format ("typeof({0}).MakeGenericType", sb.ToString ()),
38863886
false, parms [1].Name));
38873887
use.AddIfNotPresent (typeof (ConstructorInfo));
3888-
var ciLine = CSVariableDeclaration.VarLine (new CSSimpleType (typeof (ConstructorInfo)), "ci",
3888+
var ciLine = CSVariableDeclaration.VarLine (new CSSimpleType (typeof (ConstructorInfo)).Nullable, "ci",
38893889
new CSFunctionCall ("t.GetConstructor", false,
38903890
new CSIdentifier ("BindingFlags.Instance") | new CSIdentifier ("BindingFlags.NonPublic"),
38913891
CSConstant.Null,
38923892
new CSArray1DInitialized (new CSSimpleType (typeof (Type)),
38933893
new CSSimpleType (typeof (IntPtr)).Typeof (),
38943894
new CSSimpleType (typeof (SwiftObjectRegistry)).Typeof ()),
38953895
CSConstant.Null));
3896-
var retLine = CSReturn.ReturnLine (new CSFunctionCall ("ci.Invoke", false,
3896+
var retLine = CSReturn.ReturnLine (new CSFunctionCall ("ci!.Invoke", false,
38973897
new CSArray1DInitialized (CSSimpleType.Object, parms [0].Name, extraFactoryParam)));
38983898
var meth = new CSMethod (CSVisibility.Public, CSMethodKind.Static, CSSimpleType.Object,
38993899
new CSIdentifier (SwiftObjectRegistry.kXamarinFactoryMethodName), new CSParameterList (parms), new CSCodeBlock ()
@@ -4170,7 +4170,7 @@ public static CSBaseExpression SafeMarshalClassFromIntPtr (CSBaseExpression expr
41704170
throw ErrorHelper.CreateError (ReflectorError.kCompilerReferenceBase + 66, $"Unable to find entity for type {fullClassName} while marshaling from swift.");
41714171
}
41724172
if (isObjCProtocol) {
4173-
return new CSFunctionCall ($"ObjCRuntime.Runtime.GetINativeObject<{expectedType.ToString ()}>", false, expr, CSConstant.Val (false));
4173+
return CSUnaryExpression.PostBang (new CSFunctionCall ($"ObjCRuntime.Runtime.GetINativeObject<{expectedType.ToString ()}>", false, expr, CSConstant.Val (false)));
41744174
} else {
41754175
var call = entity.Type.IsObjCOrInheritsObjC (typeMapper) ? "ObjCRuntime.Runtime.GetNSObject<{0}>" : "SwiftObjectRegistry.Registry.CSObjectForSwiftObject <{0}>";
41764176
return CSUnaryExpression.PostBang (new CSFunctionCall (String.Format (call, expectedType.ToString ()), false, expr));

SwiftRuntimeLibrary/BaseProxy.cs

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,17 +2,20 @@
22
// Licensed under the MIT License.
33

44
using System;
5+
6+
#nullable enable
7+
58
namespace SwiftRuntimeLibrary {
69
public class BaseProxy {
7-
public BaseProxy (Type interfaceType, EveryProtocol everyProtocol)
10+
public BaseProxy (Type interfaceType, EveryProtocol? everyProtocol)
811
{
912
InterfaceType = interfaceType;
1013
EveryProtocol = everyProtocol;
1114
}
1215
// be aware that EveryProtocol can be null in the case of the protocol coming from Swift
13-
public EveryProtocol EveryProtocol { get; private set; }
16+
public EveryProtocol? EveryProtocol { get; private set; }
1417
public Type InterfaceType { get; private set; }
1518

16-
public virtual ISwiftExistentialContainer ProxyExistentialContainer => null;
19+
public virtual ISwiftExistentialContainer ProxyExistentialContainer => null!;
1720
}
1821
}

0 commit comments

Comments
 (0)