Fix caching recursive interface definitions

This commit is contained in:
Dan Balasescu
2022-11-26 15:03:49 +09:00
parent bb24637e35
commit 31b1790914
5 changed files with 47 additions and 2 deletions

View File

@@ -30,6 +30,7 @@ namespace osu.Framework.SourceGeneration.Tests
[InlineData("PartialNestedClasses")]
[InlineData("NestedCachedClass")]
[InlineData("MultipleCachedMember")]
[InlineData("CachedInheritedInterface")]
public async Task Check(string name) => await RunTest(name).ConfigureAwait(false);
protected override Task Verify((string filename, string content)[] sources, (string filename, string content)[] generated)

View File

@@ -0,0 +1,20 @@
// <auto-generated/>
#nullable enable
#pragma warning disable CS4014
partial class CachedInheritedInterface : osu.Framework.Allocation.ISourceGeneratedDependencyActivator
{
public override void RegisterForDependencyActivation(osu.Framework.Allocation.IDependencyActivatorRegistry registry)
{
if (registry.IsRegistered(typeof(CachedInheritedInterface)))
return;
base.RegisterForDependencyActivation(registry);
registry.Register(typeof(CachedInheritedInterface), null, (t, d, i) =>
{
var dependencies = new osu.Framework.Allocation.DependencyContainer(d);
osu.Framework.Utils.SourceGeneratorUtils.CacheDependency(dependencies, typeof(CachedInheritedInterface), t, i, typeof(IInterface1), null, null);
osu.Framework.Utils.SourceGeneratorUtils.CacheDependency(dependencies, typeof(CachedInheritedInterface), t, i, typeof(IInterface2), null, null);
return dependencies;
});
}
}

View File

@@ -0,0 +1,13 @@
public partial class CachedInheritedInterface : osu.Framework.Graphics.Drawable, IInterface1
{
}
[osu.Framework.Allocation.Cached]
public interface IInterface1 : IInterface2
{
}
[osu.Framework.Allocation.Cached]
public interface IInterface2
{
}

View File

@@ -48,9 +48,9 @@ namespace osu.Framework.SourceGeneration
}
// Process any [Cached] attributes on any interface on the class excluding base types.
foreach (var iFace in symbol.Interfaces)
foreach (var iFace in SyntaxHelpers.GetDeclaredInterfacesOnType(symbol))
{
// Add an interface entry for all interfaces that have a cached attribute.
// Add an entry if this interface has a cached attribute.
if (iFace.GetAttributes().Any(attrib => SyntaxHelpers.IsCachedAttribute(attrib.AttributeClass)))
addCandidate(context, classSyntax).CachedInterfaces.Add(iFace);
}

View File

@@ -176,5 +176,16 @@ namespace osu.Framework.SourceGeneration
|| IsResolvedAttribute(semanticModel, attrib)
|| IsCachedAttribute(semanticModel, attrib));
}
public static IEnumerable<ITypeSymbol> GetDeclaredInterfacesOnType(INamedTypeSymbol type)
{
foreach (var declared in type.Interfaces)
{
yield return declared;
foreach (var nested in declared.AllInterfaces)
yield return nested;
}
}
}
}