Skip to content

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

Merged
ivdiazsa merged 3 commits into
dotnet:mainfrom
Faolan-Rad:main
Aug 16, 2024
Merged

Fixed creating generic type with abstract type when it is has a default constractor constrant#101963
ivdiazsa merged 3 commits into
dotnet:mainfrom
Faolan-Rad:main

Conversation

@Faolan-Rad

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

Copy link
Copy Markdown
Contributor

I think this fixes the bug that I was having

@ghost ghost added the area-VM-coreclr label May 7, 2024
@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
@dotnet-policy-service

Copy link
Copy Markdown
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

jkotas commented May 7, 2024

Copy link
Copy Markdown
Member
  • 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
Copy Markdown
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
Copy Markdown
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
Copy Markdown
Contributor Author

@dotnet-policy-service agree

@MichalStrehovsky

Copy link
Copy Markdown
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
Copy Markdown
Member

The change to mono runtime looks good to me.

@ivdiazsa

ivdiazsa commented Aug 2, 2024

Copy link
Copy Markdown
Contributor

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

@Faolan-Rad

Copy link
Copy Markdown
Contributor Author

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

@ivdiazsa

ivdiazsa commented Aug 2, 2024

Copy link
Copy Markdown
Contributor

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
Copy Markdown
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

ivdiazsa commented Aug 5, 2024

Copy link
Copy Markdown
Contributor

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

@jkotas jkotas left a comment

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.

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

ivdiazsa commented Aug 5, 2024

Copy link
Copy Markdown
Contributor

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.

@MichalStrehovsky MichalStrehovsky left a comment

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.

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

@fanyang-mono fanyang-mono left a comment

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.

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