diff --git a/README.md b/README.md index 3e65b86..9e3446d 100644 --- a/README.md +++ b/README.md @@ -55,6 +55,9 @@ Here's an example of some of the new features that **PolySharp** can enable down - `[OverloadResolutionPriority]` (needed for [overload resolution priority](https://learn.microsoft.com/en-us/dotnet/csharp/whats-new/csharp-13#overload-resolution-priority)) - `[ParamsCollection]` (needed for [params collection](https://learn.microsoft.com/en-us/dotnet/csharp/whats-new/csharp-13#params-collections)) - `[ConstantExpected]` (see [proposal](https://github.com/dotnet/runtime/issues/33771)) +- Union types (needed for [C# 15 union types](https://devblogs.microsoft.com/dotnet/csharp-15-union-types/)) + - `[Union]` + - `IUnion` To leverage them, make sure to bump your C# language version. You can do this by setting the `` MSBuild property in your project. For instance, by adding `13.0` (or your desired C# version) to the first `` of your .csproj file. For more info on this, [see here](https://sergiopedri.medium.com/enabling-and-using-c-9-features-on-older-and-unsupported-runtimes-ce384d8debb), but remember that you don't need to manually copy polyfills anymore: simply adding a reference to **PolySharp** will do this for you automatically. diff --git a/src/PolySharp.SourceGenerators/EmbeddedResources/System.Runtime.CompilerServices.IUnion.cs b/src/PolySharp.SourceGenerators/EmbeddedResources/System.Runtime.CompilerServices.IUnion.cs new file mode 100644 index 0000000..d8371b7 --- /dev/null +++ b/src/PolySharp.SourceGenerators/EmbeddedResources/System.Runtime.CompilerServices.IUnion.cs @@ -0,0 +1,20 @@ +// +#pragma warning disable +#nullable enable annotations + +// Licensed to the .NET Foundation under one or more agreements. +// The .NET Foundation licenses this file to you under the MIT license. + +namespace System.Runtime.CompilerServices +{ + /// + /// Defines the contract for union types, providing access to the underlying value. + /// + internal interface IUnion + { + /// + /// Gets the underlying value of the union. + /// + object? Value { get; } + } +} diff --git a/src/PolySharp.SourceGenerators/EmbeddedResources/System.Runtime.CompilerServices.UnionAttribute.cs b/src/PolySharp.SourceGenerators/EmbeddedResources/System.Runtime.CompilerServices.UnionAttribute.cs new file mode 100644 index 0000000..609f09c --- /dev/null +++ b/src/PolySharp.SourceGenerators/EmbeddedResources/System.Runtime.CompilerServices.UnionAttribute.cs @@ -0,0 +1,21 @@ +// +#pragma warning disable +#nullable enable annotations + +// Licensed to the .NET Foundation under one or more agreements. +// The .NET Foundation licenses this file to you under the MIT license. + +namespace System.Runtime.CompilerServices +{ + /// + /// Indicates that a type is a union type, allowing the C# compiler to recognize it for union type features. + /// + [global::System.AttributeUsage( + global::System.AttributeTargets.Class | + global::System.AttributeTargets.Struct, + AllowMultiple = false)] + [global::System.Diagnostics.CodeAnalysis.ExcludeFromCodeCoverage] + internal sealed class UnionAttribute : global::System.Attribute + { + } +} diff --git a/tests/PolySharp.Tests/LanguageFeatures.cs b/tests/PolySharp.Tests/LanguageFeatures.cs index d5df709..6c0cdd1 100644 --- a/tests/PolySharp.Tests/LanguageFeatures.cs +++ b/tests/PolySharp.Tests/LanguageFeatures.cs @@ -297,3 +297,22 @@ public static void AnotherCpuIntrinsic([ConstantExpected(Min = 0, Max = 8)] int { } } + +// UnionAttribute, IUnion +[Union] +internal sealed class TestUnion : IUnion +{ + private readonly object? value; + + public TestUnion(int value) + { + this.value = value; + } + + public TestUnion(string value) + { + this.value = value; + } + + public object? Value => this.value; +}