Skip to content

Commit

Permalink
SourceGen now supports internal duplicate types
Browse files Browse the repository at this point in the history
Fixes #14158
  • Loading branch information
mattleibow committed Mar 25, 2023
1 parent 3859318 commit db74ff7
Show file tree
Hide file tree
Showing 34 changed files with 460 additions and 42 deletions.
14 changes: 14 additions & 0 deletions Microsoft.Maui-dev.sln
Original file line number Diff line number Diff line change
Expand Up @@ -201,6 +201,10 @@ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Graphics.DeviceTests", "src
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Controls.Xaml.UnitTests.ExternalAssembly", "src\Controls\tests\Xaml.UnitTests.ExternalAssembly\Controls.Xaml.UnitTests.ExternalAssembly.csproj", "{B5F38A9E-1949-4779-8739-D682B09E0CB3}"
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Controls.Xaml.UnitTests.InternalsVisibleAssembly", "src\Controls\tests\Xaml.UnitTests.InternalsVisibleAssembly\Controls.Xaml.UnitTests.InternalsVisibleAssembly.csproj", "{B5F38A9E-1949-4779-6666-D682B09E0CB3}"
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Controls.Xaml.UnitTests.InternalsHiddenAssembly", "src\Controls\tests\Xaml.UnitTests.InternalsHiddenAssembly\Controls.Xaml.UnitTests.InternalsHiddenAssembly.csproj", "{FB39FACE-BAF2-4F97-A249-E45BA63D77FE}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Expand Down Expand Up @@ -507,6 +511,14 @@ Global
{B5F38A9E-1949-4779-8739-D682B09E0CB3}.Debug|Any CPU.Build.0 = Debug|Any CPU
{B5F38A9E-1949-4779-8739-D682B09E0CB3}.Release|Any CPU.ActiveCfg = Release|Any CPU
{B5F38A9E-1949-4779-8739-D682B09E0CB3}.Release|Any CPU.Build.0 = Release|Any CPU
{B5F38A9E-1949-4779-6666-D682B09E0CB3}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{B5F38A9E-1949-4779-6666-D682B09E0CB3}.Debug|Any CPU.Build.0 = Debug|Any CPU
{B5F38A9E-1949-4779-6666-D682B09E0CB3}.Release|Any CPU.ActiveCfg = Release|Any CPU
{B5F38A9E-1949-4779-6666-D682B09E0CB3}.Release|Any CPU.Build.0 = Release|Any CPU
{FB39FACE-BAF2-4F97-A249-E45BA63D77FE}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{FB39FACE-BAF2-4F97-A249-E45BA63D77FE}.Debug|Any CPU.Build.0 = Debug|Any CPU
{FB39FACE-BAF2-4F97-A249-E45BA63D77FE}.Release|Any CPU.ActiveCfg = Release|Any CPU
{FB39FACE-BAF2-4F97-A249-E45BA63D77FE}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
Expand Down Expand Up @@ -599,6 +611,8 @@ Global
{66CC98E3-6A1A-4C44-A23C-B575E82106EC} = {C564DDD6-DE79-45CD-88EA-3F690481572A}
{34969E49-FA6E-41BB-9813-5689BB14021E} = {936C47A9-A7EA-4FBD-8733-CED1D4100E69}
{B5F38A9E-1949-4779-8739-D682B09E0CB3} = {25D0D27A-C5FE-443D-8B65-D6C987F4A80E}
{B5F38A9E-1949-4779-6666-D682B09E0CB3} = {25D0D27A-C5FE-443D-8B65-D6C987F4A80E}
{FB39FACE-BAF2-4F97-A249-E45BA63D77FE} = {25D0D27A-C5FE-443D-8B65-D6C987F4A80E}
EndGlobalSection
GlobalSection(ExtensibilityGlobals) = postSolution
SolutionGuid = {0B8ABEAD-D2B5-4370-A187-62B5ABE4EE50}
Expand Down
2 changes: 2 additions & 0 deletions Microsoft.Maui-mac.slnf
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@
"src\\Controls\\tests\\CustomAttributes\\Controls.CustomAttributes.csproj",
"src\\Controls\\tests\\DeviceTests\\Controls.DeviceTests.csproj",
"src\\Controls\\tests\\Xaml.UnitTests.ExternalAssembly\\Controls.Xaml.UnitTests.ExternalAssembly.csproj",
"src\\Controls\\tests\\Xaml.UnitTests.InternalsVisibleAssembly\\Controls.Xaml.UnitTests.InternalsVisibleAssembly.csproj",
"src\\Controls\\tests\\Xaml.UnitTests.InternalsHiddenAssembly\\Controls.Xaml.UnitTests.InternalsHiddenAssembly.csproj",
"src\\Controls\\tests\\Xaml.UnitTests\\Controls.Xaml.UnitTests.csproj",
"src\\Core\\maps\\src\\Maps.csproj",
"src\\Core\\src\\Core.csproj",
Expand Down
2 changes: 2 additions & 0 deletions Microsoft.Maui-windows.slnf
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,8 @@
"src\\Controls\\tests\\CustomAttributes\\Controls.CustomAttributes.csproj",
"src\\Controls\\tests\\DeviceTests\\Controls.DeviceTests.csproj",
"src\\Controls\\tests\\Xaml.UnitTests.ExternalAssembly\\Controls.Xaml.UnitTests.ExternalAssembly.csproj",
"src\\Controls\\tests\\Xaml.UnitTests.InternalsVisibleAssembly\\Controls.Xaml.UnitTests.InternalsVisibleAssembly.csproj",
"src\\Controls\\tests\\Xaml.UnitTests.InternalsHiddenAssembly\\Controls.Xaml.UnitTests.InternalsHiddenAssembly.csproj",
"src\\Controls\\tests\\Xaml.UnitTests\\Controls.Xaml.UnitTests.csproj",
"src\\Core\\maps\\src\\Maps.csproj",
"src\\Core\\src\\Core.csproj",
Expand Down
2 changes: 1 addition & 1 deletion Microsoft.Maui.BuildTasks.slnf
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
"src\\Controls\\src\\Build.Tasks\\Controls.Build.Tasks.csproj",
"src\\Controls\\src\\Core.Design\\Controls.Core.Design.csproj",
"src\\Controls\\src\\Core\\Controls.Core.csproj",
"src\\Controls\\src\\NuGet\\Controls.NuGet.csproj",
"src\\Controls\\src\\SourceGen\\Controls.SourceGen.csproj",
"src\\Controls\\src\\Xaml.Design\\Controls.Xaml.Design.csproj",
"src\\Controls\\src\\Xaml\\Controls.Xaml.csproj",
"src\\Core\\src\\Core.csproj",
Expand Down
14 changes: 14 additions & 0 deletions Microsoft.Maui.sln
Original file line number Diff line number Diff line change
Expand Up @@ -249,8 +249,12 @@ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Compatibility.Core.UnitTest
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Controls.Xaml.UnitTests.ExternalAssembly", "src\Controls\tests\Xaml.UnitTests.ExternalAssembly\Controls.Xaml.UnitTests.ExternalAssembly.csproj", "{F2ADA552-6328-4B2D-8D48-FCDD32C4AF60}"
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Controls.Xaml.UnitTests.InternalsVisibleAssembly", "src\Controls\tests\Xaml.UnitTests.InternalsVisibleAssembly\Controls.Xaml.UnitTests.InternalsVisibleAssembly.csproj", "{B5F38A9E-1949-4779-6666-D682B09E0CB3}"
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Microsoft.Maui.IntegrationTests", "src\TestUtils\src\Microsoft.Maui.IntegrationTests\Microsoft.Maui.IntegrationTests.csproj", "{1C899C94-8744-4E2A-8E23-1660393236FD}"
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Controls.Xaml.UnitTests.InternalsHiddenAssembly", "src\Controls\tests\Xaml.UnitTests.InternalsHiddenAssembly\Controls.Xaml.UnitTests.InternalsHiddenAssembly.csproj", "{FB39FACE-BAF2-4F97-A249-E45BA63D77FE}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Expand Down Expand Up @@ -623,10 +627,18 @@ Global
{F2ADA552-6328-4B2D-8D48-FCDD32C4AF60}.Debug|Any CPU.Build.0 = Debug|Any CPU
{F2ADA552-6328-4B2D-8D48-FCDD32C4AF60}.Release|Any CPU.ActiveCfg = Release|Any CPU
{F2ADA552-6328-4B2D-8D48-FCDD32C4AF60}.Release|Any CPU.Build.0 = Release|Any CPU
{B5F38A9E-1949-4779-6666-D682B09E0CB3}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{B5F38A9E-1949-4779-6666-D682B09E0CB3}.Debug|Any CPU.Build.0 = Debug|Any CPU
{B5F38A9E-1949-4779-6666-D682B09E0CB3}.Release|Any CPU.ActiveCfg = Release|Any CPU
{B5F38A9E-1949-4779-6666-D682B09E0CB3}.Release|Any CPU.Build.0 = Release|Any CPU
{1C899C94-8744-4E2A-8E23-1660393236FD}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{1C899C94-8744-4E2A-8E23-1660393236FD}.Debug|Any CPU.Build.0 = Debug|Any CPU
{1C899C94-8744-4E2A-8E23-1660393236FD}.Release|Any CPU.ActiveCfg = Release|Any CPU
{1C899C94-8744-4E2A-8E23-1660393236FD}.Release|Any CPU.Build.0 = Release|Any CPU
{FB39FACE-BAF2-4F97-A249-E45BA63D77FE}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{FB39FACE-BAF2-4F97-A249-E45BA63D77FE}.Debug|Any CPU.Build.0 = Debug|Any CPU
{FB39FACE-BAF2-4F97-A249-E45BA63D77FE}.Release|Any CPU.ActiveCfg = Release|Any CPU
{FB39FACE-BAF2-4F97-A249-E45BA63D77FE}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
Expand Down Expand Up @@ -742,7 +754,9 @@ Global
{34969E49-FA6E-41BB-9813-5689BB14021E} = {936C47A9-A7EA-4FBD-8733-CED1D4100E69}
{13A1AFD2-78D3-4463-9977-C8B642BCE2E2} = {85EBD2BA-EA5A-4B1F-AF16-81FBD58579DF}
{F2ADA552-6328-4B2D-8D48-FCDD32C4AF60} = {25D0D27A-C5FE-443D-8B65-D6C987F4A80E}
{B5F38A9E-1949-4779-6666-D682B09E0CB3} = {25D0D27A-C5FE-443D-8B65-D6C987F4A80E}
{1C899C94-8744-4E2A-8E23-1660393236FD} = {7AC28763-9C68-4BF9-A1BA-25CBFFD2D15C}
{FB39FACE-BAF2-4F97-A249-E45BA63D77FE} = {25D0D27A-C5FE-443D-8B65-D6C987F4A80E}
EndGlobalSection
GlobalSection(ExtensibilityGlobals) = postSolution
SolutionGuid = {0B8ABEAD-D2B5-4370-A187-62B5ABE4EE50}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -100,10 +100,7 @@ static bool IsPublicOrVisibleInternal(FieldDefinition fd, ModuleDefinition modul
{
if (fd.Module == module)
return true;
if (fd.Module.GetCustomAttributes().Any(ca => ca.AttributeType.FullName == "System.Runtime.CompilerServices.InternalsVisibleToAttribute"
&& ca.HasConstructorArguments
&& (ca.ConstructorArguments[0].Value as string) != null
&& (ca.ConstructorArguments[0].Value as string).StartsWith(module.Assembly.Name.Name, System.StringComparison.InvariantCulture)))
if (fd.Module.IsVisibleInternal(module))
return true;
}
return false;
Expand All @@ -125,10 +122,7 @@ static bool IsPublicOrVisibleInternal(MethodDefinition md, ModuleDefinition modu
{
if (md.Module == module)
return true;
if (md.Module.GetCustomAttributes().Any(ca => ca.AttributeType.FullName == "System.Runtime.CompilerServices.InternalsVisibleToAttribute"
&& ca.HasConstructorArguments
&& (ca.ConstructorArguments[0].Value as string) != null
&& (ca.ConstructorArguments[0].Value as string).StartsWith(module.Assembly.Name.Name, System.StringComparison.InvariantCulture)))
if (md.Module.IsVisibleInternal(module))
return true;
}
return false;
Expand Down
7 changes: 7 additions & 0 deletions src/Controls/src/Build.Tasks/ModuleDefinitionExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -294,5 +294,12 @@ static string SerializeTypeReference(TypeReference tr)
var gitr = tr as GenericInstanceType;
return gitr == null ? serialized : $"{serialized}<{string.Join(",", gitr.GenericArguments.Select(SerializeTypeReference))}>";
}

public static bool IsVisibleInternal(this ModuleDefinition from, ModuleDefinition to) =>
from.GetCustomAttributes().Any(ca =>
ca.AttributeType.FullName == "System.Runtime.CompilerServices.InternalsVisibleToAttribute" &&
ca.HasConstructorArguments &&
ca.ConstructorArguments[0].Value is string visibleTo &&
visibleTo.StartsWith(to.Assembly.Name.Name, StringComparison.InvariantCulture));
}
}
11 changes: 11 additions & 0 deletions src/Controls/src/Build.Tasks/TypeDefinitionExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -57,5 +57,16 @@ public static IEnumerable<(MethodDefinition methodDef, TypeReference declTypeRef
self = self.BaseType?.ResolveCached(cache);
}
}

public static bool IsPublicOrVisibleInternal(this TypeDefinition self, ModuleDefinition module)
{
if (self.IsPublic)
return true;
if (self.Module == module)
return true;
if (self.Module.IsVisibleInternal(module))
return true;
return false;
}
}
}
2 changes: 2 additions & 0 deletions src/Controls/src/Build.Tasks/TypeReferenceExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -278,6 +278,8 @@ public static CustomAttribute GetCustomAttribute(this TypeReference typeRef, Xam
Func<MethodDefinition, TypeReference, bool> predicate, ModuleDefinition module)
{
var typeDef = typeRef.ResolveCached(cache);
if (typeDef is null)
yield break;
foreach (var method in typeDef.Methods.Where(md => predicate(md, typeRef)))
yield return new Tuple<MethodDefinition, TypeReference>(method, typeRef);
if (typeDef.IsInterface)
Expand Down
5 changes: 4 additions & 1 deletion src/Controls/src/Build.Tasks/XmlTypeExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,10 @@ public static bool TryGetTypeReference(this XmlType xmlType, XamlCache cache, Mo
TypeReference type = xmlType.GetTypeReference(xmlnsDefinitions, module.Assembly.Name.Name, (typeInfo) =>
{
string typeName = typeInfo.typeName.Replace('+', '/'); //Nested types
return module.GetTypeDefinition(cache, (typeInfo.assemblyName, typeInfo.clrNamespace, typeName));
var type = module.GetTypeDefinition(cache, (typeInfo.assemblyName, typeInfo.clrNamespace, typeName));
if (type is not null && type.IsPublicOrVisibleInternal(module))
return type;
return null;
});

if (type != null && typeArguments != null && type.HasGenericParameters)
Expand Down
Loading

0 comments on commit db74ff7

Please sign in to comment.