Skip to content
This repository was archived by the owner on Jan 23, 2023. It is now read-only.

Nullable: NumberFormatInfo, CultureInfo#23459

Merged
krwq merged 7 commits intodotnet:NullableFeaturefrom
krwq:nullable-number-format-info
Mar 28, 2019
Merged

Nullable: NumberFormatInfo, CultureInfo#23459
krwq merged 7 commits intodotnet:NullableFeaturefrom
krwq:nullable-number-format-info

Conversation

@krwq
Copy link
Copy Markdown
Member

@krwq krwq commented Mar 26, 2019

No description provided.

@krwq krwq requested review from buyaa-n and safern March 26, 2019 17:51
Comment thread src/System.Private.CoreLib/shared/System/Globalization/CultureInfo.Unix.cs Outdated
Comment thread src/System.Private.CoreLib/shared/System/Globalization/CultureInfo.cs Outdated
{
Interlocked.CompareExchange(ref s_userDefaultUICulture, GetUserDefaultUICulture(), null);
return s_userDefaultUICulture;
return s_userDefaultUICulture!;
Copy link
Copy Markdown
Member

@stephentoub stephentoub Mar 26, 2019

Choose a reason for hiding this comment

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

This is an interesting case, @jaredpar. In general nullability analysis explicitly ignores threading, e.g. a property that's been validated to be non-null will suppress the compiler warning:

if (obj.SomeProp != null) obj.SomeProp.SomeMethod(); // no warning

even though the second call to SomeProp could return null (either due to some other thread changing things, or just more simply the property not being particularly well-behaved).

This feels like a similar case, but at the same time, if you're using CompareExchange, there be dragons. It seems like the net result here will be that lots of cases involving CompareExchange, in particular for lazy initialization, will also require using the null-forgiveness operator.

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 was a deliberate design trade off the nullable feature made. The explicit assumption was made that properties values will be non-null or null across various invocations. Had a healthy debate about this but eventually came down on this side.

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.

Right, I understand the decision about properties. My point (which I didn't make particularly well) is that this case is similar: the line right above it is initializing the field, and then we read from the field. Following the same logic as was used with properties, you could see an argument being made for the compiler treating it as initialized here as well, assuming GetUserDefaultUICulture is typed to return non-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.

Ah I see. How are you annotating CompareExachange first parameter here: nullable or nonnullableL

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.

How are you annotating CompareExachange first parameter here

How are you allowing generics to be annotated? :-)
https://source.dot.net/#System.Private.CoreLib/src/System/Threading/Interlocked.cs,105

Regardless, you're absolutely able to pass null for either/both the second or third arguments.

@@ -295,7 +296,7 @@ private static CultureInfo GetCultureByName(string name)
/// </summary>
public static CultureInfo CreateSpecificCulture(string name)
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.

We're not doing any argument validation on the input name. Seems like we should be. If you pass in null, this method will end up throwing NullReferenceException instead of ArgumentNullException.

@safern, @danmosemsft, can we keep a log somewhere of issues found from doing this annotation effort? Ideally, categorized. e.g. methods that don't expect null and fail with the wrong exception type if you pass null, methods that should be handling null but aren't correctly, etc.? I think the results of that will be helpful as we look back on this effort and figure out where to take it next.

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.

also - presumably - should we be adding checks as we annote it or leave it and assume in the future more people will opt-into this feature and it just won't compile for them? (no personal preference either way but since we chose compile errors I think it makes sense not to do any checks)

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.

@safern, @danmosemsft, can we keep a log somewhere of issues found from doing this annotation effort? Ideally, categorized. e.g. methods that don't expect null and fail with the wrong exception type if you pass null, methods that should be handling null but aren't correctly, etc.? I think the results of that will be helpful as we look back on this effort and figure out where to take it next.

Would an issue work fine?

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.

Copy link
Copy Markdown
Member Author

@krwq krwq Mar 26, 2019

Choose a reason for hiding this comment

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

let's just reference it without any notes on the PRs unfortunately we lose info about API this way - we need to edit the issue

Comment thread src/System.Private.CoreLib/shared/System/Globalization/CultureInfo.cs Outdated
}

public override bool Equals(object value)
public override bool Equals(object? value)
Copy link
Copy Markdown
Member

@stephentoub stephentoub Mar 26, 2019

Choose a reason for hiding this comment

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

GitHub won't let me comment on it, but I think a bunch of the virtuals above are wrong, in that there's nothing preventing a derived type from returning null, e.g. this compiles fine and prints out true:

using System;
using System.Globalization;

    class Program
    {
        static void Main() => Console.WriteLine(new MyCultureInfo().DisplayName == null);
    }

class MyCultureInfo : CultureInfo
{
    public MyCultureInfo() : base("en-US") { }
    public override string DisplayName => null;
}

@MadsTorgersen, @terrajobst, how should we be thinking about this? If the annotations are meant to reflect what's possible, then most of these should return nullable. If the annotations are meant to reflect what's intended, then most of these should return non-nullable, and derived types that violate that would need to be fixed.

cc: @danmosemsft

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've not fixed this yet until we decide on guidelines - I personally lean toward what makes sense vs what is possible - so i.e. would not expect CompareInfo and other similar to be null so IMO should be non-nullable but technically you could do what you described above.

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

I feel strongly that we should annotate intent. That's what the feature is for. It puts the onus on misbehaving implementations, not innocent users.

@safern safern force-pushed the NullableFeature branch 3 times, most recently from c01bfd0 to 652234c Compare March 28, 2019 05:35
@krwq krwq force-pushed the nullable-number-format-info branch from bab37bb to 45941dc Compare March 28, 2019 17:09
@krwq krwq force-pushed the nullable-number-format-info branch from 45941dc to c4beadf Compare March 28, 2019 22:15
Comment thread src/System.Private.CoreLib/shared/System/Globalization/NumberFormatInfo.cs Outdated
Comment thread src/System.Private.CoreLib/shared/System/Globalization/CultureInfo.cs Outdated
@krwq krwq merged commit f414bbd into dotnet:NullableFeature Mar 28, 2019
Dotnet-GitSync-Bot pushed a commit to Dotnet-GitSync-Bot/corefx that referenced this pull request Apr 5, 2019
* Nullable: NumberFormatInfo, CultureInfo

* fix build

* fix windows

* fix APPX issue

* another appx failure

* get rid of line split

* mark retVal as nullable

Signed-off-by: dotnet-bot <dotnet-bot@microsoft.com>
stephentoub pushed a commit to dotnet/corefx that referenced this pull request Apr 6, 2019
* Nullable: NumberFormatInfo, CultureInfo

* fix build

* fix windows

* fix APPX issue

* another appx failure

* get rid of line split

* mark retVal as nullable

Signed-off-by: dotnet-bot <dotnet-bot@microsoft.com>
Dotnet-GitSync-Bot pushed a commit to Dotnet-GitSync-Bot/corert that referenced this pull request Apr 9, 2019
* Nullable: NumberFormatInfo, CultureInfo

* fix build

* fix windows

* fix APPX issue

* another appx failure

* get rid of line split

* mark retVal as nullable

Signed-off-by: dotnet-bot <anirudhagnihotry098@gmail.com>
Dotnet-GitSync-Bot pushed a commit to Dotnet-GitSync-Bot/corert that referenced this pull request Apr 9, 2019
* Nullable: NumberFormatInfo, CultureInfo

* fix build

* fix windows

* fix APPX issue

* another appx failure

* get rid of line split

* mark retVal as nullable

Signed-off-by: dotnet-bot <anirudhagnihotry098@gmail.com>
Dotnet-GitSync-Bot pushed a commit to Dotnet-GitSync-Bot/mono that referenced this pull request Apr 9, 2019
* Nullable: NumberFormatInfo, CultureInfo

* fix build

* fix windows

* fix APPX issue

* another appx failure

* get rid of line split

* mark retVal as nullable

Signed-off-by: dotnet-bot <anirudhagnihotry098@gmail.com>
Anipik pushed a commit to Dotnet-GitSync-Bot/corert that referenced this pull request Apr 9, 2019
* Nullable: NumberFormatInfo, CultureInfo

* fix build

* fix windows

* fix APPX issue

* another appx failure

* get rid of line split

* mark retVal as nullable

Signed-off-by: dotnet-bot <dotnet-bot@microsoft.com>
jkotas pushed a commit to dotnet/corert that referenced this pull request Apr 10, 2019
* Nullable: NumberFormatInfo, CultureInfo

* fix build

* fix windows

* fix APPX issue

* another appx failure

* get rid of line split

* mark retVal as nullable

Signed-off-by: dotnet-bot <dotnet-bot@microsoft.com>
picenka21 pushed a commit to picenka21/runtime that referenced this pull request Feb 18, 2022
* Nullable: NumberFormatInfo, CultureInfo

* fix build

* fix windows

* fix APPX issue

* another appx failure

* get rid of line split

* mark retVal as nullable


Commit migrated from dotnet/coreclr@f414bbd
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

6 participants