Skip to content

Commit

Permalink
fix: Fix thread-safety of PropertySelectorGenerator
Browse files Browse the repository at this point in the history
  • Loading branch information
Youssef1313 committed Aug 11, 2023
1 parent 08d3b2b commit 688c31c
Showing 1 changed file with 15 additions and 19 deletions.
34 changes: 15 additions & 19 deletions src/Uno.Extensions.Core.Generators/PropertySelectorGenerator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -91,11 +91,9 @@ public void Initialize(IncrementalGeneratorInitializationContext context)
{
if (compilation.GetAssemblyOrModuleSymbol(reference) is IAssemblySymbol assembly)
{
if (!_assemblyToNamesMap.TryGetValue(assembly.Identity, out var interestingNames))
{
interestingNames = GetFromNamespaceExpensive(assembly.GlobalNamespace, propertySelectorSymbol, callerFilePathSymbol, callerLineNumberSymbol, ct).ToImmutableArray();
_assemblyToNamesMap.Add(assembly.Identity, interestingNames);
}
var interestingNames = _assemblyToNamesMap.GetValue(
assembly.Identity,
_ => GetFromNamespaceExpensive(assembly.GlobalNamespace, propertySelectorSymbol, callerFilePathSymbol, callerLineNumberSymbol, ct).ToImmutableArray());

foreach (var name in (ImmutableArray<string>)interestingNames)
{
Expand Down Expand Up @@ -281,27 +279,25 @@ private static ImmutableArray<SyntaxTree> GetInterestingTrees(Compilation compil

private static bool IsCandidateTree(SyntaxTree tree, CancellationToken ct)
{
if (_treeToIsCandidate.TryGetValue(tree, out var candidate))
{
return (bool)candidate;
}
return (bool)_treeToIsCandidate.GetValue(tree, tree => IsCandidateTreeCore(tree, ct));

var root = (CompilationUnitSyntax)tree.GetRoot(ct);
foreach (var member in root.Members)
static bool IsCandidateTreeCore(SyntaxTree tree, CancellationToken ct)
{
foreach (var node in member.DescendantNodesAndSelf())
var root = (CompilationUnitSyntax)tree.GetRoot(ct);
foreach (var member in root.Members)
{
ct.ThrowIfCancellationRequested();
if (IsCandidate(node))
foreach (var node in member.DescendantNodesAndSelf())
{
_treeToIsCandidate.Add(tree, true);
return true;
ct.ThrowIfCancellationRequested();
if (IsCandidate(node))
{
return true;
}
}
}
}

_treeToIsCandidate.Add(tree, false);
return false;
return false;
}
}

internal static bool IsCandidate(IMethodSymbol method, INamedTypeSymbol? propertySelectorSymbol, INamedTypeSymbol? callerFilePathSymbol, INamedTypeSymbol? callerLineNumberSymbol)
Expand Down

0 comments on commit 688c31c

Please sign in to comment.