Unsafe evolution: allow await in unsafe context - #84616
Conversation
|
Azure Pipelines: Successfully started running 1 pipeline(s). 1 pipeline(s) were filtered out due to trigger conditions. There may be pipelines that require an authorized user to comment /azp run to run. |
There was a problem hiding this comment.
Pull request overview
This PR updates the C# compiler’s await context checks to support the Unsafe Evolution feature: await is permitted in unsafe contexts when the feature is enabled, while await remains disallowed inside a fixed statement (with a new dedicated diagnostic). It also updates compiler tests and the Expression Evaluator tests accordingly, and adds localized resources for the new diagnostic.
Changes:
- Gate
ERR_AwaitInUnsafeContexton the Unsafe Evolution feature, and introduceERR_BadAwaitInFixedforawaitinsidefixedwhen the feature is enabled. - Track
fixed-statement binding context via a newBinderFlags.InFixedStatementand propagate it throughFixedStatementBinder. - Update/extend tests and resources (resx + xlf) to reflect the new behavior across language versions and debugger EE behavior.
Reviewed changes
Copilot reviewed 27 out of 27 changed files in this pull request and generated 1 comment.
Show a summary per file
| File | Description |
|---|---|
| src/ExpressionEvaluator/CSharp/Test/ExpressionCompiler/ExpressionCompilerTests.cs | Expands EE coverage across language versions; adds EE test for await inside fixed under debugger binder flags. |
| src/Compilers/CSharp/Test/Semantic/Semantics/StackAllocInitializerTests.cs | Pins legacy behavior to C# 14 for await diagnostics in unsafe stackalloc initializers. |
| src/Compilers/CSharp/Test/Semantic/Semantics/LocalFunctionTests.cs | Updates expectations to reflect that preview/next no longer errors on await in unsafe contexts. |
| src/Compilers/CSharp/Test/Semantic/Semantics/BindingAsyncTests.cs | Updates/retargets diagnostics checks for legacy vs next behavior around await in unsafe contexts. |
| src/Compilers/CSharp/Test/Emit3/RefUnsafeInIteratorAndAsyncTests.cs | Updates diagnostics now that await in unsafe can succeed and reach ref-across-await checks. |
| src/Compilers/CSharp/Test/Emit/CodeGen/CodeGenAwaitUsingTests.cs | Pins legacy await-in-unsafe behavior to C# 14. |
| src/Compilers/CSharp/Test/Emit/CodeGen/CodeGenAwaitForeachTests.cs | Pins legacy await-in-unsafe behavior to C# 14. |
| src/Compilers/CSharp/Test/CSharp15/UnsafeEvolutionTests.cs | Adds new coverage for await in unsafe blocks and for the new await-in-fixed restriction/diagnostic. |
| src/Compilers/CSharp/Portable/xlf/CSharpResources.zh-Hant.xlf | Adds ERR_BadAwaitInFixed localization entry (new). |
| src/Compilers/CSharp/Portable/xlf/CSharpResources.zh-Hans.xlf | Adds ERR_BadAwaitInFixed localization entry (new). |
| src/Compilers/CSharp/Portable/xlf/CSharpResources.tr.xlf | Adds ERR_BadAwaitInFixed localization entry (new). |
| src/Compilers/CSharp/Portable/xlf/CSharpResources.ru.xlf | Adds ERR_BadAwaitInFixed localization entry (new). |
| src/Compilers/CSharp/Portable/xlf/CSharpResources.pt-BR.xlf | Adds ERR_BadAwaitInFixed localization entry (new). |
| src/Compilers/CSharp/Portable/xlf/CSharpResources.pl.xlf | Adds ERR_BadAwaitInFixed localization entry (new). |
| src/Compilers/CSharp/Portable/xlf/CSharpResources.ko.xlf | Adds ERR_BadAwaitInFixed localization entry (new). |
| src/Compilers/CSharp/Portable/xlf/CSharpResources.ja.xlf | Adds ERR_BadAwaitInFixed localization entry (new). |
| src/Compilers/CSharp/Portable/xlf/CSharpResources.it.xlf | Adds ERR_BadAwaitInFixed localization entry (new). |
| src/Compilers/CSharp/Portable/xlf/CSharpResources.fr.xlf | Adds ERR_BadAwaitInFixed localization entry (new). |
| src/Compilers/CSharp/Portable/xlf/CSharpResources.es.xlf | Adds ERR_BadAwaitInFixed localization entry (new). |
| src/Compilers/CSharp/Portable/xlf/CSharpResources.de.xlf | Adds ERR_BadAwaitInFixed localization entry (new). |
| src/Compilers/CSharp/Portable/xlf/CSharpResources.cs.xlf | Adds ERR_BadAwaitInFixed localization entry (new). |
| src/Compilers/CSharp/Portable/Errors/ErrorFacts.cs | Marks the new error as build-only diagnostic. |
| src/Compilers/CSharp/Portable/Errors/ErrorCode.cs | Introduces ERR_BadAwaitInFixed = 9396. |
| src/Compilers/CSharp/Portable/CSharpResources.resx | Adds the user-facing message for ERR_BadAwaitInFixed. |
| src/Compilers/CSharp/Portable/Binder/FixedStatementBinder.cs | Sets BinderFlags.InFixedStatement for the fixed-statement binding scope. |
| src/Compilers/CSharp/Portable/Binder/BinderFlags.cs | Introduces InFixedStatement and clears it at executable code boundaries. |
| src/Compilers/CSharp/Portable/Binder/Binder_Await.cs | Updates await-context checking for Unsafe Evolution + new fixed-statement restriction. |
|
Azure Pipelines: Successfully started running 2 pipeline(s). There may be pipelines that require an authorized user to comment /azp run to run. |
|
Done with review pass (commit 2) |
There was a problem hiding this comment.
Pull request overview
Copilot reviewed 29 out of 29 changed files in this pull request and generated no new comments.
Comments suppressed due to low confidence (1)
src/Compilers/CSharp/Portable/Binder/Binder_Await.cs:228
ReportBadAwaitContextreportsERR_BadAwaitInFixedfor anyawaitin afixedstatement body, even whenBinderFlags.AllowAwaitInUnsafeContextis set. This looks inconsistent with the intended debugger behavior (and the new ExpressionCompiler test remarks) where the debugger should allow awaiting in unsafe contexts, including withinfixedbodies.
if (this.Flags.Includes(BinderFlags.InFixedStatementBody))
{
Error(diagnostics, ErrorCode.ERR_BadAwaitInFixed, awaitNodeOrToken);
return true;
}
| <data name="ERR_AwaitInUnsafeContext" xml:space="preserve"> | ||
| <value>Cannot await in an unsafe context</value> | ||
| <data name="ERR_BadAwaitInFixed" xml:space="preserve"> | ||
| <value>Cannot await in the body or the initializer of a 'fixed' statement</value> |
|
@333fred for a second review, thanks |
There was a problem hiding this comment.
Pull request overview
Copilot reviewed 30 out of 30 changed files in this pull request and generated no new comments.
Suppressed comments (3)
src/Compilers/CSharp/Test/Semantic/Semantics/UnsafeTests.cs:900
- Same as above: this comment mentions "ref and unsafe in async and iterator methods", but the test is asserting a preview-gated diagnostic for
awaitin an unsafe context. Please update the comment to match the scenario being tested.
// https://github.com/dotnet/roslyn/issues/73280 - these should ideally be langversion errors for 'ref and unsafe in async and iterator methods'
src/Compilers/CSharp/Portable/CSharpResources.resx:3836
- The new diagnostic text is slightly ungrammatical compared to adjacent messages (e.g., "Cannot await in the body of a lock statement"). Consider adding "the" so the message reads naturally.
<data name="ERR_BadAwaitInFixed" xml:space="preserve">
<value>Cannot await in context of a 'fixed' statement</value>
</data>
src/Compilers/CSharp/Test/Semantic/Semantics/UnsafeTests.cs:873
- This comment references issue #73280 and "ref and unsafe in async and iterator methods", but the asserted diagnostic here is for the preview-gated "updated memory safety rules" feature (triggered by
awaitin an unsafe context). The comment should be updated to reflect what this test is actually asserting.
This issue also appears on line 900 of the same file.
// https://github.com/dotnet/roslyn/issues/73280 - these should ideally be langversion errors for 'ref and unsafe in async and iterator methods'
|
@333fred for a second review, thanks |
…-15-langver * upstream/main: Unsafe evolution: allow await in unsafe context (dotnet#84616) Fix empty localization stage on non-main/release branches in official pipeline (dotnet#84845) Reorder stages for localization and build (dotnet#84841) Revert the sonic decl/impl split merge from main (dotnet#84831) AI Workflow - switch to use personal token instead of organizational (dotnet#84832) [main] Update dependencies from dotnet/arcade (dotnet#84819) Do not offer introduce parameter for incomplete calls (dotnet#84769)
Test plan: #81207
Relevant speclet section:
awaitinunsafecontextsMicrosoft Reviewers: Open in CodeFlow