Skip to content

Add GetDeclaringType to PropertyDefinition and EventDefinition. #111646

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

Merged
merged 9 commits into from
Feb 10, 2025

Conversation

teo-tsirpanis
Copy link
Contributor

@teo-tsirpanis teo-tsirpanis commented Jan 21, 2025

Fixes #60380. Implementation follows the corresponding methods for method and field definitions, except that the binary search is performed on rows of the PropertyMap and EventMap tables for properties and fields respectively, and we don't account for rows with no properties/events.

@ghost
Copy link

ghost commented Jan 21, 2025

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.

@ghost
Copy link

ghost commented Jan 21, 2025

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.

@dotnet-policy-service dotnet-policy-service bot added the community-contribution Indicates that the PR has been added by a community member label Jan 21, 2025
@teo-tsirpanis teo-tsirpanis requested a review from Copilot January 21, 2025 00:53
Copy link
Contributor

@Copilot Copilot AI left a comment

Choose a reason for hiding this comment

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

Copilot reviewed 5 out of 5 changed files in this pull request and generated no comments.

Comments suppressed due to low confidence (4)

src/libraries/System.Reflection.Metadata/src/System/Reflection/Metadata/TypeSystem/PropertyDefinition.cs:72

  • The new method GetDeclaringType should be covered by tests to ensure its correctness.
public TypeDefinitionHandle GetDeclaringType()

src/libraries/System.Reflection.Metadata/src/System/Reflection/Metadata/MetadataReader.cs:1370

  • Ensure that the new method GetDeclaringType for PropertyDefinitionHandle is covered by tests.
return PropertyMapTable.FindTypeContainingProperty(propertyDef.RowId, PropertyTable.NumberOfRows);

src/libraries/System.Reflection.Metadata/src/System/Reflection/Metadata/MetadataReader.cs:1360

  • Ensure that the new method GetDeclaringType for EventDefinitionHandle is covered by tests.
return EventMapTable.FindTypeContainingEvent(eventDef.RowId, EventTable.NumberOfRows);

src/libraries/System.Reflection.Metadata/src/System/Reflection/Metadata/TypeSystem/EventDefinition.cs:60

  • The new method GetDeclaringType should be covered by tests to ensure it works as expected.
public TypeDefinitionHandle GetDeclaringType()

@teo-tsirpanis teo-tsirpanis marked this pull request as ready for review January 25, 2025 21:06
We have to return the value of the parent type column in the map table, not the row to the map table itself.
@teo-tsirpanis
Copy link
Contributor Author

@dotnet/area-system-reflection-metadata this is ready for review.

foreach (var typeDefHandle in reader.TypeDefinitions)
{
var typeDef = reader.GetTypeDefinition(typeDefHandle);
foreach (var nestedTypeHandle in typeDef.GetNestedTypes())
Copy link
Contributor

Choose a reason for hiding this comment

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

Can we add a sanity check here to make sure that these foreach statement actually loop through at least one? Like:

Assert.True(typeDef.GetNestedTypes().Length > 0);

Copy link
Contributor Author

Choose a reason for hiding this comment

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

The test assembly actually dosen't have any nested types. I tried to add some but after recompiling it this assert failed.

Your suggested assert wouldn't currently work, because not all types have all kinds of members each.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Added asserts that GetTypesWithEvents() and GetTypesWithProperties() are not empty, and commented out the asserts on the nested classes.

@steveharter steveharter added the needs-author-action An issue or pull request that requires more info or actions from the author. label Feb 5, 2025
We assert that the test assembly has types with events and properties, and acknowledge that it does not have nested types.
foreach (var typeDefHandle in reader.TypeDefinitions)
{
var typeDef = reader.GetTypeDefinition(typeDefHandle);
// The assembly does not declare any nested types yet.
Copy link
Member

@jkotas jkotas Feb 9, 2025

Choose a reason for hiding this comment

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

Would it be better to add some nested types to this assembly instead?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Added a test.

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.

LGTM

@jkotas
Copy link
Member

jkotas commented Feb 9, 2025

@MichalStrehovsky Do you know on top your head where there any places in the AOT compilers that can use these APIs? If yes, we should create an issue to adopt these APIs once they make it into the tools runtime refence assemblies.

@jkotas
Copy link
Member

jkotas commented Feb 10, 2025

/ba-g unrelated timeout

@jkotas jkotas merged commit 1c3b1b5 into dotnet:main Feb 10, 2025
82 of 85 checks passed
@jkotas
Copy link
Member

jkotas commented Feb 10, 2025

Thank you!

@teo-tsirpanis teo-tsirpanis deleted the srm-declaring-type branch February 10, 2025 02:17
@MichalStrehovsky
Copy link
Member

@MichalStrehovsky Do you know on top your head where there any places in the AOT compilers that can use these APIs? If yes, we should create an issue to adopt these APIs once they make it into the tools runtime refence assemblies.

Nothing comes to mind. We ran into lack of this API in the ILTrim hackathon.

grendello added a commit to grendello/runtime that referenced this pull request Feb 10, 2025
* main: (41 commits)
  Automated bump of chrome version (dotnet#112309)
  Add `GetDeclaringType` to `PropertyDefinition` and `EventDefinition`. (dotnet#111646)
  Update the System.ComponentModel.Annotations solution to build in VS (dotnet#112313)
  JIT: initial support for stack allocating arrays of GC type (dotnet#112250)
  [main] Update dependencies from dotnet/roslyn (dotnet#112260)
  Update Xcode casing (dotnet#112307)
  update the location of assert for REG_ZR check (dotnet#112294)
  Enable `SA1206`: Keyword ordering (dotnet#112303)
  Address feedback on dense FrozenDictionary optimization (dotnet#112298)
  Start regular pri-1 tests runs with native AOT (dotnet#111391)
  Observe exceptions from _connectionCloseTcs (dotnet#112190)
  Test failure - SendAsync_RequestVersion20_ResponseVersion20 (dotnet#112232)
  Fix init race in mono_class_try_get_[shortname]_class. (dotnet#112282)
  Remove repeated call to DllMain (dotnet#112285)
  Replace bitvector.h/cpp with ptrArgTP type in gc_unwind_x86.h/inl (dotnet#112268)
  JIT: Limit 3-opt to 1000 swaps per run (dotnet#112259)
  [main] Update dependencies from dotnet/icu, dotnet/runtime-assets (dotnet#112120)
  Update dependencies from https://github.com/dotnet/emsdk build 20250205.3 (dotnet#112223)
  Fix EventPipe on Android CoreClr. (dotnet#112270)
  Fix exception handling in the prestub worker (dotnet#111937)
  ...
@teo-tsirpanis teo-tsirpanis added this to the 10.0.0 milestone Feb 11, 2025
@github-actions github-actions bot locked and limited conversation to collaborators Mar 13, 2025
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

[API Proposal]: Add GetDeclaringType to PropertyDefinition and EventDefinition
4 participants