dotnet / dotnet/roslyn

Some struct layout cycles caused by static fields involving generics are not detected

Open
#75,701 3 comments 0 reactions 0 assignees View on GitHub
Area-Compilers
Dominant language
C#
Stars
20.7k
Forks
4.3k
PR merge metrics
PR metrics pending

Description

Compile the following code:
```
#pragma warning disable CS0169 // The field is never used
struct E
{}

struct X
{
static T _t;
}

struct Y
{
static X xz;
}

struct Z
{
static X xe;
static X xy;
}
```

Observed:
No errors.
PEVerify output:
```
Microsoft (R) .NET Framework PE Verifier. Version 4.0.30319.0
Copyright (c) Microsoft Corporation. All rights reserved.

[token 0x02000006] Type load failed.
[token 0x02000007] Type load failed.
2 Error(s)
```

Expected:
An error about a circular struct definition.
For example, native compiler reports the following errors:
```
.cs(11,17): error CS0523: Struct member 'Y.xz' of type 'X' causes a cycle in the struct layout
.cs(6,14): error CS0523: Struct member 'X._t' of type 'Z' causes a cycle in the struct layout
.cs(17,17): error CS0523: Struct member 'Z.xy' of type 'X' causes a cycle in the struct layout
.cs(6,14): error CS0523: Struct member 'X._t' of type 'Y' causes a cycle in the struct layout
```

If fields are changed to instance fields, the cycles are detected:
```
.cs(17,10): error CS0523: Struct member 'Z.xy' of type 'X' causes a cycle in the struct layout
.cs(11,10): error CS0523: Struct member 'Y.xz' of type 'X' causes a cycle in the struct layout
```

Contributor guide

Open the contributing guide

Research direction

Start by compiling the minimal reproduction in the issue and compare the diagnostics for static fields with the instance-field variant. Trace the compiler's struct layout cycle detection for generic instantiations; done means the static-field cases report circular struct definitions without breaking the existing instance-field diagnostics.

Written by the indexing model from the issue text.

Assessment

Tech stack
csharp
Domain
compilers
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Clearly specified
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.