Skip to content

Commit

Permalink
Address feedback & do misc clean up
Browse files Browse the repository at this point in the history
  • Loading branch information
layomia committed Sep 25, 2023
1 parent 1b11a80 commit 10f2579
Show file tree
Hide file tree
Showing 15 changed files with 174 additions and 216 deletions.
2 changes: 2 additions & 0 deletions src/libraries/Common/src/Roslyn/DiagnosticDescriptorHelper.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.

using Microsoft.CodeAnalysis;

namespace Microsoft.CodeAnalysis.DotnetRuntime.Extensions
{
internal static partial class DiagnosticDescriptorHelper
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -75,9 +75,6 @@ public bool MoveNext()

internal static class ImmutableEquatableArray
{
public static ImmutableEquatableArray<T> Empty<T>() where T : IEquatable<T>
=> ImmutableEquatableArray<T>.Empty;

public static ImmutableEquatableArray<T> ToImmutableEquatableArray<T>(this IEnumerable<T> values) where T : IEquatable<T>
=> new(values);
}
Expand Down
55 changes: 0 additions & 55 deletions src/libraries/Common/src/SourceGenerators/TypeModelHelper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -36,60 +36,5 @@ void TraverseContainingTypes(INamedTypeSymbol current)
}

public static string GetFullyQualifiedName(this ITypeSymbol type) => type.ToDisplayString(SymbolDisplayFormat.FullyQualifiedFormat);

/// <summary>
/// Removes any type metadata that is erased at compile time, such as NRT annotations and tuple labels.
/// </summary>
public static ITypeSymbol EraseCompileTimeMetadata(this Compilation compilation, ITypeSymbol type)
{
if (type.NullableAnnotation is NullableAnnotation.Annotated)
{
type = type.WithNullableAnnotation(NullableAnnotation.None);
}

if (type is INamedTypeSymbol namedType)
{
if (namedType.IsTupleType)
{
if (namedType.TupleElements.Length < 2)
{
return type;
}

ImmutableArray<ITypeSymbol> erasedElements = namedType.TupleElements
.Select(e => compilation.EraseCompileTimeMetadata(e.Type))
.ToImmutableArray();

type = compilation.CreateTupleTypeSymbol(erasedElements);
}
else if (namedType.IsGenericType)
{
if (namedType.IsUnboundGenericType)
{
return namedType;
}

ImmutableArray<ITypeSymbol> typeArguments = namedType.TypeArguments;
INamedTypeSymbol? containingType = namedType.ContainingType;

if (containingType?.IsGenericType == true)
{
containingType = (INamedTypeSymbol)compilation.EraseCompileTimeMetadata(containingType);
type = namedType = containingType.GetTypeMembers().First(t => t.Name == namedType.Name && t.Arity == namedType.Arity);
}

if (typeArguments.Length > 0)
{
ITypeSymbol[] erasedTypeArgs = typeArguments
.Select(compilation.EraseCompileTimeMetadata)
.ToArray();

type = namedType.ConstructedFrom.Construct(erasedTypeArgs);
}
}
}

return type;
}
}
}
Original file line number Diff line number Diff line change
@@ -1,8 +1,4 @@

Microsoft Visual Studio Solution File, Format Version 12.00
# Visual Studio Version 17
VisualStudioVersion = 17.8.34004.107
MinimumVisualStudioVersion = 10.0.40219.1
Microsoft Visual Studio Solution File, Format Version 12.00
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "TestUtilities", "..\Common\tests\TestUtilities\TestUtilities.csproj", "{5FB89358-3575-45AA-ACFE-EF3598B9AB7E}"
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Microsoft.Bcl.AsyncInterfaces", "..\Microsoft.Bcl.AsyncInterfaces\ref\Microsoft.Bcl.AsyncInterfaces.csproj", "{64CB04AC-E5B9-4FB8-A1AF-636A6DF23829}"
Expand Down Expand Up @@ -115,11 +111,11 @@ Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "src", "src", "{94EEF122-C30
EndProject
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "gen", "gen", "{CC3961B0-C62D-44B9-91DB-11D94A3F91A5}"
EndProject
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "gen", "gen", "{04FEFFC9-5F1D-47CA-885C-3D7F9643D63D}"
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "gen", "tools\gen", "{04FEFFC9-5F1D-47CA-885C-3D7F9643D63D}"
EndProject
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "src", "src", "{948EC8B5-A259-4D8C-B911-EDE19F5BAE40}"
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "src", "tools\src", "{948EC8B5-A259-4D8C-B911-EDE19F5BAE40}"
EndProject
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "ref", "ref", "{8F2204E7-8124-4F7C-B663-8731C7959001}"
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "ref", "tools\ref", "{8F2204E7-8124-4F7C-B663-8731C7959001}"
EndProject
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "tools", "tools", "{B343C975-AC74-4843-899C-623B6E7A6321}"
EndProject
Expand Down Expand Up @@ -343,66 +339,62 @@ Global
EndGlobalSection
GlobalSection(NestedProjects) = preSolution
{5FB89358-3575-45AA-ACFE-EF3598B9AB7E} = {AAE738F3-89AC-4406-B1D9-A61A2C3A1CF0}
{56F4D38E-41A0-45E2-9F04-9E670D002E71} = {AAE738F3-89AC-4406-B1D9-A61A2C3A1CF0}
{75BA0154-A24D-421E-9046-C7949DF12A55} = {AAE738F3-89AC-4406-B1D9-A61A2C3A1CF0}
{64CB04AC-E5B9-4FB8-A1AF-636A6DF23829} = {F43534CA-C419-405E-B239-CDE2BDC703BE}
{3E443131-1C04-48F8-9635-99ABD9DA0E6A} = {94EEF122-C307-4BF0-88FE-263B89B59F9F}
{CB09105A-F475-4A91-8836-434FA175F4F9} = {F43534CA-C419-405E-B239-CDE2BDC703BE}
{958E5946-FD21-4167-B471-8CEC2CB8D75D} = {94EEF122-C307-4BF0-88FE-263B89B59F9F}
{D4B3EEA1-7394-49EA-A088-897C0CD26D11} = {CC3961B0-C62D-44B9-91DB-11D94A3F91A5}
{F05212B2-FD66-4E8E-AEA0-FAC06A0D6808} = {F43534CA-C419-405E-B239-CDE2BDC703BE}
{8C7443D8-864A-4DAE-8835-108580C289F5} = {94EEF122-C307-4BF0-88FE-263B89B59F9F}
{56F4D38E-41A0-45E2-9F04-9E670D002E71} = {AAE738F3-89AC-4406-B1D9-A61A2C3A1CF0}
{75BA0154-A24D-421E-9046-C7949DF12A55} = {AAE738F3-89AC-4406-B1D9-A61A2C3A1CF0}
{CB5C87BF-7E0D-4BAC-86BB-06FFC7D1CEE1} = {F43534CA-C419-405E-B239-CDE2BDC703BE}
{C5073E23-D3CD-43D3-9AE3-87E570B1B930} = {94EEF122-C307-4BF0-88FE-263B89B59F9F}
{A519F26A-3187-40C3-BBDC-FD22EE1FC47B} = {F43534CA-C419-405E-B239-CDE2BDC703BE}
{45DB88E7-487F-49F2-A309-64315FB218B8} = {94EEF122-C307-4BF0-88FE-263B89B59F9F}
{39D99379-E744-4295-9CA8-B5C6DE286EA0} = {F43534CA-C419-405E-B239-CDE2BDC703BE}
{5177A566-05AF-4DF0-93CC-D2876F7E6EBB} = {94EEF122-C307-4BF0-88FE-263B89B59F9F}
{CB1C57BA-64C0-4626-A156-4E89A1995C31} = {F43534CA-C419-405E-B239-CDE2BDC703BE}
{56C28B9C-C3FA-4A22-A304-1549F681CE7F} = {94EEF122-C307-4BF0-88FE-263B89B59F9F}
{A9D0ED56-4B34-4010-A5C3-4E36EE8E9FDF} = {F43534CA-C419-405E-B239-CDE2BDC703BE}
{7F71CC3D-D4DA-42C4-8470-72C22806FD89} = {94EEF122-C307-4BF0-88FE-263B89B59F9F}
{42DAD2C1-0FD4-442E-B218-9CDACE6EEB01} = {F43534CA-C419-405E-B239-CDE2BDC703BE}
{091844B8-A79E-4DF4-9A73-B2F01968E982} = {94EEF122-C307-4BF0-88FE-263B89B59F9F}
{A86A8B8E-CC45-46F4-BB95-29410AA7B6C2} = {F43534CA-C419-405E-B239-CDE2BDC703BE}
{7F2A8B79-8E07-4D70-A7A3-144E71647110} = {94EEF122-C307-4BF0-88FE-263B89B59F9F}
{98760FB8-A548-4BEE-914B-4D5D0D13FA09} = {F43534CA-C419-405E-B239-CDE2BDC703BE}
{6C8B4068-73D3-47A0-B9A7-840475175FC6} = {94EEF122-C307-4BF0-88FE-263B89B59F9F}
{FB70235B-6F82-4495-9011-183E65C78CCF} = {F43534CA-C419-405E-B239-CDE2BDC703BE}
{856C015E-982A-4834-88F7-3F8FFD33981C} = {94EEF122-C307-4BF0-88FE-263B89B59F9F}
{F132C649-6660-4583-8AD1-36D38711A994} = {CC3961B0-C62D-44B9-91DB-11D94A3F91A5}
{2D5B1B6B-1557-4A14-BC00-C36A21BC97C4} = {F43534CA-C419-405E-B239-CDE2BDC703BE}
{78D796C3-227B-4648-9BCB-2A000852B2F7} = {94EEF122-C307-4BF0-88FE-263B89B59F9F}
{3C288A6A-17C9-40A9-A1AA-E0FC3927DFE3} = {F43534CA-C419-405E-B239-CDE2BDC703BE}
{9B21B87F-084B-411B-A513-C22B5B961BF3} = {94EEF122-C307-4BF0-88FE-263B89B59F9F}
{9EB52984-66B5-40C4-8BE7-D75CA612948F} = {F43534CA-C419-405E-B239-CDE2BDC703BE}
{4A94F874-A405-4B2E-912B-81887987103B} = {F43534CA-C419-405E-B239-CDE2BDC703BE}
{88A22638-3937-4AD2-965B-0A69B616F41F} = {F43534CA-C419-405E-B239-CDE2BDC703BE}
{9E2C43CF-2E01-4570-8C02-F678607DDAFF} = {CC3961B0-C62D-44B9-91DB-11D94A3F91A5}
{05B7F752-4991-4DC8-9B06-8269211E7817} = {CC3961B0-C62D-44B9-91DB-11D94A3F91A5}
{6E58AF2F-AB35-4279-9135-67E97BCE1432} = {CC3961B0-C62D-44B9-91DB-11D94A3F91A5}
{ACD597F0-0A1A-476A-8AE5-2FB6A0039ACD} = {F43534CA-C419-405E-B239-CDE2BDC703BE}
{71F7FC59-D0B0-459C-9AFB-0A41B7B73E4C} = {F43534CA-C419-405E-B239-CDE2BDC703BE}
{DA0EC3F6-1D31-41DA-B2A5-CEC8D32E1023} = {F43534CA-C419-405E-B239-CDE2BDC703BE}
{3E443131-1C04-48F8-9635-99ABD9DA0E6A} = {94EEF122-C307-4BF0-88FE-263B89B59F9F}
{958E5946-FD21-4167-B471-8CEC2CB8D75D} = {94EEF122-C307-4BF0-88FE-263B89B59F9F}
{8C7443D8-864A-4DAE-8835-108580C289F5} = {94EEF122-C307-4BF0-88FE-263B89B59F9F}
{C5073E23-D3CD-43D3-9AE3-87E570B1B930} = {94EEF122-C307-4BF0-88FE-263B89B59F9F}
{45DB88E7-487F-49F2-A309-64315FB218B8} = {94EEF122-C307-4BF0-88FE-263B89B59F9F}
{5177A566-05AF-4DF0-93CC-D2876F7E6EBB} = {94EEF122-C307-4BF0-88FE-263B89B59F9F}
{56C28B9C-C3FA-4A22-A304-1549F681CE7F} = {94EEF122-C307-4BF0-88FE-263B89B59F9F}
{7F71CC3D-D4DA-42C4-8470-72C22806FD89} = {94EEF122-C307-4BF0-88FE-263B89B59F9F}
{091844B8-A79E-4DF4-9A73-B2F01968E982} = {94EEF122-C307-4BF0-88FE-263B89B59F9F}
{7F2A8B79-8E07-4D70-A7A3-144E71647110} = {94EEF122-C307-4BF0-88FE-263B89B59F9F}
{6C8B4068-73D3-47A0-B9A7-840475175FC6} = {94EEF122-C307-4BF0-88FE-263B89B59F9F}
{856C015E-982A-4834-88F7-3F8FFD33981C} = {94EEF122-C307-4BF0-88FE-263B89B59F9F}
{78D796C3-227B-4648-9BCB-2A000852B2F7} = {94EEF122-C307-4BF0-88FE-263B89B59F9F}
{9B21B87F-084B-411B-A513-C22B5B961BF3} = {94EEF122-C307-4BF0-88FE-263B89B59F9F}
{5DF5B4A7-D78A-400E-A49E-54F33CA51006} = {94EEF122-C307-4BF0-88FE-263B89B59F9F}
{94C9AA55-C6AD-41CC-B6C8-9186C27A6FFE} = {94EEF122-C307-4BF0-88FE-263B89B59F9F}
{D4B3EEA1-7394-49EA-A088-897C0CD26D11} = {CC3961B0-C62D-44B9-91DB-11D94A3F91A5}
{F132C649-6660-4583-8AD1-36D38711A994} = {CC3961B0-C62D-44B9-91DB-11D94A3F91A5}
{9E2C43CF-2E01-4570-8C02-F678607DDAFF} = {CC3961B0-C62D-44B9-91DB-11D94A3F91A5}
{05B7F752-4991-4DC8-9B06-8269211E7817} = {CC3961B0-C62D-44B9-91DB-11D94A3F91A5}
{6E58AF2F-AB35-4279-9135-67E97BCE1432} = {CC3961B0-C62D-44B9-91DB-11D94A3F91A5}
{D2FE6BEA-70F5-4AE3-8D29-DB6568A9DE49} = {CC3961B0-C62D-44B9-91DB-11D94A3F91A5}
{47E22F0C-5F36-450B-8020-B83B36936597} = {CC3961B0-C62D-44B9-91DB-11D94A3F91A5}
{4A2E7449-5480-4872-91B3-B5A479D6A787} = {CC3961B0-C62D-44B9-91DB-11D94A3F91A5}
{DA0EC3F6-1D31-41DA-B2A5-CEC8D32E1023} = {F43534CA-C419-405E-B239-CDE2BDC703BE}
{94C9AA55-C6AD-41CC-B6C8-9186C27A6FFE} = {94EEF122-C307-4BF0-88FE-263B89B59F9F}
{AA7E297B-0BD9-4E6B-96FE-BC69FC65DB64} = {04FEFFC9-5F1D-47CA-885C-3D7F9643D63D}
{D187AB48-A05A-4C8C-900D-7F2A3D87769A} = {04FEFFC9-5F1D-47CA-885C-3D7F9643D63D}
{04FEFFC9-5F1D-47CA-885C-3D7F9643D63D} = {B343C975-AC74-4843-899C-623B6E7A6321}
{44059045-A065-4123-AF8C-107D06782422} = {948EC8B5-A259-4D8C-B911-EDE19F5BAE40}
{EDE541FC-0F9D-4D3E-A513-7E74788B8C0B} = {948EC8B5-A259-4D8C-B911-EDE19F5BAE40}
{10106702-31E6-4D1A-BDE0-BB8F9F6C258D} = {8F2204E7-8124-4F7C-B663-8731C7959001}
{04FEFFC9-5F1D-47CA-885C-3D7F9643D63D} = {B343C975-AC74-4843-899C-623B6E7A6321}
{948EC8B5-A259-4D8C-B911-EDE19F5BAE40} = {B343C975-AC74-4843-899C-623B6E7A6321}
{10106702-31E6-4D1A-BDE0-BB8F9F6C258D} = {8F2204E7-8124-4F7C-B663-8731C7959001}
{8F2204E7-8124-4F7C-B663-8731C7959001} = {B343C975-AC74-4843-899C-623B6E7A6321}
EndGlobalSection
GlobalSection(ExtensibilityGlobals) = postSolution
SolutionGuid = {A97DC4BF-32F0-46E8-B91C-84D1E7F2A27E}
EndGlobalSection
GlobalSection(SharedMSBuildProjectFiles) = preSolution
..\..\tools\illink\src\ILLink.Shared\ILLink.Shared.projitems*{d187ab48-a05a-4c8c-900d-7f2a3d87769a}*SharedItemsImports = 5
..\..\tools\illink\src\ILLink.Shared\ILLink.Shared.projitems*{ede541fc-0f9d-4d3e-a513-7e74788b8c0b}*SharedItemsImports = 5
EndGlobalSection
EndGlobal
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,21 @@ internal sealed partial class Parser(CompilationData compilationData)
};
}

