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

Warn for yield return in lock #72755

Merged

Conversation

jjonescz
Copy link
Member

@jjonescz jjonescz commented Mar 27, 2024

@dotnet-issue-labeler dotnet-issue-labeler bot added Area-Compilers untriaged Issues and PRs which have not yet been triaged by a lead labels Mar 27, 2024
@jjonescz jjonescz force-pushed the RefInAsync-02-YieldInLock branch from c74941c to b201c76 Compare March 27, 2024 14:38
@jjonescz jjonescz marked this pull request as ready for review March 27, 2024 18:58
@jjonescz jjonescz requested a review from a team as a code owner March 27, 2024 18:58
@@ -268,6 +268,10 @@ private BoundStatement BindYieldReturnStatement(YieldStatementSyntax node, Bindi
{
Error(diagnostics, ErrorCode.ERR_YieldNotAllowedInScript, node.YieldKeyword);
}
else if (this.Flags.Includes(BinderFlags.InLockBody))
Copy link
Contributor

@AlekseyTs AlekseyTs Apr 1, 2024

Choose a reason for hiding this comment

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

else if (this.Flags.Includes(BinderFlags.InLockBody))

It is very easy to make a mistake and start adding checks for other errors (not warnings) in the else of this if statement. Consider adding a comment right above it saying that all error conditions should be checked outside its else if one is added in the future. #Closed

if (needsFilterDiagnostics)
{
bodyDiagnostics.CopyFilteredToAndFree(diagnostics,
static code => code is not ErrorCode.WRN_BadYieldInLock);
Copy link
Contributor

@AlekseyTs AlekseyTs Apr 1, 2024

Choose a reason for hiding this comment

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

While it feels tempting to always report a warning and then filter it out, I would prefer us to not report the warning in the first place. A diagnostic filtering is an exception rather than a usual way of doing things. In this case, I think, we can avoid it. See, for example, how a set of LockedOrDisposedVariables is exposed and used. I think the fact whether we are in context of a "modern" lock could be accessed/exposed in a similar way, and we could simply not report the warning in the first place. #Closed

Copy link
Contributor

Choose a reason for hiding this comment

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

Another thing to consider is to simplify and not attempt to suppress the warning at all. I do not think it is going to lead to any confusion and the problem with the lock object itself is a separate matter.


var expectedOutput = ExecutionConditionUtil.IsDesktop
? null
: "0123Object synchronization method was called from an unsynchronized block of code.";
Copy link
Contributor

@AlekseyTs AlekseyTs Apr 1, 2024

Choose a reason for hiding this comment

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

"0123Object synchronization method was called from an unsynchronized block of code."

I do not think we should be testing this specific runtime exception behavior, this isn't an exception from a specific API or an exception explicitly thrown by compiler generated code. Consider not validating the output at all. #Closed

Debug.Assert(this.Locals.IsDefaultOrEmpty);

if (needsFilterDiagnostics)
Copy link
Contributor

@AlekseyTs AlekseyTs Apr 1, 2024

Choose a reason for hiding this comment

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

needsFilterDiagnostics

Could you point me to the test that observes effect of this filtering? #Closed

Copy link
Member Author

Choose a reason for hiding this comment

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

LockTests.Yield and Yield_Async

@AlekseyTs
Copy link
Contributor

Done with review pass (commit 5)

Copy link
Contributor

@AlekseyTs AlekseyTs left a comment

Choose a reason for hiding this comment

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

LGTM (commit 10)

@jjonescz jjonescz requested a review from cston April 3, 2024 10:57
@jjonescz
Copy link
Member Author

@cston for a second review, thanks

}
catch (SynchronizationLockException e)
{
Console.WriteLine(e.Message);
Copy link
Member

Choose a reason for hiding this comment

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

Console.WriteLine(e.Message);

Was the intent to execute the test code? If not, perhaps remove the caller.

Copy link
Member Author

Choose a reason for hiding this comment

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

Originally the intent was to execute the test code; but that was removed in #72755 (comment). I will remove the caller, thanks.

@jjonescz jjonescz merged commit 111ec28 into dotnet:features/RefInAsync Apr 16, 2024
24 checks passed
@jjonescz jjonescz deleted the RefInAsync-02-YieldInLock branch April 16, 2024 08:21
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Area-Compilers Feature - Ref/Unsafe in Iterators/Async untriaged Issues and PRs which have not yet been triaged by a lead
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants