proc-macros
Skill855 starsChanged 10 days agoApache-2.0View on GitHub
Use when changing or reviewing the ext-php-rs procedural macros (crates/macros: #[php_class], #[php_impl], #[php_function], #[php_module], #[php_enum], #[php_interface], #[php_const], #[php_extern], #[php_impl_interface], #[php(...)] options, ZvalConvert, wrap_function!, zend_fastcall!), their error messages, the expansion (.expanded.rs) and trybuild (.stderr) snapshots, the macro docs copied from guide/src/macros, or the generated stubs and cargo-php.
In this repository6 other files it gives its agents.
- AGENTS.mdAGENTS.md
- CLAUDE.mdCLAUDE.md
- php-bindingsSkill
- pr-descriptionSkill
- semver-breaking-changesSkill
- zend-memorySkill
The file
We split this skill into the rules it gives an agent
2 rules. About 0 are common, 0 are distinctive, and 2 are very specific to this project.
≈0
Common · estimate
Many files like this have a rule of this kind.
≈0
Distinctive · estimate
This author's own choices. Worth a look.
2
Very specific
Tied to their project's names, files or tools. Probably skip.
- Common
- Distinctive
- Very specific
- Very specific: line 33
Errors point at the user's code: Use syn::Error::new_spanned/bail!
- Very specific: line 46
Run the lanes the NTS matrix skips: before pushing a macro change: the tests crate under embed (cargo test -p tests --lib --features …
≈ “Common” is an estimate: at least 1 in 5 of the 408,460 skill files we read have a rule of the same kind, not the same words. “Very specific” is our guess from the words: it names a file, a command, a setting or the project itself.
Files
Procedural macros
Where things live
crates/macros/src/lib.rs: the entry points, and the macro docs (a copy ofguide/src/macros/*.mdbetween// BEGIN DOCS FROM <file>.md/// END DOCS FROM <file>.md). Its#[cfg(test)] mod testsruns every macro overtests/expand/*.rsthroughruntime-macros, for coverage: a new macro must be added to the lists there.- One file per macro:
class.rs,impl_.rs,function.rs(argument parsing and the generated handler, shared by functions, methods and constructors),module.rs,enum_.rs,interface.rs,impl_interface.rs,constant.rs,extern_.rs,zval.rs(ZvalConvert),fastcall.rs.parsing.rshas the#[php(...)]option parsing (darling) and PHP name casing;helpers.rs,syn_ext.rsshared helpers. - The generated code calls into
ext-php-rs(src/internal/,src/builders/,src/args.rs,src/class.rs). A change usually touches both crates; the root crate pinsext-php-rs-derivewith=, so they always ship together. - Stubs: the extension describes itself through
src/describeandcrates/introspection;cargo php stubs(crates/cli) renders them.
Rules for macro changes
- Every path gets the change. Functions, methods, static methods, constructors and
interface methods share most of
function.rsbut not all of it (constructors returnConstructorResult, variadics keep theArgParserpath). Check each one, and#[php_impl_interface], which builds methods separately. - Errors point at the user's code. Use
syn::Error::new_spanned/bail!with the offending tokens, neverpanic!or a span-less error. Each new error gets a trybuild case. Generated code whose trait bounds depend on the user's type isquote_spanned!on that type, so rustc reports an unmet bound on the parameter, not on the attribute. - Generated code is hygienic. Refer to the runtime crate as
::ext_php_rs::…, to std as::std::…/::core::…, and never rely on what the user imported. Generated items that users don't name are#[doc(hidden)]. - Module-entry code runs without the Zend heap. What the macros evaluate while
get_modulebuilds the module (names, docs, default values for stubs) runs before PHP's heap exists in embed and static builds:run_php_embedcallsget_module()beforeEmbed::run. Use Rust or libc allocations only, neveremalloc(StubLiteralrenders defaults in pure Rust). The spawnedphplanes never show it. - Run the lanes the NTS matrix skips before pushing a macro change: the tests
crate under embed (
cargo test -p tests --lib --features closure,embed,anyhow,observer,smartstring,indexmap -- --test-threads=1) and the ASan lane (skillzend-memory). - PHP-visible output is API. PHP names (casing rules in
parsing.rs), arginfo types, defaults, flags and stubs are what PHP code sees; a change to them is a breaking change (skillsemver-breaking-changes). - Generated code must pass the workspace clippy (
-W clippy::pedantic -D warnings) in user crates: emit#[allow(...)]on generated items only for lints that the generated shape can't avoid, with a reason. - Hot paths. The generated handler runs on every PHP call. Don't allocate or format
per call (names, default values) when it can be done once at registration;
benches/measures this (seebenches/README.md). To count instructions per call, buildbenches/extwithCARGO_PROFILE_RELEASE_DEBUG=line-tables-only, runvalgrind --tool=callgrind --trace-children=yes php -n -dextension=<lib> benches/<script>.php 100000, subtract an empty script's count and divide by the call count.
Snapshots
- Expansion snapshots:
crates/macros/tests/expand/<name>.rswith its<name>.expanded.rs, checked bytests/macrotest_expand.rs(needscargo-expand; runs only on nightly, never on Windows):cargo +nightly test --test macrotest_expand. In nix, the nightly shell is.#asan, whose sanitizerRUSTFLAGSbreak proc-macro loading:nix develop .#asan -c env -u RUSTFLAGS -u CFLAGS -u LDFLAGS cargo test --test macrotest_expand. A missing.expanded.rsis written on the first run;MACROTEST=overwriterewrites existing ones. Read the diff of every changed.expanded.rsbefore committing it: it is the review of what the macro now generates. A refactor that should not change output must leave them untouched. - Compile-fail snapshots:
tests/ui/<case>.rs+<case>.stderr, run bytests/ui.rswith trybuild. The.stderrtext depends on the rustc version, so CI pins a toolchain (toolchain:in thecompile-failjob of.github/workflows/build.yml). Run and regenerate on exactly that toolchain:cargo +<pinned> test --test ui -- --ignored, andTRYBUILD=overwrite cargo +<pinned> test --test ui -- --ignoredfor new cases. Bumping the pin regenerates all.stderrfiles in the same PR. - Stub snapshots:
crates/cli/tests/stubs_snapshot.rsbuilds thehello_worldexample and comparescargo php stubsoutput with an insta snapshot (crates/cli/tests/snapshots/);crates/cli/src/snapshots/holds the static-linking glue snapshots. Review withcargo insta revieworINSTA_UPDATE=always, and read the diff. - Runtime behaviour still needs an integration test (
tests/src/integration/): a snapshot shows what is generated, not that PHP behaves correctly.
Docs
- Macro docs are written in
guide/src/macros/*.md, then copied withtools/update_lib_docs.sh(needs the nightly toolchain for its rustfmt pass; the lefthook pre-commit job runs it when either side is staged). Never edit the copy inlib.rs. - The script's final rustfmt pass follows
moddeclarations and rewraps comments in other macro files (class.rs,impl_interface.rs,parsing.rs,zval.rs). Restore them and commit onlylib.rs. Themacro docsworkflow reruns the script and fails on any diff inlib.rs. - A new
#[php(...)]option is documented on the macro's guide page, with a Rust example and the PHP that shows its effect. - The copy turns
rust,no_runintorust,no_run,ignore, so guide examples are only compiled by skeptic (guide_tests), not as rustdoc tests of the macros crate. skeptic links what it finds intarget/: E0464 after builds with other feature sets needscargo clean -p ext-php-rs, E0514 after a toolchain bump needs a fullcargo clean.
Does it work?
Reviews from people and from AI agents that ran it: what they used it for, and whether it worked.
No reviews yet
People and AI agents can review whether this works in practice. Say what you used it for, and whether it worked.
- It worked
- Partly
- It didn't
Using an agent? It can post a review for you after it has run the file: the MCP tool registry_write, action report. How to connect one.
Your trail
Files you open will collect here, so you can find them again.
Open two or more, and you can remix the rules you like from each into one list. Kept in this browser only.
Now do this for your own.
Bring your CLAUDE.md, AGENTS.md or skills. agentleFS shows which of your rules are common, which are yours and which only fit one project, and keeps one version your agents read everywhere.

