
Wrapper around compiler's procedural-macro API, enabling proc-macro-like token streams outside macros, making macro logic unit-testable; faithful line-by-line transliteration preserving upstream behavior and provenance.
This is a Kotlin Multiplatform line-by-line transliteration port of dtolnay/proc-macro2.
Original Project: This port is based on dtolnay/proc-macro2. All design credit and project intent belong to the upstream authors; this repository is a faithful port to Kotlin Multiplatform with no behavioural changes intended.
This is an in-progress port. The goal is feature parity with the upstream Rust crate while providing a native Kotlin Multiplatform API. Every Kotlin file carries a // port-lint: source <path> header naming its upstream Rust counterpart so the AST-distance tool can track provenance.
The text below is reproduced and lightly edited from
https://github.com/dtolnay/proc-macro2. It is the upstream project's own description and remains under the upstream authors' authorship; links have been rewritten to absolute upstream URLs so they continue to resolve from this repository.
A wrapper around the procedural macro API of the compiler's proc_macro crate.
This library serves two purposes:
Bring proc-macro-like functionality to other contexts like build.rs and
main.rs. Types from proc_macro are entirely specific to procedural macros
and cannot ever exist in code outside of a procedural macro. Meanwhile
proc_macro2 types may exist anywhere including non-macro code. By developing
foundational libraries like syn and quote against proc_macro2 rather
than proc_macro, the procedural macro ecosystem becomes easily applicable to
many other use cases and we avoid reimplementing non-macro equivalents of
those libraries.
Make procedural macros unit testable. As a consequence of being specific
to procedural macros, nothing that uses proc_macro can be executed from a
unit test. In order for helper libraries or components of a macro to be
testable in isolation, they must be implemented using proc_macro2.
[dependencies]
proc-macro2 = "1.0"The skeleton of a typical procedural macro typically looks like this:
extern crate proc_macro;
#[proc_macro_derive(MyDerive)]
pub fn my_derive(input: proc_macro::TokenStream) -> proc_macro::TokenStream {
let input = proc_macro2::TokenStream::from(input);
let output: proc_macro2::TokenStream = {
/* transform input */
};
proc_macro::TokenStream::from(output)
}If parsing with Syn, you'll use parse_macro_input! instead to propagate
parse errors correctly back to the compiler when parsing fails.
The default feature set of proc-macro2 tracks the most recent stable compiler
API. Functionality in proc_macro that is not yet stable is not exposed by
proc-macro2 by default.
To opt into the additional APIs available in the most recent nightly compiler,
the procmacro2_semver_exempt config flag must be passed to rustc. We will
polyfill those nightly-only APIs back to Rust 1.71.0. As these are unstable APIs
that track the nightly compiler, minor versions of proc-macro2 may make breaking
changes to them at any time.
RUSTFLAGS='--cfg procmacro2_semver_exempt' cargo build
Note that this must not only be done for your crate, but for any crate that depends on your crate. This infectious nature is intentional, as it serves as a reminder that you are outside of the normal semver guarantees.
Semver exempt methods are marked as such in the proc-macro2 documentation.
dependencies {
implementation("io.github.kotlinmania:proc-macro2-kotlin:0.1.0")
}./gradlew build
./gradlew testSee AGENTS.md and CLAUDE.md for translator discipline, port-lint header convention, and Rust → Kotlin idiom mapping.
This Kotlin port is distributed under the same MIT license as the upstream dtolnay/proc-macro2. See LICENSE (and any sibling LICENSE-* / NOTICE files mirrored from upstream) for the full text.
Original work copyrighted by the proc-macro2 authors.
Kotlin port: Copyright (c) 2026 Sydney Renee and The Solace Project.
Thanks to the dtolnay/proc-macro2 maintainers and contributors for the original Rust implementation. This port reproduces their work in Kotlin Multiplatform; bug reports about upstream design or behavior should go to the upstream repository.
This is a Kotlin Multiplatform line-by-line transliteration port of dtolnay/proc-macro2.
Original Project: This port is based on dtolnay/proc-macro2. All design credit and project intent belong to the upstream authors; this repository is a faithful port to Kotlin Multiplatform with no behavioural changes intended.
This is an in-progress port. The goal is feature parity with the upstream Rust crate while providing a native Kotlin Multiplatform API. Every Kotlin file carries a // port-lint: source <path> header naming its upstream Rust counterpart so the AST-distance tool can track provenance.
The text below is reproduced and lightly edited from
https://github.com/dtolnay/proc-macro2. It is the upstream project's own description and remains under the upstream authors' authorship; links have been rewritten to absolute upstream URLs so they continue to resolve from this repository.
A wrapper around the procedural macro API of the compiler's proc_macro crate.
This library serves two purposes:
Bring proc-macro-like functionality to other contexts like build.rs and
main.rs. Types from proc_macro are entirely specific to procedural macros
and cannot ever exist in code outside of a procedural macro. Meanwhile
proc_macro2 types may exist anywhere including non-macro code. By developing
foundational libraries like syn and quote against proc_macro2 rather
than proc_macro, the procedural macro ecosystem becomes easily applicable to
many other use cases and we avoid reimplementing non-macro equivalents of
those libraries.
Make procedural macros unit testable. As a consequence of being specific
to procedural macros, nothing that uses proc_macro can be executed from a
unit test. In order for helper libraries or components of a macro to be
testable in isolation, they must be implemented using proc_macro2.
[dependencies]
proc-macro2 = "1.0"The skeleton of a typical procedural macro typically looks like this:
extern crate proc_macro;
#[proc_macro_derive(MyDerive)]
pub fn my_derive(input: proc_macro::TokenStream) -> proc_macro::TokenStream {
let input = proc_macro2::TokenStream::from(input);
let output: proc_macro2::TokenStream = {
/* transform input */
};
proc_macro::TokenStream::from(output)
}If parsing with Syn, you'll use parse_macro_input! instead to propagate
parse errors correctly back to the compiler when parsing fails.
The default feature set of proc-macro2 tracks the most recent stable compiler
API. Functionality in proc_macro that is not yet stable is not exposed by
proc-macro2 by default.
To opt into the additional APIs available in the most recent nightly compiler,
the procmacro2_semver_exempt config flag must be passed to rustc. We will
polyfill those nightly-only APIs back to Rust 1.71.0. As these are unstable APIs
that track the nightly compiler, minor versions of proc-macro2 may make breaking
changes to them at any time.
RUSTFLAGS='--cfg procmacro2_semver_exempt' cargo build
Note that this must not only be done for your crate, but for any crate that depends on your crate. This infectious nature is intentional, as it serves as a reminder that you are outside of the normal semver guarantees.
Semver exempt methods are marked as such in the proc-macro2 documentation.
dependencies {
implementation("io.github.kotlinmania:proc-macro2-kotlin:0.1.0")
}./gradlew build
./gradlew testSee AGENTS.md and CLAUDE.md for translator discipline, port-lint header convention, and Rust → Kotlin idiom mapping.
This Kotlin port is distributed under the same MIT license as the upstream dtolnay/proc-macro2. See LICENSE (and any sibling LICENSE-* / NOTICE files mirrored from upstream) for the full text.
Original work copyrighted by the proc-macro2 authors.
Kotlin port: Copyright (c) 2026 Sydney Renee and The Solace Project.
Thanks to the dtolnay/proc-macro2 maintainers and contributors for the original Rust implementation. This port reproduces their work in Kotlin Multiplatform; bug reports about upstream design or behavior should go to the upstream repository.