Skip to content

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

Closed
@Forsworns

Description

@Forsworns

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...

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions