Skip to content

Commit 8f178b1

Browse files
committed
Move generating deleted method bodies before freezing PrivateImplementationDetails.
Fix tests. Improve IL/field data validation in EnC tests
1 parent 498130d commit 8f178b1

File tree

17 files changed

+583
-186
lines changed

17 files changed

+583
-186
lines changed

src/Compilers/CSharp/Portable/Compiler/MethodCompiler.cs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -183,6 +183,11 @@ public static void CompileMethodBodies(
183183

184184
methodCompiler.WaitForWorkers();
185185

186+
// Deleted definitions must be emitted before PrivateImplementationDetails are frozen since
187+
// it may add new members to it. All changes to PrivateImplementationDetails are additions,
188+
// so we don't need to create deleted method defs for those.
189+
moduleBeingBuiltOpt.CreateDeletedMethodDefinitions(diagnostics.DiagnosticBag);
190+
186191
// all threads that were adding methods must be finished now, we can freeze the class:
187192
var privateImplClass = moduleBeingBuiltOpt.FreezePrivateImplementationDetails();
188193
if (privateImplClass != null)

src/Compilers/CSharp/Portable/Emitter/EditAndContinue/EmitHelpers.cs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -132,7 +132,6 @@ internal static EmitDifferenceResult EmitDifference(
132132
newBaseline = compilation.SerializeToDeltaStreams(
133133
moduleBeingBuilt,
134134
definitionMap,
135-
changes,
136135
metadataStream,
137136
ilStream,
138137
pdbStream,

src/Compilers/CSharp/Portable/Emitter/EditAndContinue/PEDeltaAssemblyBuilder.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -238,7 +238,7 @@ public SynthesizedTypeMaps GetSynthesizedTypes()
238238
}
239239

240240
public override IEnumerable<Cci.INamespaceTypeDefinition> GetTopLevelTypeDefinitions(EmitContext context)
241-
=> GetTopLevelTypeDefinitionsCore(context);
241+
=> GetTopLevelTypeDefinitionsExcludingNoPiaAndRootModule(context, includePrivateImplementationDetails: true);
242242

243243
public override IEnumerable<Cci.INamespaceTypeDefinition> GetTopLevelSourceTypeDefinitions(EmitContext context)
244244
{

src/Compilers/CSharp/Test/Emit/Emit/EmitMetadataTests.cs

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3144,6 +3144,65 @@ .maxstack 1
31443144
Assert.Equal(0, verifier.Compilation.DataSectionStringLiteralThreshold);
31453145
}
31463146

3147+
[Fact]
3148+
public void DataSectionStringLiterals_Switch()
3149+
{
3150+
var source = """
3151+
System.Console.Write(args[0] switch
3152+
{
3153+
"a" => 1,
3154+
"bb" => 2,
3155+
"ccc" => 3,
3156+
_ => 4
3157+
});
3158+
""";
3159+
3160+
var verifier = CompileAndVerify(
3161+
source,
3162+
parseOptions: TestOptions.Regular.WithFeature("experimental-data-section-string-literals", "0"),
3163+
verify: Verification.Skipped);
3164+
3165+
verifier.VerifyIL("<top-level-statements-entry-point>", """
3166+
{
3167+
// Code size 66 (0x42)
3168+
.maxstack 2
3169+
.locals init (int V_0,
3170+
string V_1)
3171+
IL_0000: ldarg.0
3172+
IL_0001: ldc.i4.0
3173+
IL_0002: ldelem.ref
3174+
IL_0003: stloc.1
3175+
IL_0004: ldloc.1
3176+
IL_0005: ldsfld "string <PrivateImplementationDetails>.<S>A96FAF705AF16834E6C632B61E964E1F.s"
3177+
IL_000a: call "bool string.op_Equality(string, string)"
3178+
IL_000f: brtrue.s IL_002d
3179+
IL_0011: ldloc.1
3180+
IL_0012: ldsfld "string <PrivateImplementationDetails>.<S>DB1DE4B3DA6C7871B776D5CB968AA5A4.s"
3181+
IL_0017: call "bool string.op_Equality(string, string)"
3182+
IL_001c: brtrue.s IL_0031
3183+
IL_001e: ldloc.1
3184+
IL_001f: ldsfld "string <PrivateImplementationDetails>.<S>BE20CA004CC2993A396345E0D52DF013.s"
3185+
IL_0024: call "bool string.op_Equality(string, string)"
3186+
IL_0029: brtrue.s IL_0035
3187+
IL_002b: br.s IL_0039
3188+
IL_002d: ldc.i4.1
3189+
IL_002e: stloc.0
3190+
IL_002f: br.s IL_003b
3191+
IL_0031: ldc.i4.2
3192+
IL_0032: stloc.0
3193+
IL_0033: br.s IL_003b
3194+
IL_0035: ldc.i4.3
3195+
IL_0036: stloc.0
3196+
IL_0037: br.s IL_003b
3197+
IL_0039: ldc.i4.4
3198+
IL_003a: stloc.0
3199+
IL_003b: ldloc.0
3200+
IL_003c: call "void System.Console.Write(int)"
3201+
IL_0041: ret
3202+
}
3203+
""");
3204+
}
3205+
31473206
[Fact]
31483207
public void DataSectionStringLiterals_InvalidUtf8()
31493208
{

src/Compilers/CSharp/Test/Emit2/Emit/EditAndContinue/AssemblyReferencesTests.cs

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -83,7 +83,10 @@ class C
8383

8484
compilation1.EmitDifference(baseline, edits, s => false, mdStream, ilStream, pdbStream, EmitDifferenceOptions.Default, CancellationToken.None);
8585

86-
var actualIL = ImmutableArray.Create(ilStream.ToArray()).GetMethodIL();
86+
var il = ImmutableArray.Create(ilStream.ToArray());
87+
using var mdReaderProvider = MetadataReaderProvider.FromMetadataStream(mdStream);
88+
89+
var actualIL = ILValidation.DumpEncDeltaMethodBodies(il, [mdReaderProvider.GetMetadataReader()]);
8790
var expectedIL = @"
8891
{
8992
// Code size 7 (0x7)
@@ -155,7 +158,10 @@ class C
155158

156159
compilation1.EmitDifference(baseline, edits, s => false, mdStream, ilStream, pdbStream, EmitDifferenceOptions.Default, CancellationToken.None);
157160

158-
var actualIL = ImmutableArray.Create(ilStream.ToArray()).GetMethodIL();
161+
var il = ImmutableArray.Create(ilStream.ToArray());
162+
using var mdReaderProvider = MetadataReaderProvider.FromMetadataStream(mdStream);
163+
164+
var actualIL = ILValidation.DumpEncDeltaMethodBodies(il, [mdReaderProvider.GetMetadataReader()]);
159165

160166
// Symbol matcher should ignore overloads with missing type symbols and match
161167
// F(object).

0 commit comments

Comments
 (0)