Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Maybe incorrect usage of proc-macro-crate in some proc macros #5013

Closed
indirection42 opened this issue Jul 14, 2024 · 4 comments
Closed

Maybe incorrect usage of proc-macro-crate in some proc macros #5013

indirection42 opened this issue Jul 14, 2024 · 4 comments
Labels
I2-bug The node fails to follow expected behavior. I10-unconfirmed Issue might be valid, but it's not yet known.

Comments

@indirection42
Copy link

indirection42 commented Jul 14, 2024

There are bunch of codes in the codebase use the @bkchr 's proc-macro-crate to generate crate access like this:

use proc_macro_crate::{crate_name, FoundCrate};
...
match crate_name(def_crate) {
     Ok(FoundCrate::Itself) => Ident::new(def_crate.replace('-', "_"), Span::call_site())
     Ok(FoundCrate::Name(name) => Ident::new(&name, Span::call_site()),
     Err(e) => return 	Error::new(Span::call_site(), e.to_compile_error());
}
...

However, the Itself branch seems incorrect. We cannot directly refer def_crate in def_crate itself, see also rust-lang/rust#54363.
I think the correct usage is Ok(FoundCrate::Itself) => Ident::new("crate", Span::call_site()).

@indirection42 indirection42 added I10-unconfirmed Issue might be valid, but it's not yet known. I2-bug The node fails to follow expected behavior. labels Jul 14, 2024
@bkchr
Copy link
Member

bkchr commented Jul 14, 2024

Do you have any issues or what is the problem? Using crate does not work for integration tests for example.

@indirection42
Copy link
Author

Since there are no usages of referring def_crate in def_crate in this repo except integration test (actually each integration test is compiled as a separate crate), this issue doesn't show off.
I wrote an example to demonstrate this issue.

https://github.com/indirection42/proc-macro-crate-example/blob/465ad32f5f6dbb11d1d36caa81462c2daadd1265/adder/procedural/src/lib.rs#L24-L30
If we use quote!(adder::submod) in FoundCrate::Itself branch, we cannot use the macro in adder's unit test.
https://github.com/indirection42/proc-macro-crate-example/blob/465ad32f5f6dbb11d1d36caa81462c2daadd1265/adder/src/lib.rs#L11

@bkchr
Copy link
Member

bkchr commented Jul 15, 2024

Everything is compiling in this repo. I have no idea why exactly you opened this issue. I mean did something not work for you? You are just confused that it works?

BTW, you can write in your lib.rs:

extern crate self as whatever;

Then you can refer to your own crate in your own crate using whatever.

Given that this is more an issue for the proc-macro-crate issue tracker and there is not really an issue, I'm going to close this.

@bkchr bkchr closed this as completed Jul 15, 2024
@indirection42
Copy link
Author

indirection42 commented Jul 16, 2024

I mean it's a potential bug. Considering this pattern is used often in polkadot-sdk, if someone is referring the pattern used in this repo, and he/she tests the some-crate-proc-macro in a unit test within the some-crate, then he/she will get confused why it won't compile.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
I2-bug The node fails to follow expected behavior. I10-unconfirmed Issue might be valid, but it's not yet known.
Projects
None yet
Development

No branches or pull requests

2 participants