private bool IsValidRootConfigType([NotNullWhen(true)] ITypeSymbol? type)
{
if (type is null ||
type.SpecialType is SpecialType.System_Object or SpecialType.System_Void ||
!_typeSymbols.Compilation.IsSymbolAccessibleWithin(type, _typeSymbols.Compilation.Assembly) ||
type.TypeKind is TypeKind.TypeParameter or TypeKind.Pointer or TypeKind.Error ||
type.IsRefLikeType ||
ContainsGenericParameters(type))
{
return false;
}

return true;
}

private void ParseInvocations(ImmutableArray<BinderInvocation?> invocations)
{
foreach (BinderInvocation? invocation in invocations)
Expand Down Expand Up @@ -120,29 +135,23 @@ private void RegisterInterceptors()
}
}

private void EnqueueTargetTypeForRootInvocation(ITypeSymbol? typeSymbol, MethodsToGen bindingOverload, BinderInvocation binderInvocation)
private void EnqueueTargetTypeForRootInvocation(ITypeSymbol? typeSymbol, MethodsToGen overload, BinderInvocation invocation)
{
if (!IsValidRootConfigType(typeSymbol))
{
RecordDiagnostic(DiagnosticDescriptors.CouldNotDetermineTypeInfo, binderInvocation.Location);
RecordDiagnostic(DiagnosticDescriptors.CouldNotDetermineTypeInfo, invocation.Location);
}
else
{
TypeParseInfo typeParseInfo = new TypeParseInfo(typeSymbol, _typeSymbols.Compilation)
{
BindingOverload = bindingOverload,
BinderInvocation = binderInvocation,
ContainingTypeDiagnosticInfo = null,
};

TypeParseInfo typeParseInfo = TypeParseInfo.Create(typeSymbol, overload, invocation, containingTypeDiagInfo: null);
_typesToParse.Enqueue(typeParseInfo);
_invocationTypeParseInfo.Add(typeParseInfo);
}
}

private TypeRef EnqueueTransitiveType(TypeParseInfo containingTypeParseInfo, ITypeSymbol memberTypeSymbol, DiagnosticDescriptor diagDescriptor, string? memberName = null)
{
TypeParseInfo memberTypeParseInfo = containingTypeParseInfo.ToTransitiveTypeParseInfo(memberTypeSymbol, _typeSymbols.Compilation, diagDescriptor, memberName);
TypeParseInfo memberTypeParseInfo = containingTypeParseInfo.ToTransitiveTypeParseInfo(memberTypeSymbol, diagDescriptor, memberName);

if (_createdTypeSpecs.TryGetValue(memberTypeSymbol, out TypeSpec? memberTypeSpec))
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,6 @@
<Compile Include="Parser\ConfigurationBinder.cs" />
<Compile Include="Parser\Extensions.cs" />
<Compile Include="Parser\DiagnosticDescriptors.cs" />
<Compile Include="Parser\Helpers.cs" />
<Compile Include="Parser\KnownTypeSymbols.cs" />
<Compile Include="Parser\OptionsBuilderConfigurationExtensions.cs" />
<Compile Include="Parser\OptionsConfigurationServiceCollectionExtensions.cs" />
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,52 @@

namespace Microsoft.Extensions.Configuration.Binder.SourceGeneration
{
public sealed partial class ConfigurationBindingGenerator
{
internal sealed partial class Parser
{
private readonly struct TypeParseInfo
{
public ITypeSymbol TypeSymbol { get; private init; }
public string TypeName { get; private init; }
public MethodsToGen BindingOverload { get; private init; }
public BinderInvocation BinderInvocation { get; private init; }
public ContainingTypeDiagnosticInfo? ContainingTypeDiagnosticInfo { get; private init; }

public static TypeParseInfo Create(ITypeSymbol typeSymbol, MethodsToGen overload, BinderInvocation invocation, ContainingTypeDiagnosticInfo? containingTypeDiagInfo = null) =>
new TypeParseInfo
{
TypeSymbol = typeSymbol,
TypeName = typeSymbol.GetName(),
BindingOverload = overload,
BinderInvocation = invocation,
ContainingTypeDiagnosticInfo = containingTypeDiagInfo,
};

public TypeParseInfo ToTransitiveTypeParseInfo(ITypeSymbol memberType, DiagnosticDescriptor? diagDescriptor = null, string? memberName = null)
{
ContainingTypeDiagnosticInfo? diagnosticInfo = diagDescriptor is null
? null
: new()
{
TypeName = TypeName,
Descriptor = diagDescriptor,
MemberName = memberName,
};

return Create(memberType, BindingOverload, BinderInvocation, diagnosticInfo);
}
}

private readonly struct ContainingTypeDiagnosticInfo
{
public required string TypeName { get; init; }
public required string? MemberName { get; init; }
public required DiagnosticDescriptor Descriptor { get; init; }
}
}
}

internal static class ParserExtensions
{
private static readonly SymbolDisplayFormat s_identifierCompatibleFormat = new SymbolDisplayFormat(
Expand Down Expand Up @@ -67,8 +113,7 @@ public static string ToIdentifierCompatibleSubstring(this ITypeSymbol type)

public static (string? Namespace, string DisplayString, string Name) GetTypeName(this ITypeSymbol type)
{
string? @namespace = type.ContainingNamespace?.ToDisplayString();
@namespace = @namespace is not "<global namespace>" ? @namespace : null;
string? @namespace = type.ContainingNamespace is { IsGlobalNamespace: false } containingNamespace ? containingNamespace.ToDisplayString() : null;
string displayString = type.ToDisplayString(s_minimalDisplayFormat);
string name = (@namespace is null ? string.Empty : @namespace + ".") + displayString.Replace(".", "+");
return (@namespace, displayString, name);
Expand Down
Loading

0 comments on commit 10f2579

Please sign in to comment.