dotnet / dotnet/csharpstandard

C# allows static constructor to write to readonly static field of another instantiation (code won't verify)

Open
#280 2 comments 0 reactions 0 assignees View on GitHub
type: bug
Dominant language
C#
Stars
815
Forks
99
Avg merge
1d 14h
Merged PRs (30d)
16

Description

There is a small “loophole” in C#. The language allows the following

``` cs
struct G
{
public static readonly int g;

static G()
{
G.g = 15;
G.g = 27;
}
}
```

But the generated code for the assignments inside the static constructor will not verify.

I don’t think the (safe subset of the) language should be allowing something that requires generating unverifiable code. I prefer to think of this as a compiler bug, and possibly a spec bug too (I didn’t check to see what the spec says about this).

I don’t think it is high priority (we’ve lived with this same issue in the native compiler for years). If you agree that this should be fixed, can you (Mads) please take an action item to make some relevant change to the specification?

(moved from https://github.com/dotnet/roslyn/issues/624)

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.