Skip to content

Remove warning for yield return in lock #74024

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

Merged
merged 3 commits into from
Jun 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: 0 additions & 8 deletions docs/compilers/CSharp/Warnversion Warning Waves.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,14 +13,6 @@ In a typical project, this setting is controlled by the `AnalysisLevel` property
which determines the `WarningLevel` property (passed to the `Csc` task).
For more information on `AnalysisLevel`, see https://devblogs.microsoft.com/dotnet/automatically-find-latent-bugs-in-your-code-with-net-5/

## Warning level 9

The compiler shipped with .NET 9 (the C# 13 compiler) contains the following warnings which are reported only under `/warn:9` or higher.

| Warning ID | Description |
|------------|-------------|
| CS9237 | ['yield return' should not be used in the body of a lock statement](https://github.com/dotnet/roslyn/issues/72443) |

## Warning level 8

The compiler shipped with .NET 8 (the C# 12 compiler) contains the following warnings which are reported only under `/warn:8` or higher.
Expand Down
5 changes: 0 additions & 5 deletions src/Compilers/CSharp/Portable/Binder/Binder_Statements.cs
Original file line number Diff line number Diff line change
Expand Up @@ -270,11 +270,6 @@ private BoundStatement BindYieldReturnStatement(YieldStatementSyntax node, Bindi
{
Error(diagnostics, ErrorCode.ERR_BadYieldInUnsafe, node.YieldKeyword);
}
// NOTE: Error conditions should be checked above this point; only warning conditions below.
else if (this.Flags.Includes(BinderFlags.InLockBody))
{
Error(diagnostics, ErrorCode.WRN_BadYieldInLock, node.YieldKeyword);
}

CheckRequiredLangVersionForIteratorMethods(node, diagnostics);
return new BoundYieldReturnStatement(node, argument);
Expand Down
6 changes: 0 additions & 6 deletions src/Compilers/CSharp/Portable/CSharpResources.resx
Original file line number Diff line number Diff line change
Expand Up @@ -7899,12 +7899,6 @@ To remove the warning, you can use /reference instead (set the Embed Interop Typ
<data name="ERR_InterceptsLocationDataInvalidPosition" xml:space="preserve">
<value>The data argument to InterceptsLocationAttribute refers to an invalid position in file '{0}'.</value>
</data>
<data name="WRN_BadYieldInLock" xml:space="preserve">
<value>'yield return' should not be used in the body of a lock statement</value>
</data>
<data name="WRN_BadYieldInLock_Title" xml:space="preserve">
<value>'yield return' should not be used in the body of a lock statement</value>
</data>
<data name="IDS_FeatureRefUnsafeInIteratorAsync" xml:space="preserve">
<value>ref and unsafe in async and iterator methods</value>
</data>
Expand Down
2 changes: 1 addition & 1 deletion src/Compilers/CSharp/Portable/Errors/ErrorCode.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2312,7 +2312,7 @@ internal enum ErrorCode

#endregion

WRN_BadYieldInLock = 9237,
// available 9237
ERR_BadYieldInUnsafe = 9238,
ERR_AddressOfInIterator = 9239,

Expand Down
5 changes: 0 additions & 5 deletions src/Compilers/CSharp/Portable/Errors/ErrorFacts.cs
Original file line number Diff line number Diff line change
Expand Up @@ -207,10 +207,6 @@ internal static int GetWarningLevel(ErrorCode code)
// docs/compilers/CSharp/Warnversion Warning Waves.md
switch (code)
{
case ErrorCode.WRN_BadYieldInLock:
// Warning level 9 is exclusively for warnings introduced in the compiler
// shipped with dotnet 9 (C# 13) and that can be reported for pre-existing code.
return 9;
case ErrorCode.WRN_AddressOfInAsync:
case ErrorCode.WRN_ByValArraySizeConstRequired:
// Warning level 8 is exclusively for warnings introduced in the compiler
Expand Down Expand Up @@ -2434,7 +2430,6 @@ or ErrorCode.ERR_InterceptsLocationDuplicateFile
or ErrorCode.ERR_InterceptsLocationFileNotFound
or ErrorCode.ERR_InterceptsLocationDataInvalidPosition
or ErrorCode.INF_TooManyBoundLambdas
or ErrorCode.WRN_BadYieldInLock
or ErrorCode.ERR_BadYieldInUnsafe
or ErrorCode.ERR_AddressOfInIterator
or ErrorCode.ERR_RuntimeDoesNotSupportByRefLikeGenerics
Expand Down

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

10 changes: 0 additions & 10 deletions src/Compilers/CSharp/Portable/xlf/CSharpResources.cs.xlf

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

10 changes: 0 additions & 10 deletions src/Compilers/CSharp/Portable/xlf/CSharpResources.de.xlf

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

10 changes: 0 additions & 10 deletions src/Compilers/CSharp/Portable/xlf/CSharpResources.es.xlf

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

10 changes: 0 additions & 10 deletions src/Compilers/CSharp/Portable/xlf/CSharpResources.fr.xlf

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

10 changes: 0 additions & 10 deletions src/Compilers/CSharp/Portable/xlf/CSharpResources.it.xlf

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

10 changes: 0 additions & 10 deletions src/Compilers/CSharp/Portable/xlf/CSharpResources.ja.xlf

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

10 changes: 0 additions & 10 deletions src/Compilers/CSharp/Portable/xlf/CSharpResources.ko.xlf

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

10 changes: 0 additions & 10 deletions src/Compilers/CSharp/Portable/xlf/CSharpResources.pl.xlf

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

10 changes: 0 additions & 10 deletions src/Compilers/CSharp/Portable/xlf/CSharpResources.pt-BR.xlf

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

10 changes: 0 additions & 10 deletions src/Compilers/CSharp/Portable/xlf/CSharpResources.ru.xlf

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

10 changes: 0 additions & 10 deletions src/Compilers/CSharp/Portable/xlf/CSharpResources.tr.xlf

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

10 changes: 0 additions & 10 deletions src/Compilers/CSharp/Portable/xlf/CSharpResources.zh-Hans.xlf

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

10 changes: 0 additions & 10 deletions src/Compilers/CSharp/Portable/xlf/CSharpResources.zh-Hant.xlf

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
Expand Up @@ -5463,10 +5463,7 @@ static IEnumerable<int> F()
var compilation1 = compilation0.WithSource(source1.Tree);

var v0 = CompileAndVerify(compilation0);
v0.VerifyDiagnostics(
// (17,34): warning CS9237: 'yield return' should not be used in the body of a lock statement
// yield return 1;
Diagnostic(ErrorCode.WRN_BadYieldInLock, "yield").WithLocation(17, 34));
v0.VerifyDiagnostics();
var md0 = ModuleMetadata.CreateFromImage(v0.EmittedAssemblyData);

var f0 = compilation0.GetMember<MethodSymbol>("C.F");
Expand Down
10 changes: 2 additions & 8 deletions src/Compilers/CSharp/Test/Emit2/Semantics/LockTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3448,10 +3448,7 @@ IEnumerable<int> M()
CreateCompilation([source, LockTypeDefinition]).VerifyEmitDiagnostics(
// (9,15): error CS4007: Instance of type 'System.Threading.Lock.Scope' cannot be preserved across 'await' or 'yield' boundary.
// lock (new Lock())
Diagnostic(ErrorCode.ERR_ByRefTypeAndAwait, "new Lock()").WithArguments("System.Threading.Lock.Scope").WithLocation(9, 15),
// (11,13): warning CS9237: 'yield return' should not be used in the body of a lock statement
// yield return 2;
Diagnostic(ErrorCode.WRN_BadYieldInLock, "yield").WithLocation(11, 13));
Diagnostic(ErrorCode.ERR_ByRefTypeAndAwait, "new Lock()").WithArguments("System.Threading.Lock.Scope").WithLocation(9, 15));
}

[Fact]
Expand Down Expand Up @@ -3560,10 +3557,7 @@ async IAsyncEnumerable<int> M()
CreateCompilationWithTasksExtensions([source, LockTypeDefinition, AsyncStreamsTypes]).VerifyEmitDiagnostics(
// (10,15): error CS4007: Instance of type 'System.Threading.Lock.Scope' cannot be preserved across 'await' or 'yield' boundary.
// lock (new Lock())
Diagnostic(ErrorCode.ERR_ByRefTypeAndAwait, "new Lock()").WithArguments("System.Threading.Lock.Scope").WithLocation(10, 15),
// (12,13): warning CS9237: 'yield return' should not be used in the body of a lock statement
// yield return 2;
Diagnostic(ErrorCode.WRN_BadYieldInLock, "yield").WithLocation(12, 13));
Diagnostic(ErrorCode.ERR_ByRefTypeAndAwait, "new Lock()").WithArguments("System.Threading.Lock.Scope").WithLocation(10, 15));
}

[Fact]
Expand Down
Loading