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

Parse [ after nullable type as part of the type #55044

Merged
merged 2 commits into from
Jul 23, 2021
Merged
Show file tree
Hide file tree
Changes from all 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
8 changes: 4 additions & 4 deletions src/Compilers/CSharp/Portable/Parser/LanguageParser.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9723,18 +9723,18 @@ private ExpressionSyntax ParseExpressionCore()
/// </summary>
private bool CanStartExpression()
{
return IsPossibleExpression(allowBinaryExpressions: false, allowAssignmentExpressions: false);
return IsPossibleExpression(allowBinaryExpressions: false, allowAssignmentExpressions: false, allowAttributes: false);
}

/// <summary>
/// Is the current token one that could be in an expression?
/// </summary>
private bool IsPossibleExpression()
{
return IsPossibleExpression(allowBinaryExpressions: true, allowAssignmentExpressions: true);
return IsPossibleExpression(allowBinaryExpressions: true, allowAssignmentExpressions: true, allowAttributes: true);
}

private bool IsPossibleExpression(bool allowBinaryExpressions, bool allowAssignmentExpressions)
private bool IsPossibleExpression(bool allowBinaryExpressions, bool allowAssignmentExpressions, bool allowAttributes)
{
SyntaxKind tk = this.CurrentToken.Kind;
switch (tk)
Expand Down Expand Up @@ -9770,7 +9770,7 @@ private bool IsPossibleExpression(bool allowBinaryExpressions, bool allowAssignm
case SyntaxKind.StaticKeyword:
return IsPossibleAnonymousMethodExpression() || IsPossibleLambdaExpression(Precedence.Expression);
case SyntaxKind.OpenBracketToken:
return IsPossibleLambdaExpression(Precedence.Expression);
return allowAttributes && IsPossibleLambdaExpression(Precedence.Expression);
case SyntaxKind.IdentifierToken:
// Specifically allow the from contextual keyword, because it can always be the start of an
// expression (whether it is used as an identifier or a keyword).
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -599,7 +599,7 @@ private SubpatternSyntax ParseSubpatternElement()
/// </summary>
private bool IsPossibleSubpatternElement()
{
return this.IsPossibleExpression(allowBinaryExpressions: false, allowAssignmentExpressions: false) ||
return this.IsPossibleExpression(allowBinaryExpressions: false, allowAssignmentExpressions: false, allowAttributes: false) ||
this.CurrentToken.Kind switch
{
SyntaxKind.OpenBraceToken => true,
Expand Down
24 changes: 24 additions & 0 deletions src/Compilers/CSharp/Test/Semantic/Semantics/LambdaTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4173,6 +4173,30 @@ static void Main()
Diagnostic(ErrorCode.WRN_NullReferenceReceiver, "x").WithLocation(8, 50));
}

[WorkItem(55013, "https://github.com/dotnet/roslyn/issues/55013")]
[Fact]
public void NullableTypeArraySwitchPattern()
{
var source =
@"#nullable enable
class C
{
object? field;
string Prop => field switch
{
string?[] a => ""a""
};
}";
var comp = CreateCompilation(source);
comp.VerifyDiagnostics(
// (4,13): warning CS0649: Field 'C.field' is never assigned to, and will always have its default value null
// object? field;
Diagnostic(ErrorCode.WRN_UnassignedInternalField, "field").WithArguments("C.field", "null").WithLocation(4, 13),
// (5,26): warning CS8509: The switch expression does not handle all possible values of its input type (it is not exhaustive). For example, the pattern '_' is not covered.
// string Prop => field switch
Diagnostic(ErrorCode.WRN_SwitchExpressionNotExhaustive, "switch").WithArguments("_").WithLocation(5, 26));
}

[Fact]
public void LambdaAttributes_DoesNotReturn()
{
Expand Down
Loading