Repository navigation
[docs] Add concise unsafe C# coding guidance - #13332
Merged
Merged
Conversation
Contributor
There was a problem hiding this comment.
🟢 Approval recommended
No active review issues are recorded for the unsafe C# guidance update.
No open issues 🎉
What changed in this PR
Adds concise unsafe C# guidance for future Copilot work in .NET for Android, complementing the memory-safety migration and unsafe-expression cleanup without changing code behavior.
Changes:
- Defines caller-unsafe contracts and when to prefer narrow unsafe expressions or blocks.
- Requires safety documentation, compiler compatibility, and preservation of API contracts, lifetimes, and generated outputs.
- Links to the C# memory-safety explanation.
| File | Description |
|---|---|
.github/copilot-instructions.md |
Adds five unsafe C# and memory-safety rules and a supporting reference link. |
Review effort: Balanced
simonrozsival
added a commit
that referenced
this pull request
Oct 30, 2026
Context: https://devblogs.microsoft.com/dotnet/improving-csharp-memory-safety/ Context: https://learn.microsoft.com/dotnet/standard/unsafe-code/best-practices The two articles answer different questions: how to express safety contracts and how to identify concrete implementation hazards. Replace the introductory paragraphs with a comparison table so future sessions can choose the relevant refresher. Retain the scoped review guidance in the existing validation rule. Co-authored-by: Copilot App <[email protected]>
simonrozsival
added a commit
that referenced
this pull request
Oct 30, 2026
Context: #13332 (comment) The guide is loaded into every session. Remove repetitive wording while retaining both references, all safety and compatibility constraints, scoped reviews, and evidence requirements. Keep unrelated instructions unchanged. Co-authored-by: Copilot App <[email protected]>
simonrozsival
added a commit
that referenced
this pull request
Oct 30, 2026
Context: #13332 (comment) Point sessions to the unsafe-code best practices instead of repeating a detailed audit checklist. Keep the inline unsafe expression preference, narrow scopes, safety documentation, and compiler compatibility reminders in a 107-word section. Unrelated instructions remain unchanged. Co-authored-by: Copilot App <[email protected]>
jonathanpeppers
approved these changes
Oct 30, 2026
This was referenced Oct 31, 2026
simonrozsival
added a commit
that referenced
this pull request
Nov 1, 2026
Context: #13332 Remove the duplicate Copilot audit-policy section from this activation branch. The dedicated instructions PR owns that change. Preserve all existing main instructions and leave the activation code untouched. Co-authored-by: Copilot App <[email protected]>
Base automatically changed from
simonrozsival-unsafe-java-interop-activation-stack
to
main
November 2, 2026 11:34
simonrozsival
added a commit
that referenced
this pull request
Nov 2, 2026
Context: #11467 Context: #13191 Enable `updated-memory-safety-rules` in `Java.Interop` now that the preparation PRs have merged. The preparation distinguished APIs whose callers own JNI ABI, buffer, or lifetime obligations from managed wrappers that establish those guarantees internally. This change turns on compiler enforcement without making those managed wrappers caller-unsafe. **Most of the reduced diff is API bookkeeping in `PublicAPI.Unshipped.txt`, not implementation changes.** Leave `PublicAPI.Shipped.txt` unchanged. Record the 156 changed shipped contracts as `*REMOVED*` entries followed by their replacements, and update already-unshipped entries in place. These changes track `unsafe` markers and the two runtime-constructor records; they do not change CLR signatures. The additional lines make the released-to-current contract changes explicit rather than rewriting the released baseline. ### Activation Add the compiler feature and remove the transitional declaration-level `unsafe` modifiers marked for removal at opt-in: pointer storage and address getters, address-only `JniNativeMethod` construction, owned control-block allocation, and `StartCreateInstance`, which ignores its pointer argument. Narrow the typed remapping properties to unsafe setters only. Existing implementation-local unsafe regions and their concrete safety explanations remain intact. Keep all nine `GetElements()` methods safe-callable and all eight ref-returning array indexers caller-unsafe. Raw JNI consumption, registration, VM construction, `FinishCreateInstance`, and frees retain their caller contracts. Preserve the six released RC2 native-int accessors and two virtual hooks as safe-callable, warning-obsolete exceptions, including existing override/base dispatch. Only Java.Interop opts in; Mono.Android and customer projects do not. There are no runtime algorithm, allocation, ownership, cleanup, or native-layout changes, and no new tests or generic comment blocks. Main's `IntPtr` callback storage is preserved. `Architecture.md` receives only the activation and accuracy updates; Copilot audit guidance belongs to [#13332](#13332). Broader non-Throwable raw-peer constructor enforcement remains deferred because accepted zero/`DoNotTransfer` derived initializers would require a separate initialization redesign. The approved Object/Throwable None-only route remains intact. Partial JNI registration ownership redesign also remains deferred: callers must retain callback roots after failed or partial registration. This PR does not close #11467.
Future Copilot sessions need guidance for inline unsafe expressions and caller-unsafe contracts without widening unsafe scopes or changing APIs. Add five compact rules and link the memory-safety overview, as requested in review: #13315 (review) Documentation only; no build or runtime behavior changes. Co-authored-by: Copilot App <[email protected]>
Capture recurring lessons from the unsafe-code stack without turning the guide into an audit ledger. Clarify real caller obligations, full-borrow safety proofs, producer and consumer compatibility, released API contracts, canonical generation, and evidence-scoped validation. Keep the existing blog link and unrelated instructions unchanged. Documentation only; no build or runtime behavior changes. Co-authored-by: Copilot App <[email protected]>
Keep the .NET unsafe-code best practices in view when working with unsafe APIs, without triggering repository-wide audits for every change. Separate potential correctness issues from anti-patterns in review-only tasks, and leave reproductions and fixes to explicit requests. Co-authored-by: Copilot App <[email protected]>
Context: https://devblogs.microsoft.com/dotnet/improving-csharp-memory-safety/ Context: https://learn.microsoft.com/dotnet/standard/unsafe-code/best-practices The two articles answer different questions: how to express safety contracts and how to identify concrete implementation hazards. Replace the introductory paragraphs with a comparison table so future sessions can choose the relevant refresher. Retain the scoped review guidance in the existing validation rule. Co-authored-by: Copilot App <[email protected]>
Context: #13332 (comment) The guide is loaded into every session. Remove repetitive wording while retaining both references, all safety and compatibility constraints, scoped reviews, and evidence requirements. Keep unrelated instructions unchanged. Co-authored-by: Copilot App <[email protected]>
Context: #13332 (comment) Point sessions to the unsafe-code best practices instead of repeating a detailed audit checklist. Keep the inline unsafe expression preference, narrow scopes, safety documentation, and compiler compatibility reminders in a 107-word section. Unrelated instructions remain unchanged. Co-authored-by: Copilot App <[email protected]>
simonrozsival
force-pushed
the
simonrozsival-unsafe-coding-instructions
branch
from
November 2, 2026 11:36
a1d6f5f to
a083175
Compare
Member
Author
|
@dalexsoto review |
simonrozsival
enabled stacked auto-merge (squash)
November 2, 2026 14:47
jonathanpeppers
approved these changes
Nov 2, 2026
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Context: #13315 (review)
Context: #13332 (comment)
Future sessions need to know about inline
unsafe (expression)without loading a detailed unsafe-code checklist into every conversation.Add a 107-word section to
.github/copilot-instructions.md, primarily pointing to Unsafe code best practices. Keep the preference for narrow unsafe expressions, the appropriate block contexts, safety documentation, and producer/consumer language compatibility. Link Improving C# memory safety for caller contracts. Apply the guidance to affected code rather than requiring repository-wide scans.Documentation only. After rebasing onto
main, the exact 107-word section was preserved and all surrounding guide text was verified againstmain;git diff origin/main HEAD --checkpassed. Builds and unit/device tests were not run because no executable code changed.