Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Don't get nullable slot if property sub-pattern has error #59813

Merged
merged 4 commits into from
Mar 1, 2022
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -187,7 +187,8 @@ private void LearnFromAnyNullPatterns(
{
foreach (BoundPropertySubpattern subpattern in rp.Properties)
{
if (subpattern.Member is BoundPropertySubpatternMember member)
if (subpattern.Member is BoundPropertySubpatternMember member
&& member.Symbol.Kind is SymbolKind.Property or SymbolKind.Field)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

subpattern.Member is BoundPropertySubpatternMember { Symbol.Kind: SymbolKind.Property or SymbolKind.Field } member

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That would break for extended property scenario. Will add test to illustrate

{
LearnFromAnyNullPatterns(getExtendedPropertySlot(member, inputSlot), member.Type, subpattern.Pattern);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2746,5 +2746,76 @@ public string M6(C? a)
Diagnostic(ErrorCode.WRN_NullReferenceReceiver, "a").WithLocation(74, 28)
);
}

[Fact, WorkItem(59804, "https://github.com/dotnet/roslyn/issues/59804")]
public void NestedTypeUsedInPropertyPattern()
{
var source = @"
public class Class1
{
public class Inner1
{
}
}

public class Class2
{
public bool Test()
{
Class1 test = null;
test switch { { Inner1: """" } => """" };
}
}
";
var comp = CreateCompilation(source);
comp.VerifyDiagnostics(
// (11,17): error CS0161: 'Class2.Test()': not all code paths return a value
// public bool Test()
Diagnostic(ErrorCode.ERR_ReturnExpected, "Test").WithArguments("Class2.Test()").WithLocation(11, 17),
// (14,9): error CS0201: Only assignment, call, increment, decrement, await, and new object expressions can be used as a statement
// test switch { { Inner1: "" } => "" };
Diagnostic(ErrorCode.ERR_IllegalStatement, @"test switch { { Inner1: """" } => """" }").WithLocation(14, 9),
// (14,25): error CS0572: 'Inner1': cannot reference a type through an expression; try 'Class1.Inner1' instead
// test switch { { Inner1: "" } => "" };
Diagnostic(ErrorCode.ERR_BadTypeReference, "Inner1").WithArguments("Inner1", "Class1.Inner1").WithLocation(14, 25),
// (14,25): error CS0154: The property or indexer 'Inner1' cannot be used in this context because it lacks the get accessor
// test switch { { Inner1: "" } => "" };
Diagnostic(ErrorCode.ERR_PropertyLacksGet, "Inner1").WithArguments("Inner1").WithLocation(14, 25)
);
}

[Fact, WorkItem(59804, "https://github.com/dotnet/roslyn/issues/59804")]
public void MethodUsedInPropertyPattern()
{
var source = @"
public class Class1
{
public void Method()
{
}
}

public class Class2
{
public bool Test()
{
Class1 test = null;
test switch { { Method: """" } => """" };
}
}
";
var comp = CreateCompilation(source);
comp.VerifyDiagnostics(
// (11,17): error CS0161: 'Class2.Test()': not all code paths return a value
// public bool Test()
Diagnostic(ErrorCode.ERR_ReturnExpected, "Test").WithArguments("Class2.Test()").WithLocation(11, 17),
// (14,9): error CS0201: Only assignment, call, increment, decrement, await, and new object expressions can be used as a statement
// test switch { { Method: "" } => "" };
Diagnostic(ErrorCode.ERR_IllegalStatement, @"test switch { { Method: """" } => """" }").WithLocation(14, 9),
// (14,25): error CS0154: The property or indexer 'Method' cannot be used in this context because it lacks the get accessor
// test switch { { Method: "" } => "" };
Diagnostic(ErrorCode.ERR_PropertyLacksGet, "Method").WithArguments("Method").WithLocation(14, 25)
);
}
}
}