Skip to content

Unsafe evolution spec: consolidate attribute declaration diagnostics#9999

Merged
jjonescz merged 3 commits into
dotnet:mainfrom
jjonescz:unsafe-9-warnings
Feb 24, 2026
Merged

Unsafe evolution spec: consolidate attribute declaration diagnostics#9999
jjonescz merged 3 commits into
dotnet:mainfrom
jjonescz:unsafe-9-warnings

Conversation

@jjonescz

Copy link
Copy Markdown
Member

No description provided.

@jjonescz jjonescz requested review from 333fred and jcouv February 17, 2026 09:40
@jjonescz jjonescz requested a review from a team as a code owner February 17, 2026 09:40
- fields,
- destructors.
The compiler will emit a warning if `RequiresUnsafe` is used under the legacy memory safety rules and an error if it is applied to unsupported symbol kinds
(note that this excludes symbol kinds that should be banned already by `AttributeUsageAttribute` on the attribute's definition):

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

Inspired by UnscopedRefAttribute - it also reports a custom error for locations like static methods where it doesn't make sense but relies on AttributeTargets error elsewhere:

using System.Diagnostics.CodeAnalysis;

[UnscopedRef] // error CS0592: Attribute 'UnscopedRef' is not valid on this declaration type. It is only valid on 'method, property, indexer, parameter' declarations.
class C
{
    [UnscopedRef] //  error CS9101: UnscopedRefAttribute can only be applied to struct or virtual interface instance methods and properties, and cannot be applied to constructors or init-only members.
    static void M() { }
}

@jcouv jcouv self-assigned this Feb 17, 2026

### `new()` constraint

Do we want to support `new()` with *requires-unsafe* (something we currently don't seem to support in the compiler for other features, like `Obsolete`)?

@jcouv jcouv Feb 17, 2026

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.

Consider adding a code snippet to illustrate the scenario/question. It was not clear to me whether this is the same issue described in next section or a separate question

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

The example in the next section is just a bit extended version of this, but I can add a simpler example just for this section too. Thanks.

@jjonescz jjonescz merged commit e51e047 into dotnet:main Feb 24, 2026
1 check passed
@jjonescz jjonescz deleted the unsafe-9-warnings branch February 24, 2026 09:47
jjonescz added a commit to dotnet/roslyn that referenced this pull request Feb 24, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants