Skip to content

[corefoundation] Add nullability to (generated and manual) bindings - #15090

Merged
tj-devel709 merged 7 commits into
dotnet:mainfrom
tj-devel709:Nullable-CoreFoundation
Jun 6, 2022
Merged

tj-devel709 merged 7 commits into
dotnet:mainfrom
tj-devel709:Nullable-CoreFoundation

Conversation

@tj-devel709

Copy link
Copy Markdown
Member

This PR aims to bring nullability changes to CoreFoundation.
Following the steps here:

  1. I am adding nullable enable to all manual files that are not "API_SOURCES" in src/frameworks.sources and making the required nullability changes
  2. Changing all throw new ArgumentNullException ("object")); to ObjCRuntime.ThrowHelper.ThrowArgumentNullException (nameof (object)); for size saving optimization as well to mark that this framework contains nullability changes
  3. Changing any == null or != null to is null and is not null

@tj-devel709 tj-devel709 added the not-notes-worthy Ignore for release notes label May 20, 2022
@tj-devel709 tj-devel709 added this to the Future milestone May 20, 2022
@tj-devel709
tj-devel709 requested a review from rolfbjarne as a code owner May 20, 2022 22:38
public bool Transform (ref CFRange range, string transform, bool reverse)
{
var t = NSString.CreateNative (transform);
var t = CreateNative (transform);

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

Context, NSString.CreateNative (...) is obsolete and the recommendation was to use CFString.CreateNative (...)

public bool Transform (string transform, bool reverse)
{
var t = NSString.CreateNative (transform);
var t = CreateNative (transform);

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

Context, NSString.CreateNative (...) is obsolete and the recommendation was to use CFString.CreateNative (...)

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

I love the fact that you are giving context in your changes, appreciate it.

@vs-mobiletools-engineering-service2

This comment has been minimized.

@vs-mobiletools-engineering-service2

This comment has been minimized.

@vs-mobiletools-engineering-service2

This comment has been minimized.

@vs-mobiletools-engineering-service2

This comment has been minimized.

@vs-mobiletools-engineering-service2

This comment has been minimized.

Comment thread src/CoreFoundation/CFDataBuffer.cs Outdated

public IntPtr Handle {
get { return data.Handle; }
get { return data?.Handle ?? IntPtr.Zero; }

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Suggested change
get { return data?.Handle ?? IntPtr.Zero; }
get { return data.GetHandle (); }

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

I tried to use GetHandle () here but it was not supported.. I'll double check why!

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

Oop was just missing the namespace :)

if (name is null)
throw new ArgumentNullException (nameof (name), "When using the Darwin Notification Center, the value passed must not be null");
if (darwinnc is not null && darwinnc.Handle == Handle && name is null){
ObjCRuntime.ThrowHelper.ThrowArgumentNullException ($"{nameof (name)}: When using the Darwin Notification Center, the value passed must not be null");

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

I think you have to add another ThrowArgumentNullException overload that takes the message in addition to the parameter name.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

@rolfbjarne There is not one in the ObjCRuntime.ThrowHelper class, but I could easily create one!

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Yep, adding a new method is the way to go :)

Comment thread src/CoreFoundation/CFPreferences.cs Outdated
}

throw new ArgumentException ("unsupported type: " + value.GetType (), "value");
if (value is not null)

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

This looks wrong, I think there's a missing return; statement after line 127, where it already handles the value is null case. Looks like the nullability attributes caught a bug here :)

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

I believe you are correct.

Comment thread src/CoreFoundation/Dispatch.cs Outdated
Comment on lines +676 to +677
if (left as object is null)
return right as object is null;

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

You don't need the as object part:

Suggested change
if (left as object is null)
return right as object is null;
if (left is null)
return right is null;

Comment thread src/CoreFoundation/Dispatch.cs Outdated
Comment on lines +683 to +684
if (left as object is null)
return right as object is not null;

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Suggested change
if (left as object is null)
return right as object is not null;
if (left is null)
return right is not null;

@mandel-macaque mandel-macaque left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

After all rolfs comments have been addressed.

public bool Transform (string transform, bool reverse)
{
var t = NSString.CreateNative (transform);
var t = CreateNative (transform);

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

I love the fact that you are giving context in your changes, appreciate it.

Comment thread src/CoreFoundation/CFPreferences.cs Outdated
}

throw new ArgumentException ("unsupported type: " + value.GetType (), "value");
if (value is not null)

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

I believe you are correct.

Comment thread src/CoreFoundation/CFProxySupport.cs Outdated
Comment on lines +904 to +907
string? username = proxy?.Username;
string? password = proxy?.Password;
string? hostname = proxy?.HostName;
int? port = proxy?.Port;

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

var here would have make you type less :)

