Skip to content

Wrong diagnostic output when using cfg_attr and derive-macro-helper-attributes #10736

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

Closed
Forsworns opened this issue Nov 10, 2021 · 2 comments
Closed

Comments

@Forsworns
Copy link

I'm using stable rustc 1.56.0 (09c42c458 2021-10-18) and rust-analyzer v0.2.809. I found that derive-macro-helper-attributes have to be placed before related derive-macros when using cfg_attr. Otherwise, the rust-analyzer will generate red highlights indicating there exist errors.

The problem is related to cfg_attr, I made a minimal example so that you can verify it locally.

// main.rs
#[cfg(feature = "use_kube_rs")]
use kube::CustomResource;
use schemars::JsonSchema;
use serde::{Deserialize, Serialize};

#[cfg_attr(
    feature = "use_kube_rs",
    kube(group = "clux.dev", version = "v1", kind = "Foo", namespaced),
    derive(CustomResource, Clone, Debug, Default, PartialEq, Deserialize, Serialize, JsonSchema)
)]
struct FooSpec {
    info: String,
}


#[cfg_attr(
    feature = "use_kube_rs",
    derive(CustomResource, Clone, Debug, Default, PartialEq, Deserialize, Serialize, JsonSchema),
    kube(group = "clux.dev", version = "v1", kind = "Bar", namespaced)
)]
struct BarSpec {
    info: String,
}

fn main() {
    println!("Hello, world!");
}
# Cargo.toml

[package]
name = "test_cfg_attr"
version = "0.1.0"
edition = "2021"

# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html

[features]
default = ["use_kube_rs"]
use_kube_rs = ["kube", "k8s-openapi"]

[dependencies]
kube = {version="0.63.2", features = ["default","derive", "runtime"], optional=true}
k8s-openapi = {version="0.13.1", features = ["v1_22"], optional=true}
schemars = "0.8.6"
serde = { version = "1.0.126", features = ["derive"]}
serde_json = "1.0.64"

Here are screenshot for illustrations:

image

image

image

I'm not sure but it seems a bug...

@Veykril
Copy link
Member

Veykril commented Nov 10, 2021

Duplicate of #10110, for now you can split up the cfg_attr in two, that is make one per inner attribute as a workaround to get rid of the errors.

@Veykril Veykril closed this as completed Nov 10, 2021
@nyurik
Copy link
Contributor

nyurik commented Nov 8, 2023

I tried this approach per @Veykril for #15840, but that didn't work :(

#[cfg_attr(feature = "display", derive(displaydoc::Display))]
#[cfg_attr(feature = "display", ignore_extra_doc_attributes)]

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants