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

Include file name in Contract failures #73092

Merged
merged 1 commit into from
Apr 19, 2024
Merged
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
Original file line number Diff line number Diff line change
Expand Up @@ -1019,7 +1019,7 @@ private static void ValidateCompilationTreesMatchesProjectState(Compilation comp
}

/// <summary>
/// This is just the same as <see cref="Contract.ThrowIfFalse(bool, string, int)"/> but throws a custom exception type to make this easier to find in telemetry since the exception type
/// This is just the same as <see cref="Contract.ThrowIfFalse(bool, string, int, string)"/> but throws a custom exception type to make this easier to find in telemetry since the exception type
/// is easily seen in telemetry.
/// </summary>
private static void ThrowExceptionIfFalse([DoesNotReturnIf(parameterValue: false)] bool condition, string message)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
using System;
using System.Diagnostics;
using System.Diagnostics.CodeAnalysis;
using System.IO;
using System.Runtime.CompilerServices;

namespace Roslyn.Utilities;
Expand All @@ -22,11 +23,11 @@ internal static partial class Contract
/// all builds
/// </summary>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static void ThrowIfNull<T>([NotNull] T value, [CallerLineNumber] int lineNumber = 0) where T : class?
public static void ThrowIfNull<T>([NotNull] T value, [CallerLineNumber] int lineNumber = 0, [CallerFilePath] string? filePath = null) where T : class?
{
if (value is null)
{
Fail("Unexpected null", lineNumber);
Fail("Unexpected null", lineNumber, filePath);
}
}

