diff --git a/src/Compilers/CSharp/Portable/Binder/Binder_Patterns.cs b/src/Compilers/CSharp/Portable/Binder/Binder_Patterns.cs index d4c57bfbcce6e3ab32575f0173188fb4d56512f6..9ec9f7dd06054f256d5d8bb8635a76b685798225 100644 --- a/src/Compilers/CSharp/Portable/Binder/Binder_Patterns.cs +++ b/src/Compilers/CSharp/Portable/Binder/Binder_Patterns.cs @@ -210,8 +210,7 @@ private BoundPattern BindDiscardPattern(DiscardPatternSyntax node, TypeSymbol in { var requiredVersion = MessageID.IDS_FeatureRecursivePatterns.RequiredVersion(); if (Compilation.LanguageVersion < requiredVersion && - // A null pattern can be tested against a type that can be assigned null, even in C# 7.3 - !(expression.ConstantValue == ConstantValue.Null && inputType.CanBeAssignedNull())) + !this.Conversions.ClassifyConversionFromExpression(expression, inputType, ref useSiteDiagnostics).IsImplicit) { diagnostics.Add(ErrorCode.ERR_ConstantPatternVsOpenType, expression.Syntax.Location, inputType, expression.Display, new CSharpRequiredLanguageVersion(requiredVersion)); diff --git a/src/Compilers/CSharp/Test/Semantic/Semantics/PatternMatchingTests2.cs b/src/Compilers/CSharp/Test/Semantic/Semantics/PatternMatchingTests2.cs index 03e748290934fcbef7abf37796ecdc459ffef218..7c7446eeed4b7311f329a479ebb658409d8952e4 100644 --- a/src/Compilers/CSharp/Test/Semantic/Semantics/PatternMatchingTests2.cs +++ b/src/Compilers/CSharp/Test/Semantic/Semantics/PatternMatchingTests2.cs @@ -2317,5 +2317,31 @@ static bool Test4(T t) // return t is "frog"; // 3 Diagnostic(ErrorCode.ERR_ConstantPatternVsOpenType, @"""frog""").WithArguments("T", "string", "preview").WithLocation(17, 21)); } + + [Fact] + [WorkItem(34905, "https://github.com/dotnet/roslyn/issues/34905")] + public void ConstantPatternVsUnconstrainedTypeParameter06() + { + var source = +@"public class C +{ + public enum E + { + V1, V2 + } + + public void M() + { + switch (default(E)) + { + case E.V1: + break; + } + } +} +"; + CreateCompilation(source, options: TestOptions.ReleaseDll).VerifyDiagnostics(); + CreateCompilation(source, options: TestOptions.ReleaseDll, parseOptions: TestOptions.Regular7_3).VerifyDiagnostics(); + } } }