Skip to content

Add C# 8 nullability annotations to bindings #6154

@spouliot

Description

@spouliot

Xamarin.iOS|Mac bindings already support [NullAllowed]. This controls if null checks are generated inside (the body of) API bindings.

We often talked about doing more tooling but it did not yet materialize. Now the upcoming C# 8 supports nullability annotations so our attributes are even more useful - with even less work on our side (tooling wise).

Steps

[x] Add nullability support to xtrospection tests - so we know where our bindings are missing (or extra) [NullAllowed]. The results can be ignore until we're ready for the next step;
[ ] Fix bindings (taking care not to create a merge hell with other existing/upcoming work). This will also eliminate recurrent bugs when we're missing [NullAllowed] (since using null is not possible).
[x] Add C#8 nullability annotations to the generator

Example

This binding

[Export ("alertControllerWithTitle:message:preferredStyle:")]
UIAlertController Create ([NullAllowed] string title, [NullAllowed] string message, UIAlertControllerStyle preferredStyle);

currently generate

[Export ("alertControllerWithTitle:message:preferredStyle:")]
[BindingImpl (BindingImplOptions.GeneratedCode | BindingImplOptions.Optimizable)]
public static UIAlertController Create (string title, string message, UIAlertControllerStyle preferredStyle)
{
	global::UIKit.UIApplication.EnsureUIThread ();
	var nstitle = NSString.CreateNative (title);
	var nsmessage = NSString.CreateNative (message);
	...  
}

but would now generate

[Export ("alertControllerWithTitle:message:preferredStyle:")]
[BindingImpl (BindingImplOptions.GeneratedCode | BindingImplOptions.Optimizable)]
public static UIAlertController Create (string? title, string? message, UIAlertControllerStyle preferredStyle)
{
	global::UIKit.UIApplication.EnsureUIThread ();
	var nstitle = NSString.CreateNative (title);
	var nsmessage = NSString.CreateNative (message);
	...  
}

note the extra ? on the parameter types

Metadata

Metadata

Assignees

No one assigned

    Labels

    dotnet-pri3.NET 6: wishlist for stable releaseenhancementThe issue or pull request is an enhancementestimate-8wgeneratorIssues affecting the generatorhelp wantedThis is an issue or pull request where we request help from the community to fix or completeiOSIssues affecting iOSmacOSIssues affecting macOS

    Type

    No type

    Projects

    No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions