Skip to content

Include [RequiresUnsafe] in PublicAPI signatures#82731

Open
jjonescz wants to merge 3 commits intodotnet:mainfrom
jjonescz:Unsafe-23-ApiAnalyzer
Open

Include [RequiresUnsafe] in PublicAPI signatures#82731
jjonescz wants to merge 3 commits intodotnet:mainfrom
jjonescz:Unsafe-23-ApiAnalyzer

Conversation

@jjonescz
Copy link
Member

@jjonescz jjonescz commented Mar 12, 2026

Test plan: #81207

Microsoft Reviewers: Open in CodeFlow

""");

[Fact]
public Task TestRequiresUnsafeApiOnPropertyAsync()
Copy link
Member

Choose a reason for hiding this comment

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

This is a test on accessors. Consider testing [RequiresUnsafe] on property too

Copy link
Member

@jcouv jcouv Mar 12, 2026

Choose a reason for hiding this comment

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

Consider also having a test for extern method

Copy link
Member Author

Choose a reason for hiding this comment

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

This is a test on accessors. Consider testing [RequiresUnsafe] on property too

This is [RequiresUnsafe] on property, but the list of APIs only contains the accessors.

Copy link
Member Author

Choose a reason for hiding this comment

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

Consider also having a test for extern method

Thanks. extern apparently already is put in the signature by this analyzer. I don't think we need to put the implicit RequiresUnsafe there though. (Sure if someone enables updated memory safety rules, all extern apis become unsafe and that's kind of breaking. But similarly if someone disables updated memory safety rules, all pointer methods become unsafe and that's also not something we present in the signature by this analyzer. Basically enabling/disabling the rules is a break on a different level, not on the API level.)

@jcouv jcouv self-assigned this Mar 12, 2026
@jjonescz jjonescz marked this pull request as ready for review March 13, 2026 12:39
@jjonescz jjonescz requested a review from a team as a code owner March 13, 2026 12:39
Copy link
Member

@jcouv jcouv left a comment

Choose a reason for hiding this comment

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

LGTM Thanks (commit 2)

@jcouv
Copy link
Member

jcouv commented Mar 13, 2026

Andy mentioned a bunch of other attributes that should be tracked on public APIs. Should we open a tracking issue?

@jjonescz
Copy link
Member Author

@333fred for another review, thanks

{
foreach (var attribute in symbol.GetAttributes())
{
if (attribute.AttributeClass is { Name: "RequiresUnsafeAttribute", ContainingSymbol: INamespaceSymbol { Name: "CompilerServices", ContainingNamespace: { Name: "Runtime", ContainingNamespace: { Name: nameof(System), ContainingNamespace.IsGlobalNamespace: true } } } })
Copy link
Member

Choose a reason for hiding this comment

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

Do we have a tracking issue for a public API here?

Copy link
Member Author

@jjonescz jjonescz Mar 17, 2026

Choose a reason for hiding this comment

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

Thanks, let me add this follow up comment (if that's what you meant).

// https://github.com/dotnet/roslyn/issues/82546: Confirm the attribute shape in BCL API review.

Copy link
Member

Choose a reason for hiding this comment

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

No, I mean an api for not needing to filter for an attribute and just check an api on the symbol.

Copy link
Member Author

Choose a reason for hiding this comment

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

Oh, right. Yes, we do: #82791

Copy link
Member Author

Choose a reason for hiding this comment

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

I will update the tracking comment to say that we should use that API when available.

@jjonescz
Copy link
Member Author

Andy mentioned a bunch of other attributes that should be tracked on public APIs. Should we open a tracking issue?

Feels like a feature request that the runtime team should create if they want, I don't think I know all the details.

@jjonescz jjonescz enabled auto-merge (squash) March 17, 2026 12:41
@jjonescz jjonescz disabled auto-merge March 17, 2026 19:35
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants