Add RequiresDynamicCode attribute to exception formatter to indicate incompatability with AOT

This commit is contained in:
Phil Scott 2024-11-19 23:16:09 -05:00 committed by Patrik Svensson
parent 10773a5625
commit b67af32423
4 changed files with 12 additions and 0 deletions

View File

@ -10,6 +10,7 @@ public static partial class AnsiConsole
/// </summary>
/// <param name="exception">The exception to write to the console.</param>
/// <param name="format">The exception format options.</param>
[RequiresDynamicCode(ExceptionFormatter.AotWarning)]
public static void WriteException(Exception exception, ExceptionFormats format = ExceptionFormats.Default)
{
Console.WriteException(exception, format);
@ -20,6 +21,7 @@ public static partial class AnsiConsole
/// </summary>
/// <param name="exception">The exception to write to the console.</param>
/// <param name="settings">The exception settings.</param>
[RequiresDynamicCode(ExceptionFormatter.AotWarning)]
public static void WriteException(Exception exception, ExceptionSettings settings)
{
Console.WriteException(exception, settings);

View File

@ -11,6 +11,7 @@ public static partial class AnsiConsoleExtensions
/// <param name="console">The console.</param>
/// <param name="exception">The exception to write to the console.</param>
/// <param name="format">The exception format options.</param>
[RequiresDynamicCode(ExceptionFormatter.AotWarning)]
public static void WriteException(this IAnsiConsole console, Exception exception, ExceptionFormats format = ExceptionFormats.Default)
{
console.Write(exception.GetRenderable(format));
@ -22,6 +23,7 @@ public static partial class AnsiConsoleExtensions
/// <param name="console">The console.</param>
/// <param name="exception">The exception to write to the console.</param>
/// <param name="settings">The exception settings.</param>
[RequiresDynamicCode(ExceptionFormatter.AotWarning)]
public static void WriteException(this IAnsiConsole console, Exception exception, ExceptionSettings settings)
{
console.Write(exception.GetRenderable(settings));

View File

@ -11,6 +11,7 @@ public static class ExceptionExtensions
/// <param name="exception">The exception to format.</param>
/// <param name="format">The exception format options.</param>
/// <returns>A <see cref="IRenderable"/> representing the exception.</returns>
[RequiresDynamicCode(ExceptionFormatter.AotWarning)]
public static IRenderable GetRenderable(this Exception exception, ExceptionFormats format = ExceptionFormats.Default)
{
if (exception is null)
@ -30,6 +31,7 @@ public static class ExceptionExtensions
/// <param name="exception">The exception to format.</param>
/// <param name="settings">The exception settings.</param>
/// <returns>A <see cref="IRenderable"/> representing the exception.</returns>
[RequiresDynamicCode(ExceptionFormatter.AotWarning)]
public static IRenderable GetRenderable(this Exception exception, ExceptionSettings settings)
{
if (exception is null)

View File

@ -1,7 +1,12 @@
namespace Spectre.Console;
#pragma warning disable IL2026,IL2070,IL2075,IL3050
internal static class ExceptionFormatter
{
public const string AotWarning = "ExceptionFormatter is currently not supported for AOT.";
[RequiresDynamicCode(ExceptionFormatter.AotWarning)]
public static IRenderable Format(Exception exception, ExceptionSettings settings)
{
if (exception is null)
@ -398,6 +403,7 @@ internal static class ExceptionFormatter
return builder.ToString();
}
[RequiresDynamicCode(ExceptionFormatter.AotWarning)]
private static bool TryResolveStateMachineMethod(ref MethodBase method, out Type declaringType)
{
// https://github.com/dotnet/runtime/blob/v6.0.0/src/libraries/System.Private.CoreLib/src/System/Diagnostics/StackTrace.cs#L400-L455