Skip to content
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

Merged
merged 1 commit into from
Mar 1, 2022

Conversation

MichaelKetting
Copy link
Contributor

@MichaelKetting MichaelKetting commented Jan 17, 2022

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 with NotNullWhen(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.

@dotnet-issue-labeler
Copy link

Note regarding the new-api-needs-documentation label:

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.

@ghost ghost added the community-contribution Indicates that the PR has been added by a community member label Jan 17, 2022
@ghost
Copy link

ghost commented Jan 17, 2022

Tagging subscribers to this area: @dotnet/area-system-componentmodel
See info in area-owners.md if you want to be subscribed.

Issue Details

Fixes #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.

Author: MichaelKetting
Assignees: -
Labels:

area-System.ComponentModel, new-api-needs-documentation

Milestone: -

@MichaelKetting MichaelKetting force-pushed the issue-63186 branch 5 times, most recently from 1fc20be to 3396b60 Compare January 17, 2022 20:13
@MichaelKetting
Copy link
Contributor Author

@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.

@MichaelKetting MichaelKetting force-pushed the issue-63186 branch 2 times, most recently from 3396b60 to f91820f Compare January 18, 2022 15:43
@@ -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)
Copy link
Member

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?

Copy link
Contributor Author

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?

Copy link
Member

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.

Copy link
Contributor Author

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.

Copy link
Contributor Author

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)?

Copy link
Member

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?

Copy link
Contributor Author

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?

Copy link
Member

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

Copy link
Contributor Author

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.

Copy link
Member

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

@eerhardt eerhardt added the needs-author-action An issue or pull request that requires more info or actions from the author. label Feb 8, 2022
@ghost ghost removed the needs-author-action An issue or pull request that requires more info or actions from the author. label Feb 8, 2022
@MichaelKetting MichaelKetting marked this pull request as draft February 22, 2022 07:21
@MichaelKetting MichaelKetting force-pushed the issue-63186 branch 4 times, most recently from 9186edc to ae5b312 Compare February 28, 2022 07:18
@MichaelKetting
Copy link
Contributor Author

@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
@MichaelKetting
Copy link
Contributor Author

@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)

@eerhardt
Copy link
Member

eerhardt commented Mar 1, 2022

Failures are #65890.

Copy link
Member

@eerhardt eerhardt left a 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!

@eerhardt eerhardt merged commit ac246b1 into dotnet:main Mar 1, 2022
@ghost ghost locked as resolved and limited conversation to collaborators Mar 31, 2022
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
area-System.ComponentModel community-contribution Indicates that the PR has been added by a community member new-api-needs-documentation
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Inconsistent nullability change in TypeConverter.CanConvertTo resulting in a breaking API change with .NET 6.0
3 participants