From 79ae1b3fde730dd29bb65804eda422c3663694d0 Mon Sep 17 00:00:00 2001 From: Maryam Ariyan Date: Mon, 28 Jan 2019 14:07:37 -0800 Subject: [PATCH] Add SwitchExpressionException Fixes: #33284 --- .../ref/System.Runtime.Extensions.cs | 11 +++++ .../src/Resources/Strings.resx | 3 ++ .../src/System.Runtime.Extensions.csproj | 1 + .../SwitchExpressionException.cs | 41 +++++++++++++++++++ .../System.Runtime.Extensions.Tests.csproj | 1 + .../SwitchExpressionExceptionTests.cs | 29 +++++++++++++ 6 files changed, 86 insertions(+) create mode 100644 src/System.Runtime.Extensions/src/System/Runtime/CompilerServices/SwitchExpressionException.cs create mode 100644 src/System.Runtime.Extensions/tests/System/Runtime/CompilerServices/SwitchExpressionExceptionTests.cs diff --git a/src/System.Runtime.Extensions/ref/System.Runtime.Extensions.cs b/src/System.Runtime.Extensions/ref/System.Runtime.Extensions.cs index ceab9463af72..74fdf9090e6b 100644 --- a/src/System.Runtime.Extensions/ref/System.Runtime.Extensions.cs +++ b/src/System.Runtime.Extensions/ref/System.Runtime.Extensions.cs @@ -1842,6 +1842,17 @@ public static class ProfileOptimization public static void StartProfile(string profile) { throw null; } } } +namespace System.Runtime.CompilerServices +{ + [Serializable] + public sealed class SwitchExpressionException : System.InvalidOperationException + { + public SwitchExpressionException() { } + public SwitchExpressionException(object unmatchedValue) { } + public object UnmatchedValue { get { throw null; } } + public override void GetObjectData(System.Runtime.Serialization.SerializationInfo info, System.Runtime.Serialization.StreamingContext context) { } + } +} namespace System.Runtime.Versioning { public sealed partial class FrameworkName : System.IEquatable diff --git a/src/System.Runtime.Extensions/src/Resources/Strings.resx b/src/System.Runtime.Extensions/src/Resources/Strings.resx index 525c175c7a00..20981ccb5fc1 100644 --- a/src/System.Runtime.Extensions/src/Resources/Strings.resx +++ b/src/System.Runtime.Extensions/src/Resources/Strings.resx @@ -166,6 +166,9 @@ Attempted to access an unloaded AppDomain. + + Non-exhaustive switch expression failed to match its input. + String cannot have zero length. diff --git a/src/System.Runtime.Extensions/src/System.Runtime.Extensions.csproj b/src/System.Runtime.Extensions/src/System.Runtime.Extensions.csproj index 107a833b0969..4a09272b3ac7 100644 --- a/src/System.Runtime.Extensions/src/System.Runtime.Extensions.csproj +++ b/src/System.Runtime.Extensions/src/System.Runtime.Extensions.csproj @@ -44,6 +44,7 @@ + diff --git a/src/System.Runtime.Extensions/src/System/Runtime/CompilerServices/SwitchExpressionException.cs b/src/System.Runtime.Extensions/src/System/Runtime/CompilerServices/SwitchExpressionException.cs new file mode 100644 index 000000000000..f227cf3cfd4b --- /dev/null +++ b/src/System.Runtime.Extensions/src/System/Runtime/CompilerServices/SwitchExpressionException.cs @@ -0,0 +1,41 @@ +// Licensed to the .NET Foundation under one or more agreements. +// The .NET Foundation licenses this file to you under the MIT license. +// See the LICENSE file in the project root for more information. + +using System.Runtime.Serialization; + +namespace System.Runtime.CompilerServices +{ + /// + /// Indicates that a switch expression that was non-exhaustive failed to match its input + /// at runtime, e.g. in the C# 8 expression 3 switch { 4 => 5 }. + /// The exception optionally contains an object representing the unmatched value. + /// + [Serializable] + [System.Runtime.CompilerServices.TypeForwardedFrom("mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089")] + public sealed class SwitchExpressionException : InvalidOperationException//, ISerializable + { + public SwitchExpressionException() + : base(SR.Arg_SwitchExpressionException) { } + + public SwitchExpressionException(object unmatchedValue) + : base() + { + UnmatchedValue = unmatchedValue; + } + + private SwitchExpressionException(SerializationInfo info, StreamingContext context) + : base(info, context) + { + info.AddValue(nameof(UnmatchedValue), UnmatchedValue, typeof(object)); + } + + public object UnmatchedValue { get; } + + public override void GetObjectData(SerializationInfo info, StreamingContext context) + { + base.GetObjectData(info, context); + info.AddValue(nameof(UnmatchedValue), UnmatchedValue, typeof(object)); + } + } +} \ No newline at end of file diff --git a/src/System.Runtime.Extensions/tests/System.Runtime.Extensions.Tests.csproj b/src/System.Runtime.Extensions/tests/System.Runtime.Extensions.Tests.csproj index 623e2520f7c4..64b7682300cd 100644 --- a/src/System.Runtime.Extensions/tests/System.Runtime.Extensions.Tests.csproj +++ b/src/System.Runtime.Extensions/tests/System.Runtime.Extensions.Tests.csproj @@ -50,6 +50,7 @@ + diff --git a/src/System.Runtime.Extensions/tests/System/Runtime/CompilerServices/SwitchExpressionExceptionTests.cs b/src/System.Runtime.Extensions/tests/System/Runtime/CompilerServices/SwitchExpressionExceptionTests.cs new file mode 100644 index 000000000000..e7622a2cbd77 --- /dev/null +++ b/src/System.Runtime.Extensions/tests/System/Runtime/CompilerServices/SwitchExpressionExceptionTests.cs @@ -0,0 +1,29 @@ +// Licensed to the .NET Foundation under one or more agreements. +// The .NET Foundation licenses this file to you under the MIT license. +// See the LICENSE file in the project root for more information. + +using System.Runtime.CompilerServices; +using Xunit; + +namespace System.Runtime.CompilerServices.Tests +{ + public class SwitchExpressionExceptionTests + { + [Fact] + public void UnmatchedValue() + { + var ex = new SwitchExpressionException(34); + Assert.Equal(34, ex.UnmatchedValue); + + var data = new byte[] { 1, 2, 3 }; + ex = new SwitchExpressionException(data); + Assert.Equal(data, ex.UnmatchedValue); + + ex = new SwitchExpressionException(true); + Assert.Equal(true, ex.UnmatchedValue); + + ex = new SwitchExpressionException("34"); + Assert.Equal("34", ex.UnmatchedValue); + } + } +}