Skip to content
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

[EnC] Upgrade change types for containing types #68368

Merged
merged 4 commits into from
Jun 8, 2023
Merged
Show file tree
Hide file tree
Changes from 3 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
Original file line number Diff line number Diff line change
Expand Up @@ -5050,6 +5050,91 @@ void ValidateReplacedType(CompilationDifference diff, MetadataReader[] readers)
CheckBlobValue(readers, reader3.GetCustomAttribute(reader3.CustomAttributes.First()).Value, new byte[] { 0x01, 0x00, 0x00, 0x00 });
}

[Fact]
public void ReplaceType_UpdateNestedType()
{
using var _ = new EditAndContinueTest()
.AddBaseline(
source: $$"""
using System;

class C
{
class D
{
void M()
{
Console.WriteLine("1");
}
}

void N()
{
Console.WriteLine("1");
}
}
""" + MetadataUpdateOriginalTypeAttributeSource,
validator: g =>
{
g.VerifyTypeDefNames("<Module>", "C", "MetadataUpdateOriginalTypeAttribute", "D");
g.VerifyMethodDefNames("N", ".ctor", ".ctor", "get_OriginalType", "M", ".ctor");
})

.AddGeneration(
source: """
using System;

class C
{
class D
{
void M()
{
Console.WriteLine("2");
}
}

void N()
{
Console.WriteLine("2");
}
}
""" + MetadataUpdateOriginalTypeAttributeSource,
edits: new[] {
// Note: Nested type edit needs to be seen first to repro the bug. Real world scenario requires the nested
// class to be in a separate file.
Edit(SemanticEditKind.Update, c => c.GetMember("C.D.M")),
Edit(SemanticEditKind.Replace, c => null, newSymbolProvider: c => c.GetMember("C")),
},
validator: g =>
{
g.VerifyTypeDefNames("C#1");
g.VerifyMethodDefNames("M", "N", ".ctor", ".ctor");
g.VerifyEncLogDefinitions(new[]
{
Row(5, TableIndex.TypeDef, EditAndContinueOperation.Default),
Row(5, TableIndex.MethodDef, EditAndContinueOperation.Default),
Row(5, TableIndex.TypeDef, EditAndContinueOperation.AddMethod),
Row(7, TableIndex.MethodDef, EditAndContinueOperation.Default),
Row(5, TableIndex.TypeDef, EditAndContinueOperation.AddMethod),
Row(8, TableIndex.MethodDef, EditAndContinueOperation.Default),
Row(4, TableIndex.TypeDef, EditAndContinueOperation.AddMethod),
Row(9, TableIndex.MethodDef, EditAndContinueOperation.Default),
Row(8, TableIndex.CustomAttribute, EditAndContinueOperation.Default)
});
g.VerifyEncMapDefinitions(new[]
{
Handle(5, TableIndex.TypeDef),
Handle(5, TableIndex.MethodDef),
Handle(7, TableIndex.MethodDef),
Handle(8, TableIndex.MethodDef),
Handle(9, TableIndex.MethodDef),
Handle(8, TableIndex.CustomAttribute)
});
})
.Verify();
}

[Fact]
public void EventFields_Attributes()
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -494,7 +494,18 @@ private static void CalculateChanges(IEnumerable<SemanticEdit> edits, out IReadO
}

AddContainingTypesAndNamespaces(changesBuilder, member);
changesBuilder.Add(member, change);

// If we saw an edit for a symbol that is a nested type of this symbol, we might already have a dictionary entry
// for it, flagging that it contains changes. If so we "upgrade" the change to the real one.
if (changesBuilder.TryGetValue(member, out var existingChange) && existingChange == SymbolChange.ContainsChanges)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

AddContainingTypesAndNamespaces will add properties/methods with SymbolChange.Updated

Can edits have one edit with some symbol contained within a property (with any level of nested), and another edit with the property itself?

If this can happen, then AddContainingTypesAndNamespaces would add the property with SymbolChange.Updated for the first edit, then when processing the second edit for the property itself, we will try to add it again.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It shouldn't be possible, but then again this bug shouldn't have been possible either. In general I would say that this area of the compiler is full of nastiness if you stray from the happy path, and EnC is no exception. This code is written assuming the edits will be of a certain known form. In fact, this bug could probably be fixed equally by changes to the AbstractEditAndContinueAnalyzer where it creates the edits, to ensure it checks for containing types in the Replace scenario, or in EditSession where it merges the semantic edits from all documents, to ensure a predictable order. I chose this fix because it seemed the easiest, but also deliberately left the call to .Add so that its not masking any future odd scenarios.

We'll have to wait and see where Tomas thinks that was the right call :)

Copy link
Member

@tmat tmat Jun 7, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should we assert that member is a INamedTypeSymbol? I think this only happens for nested types, right?

We don't have great coverage for nested combo updates in the IDE layer, we should add the following tests:

        [Fact]
        public void NestedType_Replace_WithUpdateInNestedType_Partial_DiffertDocument()
        {
            var srcA1 = ReloadableAttributeSrc + "[CreateNewOnMetadataUpdate]partial class C { int N() => 1; }";
            var srcB1 = "partial class C { class D { int M() => 1; } }";
            var srcA2 = ReloadableAttributeSrc + "[CreateNewOnMetadataUpdate]partial class C { int N() => 2; }";
            var srcB2 = "partial class C { class D { int M() => 2; } }";

            var editsA = GetTopEdits(srcA1, srcA2);
            var editsB = GetTopEdits(srcB1, srcB2);

            EditAndContinueValidation.VerifySemantics(
                new[] { editsA, editsB },
                new[]
                {
                    DocumentResults(semanticEdits: new[]
                    {
                        SemanticEdit(SemanticEditKind.Replace, c => c.GetMember("C"), partialType: "C")
                    }),
                    DocumentResults(semanticEdits: new[]
                    {
                        SemanticEdit(SemanticEditKind.Update, c => c.GetMember("C.D.M"))
                    })
                },
                capabilities: EditAndContinueCapabilities.NewTypeDefinition);
        }

        [Fact]
        public void NestedType_Replace_WithUpdateInNestedType_Partial_SameDocument()
        {
            var src1 = ReloadableAttributeSrc + "[CreateNewOnMetadataUpdate]partial class C { int N() => 1; } partial class C { class D { int M() => 1; } }";
            var src2 = ReloadableAttributeSrc + "[CreateNewOnMetadataUpdate]partial class C { int N() => 2; } partial class C { class D { int M() => 2; } }";

            var edits = GetTopEdits(src1, src2);

            EditAndContinueValidation.VerifySemantics(
                edits,
                semanticEdits: new[]
                {
                    SemanticEdit(SemanticEditKind.Replace, c => c.GetMember("C")),
                    SemanticEdit(SemanticEditKind.Update, c => c.GetMember("C.D.M"))
                },
                capabilities: EditAndContinueCapabilities.NewTypeDefinition);
        }

        [Fact]
        public void NestedType_Replace_WithUpdateInNestedType()
        {
            var src1 = ReloadableAttributeSrc + "[CreateNewOnMetadataUpdate]class C { int N() => 1; class D { int M() => 1; } }";
            var src2 = ReloadableAttributeSrc + "[CreateNewOnMetadataUpdate]class C { int N() => 2; class D { int M() => 2; } }";

            var edits = GetTopEdits(src1, src2);

            EditAndContinueValidation.VerifySemantics(
                edits,
                semanticEdits: new[]
                {
                    SemanticEdit(SemanticEditKind.Replace, c => c.GetMember("C")),
                    SemanticEdit(SemanticEditKind.Update, c => c.GetMember("C.D.M"))
                },
                capabilities: EditAndContinueCapabilities.NewTypeDefinition);
        }

        [Fact]
        public void NestedType_Update_WithUpdateInNestedType()
        {
            var src1 = @"[System.Obsolete(""A"")]class C { int N() => 1; class D { int M() => 1; } }";
            var src2 = @"[System.Obsolete(""B"")]class C { int N() => 1; class D { int M() => 2; } }";

            var edits = GetTopEdits(src1, src2);

            EditAndContinueValidation.VerifySemantics(
                edits,
                semanticEdits: new[]
                {
                    SemanticEdit(SemanticEditKind.Update, c => c.GetMember("C")),
                    SemanticEdit(SemanticEditKind.Update, c => c.GetMember("C.D.M"))
                },
                capabilities: EditAndContinueCapabilities.ChangeCustomAttributes);
        }

The last one demonstrates it's not an issue just for Replace edit, but we can also have "nested" Update edits.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The last one demonstrates it's not an issue just for Replace edit, but we can also have "nested" Update edits.

