Skip to content

Commit

Permalink
Filter warning for yield return in the new lock
Browse files Browse the repository at this point in the history
  • Loading branch information
jjonescz committed Mar 27, 2024
1 parent 654d458 commit eafc594
Showing 1 changed file with 14 additions and 3 deletions.
17 changes: 14 additions & 3 deletions src/Compilers/CSharp/Portable/Binder/LockBinder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -55,8 +55,9 @@ internal override BoundStatement BindLockStatementParts(BindingDiagnosticBag dia
hasErrors = true;
}

if (exprType?.IsWellKnownTypeLock() == true &&
TryFindLockTypeInfo(exprType, diagnostics, exprSyntax) is { } lockTypeInfo)
bool isLockObjectBased = exprType?.IsWellKnownTypeLock() == true;
if (isLockObjectBased &&
TryFindLockTypeInfo(exprType!, diagnostics, exprSyntax) is { } lockTypeInfo)
{
CheckFeatureAvailability(exprSyntax, MessageID.IDS_FeatureLockObject, diagnostics);

Expand All @@ -73,8 +74,18 @@ internal override BoundStatement BindLockStatementParts(BindingDiagnosticBag dia
errorCode: ErrorCode.ERR_BadSpecialByRefLock);
}

BoundStatement stmt = originalBinder.BindPossibleEmbeddedStatement(_syntax.Statement, diagnostics);
var needsFilterDiagnostics = isLockObjectBased && diagnostics.AccumulatesDiagnostics;
var bodyDiagnostics = needsFilterDiagnostics ? BindingDiagnosticBag.GetInstance(template: diagnostics) : diagnostics;

BoundStatement stmt = originalBinder.BindPossibleEmbeddedStatement(_syntax.Statement, bodyDiagnostics);
Debug.Assert(this.Locals.IsDefaultOrEmpty);

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

return new BoundLockStatement(_syntax, expr, stmt, hasErrors);
}

Expand Down

0 comments on commit eafc594

Please sign in to comment.