makepad/libs/relax/compiler/tests/macro_doc_comments.rs
Admin b22a714840 makepad run: the Rust JIT (libs/relax) and makepad-remote
The JIT that runs makepad apps from source: lexer, front end, type checker,
lazy borrow checker (Gandalf; `--check-first` opts in), codegen, session and
warm/cold caches keyed by compiler stamp, provided crates bound late,
startup images, live patching. User-facing name is `makepad run` (binary
`makepad`, MAKEPAD_RUN_* env, ~/.makepad-run); libs/relax stays the internal
library name. `makepad check` reports rustc-style diagnostics with rustc
error codes and checks a whole workspace (`--workspace`). Runs on macOS,
Linux, Windows and Android. makepad-remote: one tool schema for apps and the
WM (app/1): grabs, captures, traces, frame profiles and tweaks.

Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
2026-10-13 12:26:21 +02:00

71 lines
1.8 KiB
Rust

// Doc comments in a macro_rules call's input are matched as rustc desugars them:
// `/// x` is `#[doc = r"x"]`, `//! x` is `#![doc = r"x"]` (rustc_ast tokenstream.rs:698
// `desugar_doc_comments`, called by rustc_expand mbe/macro_rules.rs:1808 `parser_from_cx`),
// the raw string with as many `#` as the text needs (tokenstream.rs:736-753).
// syn's token.rs:521 `define_punctuation_structs!` matches `#[doc = $usage:literal]`.
macro_rules! doc_literal {
($(#[doc = $x:literal])* $name:ident) => {
const $name: &[&str] = &[$($x),*];
};
}
doc_literal! {
/// a
/// b "quoted"
/// c "#hash
DOCS
}
macro_rules! doc_meta {
($(#[$m:meta])* struct $name:ident;) => {
$(#[$m])* struct $name;
impl $name {
fn count() -> usize { [$(stringify!($m)),*].len() }
}
};
}
doc_meta! {
/// one
/// two
struct WithDocs;
}
// tt passthrough: an inner doc is `# ! [doc = ..]` and is emitted as the attribute
macro_rules! pass {
($($t:tt)*) => { $($t)* };
}
macro_rules! module {
($name:ident { $($t:tt)* }) => { mod $name { $($t)* } };
}
module! {
inside {
//! inner doc, forwarded as `#![doc = r" inner doc..."]`
pub fn after_inner() -> i32 { 7 }
}
}
pass! {
/// outer doc through tt
fn after_outer() -> i32 { 8 }
}
macro_rules! single_tt {
(#[doc = $d:literal] fn $f:ident) => { fn $f() -> &'static str { $d } };
}
single_tt! {
/// plain
fn plain_doc
}
single_tt! {
/** block "doc" */
fn block_doc
}
#[test]
fn doc_comments_desugar() {
assert_eq!(DOCS, [" a", " b \"quoted\"", " c \"#hash"]);
assert_eq!(WithDocs::count(), 2);
assert_eq!(inside::after_inner(), 7);
assert_eq!(after_outer(), 8);
assert_eq!(plain_doc(), " plain");
assert_eq!(block_doc(), " block \"doc\" ");
}