Skip to content

Commit

Permalink
Implement support for captured Primary Constructor parameters in EE w…
Browse files Browse the repository at this point in the history
…hen portable PDB is used. (#67266)

Closes #67107
Closes #67103
  • Loading branch information
AlekseyTs authored Mar 15, 2023
1 parent e8fb5b3 commit b4f1246
Show file tree
Hide file tree
Showing 24 changed files with 5,647 additions and 265 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -77,12 +77,10 @@ public static IReadOnlyDictionary<ParameterSymbol, FieldSymbol> GetCapturedParam

foreach (var parameter in captured)
{
// https://github.com/dotnet/roslyn/issues/67103: Figure out naming strategy
// Some thoughts/recommendations from Tomas:
// We should define GeneratedNameKind entry (e..g P looks free to use) and add a helper to GeneratedNames that produces the name.
// I'd also just keep the name as short as possible to avoid unnecessary metadata bloat. Could be just <name>P.
string name = "<" + parameter.Name + ">PC__BackingField";
result.Add(parameter, new SynthesizedPrimaryConstructorParameterBackingFieldSymbol(parameter, name, isReadOnly: containingType.IsReadOnly));
result.Add(parameter,
new SynthesizedPrimaryConstructorParameterBackingFieldSymbol(parameter,
GeneratedNames.MakePrimaryConstructorParameterFieldName(parameter.Name),
isReadOnly: containingType.IsReadOnly));
}

captured.Free();
Expand Down
3 changes: 2 additions & 1 deletion src/Compilers/CSharp/Portable/Compiler/MethodCompiler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1667,7 +1667,8 @@ private static MethodBody GenerateMethodBody(
stateMachineAwaiterSlots,
StateMachineStatesDebugInfo.Create(variableSlotAllocatorOpt, stateMachineStateDebugInfos),
moveNextBodyDebugInfoOpt,
codeCoverageSpans);
codeCoverageSpans,
isPrimaryConstructor: method is SynthesizedPrimaryConstructor);
}
finally
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ internal enum GeneratedNameKind
AsyncBuilderField = 't',
DelegateCacheContainerType = 'O',
DynamicCallSiteContainerType = 'o',
PrimaryConstructorParameter = 'P',
DynamicCallSiteField = 'p',
AsyncIteratorPromiseOfValueOrEndBackingField = 'v',
DisposeModeField = 'w',
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
// See the LICENSE file in the project root for more information.

using System;
using System.Diagnostics;
using System.Diagnostics.CodeAnalysis;
using System.Globalization;
using System.Text.RegularExpressions;
Expand Down Expand Up @@ -173,6 +174,21 @@ internal static bool TryParseAnonymousTypeParameterName(string typeParameterName
return false;
}

internal static bool TryParsePrimaryConstructorParameterFieldName(string fieldName, [NotNullWhen(true)] out string? parameterName)
{
Debug.Assert((char)GeneratedNameKind.PrimaryConstructorParameter == 'P');

if (fieldName.StartsWith("<", StringComparison.Ordinal) &&
fieldName.EndsWith(">P", StringComparison.Ordinal))
{
parameterName = fieldName.Substring(1, fieldName.Length - 3);
return true;
}

parameterName = null;
return false;
}

private const int sha256LengthBytes = 32;
private const int sha256LengthHexChars = sha256LengthBytes * 2;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,12 @@ internal static string MakeBackingFieldName(string propertyName)
return "<" + propertyName + ">k__BackingField";
}

internal static string MakePrimaryConstructorParameterFieldName(string parameterName)
{
Debug.Assert((char)GeneratedNameKind.PrimaryConstructorParameter == 'P');
return "<" + parameterName + ">P";
}

internal static string MakeIteratorFinallyMethodName(StateMachineState finalizeState)
{
Debug.Assert((int)finalizeState < -2);
Expand Down
Loading

0 comments on commit b4f1246

Please sign in to comment.