-
Notifications
You must be signed in to change notification settings - Fork 4.9k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Repair inconsistent nullability nullability on TypeConverter (#63186) #63874
Conversation
Note regarding the This serves as a reminder for when your PR is modifying a ref *.cs file and adding/modifying public APIs, to please make sure the API implementation in the src *.cs file is documented with triple slash comments, so the PR reviewers can sign off that change. |
Tagging subscribers to this area: @dotnet/area-system-componentmodel Issue DetailsFixes #63186 Note: I've also found one change on src/ImageConverter.CanConvertFrom and ref/ImageFormatConverter.CanConvertFrom and placed them in a dedicated commit. I can create a separate ticket for this if you prefer.
|
1fc20be
to
3396b60
Compare
@maryamariyan I believe my PR is running into #63439 (DllImporter fails on MacOS for Arm64) but is otherwise I'm ready for review. I'll rebase on top of main once #63439 is done and check again that my changes didn't also break something but figured I'll let you know in case there's something else to be done before that. |
3396b60
to
f91820f
Compare
@@ -38,7 +38,7 @@ public virtual bool CanConvertFrom(ITypeDescriptorContext? context, Type sourceT | |||
/// Gets a value indicating whether this converter can convert an object to the given | |||
/// destination type using the context. | |||
/// </summary> | |||
public virtual bool CanConvertTo(ITypeDescriptorContext? context, Type? destinationType) | |||
public virtual bool CanConvertTo(ITypeDescriptorContext? context, Type destinationType) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I looked into this more, and I think the advice I gave on #63186 (comment) wasn't 100% correct. Now that I look at it again, I think this should be annotated as:
public virtual bool CanConvertTo(ITypeDescriptorContext? context, [NotNullWhen(true)] Type? destinationType)
And then to fix the inconsistency, the above CanConvertTo overload should be annotated the same:
public bool CanConvertTo([NotNullWhen(true)] Type? destinationType)
Note that ConvertTo
doesn't need to change. It doesn't allow for null
, and will throw if someone passes null
.
This way, people can write code like:
Type? someType = ...;
if (typeConverter.CanConvertTo(someType))
{
var converted = typeConverter.ConvertTo(someType);
}
And they won't get any nullability warnings.
@krwq - since you originally added nullability here - do you have any thoughts?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@eerhardt Thanks for the input. That does make sense.
Should I expand the original ticket to also cover the sourceType
parameter on CanConvertFrom?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Should I expand the original ticket to also cover the sourceType parameter on CanConvertFrom?
No. Let's open a new issue for CanConvertFrom. The scenario there is a bit different because presumably you've gotten the type from calling something.GetType()
. ("From" implies you have something already that you are going to convert from.) I'd rather keep these separate, so we can get the CanConvertTo changes in without being held up by any issues with CanConvertFrom.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@eerhardt Sure thing. Just let me know how you want to handle the ImageConverter-fix for CanConvertFrom.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@eerhardt and @krwq Do you already have an update on the final decision on how to proceed with CanConvertTo (see #63874 (comment) , syntax update with NotNullWhen
instead of not null)?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@MichaelKetting - I think we can go forward with my above suggestion. Any plans on moving this forward?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@eerhardt Great, yes, will be updating the PR in the next couple of days / the weekend to go with the NotNullWhen solution.
Since the original null from way back changes did not update the docs, should I take the opportunity and do something there as well by adding parameter documentation and null guides?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
You can, but those docs are contained in a separate repo. Updating the xml doc above doesn't make it into the official docs.
Here are the official docs:
https://github.com/dotnet/dotnet-api-docs/blob/d90566928a7c5f6070f942a89d9be588647a7105/xml/System.ComponentModel/TypeConverter.xml#L349-L407
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@eerhardt Ah, okay, there they are :) This would then be a separate ticket and PR, correct, since they're separate repos? Which means I'll do the code first and once that's done and approved, I'll check out the docs.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes, that would be a separate PR
f91820f
to
621735f
Compare
src/libraries/System.ComponentModel.TypeConverter/ref/System.ComponentModel.TypeConverter.cs
Outdated
Show resolved
Hide resolved
9186edc
to
ae5b312
Compare
@eerhardt I'll update the PR from Draft to Ready-for-Review in the evening. Want to update the docs in the PR first. Until then: There's some crashes / failures in helix but I can't pinpoint (yet) if that's my changes or a general instability. |
The nullability of parameter 'destinationType' in TypeConverter.CanConvertTo(...) was changed from not-nullable to nullable during the development of .NET 6. Since a destination type supported by this TypeConverter can never be null, a NotNullWhenAttribute is added to the 'destinationType' parameter when the result value of TypeConverter.CanConvertTo(...) is 'true'. Fix dotnet#63186
ae5b312
to
f1849d6
Compare
@eerhardt I've rebased on the latest main and should be done. The errors in the pipeline / helix seem to be unrelated to the changes in this PR and I've found the same issues also in at least one other PR (https://github.com/dotnet/runtime/pull/65944/checks?check_run_id=5365597384) |
Failures are #65890. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks for the contribution, @MichaelKetting!
Fixes #63186
new ticket to update the docs (see Repair inconsistent nullability nullability on TypeConverter (#63186) #63874 (comment))
new ticket to discuss if a change to
TypeConverter.CanConvertFrom(Type sourceType)
to nullable withNotNullWhen(true)
is also wanted. Note: I've also found one change on src/ImageConverter.CanConvertFrom and ref/ImageFormatConverter.CanConvertFrom and placed them in a dedicated commit. I can create a separate ticket for this if you prefer.