Skip to content
Draft
Show file tree
Hide file tree
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
3 changes: 2 additions & 1 deletion .github/workflows/build.yml
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ on:

env:
DOTNET_NOLOGO: 1
TreatWarningsAsErrors: true

jobs:
build:
Expand All @@ -19,7 +20,7 @@ jobs:
- name: Restore dependencies
run: dotnet restore
- name: Build
run: dotnet build --configuration Release --no-restore /p:TreatWarningsAsErrors=true
run: dotnet build --configuration Release
- name: Run Tests
run: dotnet test --configuration Release --no-build

Expand Down
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
*.received.cs

## Ignore Visual Studio temporary files, build results, and
## files generated by popular Visual Studio add-ons.
##
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ public CollectingInterpolatedStringHandler(int literalLength, int formattedCount

public CollectingInterpolatedStringHandler()
{
_items = new List<object?>();
_items = [];
}

private readonly List<object?> _items;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,8 @@ public sealed class DiscriminatedUnionGenerator : IIncrementalGenerator
{
public void Initialize(IncrementalGeneratorInitializationContext context)
{
context.RegisterPostInitializationOutput(context => context.AddSource("DiscriminatedUnionAttribute.g.cs", DiscriminatedUnionAttributeSource));
context.RegisterPostInitializationOutput(static context => context.AddSource("Microsoft.CodeAnalysis.EmbeddedAttribute.g.cs", EmbeddedAttributeSource));
context.RegisterPostInitializationOutput(static context => context.AddSource("DiscriminatedUnionAttribute.g.cs", DiscriminatedUnionAttributeSource));

var code = context.SyntaxProvider.CreateSyntaxProvider(static (node, _) => IsSyntaxTargetForGeneration(node), GetSemanticTargetForGeneration)
.WhereNotNull()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,11 @@
<TargetFramework>netstandard2.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
<LangVersion>11.0</LangVersion>
<LangVersion>13.0</LangVersion>
<EnforceExtendedAnalyzerRules>true</EnforceExtendedAnalyzerRules>
</PropertyGroup>
<PropertyGroup Label="NuGet Metadata">
<Version>1.2.1</Version>
<Version>1.2.2</Version>
<PackageId>Funcky.DiscriminatedUnion</PackageId>
<Authors>Polyadic</Authors>
<PackageLicenseExpression>MIT OR Apache-2.0</PackageLicenseExpression>
Expand All @@ -35,12 +35,15 @@
<ContinuousIntegrationBuild Condition="'$(GITHUB_ACTIONS)' == 'true'">true</ContinuousIntegrationBuild>
<EmbedUntrackedSources>true</EmbedUntrackedSources>
</PropertyGroup>
<ItemGroup Label="Deterministic Builds and Source Link">
<PackageReference Include="Microsoft.SourceLink.GitHub" Version="1.0.0" PrivateAssets="All" />
</ItemGroup>
<ItemGroup>
<PackageReference Include="Microsoft.CodeAnalysis.Analyzers" Version="3.3.3" PrivateAssets="all" />
<!-- Careful! Updating this means that you might break compatibility
with older .NET SDKs and older versions of Visual Studio!
See <https://docs.microsoft.com/en-us/visualstudio/extensibility/roslyn-version-support>
for a detailed compatibility table. -->
<PackageReference Include="Microsoft.CodeAnalysis.CSharp" Version="4.0.1" />
<PackageReference Include="PolySharp" Version="1.14.1" PrivateAssets="all" />
</ItemGroup>
<ItemGroup>
<PackageReference Include="Microsoft.CodeAnalysis.Analyzers" Version="3.11.0" PrivateAssets="all" />
<PackageReference Include="PolySharp" Version="1.15.0" PrivateAssets="all" />
</ItemGroup>
</Project>
13 changes: 5 additions & 8 deletions Funcky.DiscriminatedUnion.SourceGeneration/SelectorComparer.cs
Original file line number Diff line number Diff line change
@@ -1,12 +1,9 @@
namespace Funcky.DiscriminatedUnion.SourceGeneration;

internal sealed class SelectorComparer<TSource, TSelected> : IEqualityComparer<TSource>
internal sealed class SelectorComparer<TSource, TSelected>(Func<TSource, TSelected> selector)
: IEqualityComparer<TSource>
{
private readonly Func<TSource, TSelected> _selector;
public bool Equals(TSource x, TSource y) => EqualityComparer<TSelected>.Default.Equals(selector(x), selector(y));

public SelectorComparer(Func<TSource, TSelected> selector) => _selector = selector;

public bool Equals(TSource x, TSource y) => EqualityComparer<TSelected>.Default.Equals(_selector(x), _selector(y));

public int GetHashCode(TSource obj) => EqualityComparer<TSelected>.Default.GetHashCode(_selector(obj));
}
public int GetHashCode(TSource obj) => EqualityComparer<TSelected>.Default.GetHashCode(selector(obj));
}
19 changes: 19 additions & 0 deletions Funcky.DiscriminatedUnion.SourceGeneration/SourceCodeSnippets.cs
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ internal static class SourceCodeSnippets

namespace Funcky
{
[global::Microsoft.CodeAnalysis.Embedded]
[global::System.Diagnostics.Conditional("Funcky_DiscriminatedUnion")]
[global::System.AttributeUsage(global::System.AttributeTargets.Class)]
internal sealed class DiscriminatedUnionAttribute : global::System.Attribute
Expand All @@ -39,6 +40,24 @@ internal sealed class DiscriminatedUnionAttribute : global::System.Attribute

""";

/// <summary>
/// This attribute can be used to mark a type as being only visible to the current assembly
/// i.e. it prevents <c>InternalsVisibleTo</c> to cause a compiler warning.
/// </summary>
/// <seealso href="https://github.com/dotnet/roslyn/pull/76583" />
/// <seealso href="https://github.com/dotnet/roslyn/blob/main/docs/features/incremental-generators.cookbook.md" />
// language=c#
public const string EmbeddedAttributeSource =
"""
namespace Microsoft.CodeAnalysis
{
[Embedded]
internal sealed partial class EmbeddedAttribute : global::System.Attribute
{
}
}
""";

private static readonly AssemblyName GeneratorAssemblyName = typeof(DiscriminatedUnionGenerator).Assembly.GetName();

public static readonly string GeneratedCodeAttributeSource = $"[global::System.CodeDom.Compiler.GeneratedCode(" +
Expand Down
Original file line number Diff line number Diff line change
@@ -1,17 +1,17 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFramework>net8.0</TargetFramework>
<TargetFramework>net9.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
<LangVersion>12.0</LangVersion>
<LangVersion>13.0</LangVersion>
<IsPackable>false</IsPackable>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.0.0" />
<PackageReference Include="Verify.SourceGenerators" Version="1.2.0" />
<PackageReference Include="Verify.Xunit" Version="14.14.1" />
<PackageReference Include="xunit" Version="2.4.1" />
<PackageReference Include="xunit.runner.visualstudio" Version="2.4.3" PrivateAssets="all" />
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.12.0" />
<PackageReference Include="Verify.SourceGenerators" Version="2.5.0" />
<PackageReference Include="Verify.Xunit" Version="28.9.0" />
<PackageReference Include="xunit" Version="2.9.3" />
<PackageReference Include="xunit.runner.visualstudio" Version="3.0.1" PrivateAssets="all" />
</ItemGroup>
<ItemGroup>
<Compile Remove="Sources/*.cs" />
Expand Down
5 changes: 1 addition & 4 deletions Funcky.DiscriminatedUnion.Test/ModuleInitializer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,5 @@ namespace Funcky.DiscriminatedUnion.Test;
public static class ModuleInitializer
{
[ModuleInitializer]
public static void Init()
{
VerifySourceGenerators.Enable();
}
public static void Init() => VerifySourceGenerators.Initialize();
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

namespace Funcky
{
[global::Microsoft.CodeAnalysis.Embedded]
[global::System.Diagnostics.Conditional("Funcky_DiscriminatedUnion")]
[global::System.AttributeUsage(global::System.AttributeTargets.Class)]
internal sealed class DiscriminatedUnionAttribute : global::System.Attribute
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
//HintName: DiscriminatedUnionGenerator.g.cs
//HintName: DiscriminatedUnionGenerator.g.cs
// <auto-generated/>
#nullable enable

Expand All @@ -14,18 +14,18 @@ partial class StaticClass
{
partial record NestedUnion
{
[global::System.CodeDom.Compiler.GeneratedCode("Funcky.DiscriminatedUnion.SourceGeneration", "1.2.1.0")]
[global::System.CodeDom.Compiler.GeneratedCode("Funcky.DiscriminatedUnion.SourceGeneration", "VERSION")]
public abstract TResult Match<TResult>(global::System.Func<Variant, TResult> variant);

[global::System.CodeDom.Compiler.GeneratedCode("Funcky.DiscriminatedUnion.SourceGeneration", "1.2.1.0")]
[global::System.CodeDom.Compiler.GeneratedCode("Funcky.DiscriminatedUnion.SourceGeneration", "VERSION")]
public abstract void Switch(global::System.Action<Variant> variant);

partial record Variant
{
[global::System.CodeDom.Compiler.GeneratedCode("Funcky.DiscriminatedUnion.SourceGeneration", "1.2.1.0")]
[global::System.CodeDom.Compiler.GeneratedCode("Funcky.DiscriminatedUnion.SourceGeneration", "VERSION")]
public override TResult Match<TResult>(global::System.Func<Variant, TResult> variant) => variant(this);

[global::System.CodeDom.Compiler.GeneratedCode("Funcky.DiscriminatedUnion.SourceGeneration", "1.2.1.0")]
[global::System.CodeDom.Compiler.GeneratedCode("Funcky.DiscriminatedUnion.SourceGeneration", "VERSION")]
public override void Switch(global::System.Action<Variant> variant) => variant(this);
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
//HintName: Microsoft.CodeAnalysis.EmbeddedAttribute.g.cs
namespace Microsoft.CodeAnalysis
{
[Embedded]
internal sealed partial class EmbeddedAttribute : global::System.Attribute
{
}
}

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

namespace Funcky
{
[global::Microsoft.CodeAnalysis.Embedded]
[global::System.Diagnostics.Conditional("Funcky_DiscriminatedUnion")]
[global::System.AttributeUsage(global::System.AttributeTargets.Class)]
internal sealed class DiscriminatedUnionAttribute : global::System.Attribute
Expand Down
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
//HintName: DiscriminatedUnionGenerator.g.cs
//HintName: DiscriminatedUnionGenerator.g.cs
// <auto-generated/>
#nullable enable

partial record EmptyUnion
{
[global::System.CodeDom.Compiler.GeneratedCode("Funcky.DiscriminatedUnion.SourceGeneration", "1.2.1.0")]
[global::System.CodeDom.Compiler.GeneratedCode("Funcky.DiscriminatedUnion.SourceGeneration", "VERSION")]
public abstract TResult Match<TResult>();

[global::System.CodeDom.Compiler.GeneratedCode("Funcky.DiscriminatedUnion.SourceGeneration", "1.2.1.0")]
[global::System.CodeDom.Compiler.GeneratedCode("Funcky.DiscriminatedUnion.SourceGeneration", "VERSION")]
public abstract void Switch();
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
//HintName: Microsoft.CodeAnalysis.EmbeddedAttribute.g.cs
namespace Microsoft.CodeAnalysis
{
[Embedded]
internal sealed partial class EmbeddedAttribute : global::System.Attribute
{
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

namespace Funcky
{
[global::Microsoft.CodeAnalysis.Embedded]
[global::System.Diagnostics.Conditional("Funcky_DiscriminatedUnion")]
[global::System.AttributeUsage(global::System.AttributeTargets.Class)]
internal sealed class DiscriminatedUnionAttribute : global::System.Attribute
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,18 +6,18 @@ namespace Funcky.DiscriminatedUnion.Test
{
partial record FlattenedNestedUnionWithPartition
{
[global::System.CodeDom.Compiler.GeneratedCode("Funcky.DiscriminatedUnion.SourceGeneration", "1.2.1.0")]
[global::System.CodeDom.Compiler.GeneratedCode("Funcky.DiscriminatedUnion.SourceGeneration", "VERSION")]
public abstract TResult Match<TResult>(global::System.Func<Keyword, TResult> keyword, global::System.Func<Literal.Number.Integer, TResult> integer);

[global::System.CodeDom.Compiler.GeneratedCode("Funcky.DiscriminatedUnion.SourceGeneration", "1.2.1.0")]
[global::System.CodeDom.Compiler.GeneratedCode("Funcky.DiscriminatedUnion.SourceGeneration", "VERSION")]
public abstract void Switch(global::System.Action<Keyword> keyword, global::System.Action<Literal.Number.Integer> integer);

partial record Keyword
{
[global::System.CodeDom.Compiler.GeneratedCode("Funcky.DiscriminatedUnion.SourceGeneration", "1.2.1.0")]
[global::System.CodeDom.Compiler.GeneratedCode("Funcky.DiscriminatedUnion.SourceGeneration", "VERSION")]
public override TResult Match<TResult>(global::System.Func<Keyword, TResult> keyword, global::System.Func<Literal.Number.Integer, TResult> integer) => keyword(this);

[global::System.CodeDom.Compiler.GeneratedCode("Funcky.DiscriminatedUnion.SourceGeneration", "1.2.1.0")]
[global::System.CodeDom.Compiler.GeneratedCode("Funcky.DiscriminatedUnion.SourceGeneration", "VERSION")]
public override void Switch(global::System.Action<Keyword> keyword, global::System.Action<Literal.Number.Integer> integer) => keyword(this);
}

Expand All @@ -27,17 +27,17 @@ partial record Number
{
partial record Integer
{
[global::System.CodeDom.Compiler.GeneratedCode("Funcky.DiscriminatedUnion.SourceGeneration", "1.2.1.0")]
[global::System.CodeDom.Compiler.GeneratedCode("Funcky.DiscriminatedUnion.SourceGeneration", "VERSION")]
public override TResult Match<TResult>(global::System.Func<Keyword, TResult> keyword, global::System.Func<Literal.Number.Integer, TResult> integer) => integer(this);

[global::System.CodeDom.Compiler.GeneratedCode("Funcky.DiscriminatedUnion.SourceGeneration", "1.2.1.0")]
[global::System.CodeDom.Compiler.GeneratedCode("Funcky.DiscriminatedUnion.SourceGeneration", "VERSION")]
public override void Switch(global::System.Action<Keyword> keyword, global::System.Action<Literal.Number.Integer> integer) => integer(this);
}
}
}
}

[global::System.CodeDom.Compiler.GeneratedCode("Funcky.DiscriminatedUnion.SourceGeneration", "1.2.1.0")]
[global::System.CodeDom.Compiler.GeneratedCode("Funcky.DiscriminatedUnion.SourceGeneration", "VERSION")]
public static partial class FlattenedNestedUnionWithPartitionEnumerableExtensions
{
public static (global::System.Collections.Generic.IReadOnlyList<FlattenedNestedUnionWithPartition.Keyword> Keyword, global::System.Collections.Generic.IReadOnlyList<FlattenedNestedUnionWithPartition.Literal.Number.Integer> Integer) Partition(this global::System.Collections.Generic.IEnumerable<FlattenedNestedUnionWithPartition> source)
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
//HintName: Microsoft.CodeAnalysis.EmbeddedAttribute.g.cs
namespace Microsoft.CodeAnalysis
{
[Embedded]
internal sealed partial class EmbeddedAttribute : global::System.Attribute
{
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

namespace Funcky
{
[global::Microsoft.CodeAnalysis.Embedded]
[global::System.Diagnostics.Conditional("Funcky_DiscriminatedUnion")]
[global::System.AttributeUsage(global::System.AttributeTargets.Class)]
internal sealed class DiscriminatedUnionAttribute : global::System.Attribute
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,32 +6,32 @@ namespace Funcky.DiscriminatedUnion.Test
{
partial record FlattenedUnionWithPartition
{
[global::System.CodeDom.Compiler.GeneratedCode("Funcky.DiscriminatedUnion.SourceGeneration", "1.2.1.0")]
[global::System.CodeDom.Compiler.GeneratedCode("Funcky.DiscriminatedUnion.SourceGeneration", "VERSION")]
public abstract TResult Match<TResult>(global::System.Func<Keyword, TResult> keyword, global::System.Func<Integer, TResult> integer);

[global::System.CodeDom.Compiler.GeneratedCode("Funcky.DiscriminatedUnion.SourceGeneration", "1.2.1.0")]
[global::System.CodeDom.Compiler.GeneratedCode("Funcky.DiscriminatedUnion.SourceGeneration", "VERSION")]
public abstract void Switch(global::System.Action<Keyword> keyword, global::System.Action<Integer> integer);

partial record Keyword
{
[global::System.CodeDom.Compiler.GeneratedCode("Funcky.DiscriminatedUnion.SourceGeneration", "1.2.1.0")]
[global::System.CodeDom.Compiler.GeneratedCode("Funcky.DiscriminatedUnion.SourceGeneration", "VERSION")]
public override TResult Match<TResult>(global::System.Func<Keyword, TResult> keyword, global::System.Func<Integer, TResult> integer) => keyword(this);

[global::System.CodeDom.Compiler.GeneratedCode("Funcky.DiscriminatedUnion.SourceGeneration", "1.2.1.0")]
[global::System.CodeDom.Compiler.GeneratedCode("Funcky.DiscriminatedUnion.SourceGeneration", "VERSION")]
public override void Switch(global::System.Action<Keyword> keyword, global::System.Action<Integer> integer) => keyword(this);
}

partial record Integer
{
[global::System.CodeDom.Compiler.GeneratedCode("Funcky.DiscriminatedUnion.SourceGeneration", "1.2.1.0")]
[global::System.CodeDom.Compiler.GeneratedCode("Funcky.DiscriminatedUnion.SourceGeneration", "VERSION")]
public override TResult Match<TResult>(global::System.Func<Keyword, TResult> keyword, global::System.Func<Integer, TResult> integer) => integer(this);

[global::System.CodeDom.Compiler.GeneratedCode("Funcky.DiscriminatedUnion.SourceGeneration", "1.2.1.0")]
[global::System.CodeDom.Compiler.GeneratedCode("Funcky.DiscriminatedUnion.SourceGeneration", "VERSION")]
public override void Switch(global::System.Action<Keyword> keyword, global::System.Action<Integer> integer) => integer(this);
}
}

[global::System.CodeDom.Compiler.GeneratedCode("Funcky.DiscriminatedUnion.SourceGeneration", "1.2.1.0")]
[global::System.CodeDom.Compiler.GeneratedCode("Funcky.DiscriminatedUnion.SourceGeneration", "VERSION")]
public static partial class FlattenedUnionWithPartitionEnumerableExtensions
{
public static (global::System.Collections.Generic.IReadOnlyList<FlattenedUnionWithPartition.Keyword> Keyword, global::System.Collections.Generic.IReadOnlyList<FlattenedUnionWithPartition.Integer> Integer) Partition(this global::System.Collections.Generic.IEnumerable<FlattenedUnionWithPartition> source)
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
//HintName: Microsoft.CodeAnalysis.EmbeddedAttribute.g.cs
namespace Microsoft.CodeAnalysis
{
[Embedded]
internal sealed partial class EmbeddedAttribute : global::System.Attribute
{
}
}
Loading
Loading