I don't think it demostrates that. The last two tests here would not have hit the bug. For the bug to repro, the edit for the nested type has to come first. I couldn't find a single-document repro of this issue (which is why I suspect it was never hit in the wild in .cshtml files). It is possible that the first test you have here could be made to hit it, if the compiler say the second document first, but none of the EnC test infra can do that end-to-end type of test that I'm aware of.

Have added the tests and assert either way :)

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

the nested type has to come first.

That can be arranged if we split into partial in two documents like in the first test. What I meant is that it can occur with two Updates rather then just Replace and Update.

{
Debug.Assert(member is INamedTypeSymbol);
changesBuilder[member] = change;
}
else
{
changesBuilder.Add(member, change);
}
}

changes = changesBuilder;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4908,6 +4908,87 @@ public void Delegate_ReadOnlyRef_ReturnType_Update()

#region Nested Types

[Fact]
public void NestedType_Replace_WithUpdateInNestedType_Partial_DiffertDocument()
davidwengier marked this conversation as resolved.
Show resolved Hide resolved
davidwengier marked this conversation as resolved.
Show resolved Hide resolved
{
var srcA1 = ReloadableAttributeSrc + "[CreateNewOnMetadataUpdate]partial class C { int N() => 1; }";
var srcB1 = "partial class C { class D { int M() => 1; } }";
var srcA2 = ReloadableAttributeSrc + "[CreateNewOnMetadataUpdate]partial class C { int N() => 2; }";
var srcB2 = "partial class C { class D { int M() => 2; } }";

var editsA = GetTopEdits(srcA1, srcA2);
var editsB = GetTopEdits(srcB1, srcB2);

EditAndContinueValidation.VerifySemantics(
new[] { editsA, editsB },
new[]
{
DocumentResults(semanticEdits: new[]
{
SemanticEdit(SemanticEditKind.Replace, c => c.GetMember("C"), partialType: "C")
}),
DocumentResults(semanticEdits: new[]
{
SemanticEdit(SemanticEditKind.Update, c => c.GetMember("C.D.M"))
})
},
capabilities: EditAndContinueCapabilities.NewTypeDefinition);
}

[Fact]
public void NestedType_Replace_WithUpdateInNestedType_Partial_SameDocument()
{
var src1 = ReloadableAttributeSrc + "[CreateNewOnMetadataUpdate]partial class C { int N() => 1; } partial class C { class D { int M() => 1; } }";
var src2 = ReloadableAttributeSrc + "[CreateNewOnMetadataUpdate]partial class C { int N() => 2; } partial class C { class D { int M() => 2; } }";

var edits = GetTopEdits(src1, src2);

EditAndContinueValidation.VerifySemantics(
edits,
semanticEdits: new[]
{
SemanticEdit(SemanticEditKind.Replace, c => c.GetMember("C")),
SemanticEdit(SemanticEditKind.Update, c => c.GetMember("C.D.M"))
},
capabilities: EditAndContinueCapabilities.NewTypeDefinition);
}

[Fact]
public void NestedType_Replace_WithUpdateInNestedType()
{
var src1 = ReloadableAttributeSrc + "[CreateNewOnMetadataUpdate]class C { int N() => 1; class D { int M() => 1; } }";
var src2 = ReloadableAttributeSrc + "[CreateNewOnMetadataUpdate]class C { int N() => 2; class D { int M() => 2; } }";

var edits = GetTopEdits(src1, src2);

EditAndContinueValidation.VerifySemantics(
edits,
semanticEdits: new[]
{
SemanticEdit(SemanticEditKind.Replace, c => c.GetMember("C")),
SemanticEdit(SemanticEditKind.Update, c => c.GetMember("C.D.M"))
},
capabilities: EditAndContinueCapabilities.NewTypeDefinition);
}

[Fact]
public void NestedType_Update_WithUpdateInNestedType()
{
var src1 = @"[System.Obsolete(""A"")]class C { int N() => 1; class D { int M() => 1; } }";
var src2 = @"[System.Obsolete(""B"")]class C { int N() => 1; class D { int M() => 2; } }";

var edits = GetTopEdits(src1, src2);

EditAndContinueValidation.VerifySemantics(
edits,
semanticEdits: new[]
{
SemanticEdit(SemanticEditKind.Update, c => c.GetMember("C")),
SemanticEdit(SemanticEditKind.Update, c => c.GetMember("C.D.M"))
},
capabilities: EditAndContinueCapabilities.ChangeCustomAttributes);
}

[Fact]
public void NestedType_Move_Sideways()
{
Expand Down