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 when taking address of local or parameter in async method #66915

Merged
merged 10 commits into from
Mar 10, 2023
9 changes: 9 additions & 0 deletions src/Compilers/CSharp/Portable/Binder/Binder.ValueChecks.cs
Original file line number Diff line number Diff line change
Expand Up @@ -817,6 +817,11 @@ private static bool CheckNotNamespaceOrType(BoundExpression expr, BindingDiagnos

private bool CheckLocalValueKind(SyntaxNode node, BoundLocal local, BindValueKind valueKind, bool checkingReceiver, BindingDiagnosticBag diagnostics)
{
if (valueKind == BindValueKind.AddressOf && this.IsInAsyncMethod())
{
Error(diagnostics, ErrorCode.WRN_AddressOfInAsync, node);
}

// Local constants are never variables. Local variables are sometimes
// not to be treated as variables, if they are fixed, declared in a using,
// or declared in a foreach.
Expand Down Expand Up @@ -906,6 +911,10 @@ internal partial class Binder
private bool CheckParameterValueKind(SyntaxNode node, BoundParameter parameter, BindValueKind valueKind, bool checkingReceiver, BindingDiagnosticBag diagnostics)
{
Debug.Assert(!RequiresAssignableVariable(BindValueKind.AddressOf));
if (valueKind == BindValueKind.AddressOf && this.IsInAsyncMethod())
{
Error(diagnostics, ErrorCode.WRN_AddressOfInAsync, node);
}

ParameterSymbol parameterSymbol = parameter.ParameterSymbol;

Expand Down
6 changes: 6 additions & 0 deletions src/Compilers/CSharp/Portable/CSharpResources.resx
Original file line number Diff line number Diff line change
Expand Up @@ -7493,4 +7493,10 @@ To remove the warning, you can use /reference instead (set the Embed Interop Typ
<data name="ERR_UnexpectedParameterList" xml:space="preserve">
<value>Unexpected parameter list.</value>
</data>
<data name="WRN_AddressOfInAsync" xml:space="preserve">
<value>The '&amp;' operator should not be used on parameters or local variables in async methods.</value>
</data>
<data name="WRN_AddressOfInAsync_Title" xml:space="preserve">
<value>The '&amp;' operator should not be used on parameters or local variables in async methods.</value>
</data>
</root>
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 @@ -2181,11 +2181,11 @@ internal enum ErrorCode
ERR_RefReturnPrimaryConstructorParameter = 9120,
ERR_StructLayoutCyclePrimaryConstructorParameter = 9121,
ERR_UnexpectedParameterList = 9122,
WRN_AddressOfInAsync = 9123,

ERR_BadRefInUsingAlias = 9130,
ERR_BadUnsafeInUsingDirective = 9131,
ERR_BadNullableReferenceTypeInUsingAlias = 9132,

#endregion

// Note: you will need to do the following after adding warnings:
Expand Down
5 changes: 5 additions & 0 deletions src/Compilers/CSharp/Portable/Errors/ErrorFacts.cs
Original file line number Diff line number Diff line change
Expand Up @@ -206,6 +206,10 @@ internal static int GetWarningLevel(ErrorCode code)
// docs/compilers/CSharp/Warnversion Warning Waves.md
switch (code)
{
case ErrorCode.WRN_AddressOfInAsync:
Copy link
Member

Choose a reason for hiding this comment

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

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Opened #67420

// Warning level 8 is exclusively for warnings introduced in the compiler
// shipped with dotnet 8 (C# 12) and that can be reported for pre-existing code.
return 8;
case ErrorCode.WRN_LowerCaseTypeName:
// Warning level 7 is exclusively for warnings introduced in the compiler
// shipped with dotnet 7 (C# 11) and that can be reported for pre-existing code.
Expand Down Expand Up @@ -2299,6 +2303,7 @@ internal static bool IsBuildOnlyDiagnostic(ErrorCode code)
case ErrorCode.ERR_RefReturnPrimaryConstructorParameter:
case ErrorCode.ERR_StructLayoutCyclePrimaryConstructorParameter:
case ErrorCode.ERR_UnexpectedParameterList:
case ErrorCode.WRN_AddressOfInAsync:
case ErrorCode.ERR_BadRefInUsingAlias:
case ErrorCode.ERR_BadUnsafeInUsingDirective:
case ErrorCode.ERR_BadNullableReferenceTypeInUsingAlias:
Expand Down

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

10 changes: 10 additions & 0 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: 10 additions & 0 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: 10 additions & 0 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: 10 additions & 0 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: 10 additions & 0 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: 10 additions & 0 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: 10 additions & 0 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: 10 additions & 0 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: 10 additions & 0 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: 10 additions & 0 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: 10 additions & 0 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: 10 additions & 0 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: 10 additions & 0 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.

Loading