cbindgen icon indicating copy to clipboard operation
cbindgen copied to clipboard

[Question] Why does parse.expand use an unstable flag to rustc?

Open bavalpey opened this issue 1 year ago • 1 comments

Currently, [parse.expand] uses the -Z unstable pretty=expand option to rustc. This requires using the nightly rustc compiler. There is nothing in the documentation that mentions why this is required, and there isn't much discussion as to why. If macro expansion is needed, is there a reason that cbindgen can't just do cargo expand which does not require any nightly rustc options?

I think that macro expansion is a relatively common requirement, and gating it behind having to use a nightly toolchain is a big cost.

bavalpey avatar Oct 21 '24 23:10 bavalpey

I've been looking into this more and I think that cbindgen should set RUSTC_BOOTSTRAP in the command line where it invokes rustc. This is what cargo-expand does. That way, we can avoid having needless error messages when using cbindgen without a nightly compiler.

bavalpey avatar Oct 28 '24 01:10 bavalpey

Well the point of it being unstable is that you shouldn't rely on it. Using RUSTC_BOOTSTRAP defeats that purpose.

emilio avatar Dec 06 '24 01:12 emilio

Cargo-expand is one of the more popular crates. It uses this option, and has for a long time.

You also can follow the same logic it uses to determine if rustc bootstrap is even needed for invocation.

Also, a user should already understand that expanding macros with this is unstable. Sort of. Macro expansion can change in minor ways anyway.

Currently, builds that rely on this will fail with misleading error messages. I had to invoke a build script that sets RUSTC_BOOTSTRAP anyway. It's already isolated to just the macro expansion invocation, and stability shouldn't matter for this portion of cbindgen.

The macro expansion with unpretty-expanded is also essentially stable anyway, if you look at its history. It would be much, much better to use this option and document the flag as unstable in the first place. Users are opting into macro expansion by specifying it in cbindgen.toml.

In other words, this answer doesn't satisfy me.

bavalpey avatar Dec 06 '24 03:12 bavalpey

Setting RUSTC_BOOTSTRAP from a tool is a big no-no. The right thing to do, if this option is stable-ish, would be to stabilize it on the rust side (https://github.com/rust-lang/rust/issues/43364)

emilio avatar Dec 06 '24 03:12 emilio

Setting RUSTC_BOOTSTRAP from a tool is a big no-no.

You're not setting it from the tool permanently. You are setting it only for your invocation of rustc, via the command line, that is used for macro expansion, which isn't used in any way to compile the code or impact downstream. It's also kind of a big no no to have an error manifest without documentation.

The right thing to do, if this option is stable-ish, would be to stabilize it on the rust side (rust-lang/rust#43364)

I agree, but we don't have control over that.

bavalpey avatar Dec 06 '24 03:12 bavalpey

just thought i would say i have a fork here where i use bootstrap like cargo expand. https://github.com/n1ght-hunter/cbindgen/tree/stable-expand i really did not want to force my projects to use nightly for the sole reason of expanding macros.

n1ght-hunter avatar Sep 18 '25 05:09 n1ght-hunter