Compile_warning! macro?

Is there an equivalent to the compile_error! macro that can put out information / warnings during a compile?

background:
I have a feature that only works on wasm. I use it like.

#[cfg(all(feature = "works-on-wasm", not(target_arch = "wasm32")))]
compile_error!("`wasm32-unknown-unknown` target is required to use feature `works-on-wasm`");

I have another feature that works on native but subpar on native. I want to warn, something like.

#[cfg(all(feature = "better-on-wasm", not(target_arch = "wasm32")))]
compile_warn!("The 'better-on-wasm' feature works better on 'wasm32-unknown-unknown`");
``

AFAICT no.

I don't think you can emit a warning in source code using macros, as a workaround, you can print a warning message using build script though.

// build.rs
fn main() {
    if std::env::var("CARGO_CFG_TARGET_ARCH).unwrap() == "wasm32"
            && std::env::var("CARGO_FEATURE_FOO_BAR").is_some() {
        println!("cargo:warning=message goes here");
    }
}

see:

https://doc.rust-lang.org/cargo/reference/environment-variables.html#environment-variables-cargo-sets-for-build-scripts

https://doc.rust-lang.org/cargo/reference/build-scripts.html#cargo-warning

1 Like

Thank for the link. Amazing how such simple things get complicated.

There's also an unstable API for proc macros to generate warnings and other diagnostics https://doc.rust-lang.org/proc_macro/struct.Diagnostic.html

Thanks. I haven't done a "build script" yet. I think I will try this, just to get the experience of "build script". But I wont use it in this project, I think I will just make the feature better-on-wasm just do nothing on native. And document that fact.