Expand All @@ -35,11 +36,11 @@ public static void ThrowIfNull<T>([NotNull] T value, [CallerLineNumber] int line
/// all builds
/// </summary>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static void ThrowIfNull<T>([NotNull] T? value, [CallerLineNumber] int lineNumber = 0) where T : struct
public static void ThrowIfNull<T>([NotNull] T? value, [CallerLineNumber] int lineNumber = 0, [CallerFilePath] string? filePath = null) where T : struct
{
if (value is null)
{
Fail("Unexpected null", lineNumber);
Fail("Unexpected null", lineNumber, filePath);
}
}

Expand All @@ -48,11 +49,11 @@ public static void ThrowIfNull<T>([NotNull] T? value, [CallerLineNumber] int lin
/// all builds
/// </summary>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static void ThrowIfNull<T>([NotNull] T value, string message, [CallerLineNumber] int lineNumber = 0)
public static void ThrowIfNull<T>([NotNull] T value, string message, [CallerLineNumber] int lineNumber = 0, [CallerFilePath] string? filePath = null)
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Note: the filePath is last specifically to avoid ambiguities with Contract methods that take an explicit string message like this one.

{
if (value is null)
{
Fail(message, lineNumber);
Fail(message, lineNumber, filePath);
}
}

Expand All @@ -61,11 +62,11 @@ public static void ThrowIfNull<T>([NotNull] T value, string message, [CallerLine
/// all builds
/// </summary>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static void ThrowIfNull<T>([NotNull] T value, [InterpolatedStringHandlerArgument("value")] ThrowIfNullInterpolatedStringHandler<T> message, [CallerLineNumber] int lineNumber = 0)
public static void ThrowIfNull<T>([NotNull] T value, [InterpolatedStringHandlerArgument("value")] ThrowIfNullInterpolatedStringHandler<T> message, [CallerLineNumber] int lineNumber = 0, [CallerFilePath] string? filePath = null)
{
if (value is null)
{
Fail(message.GetFormattedText(), lineNumber);
Fail(message.GetFormattedText(), lineNumber, filePath);
}
}

Expand All @@ -74,11 +75,11 @@ public static void ThrowIfNull<T>([NotNull] T value, [InterpolatedStringHandlerA
/// in all builds
/// </summary>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static void ThrowIfFalse([DoesNotReturnIf(parameterValue: false)] bool condition, [CallerLineNumber] int lineNumber = 0)
public static void ThrowIfFalse([DoesNotReturnIf(parameterValue: false)] bool condition, [CallerLineNumber] int lineNumber = 0, [CallerFilePath] string? filePath = null)
{
if (!condition)
{
Fail("Unexpected false", lineNumber);
Fail("Unexpected false", lineNumber, filePath);
}
}

Expand All @@ -87,11 +88,11 @@ public static void ThrowIfFalse([DoesNotReturnIf(parameterValue: false)] bool co
/// in all builds
/// </summary>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static void ThrowIfFalse([DoesNotReturnIf(parameterValue: false)] bool condition, string message, [CallerLineNumber] int lineNumber = 0)
public static void ThrowIfFalse([DoesNotReturnIf(parameterValue: false)] bool condition, string message, [CallerLineNumber] int lineNumber = 0, [CallerFilePath] string? filePath = null)
{
if (!condition)
{
Fail(message, lineNumber);
Fail(message, lineNumber, filePath);
}
}

Expand All @@ -100,11 +101,11 @@ public static void ThrowIfFalse([DoesNotReturnIf(parameterValue: false)] bool co
/// in all builds
/// </summary>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static void ThrowIfFalse([DoesNotReturnIf(parameterValue: false)] bool condition, [InterpolatedStringHandlerArgument("condition")] ThrowIfFalseInterpolatedStringHandler message, [CallerLineNumber] int lineNumber = 0)
public static void ThrowIfFalse([DoesNotReturnIf(parameterValue: false)] bool condition, [InterpolatedStringHandlerArgument("condition")] ThrowIfFalseInterpolatedStringHandler message, [CallerLineNumber] int lineNumber = 0, [CallerFilePath] string? filePath = null)
{
if (!condition)
{
Fail(message.GetFormattedText(), lineNumber);
Fail(message.GetFormattedText(), lineNumber, filePath);
}
}

Expand All @@ -113,11 +114,11 @@ public static void ThrowIfFalse([DoesNotReturnIf(parameterValue: false)] bool co
/// all builds.
/// </summary>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static void ThrowIfTrue([DoesNotReturnIf(parameterValue: true)] bool condition, [CallerLineNumber] int lineNumber = 0)
public static void ThrowIfTrue([DoesNotReturnIf(parameterValue: true)] bool condition, [CallerLineNumber] int lineNumber = 0, [CallerFilePath] string? filePath = null)
{
if (condition)
{
Fail("Unexpected true", lineNumber);
Fail("Unexpected true", lineNumber, filePath);
}
}

Expand All @@ -126,11 +127,11 @@ public static void ThrowIfTrue([DoesNotReturnIf(parameterValue: true)] bool cond
/// all builds.
/// </summary>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static void ThrowIfTrue([DoesNotReturnIf(parameterValue: true)] bool condition, string message, [CallerLineNumber] int lineNumber = 0)
public static void ThrowIfTrue([DoesNotReturnIf(parameterValue: true)] bool condition, string message, [CallerLineNumber] int lineNumber = 0, [CallerFilePath] string? filePath = null)
{
if (condition)
{
Fail(message, lineNumber);
Fail(message, lineNumber, filePath);
}
}

Expand All @@ -139,17 +140,20 @@ public static void ThrowIfTrue([DoesNotReturnIf(parameterValue: true)] bool cond
/// all builds.
/// </summary>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static void ThrowIfTrue([DoesNotReturnIf(parameterValue: true)] bool condition, [InterpolatedStringHandlerArgument("condition")] ThrowIfTrueInterpolatedStringHandler message, [CallerLineNumber] int lineNumber = 0)
public static void ThrowIfTrue([DoesNotReturnIf(parameterValue: true)] bool condition, [InterpolatedStringHandlerArgument("condition")] ThrowIfTrueInterpolatedStringHandler message, [CallerLineNumber] int lineNumber = 0, [CallerFilePath] string? filePath = null)
{
if (condition)
{
Fail(message.GetFormattedText(), lineNumber);
Fail(message.GetFormattedText(), lineNumber, filePath);
}
}

[DebuggerHidden]
[DoesNotReturn]
[MethodImpl(MethodImplOptions.NoInlining)]
public static void Fail(string message = "Unexpected", [CallerLineNumber] int lineNumber = 0)
=> throw new InvalidOperationException($"{message} - line {lineNumber}");
public static void Fail(string message = "Unexpected", [CallerLineNumber] int lineNumber = 0, [CallerFilePath] string? filePath = null)
{
var fileName = filePath is null ? null : Path.GetFileName(filePath);
throw new InvalidOperationException($"{message} - file {fileName} line {lineNumber}");
}
}
Loading