Skip to content

Commit

Permalink
[generator] Avoid NRE when using native enums on delegates (#14396)
Browse files Browse the repository at this point in the history
Fixes NRE in GetNativeEnumToManagedExpression when a native enum
with MaxValue is used on delegates.
  • Loading branch information
dalexsoto authored Mar 14, 2022
1 parent 1bcc853 commit c055c10
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 1 deletion.
2 changes: 1 addition & 1 deletion src/generator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2155,7 +2155,7 @@ bool GetNativeEnumToManagedExpression (Type enumType, out string preExpression,
// in question actually has that value at least). Same goes for Int32.MinValue/Int64.MinValue.
// var isDefined = enumType.IsEnumDefined (maxValue);
var definedMaxField = enumType.GetFields ().Where (v => v.IsLiteral).FirstOrDefault (isMaxDefinedFunc);
if (definedMaxField != null) {
if (definedMaxField is not null && postproc is not null) {
postproc.AppendLine ("#if ARCH_32");
postproc.AppendFormat ("if (({0}) ret == ({0}) {1}.MaxValue)\n", underlyingTypeName, itype);
postproc.AppendFormat ("\tret = {0}.{1}; // = {2}.MaxValue\n", renderedEnumType, definedMaxField.Name, underlyingTypeName);
Expand Down
7 changes: 7 additions & 0 deletions tests/generator/tests/nativeenum.cs
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,8 @@ public enum MyEnum8 : ulong {
Value2 = ulong.MaxValue,
}

delegate void EnumHandler (MyEnum7 seven, MyEnum8 eight);

[BaseType (typeof (NSObject))]
interface MyClass {
[Export ("myProp1")]
Expand Down Expand Up @@ -90,5 +92,10 @@ interface MyClass {
MyEnum8 MyProp8 { get; set; }
[Export ("myMethod8:")]
MyEnum8 MyMethod8 (MyEnum8 arg);

[Export ("myDelegateProp")]
EnumHandler MyDelegateProp { get; set; }
[Export ("myDelegateMethod:")]
EnumHandler MyDelegateMethod (EnumHandler arg);
}
}

1 comment on commit c055c10

@vs-mobiletools-engineering-service2
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

❌ Tests failed on macOS Mac Catalina (10.15) ❌

Tests failed on Mac Catalina (10.15).

Failed tests are:

  • xammac_tests
  • monotouch-test

Pipeline on Agent
[generator] Avoid NRE when using native enums on delegates (#14396)

Please sign in to comment.