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

Fix parser regressions of conditional expressions #75075

Merged
merged 6 commits into from
Sep 24, 2024
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
17 changes: 12 additions & 5 deletions src/Compilers/CSharp/Portable/Parser/LanguageParser.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7425,16 +7425,23 @@ bool canFollowNullableType()
// nullable-typed pattern
if (IsTrueIdentifier(this.CurrentToken))
{
// In a non-async method, `await` is a simple identifier. However, if we see `x ? await`
// 1. `async` can start a simple lambda in a conditional expression
// (e.g. `x is Y ? async a => ...`). The correct behavior is to treat `async` as a keyword
// 2. In a non-async method, `await` is a simple identifier. However, if we see `x ? await`
// it's almost certainly the start of an `await expression` in a conditional expression
// (e.g. `x is Y ? await ...`), not a nullable type pattern (since users would not use
// 'await' as the name of a variable). So just treat this as a conditional expression.
// Similarly, `async` can start a simple lambda in a conditional expression
// (e.g. `x is Y ? async a => ...`). The correct behavior is to treat `async` as a keyword
if (this.CurrentToken.ContextualKind is SyntaxKind.AsyncKeyword or SyntaxKind.AwaitKeyword)
// 3. `from` most likely starts a linq query: `x is Y ? from item in collection select item : ...`
if (this.CurrentToken.ContextualKind is SyntaxKind.AsyncKeyword or SyntaxKind.AwaitKeyword or SyntaxKind.FromKeyword)
jcouv marked this conversation as resolved.
Show resolved Hide resolved
return false;

var nextToken = PeekToken(1);

// Cases like `x is Y ? someRecord with { } : ...`
if (nextToken.ContextualKind == SyntaxKind.WithKeyword)
return false;

var nextTokenKind = PeekToken(1).Kind;
var nextTokenKind = nextToken.Kind;

// These token either 100% end a pattern or start a new one:

Expand Down
201 changes: 201 additions & 0 deletions src/Compilers/CSharp/Test/Syntax/Parsing/ExpressionParsingTests.cs
Copy link
Member

Choose a reason for hiding this comment

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

Do we have tests for:

  • global
  • yield

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Added a test for global qualified name. As of yield, since it is only valid as a yield return and yield break statements, it cannot be a contextual keyword inside an expression. However, this gave me an idea: if we have something like x is X ? yield return 0 ... we can parse it a lot better we we consider x is X? yield as one statement and return 0 ... as another one with missing ; in between. This is actually true for most keywords that start a statement: if, while, for etc. For now I added 2 yield tests with the right shape to just swap the errors and parse result in the future, but when main branch targets 17.13, I'm gnna make a follow-up PR with imrpoved error recovery for such cases

Original file line number Diff line number Diff line change
Expand Up @@ -6822,5 +6822,206 @@ public void UnsignedRightShiftAssignment_04()
EOF();
}
}

[Fact, WorkItem("https://github.com/dotnet/roslyn/issues/75074")]
public void LinqQueryInConditionalExpression1()
{
var text = "x is X ? from item in collection select item : null";

UsingExpression(text);
N(SyntaxKind.ConditionalExpression);
{
N(SyntaxKind.IsExpression);
{
N(SyntaxKind.IdentifierName);
{
N(SyntaxKind.IdentifierToken, "x");
}
N(SyntaxKind.IsKeyword);
N(SyntaxKind.IdentifierName);
{
N(SyntaxKind.IdentifierToken, "X");
}
}
N(SyntaxKind.QuestionToken);
N(SyntaxKind.QueryExpression);
{
N(SyntaxKind.FromClause);
{
N(SyntaxKind.FromKeyword);
N(SyntaxKind.IdentifierToken, "item");
N(SyntaxKind.InKeyword);
N(SyntaxKind.IdentifierName);
{
N(SyntaxKind.IdentifierToken, "collection");
}
}
N(SyntaxKind.QueryBody);
{
N(SyntaxKind.SelectClause);
{
N(SyntaxKind.SelectKeyword);
N(SyntaxKind.IdentifierName);
{
N(SyntaxKind.IdentifierToken, "item");
}
}
}
}
N(SyntaxKind.ColonToken);
N(SyntaxKind.NullLiteralExpression);
{
N(SyntaxKind.NullKeyword);
}
}
EOF();
}

[Fact, WorkItem("https://github.com/dotnet/roslyn/issues/75074")]
public void LinqQueryInConditionalExpression2()
{
var text = "x is X.Y ? from item in collection select item : null";

UsingExpression(text);
N(SyntaxKind.ConditionalExpression);
{
N(SyntaxKind.IsExpression);
{
N(SyntaxKind.IdentifierName);
{
N(SyntaxKind.IdentifierToken, "x");
}
N(SyntaxKind.IsKeyword);
N(SyntaxKind.QualifiedName);
{
N(SyntaxKind.IdentifierName);
{
N(SyntaxKind.IdentifierToken, "X");
}
N(SyntaxKind.DotToken);
N(SyntaxKind.IdentifierName);
{
N(SyntaxKind.IdentifierToken, "Y");
}
}
}
N(SyntaxKind.QuestionToken);
N(SyntaxKind.QueryExpression);
{
N(SyntaxKind.FromClause);
{
N(SyntaxKind.FromKeyword);
N(SyntaxKind.IdentifierToken, "item");
N(SyntaxKind.InKeyword);
N(SyntaxKind.IdentifierName);
{
N(SyntaxKind.IdentifierToken, "collection");
}
}
N(SyntaxKind.QueryBody);
{
N(SyntaxKind.SelectClause);
{
N(SyntaxKind.SelectKeyword);
N(SyntaxKind.IdentifierName);
{
N(SyntaxKind.IdentifierToken, "item");
}
}
}
}
N(SyntaxKind.ColonToken);
N(SyntaxKind.NullLiteralExpression);
{
N(SyntaxKind.NullKeyword);
}
}
EOF();
}

[Fact, WorkItem("https://github.com/dotnet/roslyn/issues/75074")]
public void LinqQueryInConditionalExpression_Incomplete()
{
var text = "x is X.Y ? from item";

UsingExpression(text,
// (1,21): error CS1001: Identifier expected
// x is X.Y ? from item
Diagnostic(ErrorCode.ERR_IdentifierExpected, "").WithLocation(1, 21),
// (1,21): error CS1003: Syntax error, 'in' expected
// x is X.Y ? from item
Diagnostic(ErrorCode.ERR_SyntaxError, "").WithArguments("in").WithLocation(1, 21),
// (1,21): error CS1733: Expected expression
// x is X.Y ? from item
Diagnostic(ErrorCode.ERR_ExpressionExpected, "").WithLocation(1, 21),
// (1,21): error CS0742: A query body must end with a select clause or a group clause
// x is X.Y ? from item
Diagnostic(ErrorCode.ERR_ExpectedSelectOrGroup, "").WithLocation(1, 21),
// (1,21): error CS1003: Syntax error, ':' expected
// x is X.Y ? from item
Diagnostic(ErrorCode.ERR_SyntaxError, "").WithArguments(":").WithLocation(1, 21),
// (1,21): error CS1733: Expected expression
// x is X.Y ? from item
Diagnostic(ErrorCode.ERR_ExpressionExpected, "").WithLocation(1, 21));

N(SyntaxKind.ConditionalExpression);
{
N(SyntaxKind.IsExpression);
{
N(SyntaxKind.IdentifierName);
{
N(SyntaxKind.IdentifierToken, "x");
}
N(SyntaxKind.IsKeyword);
N(SyntaxKind.QualifiedName);
{
N(SyntaxKind.IdentifierName);
{
N(SyntaxKind.IdentifierToken, "X");
}
N(SyntaxKind.DotToken);
N(SyntaxKind.IdentifierName);
{
N(SyntaxKind.IdentifierToken, "Y");
}
}
}
N(SyntaxKind.QuestionToken);
N(SyntaxKind.QueryExpression);
{
N(SyntaxKind.FromClause);
{
N(SyntaxKind.FromKeyword);
N(SyntaxKind.IdentifierName);
{
N(SyntaxKind.IdentifierToken, "item");
}
M(SyntaxKind.IdentifierToken);
M(SyntaxKind.InKeyword);
M(SyntaxKind.IdentifierName);
{
M(SyntaxKind.IdentifierToken);
}
}
M(SyntaxKind.QueryBody);
{
M(SyntaxKind.SelectClause);
{
M(SyntaxKind.SelectKeyword);
M(SyntaxKind.IdentifierName);
{
M(SyntaxKind.IdentifierToken);
}
}
}
}
M(SyntaxKind.ColonToken);
M(SyntaxKind.IdentifierName);
{
M(SyntaxKind.IdentifierToken);
}
}
EOF();
}
}
}
150 changes: 150 additions & 0 deletions src/Compilers/CSharp/Test/Syntax/Parsing/RecordParsing.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2099,6 +2099,156 @@ public void WithParsing19()
EOF();
}

