liveBook · Manning

Compilation test

As explained in Write Powerful Rust Macros

A compile test is a software test whose expected outcome is successful compilation of generated or written code.

A compile test is a software test whose expected outcome is successful compilation of generated or written code. It provides an early check that macro output is valid, even before runtime behavior is verified. section 6.4.1

Use a compile test to:

  • Exercise a minimal generated API, such as constructing a builder and calling build. section 6.4.1
  • Turn compiler diagnostics into implementation feedback when generated identifiers, return types, or quoted variables are wrong. section 6.4.1
  • Verify that type-state constraints reject incomplete builder calls by omitting a required property and checking the resulting error. section 8.5.2

Testing a generated builder

A black-box test derives Builder for an empty struct, calls ExampleStructNoFields::builder().build(), and assigns the result to the expected type. For a type-state builder, a test can omit the final property; compilation then fails because build exists only for the final builder state.

How Write Powerful Rust Macros covers it

How deep it goes
Demonstrates minimal usage tests and compiler-driven debugging, then applies compilation to type-state guarantees; it stops short of runtime behavior.
Examples
Uses an empty derived builder, a complete multi-field builder, and an incomplete builder whose missing final property produces a diagnostic.
What it assumes
Assumes working knowledge of Rust structs, methods, derives, macros, and compilation while testing generated code incrementally.
What you can do after
Diagnose generated-code failures with compiler errors and verify that macro APIs accept valid usage while rejecting incomplete states.
How it connects
Connects black-box testing, white-box testing, compiler errors, procedural macros, the builder pattern, and type-state programming.

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Write Powerful Rust Macros. The section links point to the parts of the book it draws on.