Skip to content

Commit

Permalink
Parse [ after nullable type as part of the type (dotnet#55044)
Browse files Browse the repository at this point in the history
  • Loading branch information
cston authored Jul 23, 2021
1 parent 1111703 commit ebbae5b
Show file tree
Hide file tree
Showing 4 changed files with 1,506 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
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

0 comments on commit ebbae5b

Please sign in to comment.