Skip to content
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

Improvements for Assumed.Unreachable(...) #11155

Merged
merged 1 commit into from
Nov 5, 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
Original file line number Diff line number Diff line change
Expand Up @@ -40,22 +40,37 @@ public static void True(
public static void Unreachable([CallerFilePath] string? path = null, [CallerLineNumber] int line = 0)
=> ThrowInvalidOperation(SR.This_program_location_is_thought_to_be_unreachable, path, line);

/// <summary>
/// Can be called at points that are assumed to be unreachable at runtime.
/// </summary>
/// <exception cref="InvalidOperationException"/>
[DoesNotReturn]
public static void Unreachable(string message, [CallerFilePath] string? path = null, [CallerLineNumber] int line = 0)
=> ThrowInvalidOperation(message, path, line);

/// <summary>
/// Can be called at points that are assumed to be unreachable at runtime.
/// </summary>
/// <exception cref="InvalidOperationException"/>
[DoesNotReturn]
public static T Unreachable<T>([CallerFilePath] string? path = null, [CallerLineNumber] int line = 0)
{
ThrowInvalidOperation(SR.This_program_location_is_thought_to_be_unreachable, path, line);
return default;
}
=> ThrowInvalidOperation<T>(SR.This_program_location_is_thought_to_be_unreachable, path, line);

/// <summary>
/// Can be called at points that are assumed to be unreachable at runtime.
/// </summary>
/// <exception cref="InvalidOperationException"/>
[DoesNotReturn]
public static T Unreachable<T>(string message, [CallerFilePath] string? path = null, [CallerLineNumber] int line = 0)
=> ThrowInvalidOperation<T>(message, path, line);

[DebuggerHidden]
[DoesNotReturn]
[MethodImpl(MethodImplOptions.NoInlining)]
private static void ThrowInvalidOperation(string message, string? path, int line)
{
throw new InvalidOperationException(message);
}
=> ThrowHelper.ThrowInvalidOperationException(message + Environment.NewLine + SR.FormatFile_0_Line_1(path, line));

[DebuggerHidden]
[DoesNotReturn]
private static T ThrowInvalidOperation<T>(string message, string? path, int line)
=> ThrowHelper.ThrowInvalidOperationException<T>(message + Environment.NewLine + SR.FormatFile_0_Line_1(path, line));
}