Skip to content

Commit

Permalink
Parse [] after nullable type as part of the type
Browse files Browse the repository at this point in the history
  • Loading branch information
cston committed Jul 22, 2021
1 parent d37aab1 commit 813334a
Show file tree
Hide file tree
Showing 4 changed files with 933 additions and 28 deletions.
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
23 changes: 23 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,29 @@ 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 Program
{
static void F(object x)
{
_ = x switch
{
string?[] y => y
};
}
}";
var comp = CreateCompilation(source);
comp.VerifyDiagnostics(
// (6,15): 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.
// _ = x switch
Diagnostic(ErrorCode.WRN_SwitchExpressionNotExhaustive, "switch").WithArguments("_").WithLocation(6, 15));
}

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

0 comments on commit 813334a

Please sign in to comment.