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

Conversation

davidwengier
Copy link
Contributor

Fixes #68335

Minimal repo:

A.cs:

internal partial class C
{
    public void M()
    {
        Console.WriteLine("C.M");
    }
}

B.cs:

[CreateNewOnMetadataUpdate]
internal partial class C
{
    public class A
    {
        public void M()
        {
            Console.WriteLine("C.A.M25");
        }
    }
}

@davidwengier davidwengier requested a review from tmat May 29, 2023 07:35
@davidwengier davidwengier requested a review from a team as a code owner May 29, 2023 07:35
@dotnet-issue-labeler dotnet-issue-labeler bot added Area-Interactive untriaged Issues and PRs which have not yet been triaged by a lead labels May 29, 2023
@davidwengier davidwengier changed the title Upgrade change types for containing types [EnC] Upgrade change types for containing types May 29, 2023
@davidwengier
Copy link
Contributor Author

@tmat PTAL

Copy link
Member

@Youssef1313 Youssef1313 left a comment

Choose a reason for hiding this comment

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

LGTM 🎉


// 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.

@davidwengier
Copy link
Contributor Author

ping @tmat

@davidwengier davidwengier requested a review from a team as a code owner June 7, 2023 05:05
@davidwengier davidwengier enabled auto-merge June 7, 2023 06:08
@Youssef1313
Copy link
Member

@davidwengier Can you restart the failing job?

@davidwengier davidwengier merged commit 9533f94 into dotnet:main Jun 8, 2023
@ghost ghost added this to the Next milestone Jun 8, 2023
@davidwengier davidwengier deleted the EnCNestedTypes branch June 8, 2023 20:42
@RikkiGibson RikkiGibson modified the milestones: Next, 17.7 P3 Jun 28, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Area-Interactive untriaged Issues and PRs which have not yet been triaged by a lead
Projects
None yet
Development

Successfully merging this pull request may close these issues.

HotReload crashes with "An item with the same key has already been added"
4 participants