[Fact, WorkItem("https://github.com/dotnet/roslyn/issues/75074")]
public void WithParsingInConditionalExpression1()
{
var text = "x is X ? record with { } : record with { }";

UsingExpression(text);
N(SyntaxKind.ConditionalExpression);
{
N(SyntaxKind.IsExpression);
{
N(SyntaxKind.IdentifierName);
{
N(SyntaxKind.IdentifierToken, "x");
}
N(SyntaxKind.IsKeyword);
N(SyntaxKind.IdentifierName);
{
N(SyntaxKind.IdentifierToken, "X");
}
}
N(SyntaxKind.QuestionToken);
N(SyntaxKind.WithExpression);
{
N(SyntaxKind.IdentifierName);
{
N(SyntaxKind.IdentifierToken, "record");
}
N(SyntaxKind.WithKeyword);
N(SyntaxKind.WithInitializerExpression);
{
N(SyntaxKind.OpenBraceToken);
N(SyntaxKind.CloseBraceToken);
}
}
N(SyntaxKind.ColonToken);
N(SyntaxKind.WithExpression);
{
N(SyntaxKind.IdentifierName);
{
N(SyntaxKind.IdentifierToken, "record");
}
N(SyntaxKind.WithKeyword);
N(SyntaxKind.WithInitializerExpression);
{
N(SyntaxKind.OpenBraceToken);
N(SyntaxKind.CloseBraceToken);
}
}
}
EOF();
}

[Fact, WorkItem("https://github.com/dotnet/roslyn/issues/75074")]
public void WithParsingInConditionalExpression2()
{
var text = "x is X.Y ? record with { } : record with { }";

UsingExpression(text);
N(SyntaxKind.ConditionalExpression);
{
N(SyntaxKind.IsExpression);
{
N(SyntaxKind.IdentifierName);
{
N(SyntaxKind.IdentifierToken, "x");
}
N(SyntaxKind.IsKeyword);
N(SyntaxKind.QualifiedName);
{
N(SyntaxKind.IdentifierName);
{
N(SyntaxKind.IdentifierToken, "X");
}
N(SyntaxKind.DotToken);
N(SyntaxKind.IdentifierName);
{
N(SyntaxKind.IdentifierToken, "Y");
}
}
}
N(SyntaxKind.QuestionToken);
N(SyntaxKind.WithExpression);
{
N(SyntaxKind.IdentifierName);
{
N(SyntaxKind.IdentifierToken, "record");
}
N(SyntaxKind.WithKeyword);
N(SyntaxKind.WithInitializerExpression);
{
N(SyntaxKind.OpenBraceToken);
N(SyntaxKind.CloseBraceToken);
}
}
N(SyntaxKind.ColonToken);
N(SyntaxKind.WithExpression);
{
N(SyntaxKind.IdentifierName);
{
N(SyntaxKind.IdentifierToken, "record");
}
N(SyntaxKind.WithKeyword);
N(SyntaxKind.WithInitializerExpression);
{
N(SyntaxKind.OpenBraceToken);
N(SyntaxKind.CloseBraceToken);
}
}
}
EOF();
}

[Fact, WorkItem("https://github.com/dotnet/roslyn/issues/75074")]
public void WithParsingInConditionalExpression_Incomplete()
{
var text = "x is X ? record with";

UsingExpression(text,
// (1,17): error CS1003: Syntax error, ':' expected
// x is X ? record with
Diagnostic(ErrorCode.ERR_SyntaxError, "with").WithArguments(":").WithLocation(1, 17));

N(SyntaxKind.ConditionalExpression);
{
N(SyntaxKind.IsExpression);
{
N(SyntaxKind.IdentifierName);
{
N(SyntaxKind.IdentifierToken, "x");
}
N(SyntaxKind.IsKeyword);
N(SyntaxKind.IdentifierName);
{
N(SyntaxKind.IdentifierToken, "X");
}
}
N(SyntaxKind.QuestionToken);
N(SyntaxKind.IdentifierName);
{
N(SyntaxKind.IdentifierToken, "record");
}
M(SyntaxKind.ColonToken);
N(SyntaxKind.IdentifierName);
{
N(SyntaxKind.IdentifierToken, "with");
}
}
EOF();
}

[Fact]
public void ParameterListAndBaseListOnClass()
{
Expand Down