Cyclic dependencies on feature gated function

⚓ rust    📅 2025-05-23    👤 surdeus    👁️ 4      

surdeus

Warning

This post was published 37 days ago. The information described in this article may have changed.

Suppose I have a proc-macro crate called foo_macro that depends on a impl with a Cargo.toml like this:

[package]
name = "foo_macro"
version = "0.1.0"
edition = "2024"

[lib]
proc-macro = true

[dependencies]
foo_impl = { path = "../foo_impl", features = ["foo_feature"] }

[features]

foo_macro_use = []

What I want to archieve is to export some macros to the impl crate already implemented by it. So I have the lib.rs as follows:

use ::{
    foo_impl::foo_i,
    proc_macro::TokenStream,
};

#[cfg(feature = "foo_macro_use")]
#[proc_macro]
pub fn foo(x: TokenStream) -> TokenStream {
    foo_i(x).into()
}

#[cfg(not(feature = "foo_macro_use"))]
#[proc_macro]
pub fn bar(x: TokenStream) -> TokenStream {
    foo_impl::foo_a(x)    
}

And foo_impl as follows:

Cargo.toml

[package]
name = "foo_impl"
version = "0.1.0"
edition = "2024"

[dependencies]
proc-macro2 = "*"
foo_macro = { path = "../foo_macro", optional = true }

[features]
foo_feature = ["foo_macro?/foo_macro_use"]

lib.rs

use ::{
    proc_macro2::TokenStream,
};

pub fn foo_i(x: TokenStream) -> TokenStream {
    x
}

#[cfg(feature = "foo_feature")]
pub fn foo_a(x: TokenStream) -> TokenStream {
    foo_macro::foo!("bar");
    x
}

But this fails to compile with this message:

Updating crates.io index
error: cyclic package dependency: package `foo_impl v0.1.0 (D:\foo_impl)` depends on itself. Cycle:
package `foo_impl v0.1.0 (D:\foo_impl)`
    ... which satisfies path dependency `foo_impl` of package `foo_macro v0.1.0 (D:\foo_macro)`
    ... which satisfies path dependency `foo_macro` (locked to 0.1.0) of package `foo_impl v0.1.0 (D:\foo_impl)`

So my question is,is there a way to tell the compiler to depend only on one specified function to implement others or is there a workaround the neatest as possible?

Thanks for the attention.

2 posts - 2 participants

Read full topic

🏷️ rust_feed