Skip to content

Commit a2c4dd2

Browse files
committed
Add new System.Diagnostics.UnreachableException polyfill
The System.Diagnostics.UnreachableException.cs file was not actually auto-generated (see #88). Fixes #60
1 parent 02753db commit a2c4dd2

File tree

4 files changed

+54
-0
lines changed

4 files changed

+54
-0
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,7 @@ Here's an example of some of the new features that **PolySharp** can enable down
5555
- `[OverloadResolutionPriority]` (needed for [overload resolution priority](https://learn.microsoft.com/en-us/dotnet/csharp/whats-new/csharp-13#overload-resolution-priority))
5656
- `[ParamsCollection]` (needed for [params collection](https://learn.microsoft.com/en-us/dotnet/csharp/whats-new/csharp-13#params-collections))
5757
- `[ConstantExpected]` (see [proposal](https://github.com/dotnet/runtime/issues/33771))
58+
- `UnreachableException` (see [docs](https://learn.microsoft.com/en-us/dotnet/api/system.diagnostics.unreachableexception))
5859

5960
To leverage them, make sure to bump your C# language version. You can do this by setting the `<LangVersion>` MSBuild property in your project. For instance, by adding `<LangVersion>13.0</LangVersion>` (or your desired C# version) to the first `<PropertyGroup>` 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.
6061

src/PolySharp.Package/README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,7 @@ Here's an example of some of the new features that **PolySharp** can enable down
5353
- `[OverloadResolutionPriority]` (needed for [overload resolution priority](https://learn.microsoft.com/en-us/dotnet/csharp/whats-new/csharp-13#overload-resolution-priority))
5454
- `[ParamsCollection]` (needed for [params collection](https://learn.microsoft.com/en-us/dotnet/csharp/whats-new/csharp-13#params-collections))
5555
- `[ConstantExpected]` (see [proposal](https://github.com/dotnet/runtime/issues/33771))
56+
- `UnreachableException` (see [docs](https://learn.microsoft.com/en-us/dotnet/api/system.diagnostics.unreachableexception))
5657

5758
To leverage them, make sure to bump your C# language version. You can do this by setting the `<LangVersion>` MSBuild property in your project. For instance, by adding `<LangVersion>13.0</LangVersion>` (or your desired C# version) to the first `<PropertyGroup>` 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.
5859

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
// <auto-generated/>
2+
#pragma warning disable
3+
#nullable enable annotations
4+
5+
// Licensed to the .NET Foundation under one or more agreements.
6+
// The .NET Foundation licenses this file to you under the MIT license.
7+
8+
namespace System.Diagnostics
9+
{
10+
/// <summary>
11+
/// Exception thrown when the program executes an instruction that was thought to be unreachable.
12+
/// </summary>
13+
[global::System.Diagnostics.CodeAnalysis.ExcludeFromCodeCoverage]
14+
internal sealed class UnreachableException : Exception
15+
{
16+
/// <summary>
17+
/// Initializes a new instance of the <see cref="UnreachableException"/> class with the default error message.
18+
/// </summary>
19+
public UnreachableException()
20+
: base("The program executed an instruction that was thought to be unreachable.")
21+
{
22+
}
23+
24+
/// <summary>
25+
/// Initializes a new instance of the <see cref="UnreachableException"/> class with a specified error message.
26+
/// </summary>
27+
/// <param name="message">The error message that explains the reason for the exception.</param>
28+
public UnreachableException(string? message)
29+
: base(message)
30+
{
31+
}
32+
33+
/// <summary>
34+
/// Initializes a new instance of the <see cref="UnreachableException"/> class with a specified error message and a reference to the inner exception that is the cause of this exception.
35+
/// </summary>
36+
/// <param name="message">The error message that explains the reason for the exception.</param>
37+
/// <param name="innerException">The exception that is the cause of the current exception.</param>
38+
public UnreachableException(string? message, Exception? innerException)
39+
: base(message, innerException)
40+
{
41+
}
42+
}
43+
}

tests/PolySharp.Tests/LanguageFeatures.cs

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,10 @@
11
using System;
22
using System.Collections;
33
using System.Collections.Generic;
4+
using System.Diagnostics;
45
using System.Diagnostics.CodeAnalysis;
56
using System.Runtime.CompilerServices;
7+
using System.Runtime.ExceptionServices;
68
using System.Runtime.Versioning;
79
#if !NETSTANDARD2_1
810
using System.Threading.Tasks;
@@ -38,6 +40,13 @@ public void Throws([DoesNotReturnIf(true)] bool value)
3840
{
3941
}
4042

43+
// UnreachableException
44+
public object Unreachable()
45+
{
46+
ExceptionDispatchInfo.Capture(new Exception()).Throw();
47+
throw new UnreachableException();
48+
}
49+
4150
// MaybeNullAttribute
4251
[return: MaybeNull]
4352
public string ModifyValue()

0 commit comments

Comments
 (0)