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

Recover better when a user uses commas in a for-statement instead of semicolons #75632

Merged
merged 22 commits into from
Nov 14, 2024

Conversation

CyrusNajmabadi
Copy link
Member

@CyrusNajmabadi CyrusNajmabadi commented Oct 25, 2024

Fixes #66522

I'm doing this for two main reasons:

  1. I hit this constantly. Likely because i fat-finger semi-colon and comma (which we're actually nicely resilient to in many other places).
  2. Our existing parsing here is pretty fugly and this was a good opportunity to improve it. In particular, the code to parse out the for 'declaration or initializers' was pretty unfun to deal with, as was some ugly code to try to properly handle the scoped keyword after the fact. As with a prior pr, this aims to make the flow of hte parser much simpler (and more in line with teh grammar productions), eliminating lots of complex variable flow and assignments that are hard to reason about.

If these reasons aren't sufficient, i'll understand closing (though i will be sad about it :)).

@CyrusNajmabadi CyrusNajmabadi requested a review from a team as a code owner October 25, 2024 18:51
@dotnet-issue-labeler dotnet-issue-labeler bot added Area-Compilers untriaged Issues and PRs which have not yet been triaged by a lead labels Oct 25, 2024
@CyrusNajmabadi
Copy link
Member Author

@dotnet/roslyn-compiler ptal.

@CyrusNajmabadi
Copy link
Member Author

I def run into this myself. ANd the fix was simple. So i'd likek to take this :)

@CyrusNajmabadi
Copy link
Member Author

@dotnet/roslyn-compiler ptal

@@ -21562,96 +21565,128 @@ static bool TakeOutParam(object y, out bool x)
}
";
var compilation = CreateCompilationWithMscorlib461(source, options: TestOptions.DebugExe, parseOptions: TestOptions.Regular);
int[] exclude = new int[] { (int)ErrorCode.ERR_BadVarDecl,
Copy link
Member Author

Choose a reason for hiding this comment

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

this test is insane. it's testing out-vars in a series of invalidly written for-loops. specifically, they are written as:

for (bool a, b(
             Dummy(TakeOutParam(1, out var x14), 

In the past, this was parsed terribly to begin with (since you had the varaibles bool a, b and then an error parentheszied expression following.

Now, this is at least seen as bool a as the variable, and b(...) as the condition of the for loop. This ends up changing all the binding.

@@ -2690,6 +2690,25 @@ static void Main(string[] args)
}
}
}
";
var tree = GetOperationTreeForTest<ForStatementSyntax>(source);
Copy link
Member Author

Choose a reason for hiding this comment

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

no tree produced now since hte start/end of hte span is not validly containing the for-statement. it worked before because parsing was so thrown off it through the for loop was terminated.

@CyrusNajmabadi CyrusNajmabadi requested a review from a team as a code owner November 5, 2024 17:09
? parseForStatementExpressionList(ref secondSemicolonToken, allowSemicolonAsSeparator: true)
: default,
eatUnexpectedTokensAndCloseParenToken(),
ParseEmbeddedStatement());
Copy link
Member Author

Choose a reason for hiding this comment

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

broke parsing up into distinct parts for each major chunk of hte for-loop. Teh variables/initializers, the semicolon, the condition, the semicolon, the incrementors, the close paren and the statement.


return forStatement;

(VariableDeclarationSyntax variableDeclaration, SeparatedSyntaxList<ExpressionSyntax> initializers) eatVariableDeclarationOrInitializers()
Copy link
Member Author

Choose a reason for hiding this comment

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

made intoa local function to make control flow clear.

}

haveScopedKeyword = isDeclaration;
Copy link
Member Author

Choose a reason for hiding this comment

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

removed complex control flow.

@@ -9259,6 +9281,11 @@ static PostSkipAction skipBadForStatementExpressionListTokens(
}
}

private bool IsEndOfForStatementArgument()
Copy link
Member Author

Choose a reason for hiding this comment

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

this is just a move.

@@ -9870,8 +9897,7 @@ private void ParseUsingExpression(ref VariableDeclarationSyntax declaration, ref

if (scopedKeyword != null)
{
declaration = ParseParenthesizedVariableDeclaration();
declaration = declaration.Update(_syntaxFactory.ScopedType(scopedKeyword, declaration.Type), declaration.Variables);
Copy link
Member Author

Choose a reason for hiding this comment

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

no need for this strange pattern of getting the decl, grabbing out its type, and replacing it with a newly wrapped 'ScopedType'. We just pass through teh scopedKeyword and make the ScopedType at the appropriate location inside.

@CyrusNajmabadi
Copy link
Member Author

@RikkiGibson @dotnet/roslyn-compiler ptal :)

@CyrusNajmabadi
Copy link
Member Author

@dotnet/roslyn-compiler ptal.

@CyrusNajmabadi
Copy link
Member Author

@RikkiGibson ptal :)

@CyrusNajmabadi
Copy link
Member Author

@dotnet/roslyn-compiler for another set of eyes. thanks :)

@RikkiGibson RikkiGibson self-assigned this Nov 13, 2024
@CyrusNajmabadi CyrusNajmabadi merged commit feb1350 into dotnet:main Nov 14, 2024
28 checks passed
@dotnet-policy-service dotnet-policy-service bot added this to the Next milestone Nov 14, 2024
@CyrusNajmabadi CyrusNajmabadi deleted the forRecoverys branch November 14, 2024 02:29
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Area-Compilers untriaged Issues and PRs which have not yet been triaged by a lead VSCode
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Better error fix suggestions on For loops when use commas rather than semicolons
3 participants