Skip to content

Commit

Permalink
Fix source-generator infinite loop (#356)
Browse files Browse the repository at this point in the history
  • Loading branch information
jasongin authored Aug 13, 2024
1 parent 749b0f0 commit 02ca89d
Showing 1 changed file with 11 additions and 11 deletions.
22 changes: 11 additions & 11 deletions src/NodeApi.Generator/SymbolExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Linq.Expressions;
using System.Numerics;
using System.Reflection;
using System.Reflection.Emit;
Expand Down Expand Up @@ -355,6 +356,7 @@ static PropertyInfo GetAttributeProperty(Type type, string name)
typeof(JSValue).Assembly.GetType(typeFullName) ??
typeof(BigInteger).Assembly.GetType(typeFullName) ?? // System.Runtime.Numerics
typeof(Stack<>).Assembly.GetType(typeFullName) ?? // System.Collections
typeof(Expression).Assembly.GetType(typeFullName) ?? // System.Linq.Expressions
typeof(System.Collections.ObjectModel.ReadOnlyDictionary<,>)
.Assembly.GetType(typeFullName); // System.ObjectModel
return systemType;
Expand Down Expand Up @@ -387,18 +389,20 @@ static void BuildType(
}
}

if (referencingSymbols?.Any(
(s) => SymbolEqualityComparer.Default.Equals(s, typeSymbol)) == true)
{
// Skip self-referential type symbols.
return;
}

referencingSymbols = (referencingSymbols ?? Enumerable.Empty<INamedTypeSymbol>())
.Append(typeSymbol);

foreach (INamedTypeSymbol typeArgSymbol in
typeSymbol.TypeArguments.OfType<INamedTypeSymbol>())
{
// Skip self-referential type parameters.
if (!referencingSymbols.Any(
(s) => SymbolEqualityComparer.Default.Equals(s, typeArgSymbol)))
{
BuildType(typeArgSymbol, referencingSymbols);
}
BuildType(typeArgSymbol, referencingSymbols);
}

if (GetSystemType(typeSymbol) != null)
Expand All @@ -425,11 +429,7 @@ static void BuildType(
.OfType<INamedTypeSymbol>()
.Distinct(SymbolEqualityComparer.Default).Cast<INamedTypeSymbol?>())
{
if (!referencingSymbols.Any(
(s) => SymbolEqualityComparer.Default.Equals(s, parameterTypeSymbol)))
{
BuildType(parameterTypeSymbol!, referencingSymbols);
}
BuildType(parameterTypeSymbol!, referencingSymbols);
}
}

Expand Down

0 comments on commit 02ca89d

Please sign in to comment.