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 parsing of for-loops #76476

Merged
merged 10 commits into from
Dec 17, 2024
Merged
Show file tree
Hide file tree
Changes from 6 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
26 changes: 15 additions & 11 deletions src/Compilers/CSharp/Portable/Parser/LanguageParser.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9181,23 +9181,27 @@ private ForStatementSyntax ParseForStatement(SyntaxList<AttributeListSyntax> att
var (variableDeclaration, initializers) = eatVariableDeclarationOrInitializers();

// Pulled out as we need to track this when parsing incrementors to place skipped tokens.
Copy link
Contributor

Choose a reason for hiding this comment

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

It looks like this comment was specific to secondSemicolonToken before, is it still relevant now?

Copy link
Member Author

Choose a reason for hiding this comment

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

yes/ will move.

CyrusNajmabadi marked this conversation as resolved.
Show resolved Hide resolved
SyntaxToken secondSemicolonToken;
var firstSemicolonToken = eatCommaOrSemicolon();
var condition = this.CurrentToken.Kind is not SyntaxKind.SemicolonToken and not SyntaxKind.CommaToken
? this.ParseExpressionCore()
: null;
var secondSemicolonToken = eatCommaOrSemicolon();
CyrusNajmabadi marked this conversation as resolved.
Show resolved Hide resolved
// Do allow semicolons (with diagnostics) in the incrementors list. This allows us to consume
// accidental extra incrementors that should have been separated by commas.
var incrementors = this.CurrentToken.Kind != SyntaxKind.CloseParenToken
? parseForStatementExpressionList(ref secondSemicolonToken, allowSemicolonAsSeparator: true)
: default;

var forStatement = _syntaxFactory.ForStatement(
attributes,
forToken,
openParen,
variableDeclaration,
initializers,
firstSemicolonToken: eatCommaOrSemicolon(),
condition: this.CurrentToken.Kind is not SyntaxKind.SemicolonToken and not SyntaxKind.CommaToken
? this.ParseExpressionCore()
: null,
secondSemicolonToken = eatCommaOrSemicolon(),
// Do allow semicolons (with diagnostics) in the incrementors list. This allows us to consume
// accidental extra incrementors that should have been separated by commas.
incrementors: this.CurrentToken.Kind != SyntaxKind.CloseParenToken
? parseForStatementExpressionList(ref secondSemicolonToken, allowSemicolonAsSeparator: true)
: default,
firstSemicolonToken,
condition,
secondSemicolonToken,
incrementors,
eatUnexpectedTokensAndCloseParenToken(),
ParseEmbeddedStatement());

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3666,6 +3666,33 @@ enum VirtualKey
WalkTreeAndVerify(tree.GetCompilationUnitRoot(), fullTree.GetCompilationUnitRoot());
}

[Fact, WorkItem("https://github.com/dotnet/roslyn/issues/76439")]
public void InKeywordInsideAForBlock()
{
var source = """
void Main()
{
for (int i = 0; i < n; i++)
{
}
}
""";
var tree = SyntaxFactory.ParseSyntaxTree(source);
var text = tree.GetText();

var position1 = source.IndexOf("i =") + 1;
var position2 = source.IndexOf("i <") + 1;
var position3 = source.IndexOf("i++") + 1;

text = text.WithChanges(
new TextChange(new TextSpan(position1, 0), "n"),
new TextChange(new TextSpan(position2, 0), "n"),
new TextChange(new TextSpan(position3, 0), "n"));
tree = tree.WithChangedText(text);
CyrusNajmabadi marked this conversation as resolved.
Show resolved Hide resolved
var fullTree = SyntaxFactory.ParseSyntaxTree(text.ToString());
WalkTreeAndVerify(tree.GetCompilationUnitRoot(), fullTree.GetCompilationUnitRoot());
}

#endregion

#region Helper functions
Expand Down