@tj-devel709

Copy link
Copy Markdown
Member Author

@rolfbjarne Changes addressed!

@vs-mobiletools-engineering-service2

This comment has been minimized.

@vs-mobiletools-engineering-service2

This comment has been minimized.

Comment thread src/CoreFoundation/CFException.cs Outdated
public NSString Domain {get; private set;}
public string FailureReason {get; private set;}
public string RecoverySuggestion {get; private set;}
public nint? Code {get; private set;}

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

You can't make this nullable, because it's a struct and that would be a breaking change.

Comment thread src/CoreFoundation/CFException.cs Outdated
public class CFException : Exception {

public CFException (string description, NSString domain, nint code, string failureReason, string recoverySuggestion)
public CFException (string? description, NSString? domain, nint? code, string? failureReason, string? recoverySuggestion)

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

You can't make the code argument nullable, because it's a struct and it would be a breaking change.

@vs-mobiletools-engineering-service2

This comment has been minimized.

@vs-mobiletools-engineering-service2

This comment has been minimized.

@vs-mobiletools-engineering-service2

This comment has been minimized.

@vs-mobiletools-engineering-service2

This comment has been minimized.

@tj-devel709

Copy link
Copy Markdown
Member Author

@rolfbjarne Changes applied!

@vs-mobiletools-engineering-service2

Copy link
Copy Markdown
Collaborator

📚 [PR Build] Artifacts 📚

Packages generated

View packages

Pipeline on Agent XAMBOT-1036.Monterey
Hash: 24e648b914cc2adec8e05db055c2ce8653165be0

@vs-mobiletools-engineering-service2

Copy link
Copy Markdown
Collaborator

💻 [PR Build] Tests on macOS Mac Catalina (10.15) passed 💻

✅ All tests on macOS Mac Catalina (10.15) passed.

Pipeline on Agent
Hash: 24e648b914cc2adec8e05db055c2ce8653165be0

@vs-mobiletools-engineering-service2

Copy link
Copy Markdown
Collaborator

📋 [PR Build] API Diff 📋

API diff (for current PR)

ℹ️ API Diff (from PR only) (please review changes)

API diff: vsdrops gist

Xamarin
.NET
Xamarin vs .NET
iOS vs Mac Catalyst (.NET)

API diff (vs stable)

✅ API Diff from stable

API diff: vsdrops gist

Xamarin
.NET
Xamarin vs .NET
iOS vs Mac Catalyst (.NET)

Generator diff

✅ Generator Diff (no change)

Pipeline on Agent XAMBOT-1100.Monterey
Hash: 24e648b914cc2adec8e05db055c2ce8653165be0

@vs-mobiletools-engineering-service2

Copy link
Copy Markdown
Collaborator

❌ [CI Build] Tests failed on VSTS: simulator tests iOS ❌

Tests failed on VSTS: simulator tests iOS.

Test results

4 tests failed, 144 tests passed.

Failed tests

  • fsharp/watchOS 32-bits - simulator/Debug: Crashed
  • introspection/watchOS 32-bits - simulator/Debug: Crashed
  • dont link/watchOS 32-bits - simulator/Debug: Crashed
  • mono-native-compat/watchOS 32-bits - simulator/Debug: Crashed

Pipeline on Agent XAMBOT-1099.Monterey'
Merge 24e648b into 0307004

Comment on lines +904 to +907
var username = proxy?.Username;
var password = proxy?.Password;
var hostname = proxy?.HostName;
var port = proxy?.Port;

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

I don't love the usage of var here where the right hand size doesn't make it obvious if Port is a int or a string for example, but I think it's fine enough.

@tj-devel709

Copy link
Copy Markdown
Member Author

Unrelated Test Failures: https://github.com/xamarin/maccore/issues/2558

@tj-devel709
tj-devel709 merged commit 229dd2e into dotnet:main Jun 6, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

not-notes-worthy Ignore for release notes

Projects

None yet

Development

Successfully merging this pull request may close these issues.

5 participants