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

Fixed creating generic type with abstract type when it is has a default constractor constrant #101963

Merged
merged 3 commits into from
Aug 16, 2024

Conversation

Faolan-Rad
Copy link
Contributor

@Faolan-Rad Faolan-Rad commented May 7, 2024

I think this fixes the bug that I was having

@dotnet-policy-service dotnet-policy-service bot added the community-contribution Indicates that the PR has been added by a community member label May 7, 2024
Copy link
Contributor

Tagging subscribers to this area: @mangod9
See info in area-owners.md if you want to be subscribed.

@Faolan-Rad Faolan-Rad changed the title Fixed bug with creating of invalid type Fixed creating generic type with abstract type when it is has a default constractor constrant May 7, 2024
@jkotas
Copy link
Member

jkotas commented May 7, 2024

  • This change would need tests
  • There are number of places where the constraint checks would need to be fixed (e.g.
    if (!instantiationParam.HasExplicitOrImplicitDefaultConstructor()
    && !CheckGenericSpecialConstraint(instantiationParam, GenericConstraints.DefaultConstructorConstraint))
    or
    if ((param_info->flags & GENERIC_PARAMETER_ATTRIBUTE_CONSTRUCTOR_CONSTRAINT) && !m_class_is_valuetype (paramClass) && !mono_class_has_default_constructor (paramClass, TRUE))
    return FALSE;
    )
  • We may need to file a breaking change notice for this change. I am not sure where it is worth fixing given that - the C# constraint checks are generally more restrictive compared to constraint checks done by the runtime.

@Faolan-Rad
Copy link
Contributor Author

I am trying to think of a use case where someone would want a none installable type when you are expecting one so I don't think it would be a braking change but I get what you mean someone could still be using it in that way I will still go through and do the checks for where you specified and check if there is more that I am missing

@Faolan-Rad
Copy link
Contributor Author

I don't know how you would make it do the check for runtime/src/coreclr/tools/Common/TypeSystem/Common/TypeSystemConstraintsHelpers.cs
From what I can tell TypeDesc has no way of knowing if it is an abstract class or not

@Faolan-Rad
Copy link
Contributor Author

@dotnet-policy-service agree

@MichalStrehovsky
Copy link
Member

I don't know how you would make it do the check for runtime/src/coreclr/tools/Common/TypeSystem/Common/TypeSystemConstraintsHelpers.cs From what I can tell TypeDesc has no way of knowing if it is an abstract class or not

That one already behaves according to the ECMA-335 spec. It goes to here:

public static bool HasExplicitOrImplicitDefaultConstructor(this TypeDesc type)
{
return type.IsValueType || type.GetDefaultConstructor() != null;
}

That goes to here:

public override MethodDesc GetDefaultConstructor()
{
if (IsAbstract)
return null;

@fanyang-mono
Copy link
Member

The change to mono runtime looks good to me.

@ivdiazsa
Copy link
Member

ivdiazsa commented Aug 2, 2024

@Faolan-Rad Is this work still planned to be completed?

@Faolan-Rad
Copy link
Contributor Author

I don't really know what else is needed does it need a unit test to be made for it?

@ivdiazsa
Copy link
Member

ivdiazsa commented Aug 2, 2024

I don't really know what else is needed does it need a unit test to be made for it?

Yes, a test would be great. Since we're changing behavior we would need to have it covered in our pipelines.

@Faolan-Rad
Copy link
Contributor Author

Its is up to date now and I believe I added the test correctly that was needed I put it in the reflection tests

@ivdiazsa
Copy link
Member

ivdiazsa commented Aug 5, 2024

Its is up to date now and I believe I added the test correctly that was needed I put it in the reflection tests

Thank you very much! I think that's the last thing we need for this fix. Can we get a review please? @lambdageek @thaystg @fanyang-mono @MichalStrehovsky

Copy link
Member

@jkotas jkotas left a comment

Choose a reason for hiding this comment

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

The new test does not build:

src/tests/reflection/DisallowAbstractConstructors/DisallowAbstractConstructors.cs(43,95): error CS1002: ; expected [/__w/1/s/src/tests/reflection/DisallowAbstractConstructors/DisallowAbstractConstructors.csproj]

@ivdiazsa
Copy link
Member

ivdiazsa commented Aug 5, 2024

The new test does not build:

src/tests/reflection/DisallowAbstractConstructors/DisallowAbstractConstructors.cs(43,95): error CS1002: ; expected [/__w/1/s/src/tests/reflection/DisallowAbstractConstructors/DisallowAbstractConstructors.csproj]

Seems like a typo of a missing ;. I'll just add it on top of the current changes as it's a very simple fix.

Copy link
Member

@MichalStrehovsky MichalStrehovsky left a comment

Choose a reason for hiding this comment

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

I'd hold off merging this until RC1 is snapped off due to the breaking change potential.

Copy link
Member

@fanyang-mono fanyang-mono left a comment

Choose a reason for hiding this comment

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

Mono related change looks good to me!

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
area-TypeSystem-coreclr community-contribution Indicates that the PR has been added by a community member
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants