Skip to content

Commit eb52a5c

Browse files
authored
[generator] Unsigned primitive types should not be nullable. (#960)
Fixes: #955 Context: dotnet/android-libraries#488 Primitive types are not nullable in Java, thus we do not default them to nullable when using nullable reference types in `generator`. However, when we added Kotlin's unsigned primitive types we did not special case them to not-nullable like the existing primitive types. This causes compilation errors when trying to bind Kotlin stdlib with `$(Nullable)`=enable: // Metadata.xml XPath method reference: path="/api/package[@name='kotlin.random']/class[@name='URandomKt']/method[@name='nextUInt-qCasIEU' and count(parameter)=2 and parameter[1][@type='kotlin.random.Random'] and parameter[2][@type='uint']]" [Register ("nextUInt-qCasIEU", "(Lkotlin/random/Random;I)I", "")] public static unsafe uint? NextUInt (global::Kotlin.Random.Random obj, uint? until) { const string __id = "nextUInt-qCasIEU.(Lkotlin/random/Random;I)I"; try { JniArgumentValue* __args = stackalloc JniArgumentValue [2]; __args [0] = new JniArgumentValue ((obj == null) ? IntPtr.Zero : ((global::Java.Lang.Object) obj).Handle); __args [1] = new JniArgumentValue (until); // Error CS1503 Argument 1: cannot convert from 'uint?' to 'bool' … Error CS1503 is emitted when trying to invoke the non-existent `JniArgumentValue(uint?)` constructor. Add Kotlin unsigned types to `CodeGenerationOptions.GetNullable(string)` so that unsigned types are treated the same was as signed types. This cause `generator` to instead emit: // Metadata.xml XPath method reference: path="/api/package[@name='kotlin.random']/class[@name='URandomKt']/method[@name='nextUInt-qCasIEU' and count(parameter)=2 and parameter[1][@type='kotlin.random.Random'] and parameter[2][@type='uint']]" [Register ("nextUInt-qCasIEU", "(Lkotlin/random/Random;I)I", "")] public static unsafe uint NextUInt (global::Kotlin.Random.Random obj, uint until) { const string __id = "nextUInt-qCasIEU.(Lkotlin/random/Random;I)I"; try { JniArgumentValue* __args = stackalloc JniArgumentValue [2]; __args [0] = new JniArgumentValue ((obj == null) ? IntPtr.Zero : ((global::Java.Lang.Object) obj).Handle); __args [1] = new JniArgumentValue (until); … Note the use of `uint` and not `uint?`.
1 parent aae23c9 commit eb52a5c

File tree

2 files changed

+19
-0
lines changed

2 files changed

+19
-0
lines changed

tests/generator-Tests/Unit-Tests/CodeGenerationOptionsTests.cs

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,15 +57,27 @@ public void GetTypeReferenceName_Nullable ()
5757
{
5858
var opt = new CodeGenerationOptions { SupportNullableReferenceTypes = true };
5959

60+
// System.Void isn't a valid return type, ensure it gets changed to void
6061
var system_void = new ReturnValue (null, "void", "System.Void", false, false);
6162
system_void.Validate (opt, null, null);
6263

6364
Assert.AreEqual ("void", opt.GetTypeReferenceName (system_void));
6465

66+
// Primitive types should not be nullable
6567
var primitive_void = new ReturnValue (null, "void", "void", false, false);
6668
primitive_void.Validate (opt, null, null);
6769

6870
Assert.AreEqual ("void", opt.GetTypeReferenceName (primitive_void));
71+
72+
var system_uint = new ReturnValue (null, "uint", "System.UInt32", false, false);
73+
system_uint.Validate (opt, null, null);
74+
75+
Assert.AreEqual ("System.UInt32", opt.GetTypeReferenceName (system_uint));
76+
77+
var primitive_uint = new ReturnValue (null, "uint", "uint", false, false);
78+
primitive_uint.Validate (opt, null, null);
79+
80+
Assert.AreEqual ("uint", opt.GetTypeReferenceName (primitive_uint));
6981
}
7082
}
7183
}

tools/generator/CodeGenerationOptions.cs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -157,6 +157,10 @@ string GetNullable (string s)
157157
case "char":
158158
case "double":
159159
case "short":
160+
case "uint":
161+
case "ushort":
162+
case "ulong":
163+
case "byte":
160164
case "System.Boolean":
161165
case "System.Byte":
162166
case "System.Char":
@@ -166,6 +170,9 @@ string GetNullable (string s)
166170
case "System.Int64":
167171
case "System.Single":
168172
case "System.SByte":
173+
case "System.UInt16":
174+
case "System.UInt32":
175+
case "System.UInt64":
169176
case "System.Void":
170177
case "Android.Graphics.Color":
171178
return string.Empty;

0 commit comments

Comments
 (0)