diff --git a/src/Compilers/CSharp/Portable/Binder/Binder_Patterns.cs b/src/Compilers/CSharp/Portable/Binder/Binder_Patterns.cs index 2b3a6019634561e6d7be996ebf7c3f352de3fcb4..0c0a549dff430d300c12a157e15c43fdeb832125 100644 --- a/src/Compilers/CSharp/Portable/Binder/Binder_Patterns.cs +++ b/src/Compilers/CSharp/Portable/Binder/Binder_Patterns.cs @@ -658,14 +658,16 @@ private BoundPattern BindPropertyPattern(PropertyPatternSyntax node, TypeSymbol { Symbol symbol = BindPropertyPatternMember(inputType, name, ref hasErrors, diagnostics); - if (inputType.IsErrorType() || hasErrors) + if (inputType.IsErrorType() || hasErrors || symbol == (object)null) { memberType = CreateErrorType(); return null; } - - memberType = symbol.GetTypeOrReturnType(); - return symbol; + else + { + memberType = symbol.GetTypeOrReturnType(); + return symbol; + } } private Symbol BindPropertyPatternMember( @@ -726,16 +728,18 @@ private BoundPattern BindPropertyPattern(PropertyPatternSyntax node, TypeSymbol default: Error(diagnostics, ErrorCode.ERR_PropertyLacksGet, memberName, name); - hasErrors = true; break; } } + + hasErrors = true; return null; } if (hasErrors || !CheckValueKind(node: memberName.Parent, expr: boundMember, valueKind: BindValueKind.RValue, checkingReceiver: false, diagnostics: diagnostics)) { + hasErrors = true; return null; } diff --git a/src/Compilers/CSharp/Test/Semantic/Semantics/PatternMatchingTests2.cs b/src/Compilers/CSharp/Test/Semantic/Semantics/PatternMatchingTests2.cs index 5e71ea5e1eab6b3add5ceb50b34e6ed2d208c3d5..c0f06540b1947fe33aefd813e05bf66348cf5968 100644 --- a/src/Compilers/CSharp/Test/Semantic/Semantics/PatternMatchingTests2.cs +++ b/src/Compilers/CSharp/Test/Semantic/Semantics/PatternMatchingTests2.cs @@ -1254,6 +1254,59 @@ class Cat {} var comp = CompileAndVerify(compilation, expectedOutput: @"Fox Cat"); } + [Fact] + public void PropertyPatternMemberMissing01() + { + var source = +@"class Program +{ + static void Main(string[] args) + { + Blah b = null; + if (b is Blah { X: int i }) + { + } + } +} + +class Blah +{ +}"; + var compilation = CreatePatternCompilation(source); + compilation.VerifyDiagnostics( + // (6,25): error CS0117: 'Blah' does not contain a definition for 'X' + // if (b is Blah { X: int i }) + Diagnostic(ErrorCode.ERR_NoSuchMember, "X").WithArguments("Blah", "X").WithLocation(6, 25) + ); + } + + [Fact] + public void PropertyPatternMemberMissing02() + { + var source = +@"class Program +{ + static void Main(string[] args) + { + Blah b = null; + if (b is Blah { X: int i }) + { + } + } +} + +class Blah +{ + public int X { set {} } +}"; + var compilation = CreatePatternCompilation(source); + compilation.VerifyDiagnostics( + // (6,25): error CS0154: The property or indexer 'Blah.X' cannot be used in this context because it lacks the get accessor + // if (b is Blah { X: int i }) + Diagnostic(ErrorCode.ERR_PropertyLacksGet, "X:").WithArguments("Blah.X").WithLocation(6, 25) + ); + } + // PROTOTYPE(patterns2): Need to have tests that exercise: // PROTOTYPE(patterns2): Building the decision tree for the var-pattern // PROTOTYPE(patterns2): Definite assignment for the var-pattern