From 0d0406b2be22b6a544c77c3ee868ad8065765e96 Mon Sep 17 00:00:00 2001 From: Neal Gafter Date: Wed, 10 Apr 2019 15:31:34 -0700 Subject: [PATCH] Switch on enum type nested in generic type produces LangVersion error (#34911) Fixes #34905 --- .../CSharp/Portable/Binder/Binder_Patterns.cs | 3 +-- .../Semantics/PatternMatchingTests2.cs | 26 +++++++++++++++++++ 2 files changed, 27 insertions(+), 2 deletions(-) diff --git a/src/Compilers/CSharp/Portable/Binder/Binder_Patterns.cs b/src/Compilers/CSharp/Portable/Binder/Binder_Patterns.cs index d4c57bfbcce..9ec9f7dd060 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 03e74829093..7c7446eeed4 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(); + } } } -- GitLab