Skip to content

Emit safe keyword on generated UnsafeAccessor methods under updated memory safety rules - #39001

Open
jkoritzinsky wants to merge 2 commits into
mainfrom
dev/jkoritzinsky/safe-unsafe-accessor
Open

jkoritzinsky wants to merge 2 commits into
mainfrom
dev/jkoritzinsky/safe-unsafe-accessor

Conversation

@jkoritzinsky

Copy link
Copy Markdown
Member

When C#'s "updated memory safety rules" (a.k.a. unsafe evolution) feature is enabled, generated [UnsafeAccessor] static extern methods in the precompiled-query code generation pipeline now get the safe contextual keyword added to their modifiers.

Adds an internal MemorySafetyRules helper (adapted from dotnet/runtime's System.Runtime.InteropServices source generator shim) that detects whether a Compilation was parsed with the updated memory safety rules enabled:

  • First checks the (currently experimental) Roslyn CSharpCompilationOptions.MemorySafetyRulesVersion API reflectively.
  • Falls back to the updated-memory-safety-rules entry in the parse options' Features dictionary when that API is not available on the referenced Roslyn version.

LinqToCSharpSyntaxTranslator gains a useUpdatedMemorySafetyRules constructor parameter (defaulting to false, preserving existing behavior), and PrecompiledQueryCodeGenerator computes the flag once from its Compilation and threads it through. The NativeAOT compiled-model generator (CSharpRuntimeModelCodeGenerator) is unaffected, since it generates code via string-building from reflection and has no Compilation/parse-options signal available.

Added MemorySafetyRulesTest covering flag detection; all existing EFCore.Design.Tests (1288 tests) pass with no regressions.

… memory safety rules

When the C# "updated memory safety rules" (a.k.a. unsafe evolution) feature is
enabled, generated `[UnsafeAccessor]` `static extern` methods in the
precompiled-query code generation pipeline now get the `safe` contextual
keyword added to their modifiers.

Adds an internal `MemorySafetyRules` helper (adapted from dotnet/runtime's
System.Runtime.InteropServices source generator shim) that detects whether a
Compilation was parsed with the updated memory safety rules enabled, first
checking the (currently experimental) Roslyn
`CSharpCompilationOptions.MemorySafetyRulesVersion` API reflectively, and
falling back to the `updated-memory-safety-rules` entry in the parse options'
`Features` dictionary when that API is not available on the referenced
Roslyn version.

Co-authored-by: Copilot App <223556219+Copilot@users.noreply.github.com>
Copilot AI lite review requested due to automatic review settings September 15, 2026 22:30
@jkoritzinsky
jkoritzinsky requested a review from a team as a code owner September 15, 2026 22:30

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Copilot review overview

🟡 Changes recommended

The compatibility fallback issue and missing enabled-path assertions remain unresolved.

Get a fresh assessment by requesting another Copilot review.

Review tier: Lite
Findings: 1 Medium severity · 1 Low severity

Open (2)
What changed in this PR

Updates precompiled-query generation to emit safe on generated [UnsafeAccessor] methods when updated memory safety rules are enabled.

Changes:

  • Added Roslyn-compatible memory safety rule detection.
  • Threaded the setting through query translators.
  • Added detection tests.
File Description
test/​EFCore.Design.Tests/​Query/​MemorySafetyRulesTest.cs Tests memory safety rule detection.
src/​EFCore.Design/​Query/​Internal/​RuntimeModelLinqToCSharpSyntaxTranslator.cs Forwards the translator setting.
src/​EFCore.Design/​Query/​Internal/​PrecompiledQueryCodeGenerator.cs Supplies compilation-based detection.
src/​EFCore.Design/​Query/​Internal/​MemorySafetyRules.cs Implements compatibility detection.
src/​EFCore.Design/​Query/​Internal/​LinqToCSharpSyntaxTranslator.cs Emits the safe modifier.

Comment thread src/EFCore.Design/Query/Internal/MemorySafetyRules.cs
Comment thread src/EFCore.Design/Query/Internal/LinqToCSharpSyntaxTranslator.cs
…, add translator test for safe-keyword path

- UseUpdatedMemorySafetyRules now checks the updated-memory-safety-rules parse
  feature even when MemorySafetyRulesVersion is available but returns Version1,
  since Roslyn treats the feature as an opt-in fallback in that case.
- Added a LinqToCSharpSyntaxTranslator test that constructs the translator with
  useUpdatedMemorySafetyRules: true and asserts the safe modifier is present on
  the generated UnsafeAccessor declaration (when SafeKeyword is recognized).

Co-authored-by: Copilot App <223556219+Copilot@users.noreply.github.com>
Copilot AI review requested due to automatic review settings September 15, 2026 22:59

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Copilot review overview

🔵 Needs a closer look

The reflective detection branch still lacks conditional test coverage.

Review tier: Lite
Findings: None

Resolved since last review (2)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants