Skip to content

Fix source-generator infinite loop #356

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Aug 13, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading