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

Creating new TypeName without reparsing + usage in NRBF #103713

Merged
merged 21 commits into from
Aug 14, 2024

Conversation

adamsitnik
Copy link
Member

@adamsitnik adamsitnik commented Jun 19, 2024

@jkotas I've tried to implement the APIs we have discussed in #102263 and so far it looks good.

I need to spend more time testing MakeGenericTypeName and thinking about all edge cases.

contributes to #102263
fixes #106022

@adamsitnik adamsitnik added area-System.Reflection.Metadata binaryformatter-migration Issues related to the removal of BinaryFormatter and migrations away from it labels Jun 19, 2024
@adamsitnik adamsitnik self-assigned this Jun 19, 2024
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, 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.

1 similar comment
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, 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.

@adamsitnik
Copy link
Member Author

I need to spend more time testing MakeGenericTypeName and thinking about all edge cases.

The MakeGenericType name from reflection works only for generic type definitions:

Should MakeGenericTypeName have similar limitation? Otherwise, we are going to allow for creating generic type names where typeName.IsConstructedGenericType is pointer, array etc. Such name would be illegal for runtimes we have today.

@jkotas
Copy link
Member

jkotas commented Jun 27, 2024

Should MakeGenericTypeName have similar limitation?

Sounds good to me. I assume that you are thinking about requiring IsSimpleName to be true. Correct?

@adamsitnik
Copy link
Member Author

Sounds good to me. I assume that you are thinking about requiring IsSimpleName to be true. Correct?

IsSimple or IsConstructedGenericType (so users could change generic args of given generic type)

@jkotas
Copy link
Member

jkotas commented Jun 27, 2024

IsConstructedGenericType (so users could change generic args of given generic type)

I do not think that regular reflection APIs allow this. Why would we want to allow it? If users want to change generic args of constructed generic type, they should get the generic type definition first on their side.

# Conflicts:
#	src/libraries/System.Formats.Nrbf/src/System/Formats/Nrbf/SerializationRecord.cs
@adamsitnik adamsitnik reopened this Aug 2, 2024
…SimpleNames:

- all TypeName instances remain always valid and escaped
- it's impossible to detect all sequences that are escaped
@adamsitnik adamsitnik requested a review from JeremyKuhne August 12, 2024 19:07
/// <param name="assemblyName">Assembly name.</param>
/// <returns>Created simple name.</returns>
/// <exception cref="InvalidOperationException">The current type name is not simple.</exception>
public TypeName MakeSimpleTypeName(AssemblyNameInfo? assemblyName)
Copy link
Member

Choose a reason for hiding this comment

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

If you believe that this name is wrong

Yes, I think that the method name is not right. The conceptual behavior of this method does not match any other Make... instance method in the BCL. The established BCL pattern for methods that change properties of immutable types is With... instance or extension method.

We consider BCL prior art when naming new APIs to maintain consistency. (MakeSimpleTypeName name would be ok if there was no BCL prior art.)

it does not suggest anything that it's going to work only for simple names.

I do not think that it is a problem. It is common for APIs to have additional limitations for how they can be used that are not captured in the API name. For example, WithDegreeOfParallelism can be used at most once per query, but this limitation is not captured in the API name.

The BF scenario is important to me and I am not convinced that I don't need it.

Do you agree that it is just a minor optimization, and that this exact API is not required to make the Nrbf-reader secure?

For Nrbf-reader security, you should only need to avoid non-linear amount of work that can be achieved via more general-purpose set of APIs.

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.

System.Reflection.Metadata part LGTM modulo comments. Thank you!

Copy link
Member

@bartonjs bartonjs left a comment

Choose a reason for hiding this comment

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

Style violations must be corrected.

adamsitnik and others added 3 commits August 14, 2024 09:39
Co-authored-by: Jeremy Barton <jbarton@microsoft.com>
Co-authored-by: Jan Kotas <jkotas@microsoft.com>
…n the future and should not be enforced by the parser
@adamsitnik adamsitnik dismissed bartonjs’s stale review August 14, 2024 07:47

The suggestion has been applied and the feedback was addressed.

@adamsitnik adamsitnik merged commit cb42ed8 into dotnet:main Aug 14, 2024
81 of 84 checks passed
@github-actions github-actions bot locked and limited conversation to collaborators Sep 14, 2024
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
area-System.Reflection.Metadata binaryformatter-migration Issues related to the removal of BinaryFormatter and migrations away from it new-api-needs-documentation
Projects
None yet
Development

Successfully merging this pull request may close these issues.

[API Proposal]: TypeName.MakeSimpleTypeName(AssemblyNameInfo) to avoid type name reparsing
4 participants