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 1 commit
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: 7 additions & 1 deletion src/Compilers/CSharp/Portable/Parser/LanguageParser.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7434,7 +7434,13 @@ bool canFollowNullableType()
if (this.CurrentToken.ContextualKind is SyntaxKind.AsyncKeyword or SyntaxKind.AwaitKeyword)
return false;

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

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

var nextTokenKind = nextToken.Kind;

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

Expand Down
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
Loading