Skip to content

Commit 7049a4b

Browse files
sharwelldotpaul
authored andcommitted
Improve WellKnownTypeProvider handling of duplicate types
1 parent 56e1420 commit 7049a4b

1 file changed

Lines changed: 43 additions & 1 deletion

File tree

src/Utilities/Compiler/WellKnownTypeProvider.cs

Lines changed: 43 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22

33
using System;
44
using System.Collections.Concurrent;
5+
using System.Linq;
6+
using Analyzer.Utilities.Extensions;
57
using Microsoft.CodeAnalysis;
68

79
namespace Analyzer.Utilities
@@ -46,7 +48,47 @@ public bool TryGetOrCreateTypeByMetadataName(string fullTypeName, out INamedType
4648
{
4749
namedTypeSymbol = _fullNameToTypeMap.GetOrAdd(
4850
fullTypeName,
49-
(string s) => Compilation.GetTypeByMetadataName(s)); // Caching null results in our cache is intended.
51+
fullyQualifiedMetadataName =>
52+
{
53+
// Caching null results in our cache is intended.
54+
var type = Compilation.GetTypeByMetadataName(fullyQualifiedMetadataName)
55+
?? Compilation.Assembly.GetTypeByMetadataName(fullyQualifiedMetadataName);
56+
if (type is null)
57+
{
58+
foreach (var module in Compilation.Assembly.Modules)
59+
{
60+
foreach (var referencedAssembly in module.ReferencedAssemblySymbols)
61+
{
62+
var currentType = referencedAssembly.GetTypeByMetadataName(fullyQualifiedMetadataName);
63+
if (currentType is null)
64+
{
65+
continue;
66+
}
67+
68+
switch (currentType.GetResultantVisibility())
69+
{
70+
case SymbolVisibility.Public:
71+
case SymbolVisibility.Internal when referencedAssembly.GivesAccessTo(Compilation.Assembly):
72+
break;
73+
74+
default:
75+
continue;
76+
}
77+
78+
if (type is object)
79+
{
80+
// Multiple visible types with the same metadata name are present.
81+
return null;
82+
}
83+
84+
type = currentType;
85+
}
86+
}
87+
}
88+
89+
return type;
90+
});
91+
5092
return namedTypeSymbol != null;
5193
}
5294

0 commit comments

Comments
 (0)