Skip to content

[docs] Add concise unsafe C# coding guidance - #13332

Merged
simonrozsival merged 6 commits into
mainfrom
simonrozsival-unsafe-coding-instructions
Nov 2, 2026
Merged

simonrozsival merged 6 commits into
mainfrom
simonrozsival-unsafe-coding-instructions

Conversation

@simonrozsival

@simonrozsival simonrozsival commented Oct 30, 2026 •

Copy link
Copy Markdown
Member

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 against main; git diff origin/main HEAD --check passed. Builds and unit/device tests were not run because no executable code changed.

Copilot AI balanced review requested due to automatic review settings October 30, 2026 07:32

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.

🟢 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]>
Comment thread .github/copilot-instructions.md Resolved
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]>
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.
simonrozsival and others added 6 commits November 2, 2026 12:35
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
simonrozsival force-pushed the simonrozsival-unsafe-coding-instructions branch from a1d6f5f to a083175 Compare November 2, 2026 11:36
@simonrozsival

Copy link
Copy Markdown
Member Author

@dalexsoto review

@simonrozsival
simonrozsival enabled stacked auto-merge (squash) November 2, 2026 14:47
@simonrozsival
simonrozsival merged commit 68144df into main Nov 2, 2026
41 checks passed
@simonrozsival
simonrozsival deleted the simonrozsival-unsafe-coding-instructions branch November 2, 2026 14:53
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.

3 participants