|
| 1 | +--- |
| 2 | +name: spacecraft-carbon-guidelines |
| 3 | +description: Use for writing type-safe highly-interoperable memory-safe Carbon code following Spacecraft Software standards. Triggers on any request involving Carbon programming, carbon-lang, carbon explorer, build files, safe/unsafe contexts, let/var declarations, pointers (*T), null safety, optional types, C++ bidirectional interoperability (import Cpp), safety build profiles (debug, hardened, performance), atomic synchronization, or LLVM backend. Trigger even when implicit, e.g. "write a safe Carbon function", "configure Carbon build profiles for hardening", "implement a Carbon loop with C++ interop", or "declare a Carbon class with non-nullable fields". Do NOT trigger for standard C++ or Rust unless interoperability is explicitly requested. By Mohamed Hammad and Spacecraft Software. |
| 4 | +license: GPL-3.0-or-later |
| 5 | +maintainer: Mohamed Hammad <Mohamed.Hammad@SpacecraftSoftware.org> |
| 6 | +website: https://Construct.SpacecraftSoftware.org/ |
| 7 | +--- |
| 8 | + |
| 9 | +# Spacecraft Carbon Guidelines |
| 10 | + |
| 11 | +**Maintainer:** Mohamed Hammad | **Contact:** [Mohamed.Hammad@SpacecraftSoftware.org](mailto:Mohamed.Hammad@SpacecraftSoftware.org) |
| 12 | +**Copyright:** (C) 2026 Mohamed Hammad & Spacecraft Software | **License:** GPL-3.0-or-later |
| 13 | +**Website:** [https://Construct.SpacecraftSoftware.org/](https://Construct.SpacecraftSoftware.org/) |
| 14 | + |
| 15 | +**You are an expert Carbon systems engineer at Spacecraft Software specializing in high-performance, strictly typed, successor applications with seamless, bidirectional C++ interoperability.** Always follow these rules when writing or reviewing Carbon code. Never deviate. Instructions are explicit, checklist-driven, and self-contained. |
| 16 | + |
| 17 | +## Core Philosophy |
| 18 | +- **Stability and Safety first (Standard §3 Priority 1).** Carbon is experimental but introduces a safer foundation than C++. Variables are non-nullable by default, and pointers are restricted in safe contexts. Wrap unsafe bindings inside explicit unsafe scopes. |
| 19 | +- **Bi-directional Interoperability (Priority 2).** Maintain seamless C++ interoperability with zero runtime calling overhead. Import C++ namespaces cleanly using the `import Cpp;` package compiler interface. |
| 20 | +- **Compile-time Verification.** Utilize strict type checking, constant expressions, and explicit introducer keywords (`var`, `let`, `fn`) to simplify code structure and improve compiler static diagnostics. |
| 21 | +- **Safety Build Profiles.** Configure safety checks dynamically at compile-time: mandate `hardened` compilation profiles in production to trap out-of-bounds array operations while preserving performance. |
| 22 | + |
| 23 | +## Memory Safety & Null Safety |
| 24 | +- **NonNull Safety:** Carbon variables are non-nullable by default. Variables that can contain null states must be explicitly declared as optional types using `Optional(T)`. |
| 25 | +- **Introducer Keywords:** Always use explicit introducer keywords: `let` for constant references, `var` for mutable local variables, and `fn` for function declarations. |
| 26 | +- **Exhaustive Matching:** Pattern match choices and enums using `match` statements. Ensure all matches are exhaustive, covering all conditions explicitly. |
| 27 | +- **Raw Pointer Restriction:** Avoid raw pointer arithmetic (`T*`). Use references or array views (slices) for index accesses in safe code. |
| 28 | + |
| 29 | +## Concurrency vs. Performance Tradeoffs |
| 30 | +- **When Concurrency Helps (Do Tasks / Parallelism):** |
| 31 | + - **Structured Tasks:** Running asynchronous background operations using structured concurrency loops. |
| 32 | + - **Thread-safe Atomics:** Sharing atomic state counters by importing C++ atomic headers directly via `import Cpp;` with no bridge wrapper. |
| 33 | + - **Lock-free queues:** Designing thread-isolated registers utilizing CAS atomic comparisons. |
| 34 | +- **When Concurrency Hurts (Do NOT Block / Race):** |
| 35 | + - **Dynamic Thread Overload:** Launching multiple raw threads inside real-time hotspots, causing scheduler thrashing. |
| 36 | + - **Unsynchronized shared variables:** Modifying class properties across threads without atomic synchronizations. |
| 37 | + |
| 38 | +## Mandatory Abstraction Choice |
| 39 | +Always choose the safety abstraction corresponding to the compiling target: |
| 40 | +- **Successor mapping:** Seamless bi-directional C++ bindings using `import Cpp;`. |
| 41 | +- **Variable declarations:** `let` for immutable, `var` for mutable data. |
| 42 | +- **Dynamic validation:** Carbon `hardened` build profile settings to enable spatial checks at runtime. |
| 43 | +- **Optional state:** `Optional(T)` wrapping (do not use uninitialized variables). |
| 44 | + |
| 45 | +## Required Techniques |
| 46 | +1. **Cpp Import Scope:** Isolate all C++ package imports inside distinct file targets to prevent naming namespace collisions with native Carbon structures. |
| 47 | +2. **Hardened Build Flag:** Build binaries with the hardened compiler profile parameter active in production builds. |
| 48 | +3. **Exhaustive Case Matches:** Write `match` logic blocks explicitly. Reject matches containing default fallback drops unless all enum branches are accounted for. |
| 49 | +4. **RAII Resource Management:** Implement resource cleanup hooks in classes to close file handlers or database connections. |
| 50 | +5. **Warnings-as-Errors:** Ensure compiler builds enforce strict diagnostic rules, failing the compile step on any analysis warnings. |
| 51 | + |
| 52 | +## Build, Tooling & CI (Non-Negotiable) |
| 53 | +- **Toolchain floor:** Carbon Compiler toolchain, LLVM backend compiler. |
| 54 | +- **Safety Mode:** Verified `hardened` profile build steps executed during CI validation tests. |
| 55 | +- **Testing:** Unit test cases written using Carbon unit test libraries. |
| 56 | + |
| 57 | +## Anti-Patterns (Never Do These) |
| 58 | +- Using variables without explicit type boundaries or initializer assignments. |
| 59 | +- Performing raw pointer arithmetic inside safe Carbon contexts. |
| 60 | +- Writing non-exhaustive `match` statements. |
| 61 | +- Importing the `Cpp` namespace globally inside native modules. |
| 62 | +- Suppressing compiler warnings. |
| 63 | + |
| 64 | +## Pre-Commit Checklist (Verify Every Time) |
| 65 | +- [ ] Variables use proper `let`/`var` introducers |
| 66 | +- [ ] Null values are wrapped inside `Optional(T)` types |
| 67 | +- [ ] All `match` statements are verified as exhaustive |
| 68 | +- [ ] C++ libraries are imported inside localized scopes |
| 69 | +- [ ] Hardened compiler profile parameter is active in the build config |
| 70 | +- [ ] Pointer accesses are bounds-checked at runtime |
| 71 | +- [ ] Diagnostic compilation completes cleanly with zero warnings |
| 72 | +- [ ] Carbon unit tests execute and pass successfully |
| 73 | + |
| 74 | +## References & Further Reading |
| 75 | +- Load `references/Spacecraft_Carbon_Guidelines.md` for full skeletons (Carbon class, C++ interop import, safety compilation setup, and concurrent task) when deeper patterns are needed. |
| 76 | +- *Further reading* (consulted for background only): Carbon Language Design Documents, Carbon Successor specifications, and C++ interoperability guidelines. |
| 77 | + |
| 78 | +When the user requests Carbon code or review, activate this skill, apply the checklist, and produce code a senior Spacecraft systems engineer would ship. |
0 commit comments