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

Remove buggy IsPublic method from TypeAttributesExtensions #75081

Merged
merged 4 commits into from
Sep 12, 2024

Conversation

akoeplinger
Copy link
Member

@akoeplinger akoeplinger commented Sep 12, 2024

When checking visibility we need to use the VisibilityMask because TypeAttributes is a funky flags enum that uses values that aren't binary exclusive (unlike normal flags enums) so e.g. NestedPrivate (0x3) would be interpreted as Public (0x1). See https://learn.microsoft.com/en-us/dotnet/api/system.reflection.typeattributes?view=net-8.0#examples

We got lucky because the IsPublic extension method isn't actually used anywhere so this was never hit. Remove the method.

The same applies to checking for interface, but TypeAttributes.ClassSemanticsMask is the same value as TypeAttributes.Interface so it happens to work, but it's still better to use the canonical code for clarity.

Also found a couple other *Attributes that were described in #16929 and fixed them too. Those are essentially no-ops too.

Fixes #16929

/cc @tmat

When checking visibility we need to use the VisibilityMask because TypeAttributes is a funky flags enum that has multiple entries with the same value (unlike normal flags enums).
See https://learn.microsoft.com/en-us/dotnet/api/system.reflection.typeattributes?view=net-8.0#examples

We got lucky because the IsPublic extension method isn't actually used anywhere so this was never hit.

The same applies to checking for interface, but TypeAttributes.ClassSemanticsMask is the same value as TypeAttributes.Interface so it happens to work, but it's still better to use the canonical code for clarity.
@akoeplinger akoeplinger requested a review from a team as a code owner September 12, 2024 15:20
@dotnet-issue-labeler dotnet-issue-labeler bot added Area-Compilers untriaged Issues and PRs which have not yet been triaged by a lead labels Sep 12, 2024
@dotnet-policy-service dotnet-policy-service bot added the Community The pull request was submitted by a contributor who is not a Microsoft employee. label Sep 12, 2024
@akoeplinger
Copy link
Member Author

Turns out there was already an old issue about this (#16929) and we just had a couple instances left so I fixed them too.

@akoeplinger akoeplinger changed the title Fix TypeAttributes visibility checking Fix Reflection *Attributes checking Sep 12, 2024
@tmat
Copy link
Member

tmat commented Sep 12, 2024

@dotnet/roslyn-compiler

@333fred
Copy link
Member

333fred commented Sep 12, 2024

Presumably, we should be able to observe differences in these scenarios with tests; given that there are no failures here, it seems like we have a test gap and I'd like to see some tests covering it.

@AlekseyTs
Copy link
Contributor

AlekseyTs commented Sep 12, 2024

Alternatively, if we are fixing internal dead code (never used), then I would prefer removing it rather than changing it.

@akoeplinger
Copy link
Member Author

yeah only the IsPublic change is actually a bug but the method is never used, I can remove it.

the other cases only work because the *Mask value is the same as the enum entry we're checking. in theory we could leave it as is but I think it's worthwhile just to not spread a bad pattern.

@333fred
Copy link
Member

333fred commented Sep 12, 2024

In talking with @AlekseyTs, we'd prefer to leave most of the code as is, rather than changing it, given that these flags aren't likely to change and they work as is. For the bad IsPublic check, removing it is fine.

@akoeplinger akoeplinger changed the title Fix Reflection *Attributes checking Remove buggy IsPublic method from TypeAttributesExtensions Sep 12, 2024
@akoeplinger
Copy link
Member Author

@333fred sure, I updated the PR to just remove the IsPublic method.

@333fred
Copy link
Member

333fred commented Sep 12, 2024

@dotnet/roslyn-compiler @AlekseyTs for a second review.

Copy link
Contributor

@AlekseyTs AlekseyTs left a comment

Choose a reason for hiding this comment

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

LGTM (commit 4)

@333fred 333fred enabled auto-merge (squash) September 12, 2024 22:32
@333fred 333fred merged commit 64ba034 into dotnet:main Sep 12, 2024
24 checks passed
@dotnet-policy-service dotnet-policy-service bot added this to the Next milestone Sep 12, 2024
@akoeplinger akoeplinger deleted the typeattributes-mask branch September 13, 2024 09:00
333fred added a commit to 333fred/roslyn that referenced this pull request Sep 13, 2024
* upstream/main: (45 commits)
  Adjust an assert in EmitArgument (dotnet#75067)
  Switch to new VMR control set (dotnet#75056)
  Disallow ref assignment to ternary or another ref assignment (dotnet#75076)
  Move some emit tests from Emit2 to Emit3 to avoid hitting UserString heap limit (dotnet#75091)
  BindAttributeCore - use proper binder to avoid an attribute binding cycle (dotnet#75060)
  Remove buggy IsPublic method from TypeAttributesExtensions (dotnet#75081)
  Include initial filter node when searching for nodes to order modifiers
  Remove additional Gitter link (dotnet#75086)
  Remove newlines between test run information sections
  Log messages for Test Results
  Fix stack adjustment when emitting stackalloc (dotnet#75042)
  Lock translation of strings used to demonstrate identifier naming styles
  docs: Correct SDK version in documentation to match global.json (dotnet#75038)
  Add a test observing lack of an issue. (dotnet#75057)
  Fix preview refresh on selection for enum flags checkboxes
  Semantic snippets: handle case with inline statement snippets before member access expression (dotnet#74966)
  Configure release/vscode branch for nuget publishing
  Remove MS.CA.Test.Resources.Proprietary PackageReference (dotnet#75037)
  Allow suppressing nullability warnings in more ref scenarios (dotnet#74498)
  Update dependencies from https://github.com/dotnet/arcade build 20240909.6 (dotnet#75040)
  ...
@akhera99 akhera99 removed this from the Next milestone Sep 26, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Area-Compilers Community The pull request was submitted by a contributor who is not a Microsoft employee. untriaged Issues and PRs which have not yet been triaged by a lead
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Incorrect System.Reflection.*Attributes checks in some parts of the code
5 participants