dotnet / dotnet/runtime

[Analyzer Proposal]: Warn on FixedAddressValueType/ThreadStatic on instance fields.

Open
#131,094 7 comments 0 reactions 0 assignees View on GitHub
api-suggestion area-System.Runtime.CompilerServices code-analyzer help wanted
Dominant language
C#
Stars
18.3k
Forks
5.6k
PR merge metrics
PR metrics pending

Description

### Background and motivation

The runtime ignores `FixedAddressValueType`/`ThreadStatic` if the field isn't static, this might lead to unexpected GC holes/logic bugs if the programmer doesn't know this.

### API Proposal

Warn on `FixedAddressValueType`/`ThreadStatic` if the field isn't static.

### API Usage

```cs
class C
{
[FixedAddressValueType]
public Int128 S; // Warns
[ThreadStatic]
public int I; // Warns
}
```

### Alternative Designs

See #129664 for discussion about this.

### Risks

_No response_

Contributor guide

Open the contributing guide

Research direction

Start by reading the discussion in #129664 and locate the analyzer entry point responsible for field attributes; no file or test is named in this issue. The work is done when instance fields using FixedAddressValueType or ThreadStatic produce warnings, while static fields do not.

Written by the indexing model from the issue text.

Assessment

Tech stack
csharp
Domain
devtools
Issue type
Feature
Difficulty
3/5
Estimated time
1-2 days
Activity status
Active
Clarity
Mostly clear
Newbie friendliness
58/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.