Stream: t-compiler/help

Topic: procedural macro API panic


Federico (Jul 31 2019 at 07:55, on Zulip):

Is there a documentation for the panic "Procedural macro API is used outside of a procedural macro"?

Federico (Jul 31 2019 at 07:58, on Zulip):

I'm building a procedural macro crate, which links and executes a native library, and the compiler crashes with that error (stacktrace: https://users.rust-lang.org/t/procedural-macro-api-is-used-outside-of-a-procedural-macro/30841).

Federico (Jul 31 2019 at 09:21, on Zulip):

I don't understand why the panic originates in the library: the library does not use the proc_macro API, but only the proc_macro2 crate

simulacrum (Jul 31 2019 at 11:09, on Zulip):

@Federico the proc_macro2 crate internally uses the proc_macro API

Federico (Jul 31 2019 at 11:50, on Zulip):

@simulacrum I see, thanks! I expected proc_macro2to be a opaque wrapper around proc_macro, without the limitations of the latter.

simulacrum (Jul 31 2019 at 12:03, on Zulip):

The limitations are sort of inherent and can't be removed by a library I believe

simulacrum (Jul 31 2019 at 12:03, on Zulip):

(beyond duplicating the entire compiler, but even then you don't get what you want since the result won't work with rustc)

Federico (Jul 31 2019 at 12:57, on Zulip):

But what does "outside of a procedural macro" exactly mean? Outside the crate that implements a procedural macro?

simulacrum (Jul 31 2019 at 12:57, on Zulip):

the context is loosely I believe same-thread

simulacrum (Jul 31 2019 at 13:00, on Zulip):

just based on the snippets in that post I would expect it to work but more info might be needed on how exactly PLUGIN is defined/what it does

Federico (Jul 31 2019 at 13:03, on Zulip):

Can it be that also calling a native library crosses the boundary? The plugin is a Rust crate, linked as native library in order to allow the procedural macro crate to choose via an environment variable which implementation to use

Federico (Jul 31 2019 at 13:03, on Zulip):

The code is here: https://github.com/viperproject/rust-contracts

simulacrum (Jul 31 2019 at 13:04, on Zulip):

Plausible, yeah, I'm not entirely sure myself when TLS changes

simulacrum (Jul 31 2019 at 13:04, on Zulip):

and/or is no longer accessible

simulacrum (Jul 31 2019 at 13:05, on Zulip):

I would check if you can reproduce when not dynamically loading the library (just regular cargo dep)

simulacrum (Jul 31 2019 at 13:05, on Zulip):

that way you can better isolate the failure

simulacrum (Jul 31 2019 at 13:05, on Zulip):

you could also plausibly test if it's working by declaring via thread_local! a thread local and seeing if you can access the same local from a separate library and/or googling that

simulacrum (Jul 31 2019 at 13:06, on Zulip):

https://github.com/rust-lang/rust/blob/9152fe4ea053a29469691349f4b63aa94c9aac56/src/libproc_macro/bridge/client.rs#L268-L271 is the relevant local afaik

Federico (Jul 31 2019 at 13:09, on Zulip):

Thanks for the info!

Last update: Nov 11 2019 at 22:00UTC