Skip to content

For a compile-time compat add synthesized private fields for a value type (struct) #11347

@andriipatsula

Description

@andriipatsula
public readonly partial struct Memory<T>
{
    private readonly object _dummy;
    private readonly int _dummyPrimitive;

    // ...
}

Rules:

// For compile-time compat, the following rules should work for producing a reference assembly. We drop all private fields,
// but add back certain synthesized private fields for a value type (struct) as follows:
// 1. If there is a ref field or reference type field in the struct or within the fields' type closure,
// it should emit a reference type and a value type dummy field.
// - The reference type dummy field is needed in order to inform the compiler to block
// taking pointers to this struct because the GC will not track updating those references.
// - The value type dummy field is needed in order for the compiler to error correctly on definite assignment checks in all scenarios. dotnet/roslyn#30194
// 2. If there are no reference type fields, but there are value type fields in the struct field closure,
// and at least one of these fields is a nonempty struct, then we should emit a value type dummy field.
// - The previous rules are for definite assignment checks, so the compiler knows there is a private field
// that has not been initialized to error about uninitialized structs.
//
// 3. If the type is generic, then for every type parameter of the type, if there are any private
// or internal fields that are or contain any members whose type is that type parameter,
// we add a direct private field of that type.
// - Compiler needs to see all fields that have generic arguments (even private ones) to be able
// to validate there aren't any struct layout cycles.
// Note: By "private", we mean not visible outside the assembly.
// For more details see issue https://github.com/dotnet/corefx/issues/6185
// this blog is helpful as well http://blog.paranoidcoding.com/2016/02/15/are-private-members-api-surface.html

Metadata

Metadata

Labels

No labels
No labels

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions