[Solved] Applying attributes to macro included code

I am following the example here: https://rust-lang.github.io/rust-bindgen/tutorial-4.html for using bindgen and I've included some bindings in the lib.rs file like so:

include!(concat!(env!("OUT_DIR"), "/bindings.rs"));

The project I'm trying to incorporate this bindings code into does not allow missing doc or unstable features:

#![deny(missing_docs)]
#![deny(unstable_features)]

and I am running into a problem where the included code is not documented in a way that rust accepts. As a result, I am hitting this error when I compile:

error: missing documentation for a struct field

I can stop the error by removing the deny(missing_docs) attribute, but removing the attribute is not acceptable as a solution. I tried to put an #allow(missing_docs) outer attribute before the include macro but this did not help:

#[allow(missing_docs)]
include!(concat!(env!("OUT_DIR"), "/bindings.rs"));

I was hoping I could do something in the spirt of C to set scope like this:

{
#![allow(missing_docs)]
include!(concat!(env!("OUT_DIR"), "/bindings.rs"));
}

But this is not valid syntax and I don't know how to do something of this spirt with rust. Any suggestions?

Perhaps you could put the include in a mod block and put the allow on that?

Hi .. Thanks. Probably this is what I want. I guess there are no such thing as an anonymous module? The one issue I see though is that this module container seems to be blocking all attributes such that even if I don't include an allow of missing_docs as an outer attribute, the code still compiles fine:

mod my_bindings {
include!(concat!(env!("OUT_DIR"), "/bindings.rs"));
}

Also, if I explicitly declare no deny(missing_docs) within the module block this does not have any effect as I assumed it would.

mod my_bindings {
#![deny(missing_docs)]
include!(concat!(env!("OUT_DIR"), "/bindings.rs"));
}

This is probably very basic, but how do you apply attributes to modules that are declared in this way? How do I keep the surrounding attributes (except the missing_docs one).

Thanks.

This works for me:

#![deny(missing_docs)]
//! crate docs

/// foo
fn foo() {
    
}

mod bar {
    #![allow(missing_docs)]
    fn bar() {
        
    }
}

Are you sure you just didn't type deny instead of allow?

I suspect the include macro is the difference in what you are doing. If I do this:

#![deny(missing_docs)]

include!(concat!(env!("OUT_DIR"), "/bindings.rs"));

This does not compile due to the included bindings.rs generated with bindgen not being documented for Rust.

If I wrap the include in a mod, then things compile (surprisingly even without using the allow missing_docs attribute)

#![deny(missing_docs)]

mod my_bindings {
include!(concat!(env!("OUT_DIR"), "/bindings.rs"));
}

Compiles fine.

If I explicitly deny missing docs within the module it still compiles fine.

#![deny(missing_docs)]

mod my_bindings {
#![deny(missing_docs)]
include!(concat!(env!("OUT_DIR"), "/bindings.rs"));
}

or

#![deny(missing_docs)]

mod my_bindings {
#[deny(missing_docs)]
include!(concat!(env!("OUT_DIR"), "/bindings.rs"));
}

both compile fine. No attributes related to missing_docs seem to apply to the included code. I assume then that no other attributes are applying as well, which is not necessarily what I want.

FYI ... As soon as I made the mod pub, it started to be impacted by the attributes as I expected:

//#[allow(missing_docs)]
pub mod bindings {
#![allow(missing_docs)]
include!(concat!(env!("OUT_DIR"), "/bindings.rs"));
}

#[allow(missing_docs)]
pub mod bindings {
//#![allow(missing_docs)]
include!(concat!(env!("OUT_DIR"), "/bindings.rs"));
}

Both compile fine. However, this does not:

pub mod bindings {
include!(concat!(env!("OUT_DIR"), "/bindings.rs"));
}

Which is what I was looking for initially. I think this will work. Thanks!

1 Like

missing_docs indeed only applies to exported items, and wrapping them in a non pub module was making everything private.

Now your ffi items are in the bindings namespace; if this is not desirable (e.g., you want the equivalent of an anonymous module), you can do the following:

#![deny(missing_docs)]

pub use self::bindings::*;
mod bindings {
    #![allow(missing_docs)]

    include!(...);
}
1 Like

Yes, I see. Thanks.