Skip to content

Commit

Permalink
EnC - Tell the debugger about updated type def tokens (#53217)
Browse files Browse the repository at this point in the history
  • Loading branch information
davidwengier authored May 17, 2021
1 parent 1e5a264 commit c1d0a97
Show file tree
Hide file tree
Showing 16 changed files with 189 additions and 66 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -3352,7 +3352,6 @@ internal override EmitDifferenceResult EmitDifference(
Stream metadataStream,
Stream ilStream,
Stream pdbStream,
ICollection<MethodDefinitionHandle> updatedMethods,
CompilationTestData? testData,
CancellationToken cancellationToken)
{
Expand All @@ -3364,7 +3363,6 @@ internal override EmitDifferenceResult EmitDifference(
metadataStream,
ilStream,
pdbStream,
updatedMethods,
testData,
cancellationToken);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
using System.Threading;
using Microsoft.CodeAnalysis.CodeGen;
using Microsoft.CodeAnalysis.Emit;
using Microsoft.CodeAnalysis.PooledObjects;
using Roslyn.Utilities;

namespace Microsoft.CodeAnalysis.CSharp.Emit
Expand All @@ -24,7 +25,6 @@ internal static EmitDifferenceResult EmitDifference(
Stream metadataStream,
Stream ilStream,
Stream pdbStream,
ICollection<MethodDefinitionHandle> updatedMethods,
CompilationTestData? testData,
CancellationToken cancellationToken)
{
Expand All @@ -35,6 +35,9 @@ internal static EmitDifferenceResult EmitDifference(
var serializationProperties = compilation.ConstructModuleSerializationProperties(emitOptions, runtimeMDVersion, baseline.ModuleVersionId);
var manifestResources = SpecializedCollections.EmptyEnumerable<ResourceDescription>();

var updatedMethods = ArrayBuilder<MethodDefinitionHandle>.GetInstance();
var updatedTypes = ArrayBuilder<TypeDefinitionHandle>.GetInstance();

PEDeltaAssemblyBuilder moduleBeingBuilt;
try
{
Expand All @@ -52,7 +55,7 @@ internal static EmitDifferenceResult EmitDifference(
{
// TODO: https://github.com/dotnet/roslyn/issues/9004
diagnostics.Add(ErrorCode.ERR_ModuleEmitFailure, NoLocation.Singleton, compilation.AssemblyName, e.Message);
return new EmitDifferenceResult(success: false, diagnostics: diagnostics.ToReadOnlyAndFree(), baseline: null);
return new EmitDifferenceResult(success: false, diagnostics: diagnostics.ToReadOnlyAndFree(), baseline: null, updatedMethods: updatedMethods.ToImmutableAndFree(), updatedTypes: updatedTypes.ToImmutableAndFree());
}

if (testData != null)
Expand Down Expand Up @@ -87,6 +90,7 @@ internal static EmitDifferenceResult EmitDifference(
ilStream,
pdbStream,
updatedMethods,
updatedTypes,
diagnostics,
testData?.SymWriterFactory,
emitOptions.PdbFilePath,
Expand All @@ -96,7 +100,9 @@ internal static EmitDifferenceResult EmitDifference(
return new EmitDifferenceResult(
success: newBaseline != null,
diagnostics: diagnostics.ToReadOnlyAndFree(),
baseline: newBaseline);
baseline: newBaseline,
updatedMethods: updatedMethods.ToImmutableAndFree(),
updatedTypes: updatedTypes.ToImmutableAndFree());
}

/// <summary>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ class C
c2.GlobalNamespace.GetMember<NamedTypeSymbol>("C").GetMember("Main"))
};

c2.EmitDifference(baseline, edits, mdStream, ilStream, pdbStream, updatedMethods);
c2.EmitDifference(baseline, edits, s => false, mdStream, ilStream, pdbStream);

var actualIL = ImmutableArray.Create(ilStream.ToArray()).GetMethodIL();
var expectedIL = @"
Expand Down Expand Up @@ -152,7 +152,7 @@ class C
c2.GlobalNamespace.GetMember<NamedTypeSymbol>("C").GetMember("Main"))
};

c2.EmitDifference(baseline, edits, mdStream, ilStream, pdbStream, updatedMethods);
c2.EmitDifference(baseline, edits, s => false, mdStream, ilStream, pdbStream);

var actualIL = ImmutableArray.Create(ilStream.ToArray()).GetMethodIL();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,8 @@ static IEnumerable<int> G()
using var md1 = diff1.GetMetadata();
var reader1 = md1.Reader;

diff1.VerifyUpdatedTypes("0x02000002");

CheckEncLog(reader1,
Row(2, TableIndex.AssemblyRef, EditAndContinueOperation.Default),
Row(17, TableIndex.MemberRef, EditAndContinueOperation.Default),
Expand Down Expand Up @@ -860,6 +862,8 @@ static async Task<int> F()
// only methods with sequence points should be listed in UpdatedMethods:
diff1.VerifyUpdatedMethods("0x06000004");

diff1.VerifyUpdatedTypes("0x02000002", "0x02000003");

using (var md1 = diff1.GetMetadata())
{
// Verify that no new TypeDefs, FieldDefs or MethodDefs were added,
Expand Down Expand Up @@ -3321,6 +3325,8 @@ .locals init (int V_0)
ImmutableArray.Create(
SemanticEdit.Create(SemanticEditKind.Update, f0, f1, GetSyntaxMapFromMarkers(source0, source1), preserveLocalVariables: true)));

diff1.VerifyUpdatedTypes("0x02000003", "0x02000004");

diff1.VerifySynthesizedMembers(
"C: {<F>d__0}",
"C.<F>d__0: {<>1__state, <>2__current, <>l__initialThreadId, <>4__this, <x>5__1, System.IDisposable.Dispose, MoveNext, System.Collections.Generic.IEnumerator<System.Int32>.get_Current, System.Collections.IEnumerator.Reset, System.Collections.IEnumerator.get_Current, System.Collections.Generic.IEnumerable<System.Int32>.GetEnumerator, System.Collections.IEnumerable.GetEnumerator, System.Collections.Generic.IEnumerator<System.Int32>.Current, System.Collections.IEnumerator.Current}",
Expand All @@ -3333,6 +3339,8 @@ .locals init (int V_0)
ImmutableArray.Create(
SemanticEdit.Create(SemanticEditKind.Update, f1, f2, GetSyntaxMapFromMarkers(source1, source2), preserveLocalVariables: true)));

diff2.VerifyUpdatedTypes("0x02000003", "0x02000004");

diff2.VerifySynthesizedMembers(
"C: {<F>d__0}",
"C.<F>d__0: {<>1__state, <>2__current, <>l__initialThreadId, <>4__this, <x>5__1, System.IDisposable.Dispose, MoveNext, System.Collections.Generic.IEnumerator<System.Int32>.get_Current, System.Collections.IEnumerator.Reset, System.Collections.IEnumerator.get_Current, System.Collections.Generic.IEnumerable<System.Int32>.GetEnumerator, System.Collections.IEnumerable.GetEnumerator, System.Collections.Generic.IEnumerator<System.Int32>.Current, System.Collections.IEnumerator.Current}",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -863,6 +863,10 @@ public void AddField()
var reader1 = md1.Reader;
var readers = new[] { reader0, reader1 };

diff1.VerifyUpdatedTypes("0x02000002");

CheckNames(readers, reader0.GetUpdatedTypeDefNames(diff1.EmitResult), "C");

CheckNames(readers, reader1.GetTypeDefNames());
CheckNames(readers, reader1.GetFieldDefNames(), "G");
CheckNames(readers, reader1.GetMethodDefNames(), ".ctor");
Expand Down Expand Up @@ -1615,6 +1619,10 @@ static object F()
var reader1 = md1.Reader;
var readers = new[] { reader0, reader1 };

diff1.VerifyUpdatedTypes("0x02000002", "0x02000003");

CheckNames(readers, reader0.GetUpdatedTypeDefNames(diff1.EmitResult), "A", "B`1");

CheckNames(readers, reader1.GetTypeDefNames(), "C`1");
Assert.Equal(1, reader1.GetTableRowCount(TableIndex.NestedClass));

Expand Down Expand Up @@ -2046,6 +2054,10 @@ interface J { }
var reader1 = md1.Reader;
var readers = new[] { reader0, reader1 };

diff1.VerifyUpdatedTypes("0x02000002");

CheckNames(readers, reader0.GetUpdatedTypeDefNames(diff1.EmitResult), "I");

CheckNames(readers, reader1.GetTypeDefNames(), "J");
CheckNames(readers, reader1.GetFieldDefNames(), "X", "Y");
CheckNames(readers, reader1.GetMethodDefNames(), "add_Y", "remove_Y", "M", "N", "get_P", "set_P", "get_Q", "set_Q", "add_E", "remove_E", "add_F", "remove_F", ".cctor");
Expand All @@ -2071,6 +2083,10 @@ interface J { }
var reader2 = md2.Reader;
readers = new[] { reader0, reader1, reader2 };

diff2.VerifyUpdatedTypes("0x02000002");

CheckNames(readers, reader0.GetUpdatedTypeDefNames(diff1.EmitResult), "I");

CheckNames(readers, reader2.GetTypeDefNames());
CheckNames(readers, reader2.GetFieldDefNames(), "X");
CheckNames(readers, reader2.GetMethodDefNames(), "M", "N", "get_P", "set_P", "get_Q", "set_Q", "add_E", "remove_E", "add_F", "remove_F", ".cctor");
Expand Down Expand Up @@ -2207,6 +2223,10 @@ [B] static void M2<[A]T>() { }
var reader1 = md1.Reader;
var readers = new[] { reader0, reader1 };

diff1.VerifyUpdatedTypes("0x02000004");

CheckNames(readers, reader0.GetUpdatedTypeDefNames(diff1.EmitResult), "C");

CheckNames(readers, reader1.GetTypeDefNames());
CheckNames(readers, reader1.GetMethodDefNames(), "M2", "get_P2", "add_E2", "remove_E2");

Expand Down Expand Up @@ -2352,6 +2372,11 @@ static void M()
// Verify delta metadata contains expected rows.
using var md1 = diff1.GetMetadata();
var readers = new[] { reader0, md1.Reader };

diff1.VerifyUpdatedTypes("0x02000002");

CheckNames(readers, reader0.GetUpdatedTypeDefNames(diff1.EmitResult), "C");

CheckNames(readers, md1.Reader.GetTypeDefNames());
CheckNames(readers, md1.Reader.GetMethodDefNames(), "M");
CheckEncLog(md1.Reader,
Expand Down Expand Up @@ -2422,6 +2447,11 @@ void M()
using var md1 = diff1.GetMetadata();
var reader1 = md1.Reader;
var readers = new[] { reader0, reader1 };

diff1.VerifyUpdatedTypes("0x02000003");

CheckNames(readers, reader0.GetUpdatedTypeDefNames(diff1.EmitResult), "C");

CheckNames(readers, reader1.GetTypeDefNames());
CheckNames(readers, reader1.GetEventDefNames());
CheckNames(readers, reader1.GetFieldDefNames());
Expand Down Expand Up @@ -2453,6 +2483,8 @@ static void M()

var testData0 = new CompilationTestData();
var bytes0 = compilation0.EmitToArray(testData: testData0);
using var md0 = ModuleMetadata.CreateFromImage(bytes0);
var reader0 = md0.MetadataReader;

var generation0 = EmitBaseline.CreateInitialBaseline(
ModuleMetadata.CreateFromImage(bytes0),
Expand All @@ -2465,6 +2497,10 @@ static void M()
var md1 = diff1.GetMetadata();
var reader1 = md1.Reader;

diff1.VerifyUpdatedTypes("0x02000002");

CheckNames(reader0, reader0.GetUpdatedTypeDefNames(diff1.EmitResult), "C");

CheckEncLog(reader1,
Row(2, TableIndex.AssemblyRef, EditAndContinueOperation.Default),
Row(12, TableIndex.TypeRef, EditAndContinueOperation.Default),
Expand Down Expand Up @@ -2554,11 +2590,17 @@ class C
var compilation1 = compilation0.WithSource(source1);
var bytes0 = compilation0.EmitToArray();
var generation0 = EmitBaseline.CreateInitialBaseline(ModuleMetadata.CreateFromImage(bytes0), EmptyLocalsProvider);
using var md0 = ModuleMetadata.CreateFromImage(bytes0);

var diff1 = compilation1.EmitDifference(
generation0,
ImmutableArray.Create(SemanticEdit.Create(SemanticEditKind.Insert, null, compilation1.GetMember<MethodSymbol>("C.puts"))));

diff1.VerifyUpdatedTypes("0x02000002");

var reader0 = md0.MetadataReader;
CheckNames(reader0, reader0.GetUpdatedTypeDefNames(diff1.EmitResult), "C");

using var md1 = diff1.GetMetadata();
var reader1 = md1.Reader;
CheckEncLog(reader1,
Expand Down Expand Up @@ -6178,7 +6220,7 @@ .locals init ([unchanged] V_0,
IL_0021: br.s IL_0023
-IL_0023: ldloc.s V_6
IL_0025: ret
}", methodToken: diff1.UpdatedMethods.Single());
}", methodToken: diff1.EmitResult.UpdatedMethods.Single());
}

/// <summary>
Expand Down Expand Up @@ -7381,7 +7423,6 @@ static string F()
var method1F = compilation1.GetMember<MethodSymbol>("C.F");

using MemoryStream mdStream = new MemoryStream(), ilStream = new MemoryStream(), pdbStream = new MemoryStream();
var updatedMethods = new List<MethodDefinitionHandle>();
var isAddedSymbol = new Func<ISymbol, bool>(s => false);

var badStream = new BrokenStream();
Expand All @@ -7394,7 +7435,6 @@ static string F()
badStream,
ilStream,
pdbStream,
updatedMethods,
new CompilationTestData(),
default);
Assert.False(result.Success);
Expand All @@ -7410,7 +7450,6 @@ static string F()
mdStream,
badStream,
pdbStream,
updatedMethods,
new CompilationTestData(),
default);
Assert.False(result.Success);
Expand All @@ -7426,7 +7465,6 @@ static string F()
mdStream,
ilStream,
badStream,
updatedMethods,
new CompilationTestData(),
default);
Assert.False(result.Success);
Expand Down Expand Up @@ -7470,7 +7508,6 @@ static string F()
var method1F = compilation1.GetMember<MethodSymbol>("C.F");

using MemoryStream mdStream = new MemoryStream(), ilStream = new MemoryStream(), pdbStream = new MemoryStream();
var updatedMethods = new List<MethodDefinitionHandle>();
var isAddedSymbol = new Func<ISymbol, bool>(s => false);

var badStream = new BrokenStream();
Expand All @@ -7483,7 +7520,6 @@ static string F()
mdStream,
ilStream,
badStream,
updatedMethods,
new CompilationTestData(),
default);
Assert.False(result.Success);
Expand Down Expand Up @@ -9380,6 +9416,11 @@ .maxstack 2
ImmutableArray.Create(
SemanticEdit.Create(SemanticEditKind.Update, ctor0, ctor1, GetSyntaxMapFromMarkers(source0, source1), preserveLocalVariables: true)));

diff1.VerifyUpdatedTypes("0x02000002", "0x02000004");

var reader0 = md0.MetadataReader;
CheckNames(reader0, reader0.GetUpdatedTypeDefNames(diff1.EmitResult), "C", "<>c__DisplayClass0_0");

diff1.VerifySynthesizedMembers(
"C: {<>c__DisplayClass0_0}",
"C.<>c__DisplayClass0_0: {x, <.ctor>b__0}");
Expand Down Expand Up @@ -9423,6 +9464,10 @@ .maxstack 2
ImmutableArray.Create(
SemanticEdit.Create(SemanticEditKind.Update, ctor1, ctor2, GetSyntaxMapFromMarkers(source1, source0), preserveLocalVariables: true)));

diff2.VerifyUpdatedTypes("0x02000002", "0x02000004");

CheckNames(reader0, reader0.GetUpdatedTypeDefNames(diff2.EmitResult), "C", "<>c__DisplayClass0_0");

diff2.VerifySynthesizedMembers(
"C: {<>c__DisplayClass0_0}",
"C.<>c__DisplayClass0_0: {x, <.ctor>b__0}");
Expand Down
Loading

0 comments on commit c1d0a97

Please sign in to comment.