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

Make Config generator test only for down-casts #106145

Merged
merged 3 commits into from
Aug 9, 2024

Conversation

ericstj
Copy link
Member

@ericstj ericstj commented Aug 8, 2024

Fix #94547

We were hitting compiler errors when the generator emitted test casts for value types. Since those can never be true (value types cannot be derived from, and the compiler can see if the cast will succeed or not).

Fix this by only doing a test cast when we are trying to do a runtime downcast.

We were hitting compiler errors when the generator emitted test casts
for value types.  Since those can never be true (value types cannot be
derived from, and the compiler can see if the cast will succeed or not).

Fix this by only doing a test cast when we are trying to do a runtime
downcast.
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.

This looks good. Thanks for getting this fixed.

I just had minor suggestions.

Copy link
Member

@tarekgh tarekgh left a comment

Choose a reason for hiding this comment

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

Added a couple of questions. LGTM.

These are unsupported and ignored by the generator and a diagnostic is
emitted.

Binding data to them does nothing.
Copy link
Member

@eiriktsarpalis eiriktsarpalis left a comment

Choose a reason for hiding this comment

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

Looks good, although it seems like the flag is only kicking in when predetermined pairs of types are detected. Can the source generator produce casts for types other than these collection types, and if so couldn't the shouldTryCast value be determined in the general case? (i.e. by looking at the subtype relationship between the source and target types)

@ericstj
Copy link
Member Author

ericstj commented Aug 9, 2024

Looks good, although it seems like the flag is only kicking in when predetermined pairs of types are detected

It's everywhere that PopulationCastType is specified and used.

Can the source generator produce casts for types other than these collection types

I audited this file for uses of is in generated code and didn't see anything else that stood out as applying to the target type. Most checks are around the Configuration passed in at runtime. A couple cases of null but those were guarded with CanBeNull checks. It was a good suggestion to look for variants, and I couldn't find any.

@ericstj ericstj merged commit 4bd1492 into dotnet:main Aug 9, 2024
84 checks passed
@@ -36,12 +36,12 @@ namespace Microsoft.Extensions.Configuration.Binder.SourceGeneration
{
#region IConfiguration extensions.
/// <summary>Attempts to bind the configuration instance to a new instance of type T.</summary>
[InterceptsLocation(@"src-0.cs", 12, 17)]
[InterceptsLocation(@"src-0.cs", 13, 17)]
Copy link
Member

Choose a reason for hiding this comment

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

Good to see this change in the "version 0" baseline - did you encounter any test issues or did you just remember to do this?

@github-actions github-actions bot locked and limited conversation to collaborators Sep 9, 2024
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

ConfigurationBinder source generator generates code that doesn't compile when using StringValues
5 participants