Ambiguous attribute caused by `proc_macro_derive`

The crate native_db conflicts with another crate, specta.
Here is the code/error:

    use native_db::{native_db, InnerKeyValue};
    use native_model::{native_model, Model};
    use serde::{Deserialize, Serialize};
    use specta::{Type};

    #[derive(Type, Serialize, Deserialize, Debug)]
    #[native_model(id = 1, version = 1)]
    #[native_db]
    pub struct Person {
        #[primary_key]
        pub name: String,
        pub age: i32,
    }
error[E0659]: `doc` is ambiguous
  --> src/main.rs:25:9
   |
25 |         #[native_db]
   |         ^^^^^^^^^^^^ ambiguous name
   |
   = note: ambiguous because of a name conflict with a builtin attribute
   = note: `doc` could refer to a built-in attribute
note: `doc` could also refer to the derive helper attribute defined here
  --> src/main.rs:23:18
   |
23 |         #[derive(Type, Serialize, Deserialize, Debug)]
   |                  ^^^^
   = note: this error originates in the attribute macro `native_db` (in Nightly builds, run with -Z macro-backtrace for more info)

If I understand correctly, the keyword doc conflicts with another keyword doc that would be brought by native_db, but there are no references in the code of native_db to the addition of the keyword doc generated by the macro.

Can proc_macro_derive macros conflict even if they do not have any keywords in common?
Here is the definition of the two proc_macro_derive.

#[proc_macro_derive(KeyAttributes, attributes(primary_key, secondary_key))]
pub fn key_attributes(_input: TokenStream) -> TokenStream {
...
#[proc_macro_derive(Type, attributes(specta, serde, doc))]
pub fn derive_type(input: proc_macro::TokenStream) -> proc_macro::TokenStream {
...

The problem does not appear with the release candidate version 2.0.0-rc.10 of Specta because the doc field is not present in the attributes of the macro. However, I would like to understand why I am getting this error (my curiosity :p)

Note/Context: I'm the owner of native_db, and the question coming from this topic.

The problem is triggered by your macro because you include a /// doc comment in yours, and quote::quote! turns it into the #[doc = "doc comment"] form. However, I think the bug is in specta - they should not use a built-in attribute as a derive helper. Nothing done by native_db or quote seems unreasonable to me in this case.

1 Like

Thank you very much for the explanation.

And even though I understand that they should not use the doc built-in attribute, I will remove the comments in the generation of the native_db macro that are not very useful, and this way the problem will not occur anymore.

This topic was automatically closed 90 days after the last reply. We invite you to open a new topic if you have further questions or comments.