Skip to content
HN On Hacker News ↗

CGP v0.8.0 - cargo-cgp, new documentation, and namespaces

▲ 27 points • 0 comments • by maybevoid • 5d ago • HN discussion ↗

Pangram verdict · v3.3

We believe that this text is a mix of AI and human-written content.

69 %

AI likelihood · overall

Mixed
31% human-written 69% AI-generated
SEGMENTS · HUMAN 7 of 18
SEGMENTS · AI 11 of 18
WORD COUNT 1,060
PEAK AI % 100% · §3
Analyzed
Nov 21
backend: pangram/v3.3
Segments scanned
18 windows
avg 59 words each
Distribution
31 / 69%
human / AI fraction
Verdict
Mixed
Pangram v3.3

Article text · 1,060 words · 18 segments analyzed

Human AI-generated
§1 Human · 0%

Context-Generic Programming (CGP) v0.8.0 is out!

§2 Human · 0%

This release is about making CGP easier to learn and maintain.

§3 AI · 100%

It introduces cargo-cgp, a new tool for one of CGP's longest-standing problems: compile errors that describe the code the macros generated rather than the code you wrote. cargo-cgp explains those errors in your terms. When a provider needs a field your type lacks, for example, it can tell you which field to add, and the expanded documentation explains why the provider needs it. Later, as your application grows, the new namespaces let you choose the providers for a whole group of components in one wiring entry.

§4 Human · 0%

CGP is a language extension for Rust, with pluggable trait implementations at compile-time.

§5 AI · 100%

It is an ordinary library on stable Rust: applications select providers through wiring that resolves to static calls, and you can adopt it one component at a time. The Overview explains the model. I'll start with cargo-cgp and the new documentation, then work through namespaces using a social-media application. The namespace walkthrough is the longest part, so you can read the tooling and documentation news on their own if you prefer. Allow about 35 minutes for the full post, including the code, or jump to the section you need: Diagnosing wiring with cargo-cgp Expanded documentation and AI agent support Grouping component wiring with namespaces Sharing default implementations Other improvements Breaking changes and migration Diagnosing wiring with cargo-cgp Since CGP began, verbose and confusing compiler errors have been one of the largest obstacles to adopting it. Leave out a field or a component binding, and the error message can name a generated trait or show a field name encoded as a Rust type. The compiler checks the Rust produced by macro expansion, so its explanation can be far removed from the code where we made the mistake. For a newcomer, that can mean learning advanced Rust traits and generics, along with CGP's internals, just to diagnose a small mistake. You might understand the program you are trying to write and still have little idea why it does not compile. Because the difficulty lies in how the compiler reports the expanded code, better syntax alone cannot address it.

§6 AI · 100%

So I built cargo-cgp v0.1.0, CGP's new error toolchain, to report those errors in terms of the code you wrote.

§7 AI · 100%

cargo cgp check checks CGP code and explains a missing dependency through the fields, components, and providers involved, leading with the root cause. It does not yet reshape every class of error, though, so some still reach you in the compiler's own words. When you want to look deeper, cargo cgp expand shows the Rust that the macros generated. Find a missing field Let's try it on a small mistake from the area-calculation tutorial. We have an area provider that multiplies width by height, and a Rectangle that supplies only width. Here, the rectangle itself is the context: it is the value the operation acts on, and its fields supply the provider's inputs.

§8 Human · 0%

Save this library example as src/lib.rs in a Cargo package using cgp = "0.8.0": use cgp::prelude::*;#[cgp_component(AreaCalculator)]pub trait CanCalculateArea { fn area(&self) -> f64;}#[cgp_impl(new RectangleArea)]impl AreaCalculator { fn area(&self, #[implicit] width: f64, #[implicit] height: f64) -> f64 { width * height }}#[derive(HasField)]pub struct Rectangle { pub width: f64, // missing `height` field}delegate_and_check_components!

§9 AI · 100%

{ Rectangle { AreaCalculatorComponent: RectangleArea, }} The table selects RectangleArea, whose #[implicit] arguments read the rectangle's fields.

§10 AI · 100%

Because we used delegate_and_check_components!, the compiler checks those dependencies at the wiring entry.

§11 Human · 0%

Run cargo cgp check, and it points to the field we forgot: error[E0277]: [CGP-E001] missing field `height` on `Rectangle` --> src/lib.rs:23:9 |23 | AreaCalculatorComponent: RectangleArea, | ^^^^^^^^^^^^^^^^^^^^^^^ | = note: root cause: [CGP-E110] missing field `height` on `Rectangle` this is required through the dependency chain: [CGP-E101] consumer trait impl `CanCalculateArea` for context `Rectangle` └─ [CGP-E102] provider trait impl `AreaCalculator` with context `Rectangle` for provider `RectangleArea` └─ [CGP-E110] missing field `height` on `Rectangle` = help: add the field `height: f64` to `Rectangle` The help line gives us the edit, height: f64.

§12 AI · 100%

To see why the provider needs it, the dependency tree takes us back through RectangleArea to the area operation.

§13 Human · 0%

For comparison, plain cargo check describes the same mistake through generated traits and a nested Symbol type: Compare the output from cargo checkerror[E0277]: the trait bound `Rectangle: CanUseComponent<AreaCalculatorComponent>` is not satisfied --> src/lib.rs:23:9 |23 | AreaCalculatorComponent: RectangleArea, | ^^^^^^^^^^^^^^^^^^^^^^^ unsatisfied trait bound |help: the trait `HasField<Symbol<6, Chars<'h', Chars<'e', Chars<'i', Chars<'g', Chars<_, cgp::macro_prelude::Chars<'t', Nil>>>>>>>>` is not implemented for `Rectangle` but trait `HasField<Symbol<5, Chars<'w', Chars<'i', Chars<'d', Chars<'t', Chars<_, Nil>>>>>>>` is implemented for it --> src/lib.rs:17:9 |17 | pub width: f64, | ^^^^^note: required for `RectangleArea` to implement `IsProviderFor<AreaCalculatorComponent, Rectangle>` --> src/lib.rs:8:1 | 8 | #[cgp_impl(new RectangleArea)] | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ = note: required for `Rectangle` to implement `CanUseComponent<AreaCalculatorComponent>`note: required by a bound in `__CanUseRectangle` --> src/lib.rs:21:1 |21 | / delegate_and_check_components! {22 | | Rectangle {23 | | AreaCalculatorComponent: RectangleArea,24 | | }25 | | } | |_^ required by this bound in `__CanUseRectangle` = note: this error originates in the attribute macro `cgp_impl` which comes from the expansion of the macro `delegate_and_check_components` (in Nightly builds, run with -Z macro-backtrace for more info) Following that suggestion, we add the missing field: #[derive(HasField)]pub struct Rectangle { pub width: f64, pub height: f64,} The wiring check now passes, and the provider can read both fields to calculate the area.

§14 AI · 100%

Fixing the mistake did not require decoding the type-level spelling of height. To produce this explanation, cargo-cgp wraps a nightly compiler and uses the Rust compiler's API to inspect the failed dependencies. It can follow their chain using information that would be difficult to recover from the printed diagnostic alone. That access is why the tool has its own compiler-linked driver.

§15 AI · 100%

Try the checker With cargo and rustup available, you can install the tool and set up its compiler-linked driver, then check your own project.

§16 Human · 0%

The install needs Rust 1.96 or newer, and the tool is tested on Linux only so far: cargo install --locked cargo-cgpcargo cgp setup# Run this from your CGP project's directory:cargo cgp check cargo cgp setup installs the tool's pinned nightly and builds the matching driver.

§17 AI · 100%

That nightly runs only cargo-cgp's checks, so your application keeps its normal toolchain for ordinary cargo builds. CGP itself still compiles on stable Rust, and you can use it without installing cargo-cgp at all.

§18 AI · 100%

Use plain cargo to build, run, and test applications.