Skip to content

Avoid generating dummy fields for structs that already contain appropriate public fields #2033

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 1 commit into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 6 additions & 3 deletions src/Microsoft.Cci.Extensions/Writers/CSharp/CSharpWriter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -151,14 +151,15 @@ public override void Visit(ITypeDefinition parentType, IEnumerable<IFieldDefinit

// Note: By "private", we mean not visible outside the assembly.

// For more details see issue https://github.com/dotnet/corefx/issues/6185
// 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

List<IFieldDefinition> newFields = new List<IFieldDefinition>();
var includedVisibleFields = fields.Where(f => f.IsVisibleOutsideAssembly()).Where(_cciFilter.Include);
includedVisibleFields = includedVisibleFields.OrderBy(GetMemberKey, StringComparer.OrdinalIgnoreCase);

var excludedFields = fields.Except(includedVisibleFields).Where(f => !f.IsStatic);
var visibleNonStaticFields = includedVisibleFields.Where(f => !f.IsStatic);

if (excludedFields.Any())
{
Expand All @@ -172,20 +173,22 @@ public override void Visit(ITypeDefinition parentType, IEnumerable<IFieldDefinit
// For definiteassignment checks the compiler needs to know there is a private field
// that has not been initialized so if there are any we need to add a dummy private
// field to help the compiler do its job and error about uninitialized structs
bool hasRefVisibleField = visibleNonStaticFields.Any(f => f.Type.IsOrContainsReferenceType());
bool hasRefPrivateField = excludedFields.Any(f => f.Type.IsOrContainsReferenceType());

// If at least one of the private fields contains a reference type then we need to
// set this field type to object or reference field to inform the compiler to block
// taking pointers to this struct because the GC will not track updating those references
if (hasRefPrivateField)
if (hasRefPrivateField && !hasRefVisibleField)
{
IFieldDefinition fieldType = DummyFieldWriterHelper(parentType, excludedFields, parentType.PlatformType.SystemObject);
newFields.Add(fieldType);
}

bool hasValueTypeVisibleField = visibleNonStaticFields.Any(f => !f.Type.IsOrContainsReferenceType());
bool hasValueTypePrivateField = excludedFields.Any(f => !f.Type.IsOrContainsReferenceType());

if (hasValueTypePrivateField)
if (hasValueTypePrivateField && !hasValueTypeVisibleField)
{
IFieldDefinition fieldType = DummyFieldWriterHelper(parentType, excludedFields, parentType.PlatformType.SystemInt32, "_dummyPrimitive");
newFields.Add(fieldType);
Expand Down