Skip to content

Conditional compilation: E0747 and E0107 on generics and lifetime #138716

@IzawGithub

Description

@IzawGithub

Context

I want to use a #[cfg] attribute to conditionally include additional parameters in a struct.
Those parameters sometimes have generics or lifetime that are viral, and need to be attached to the parent.

Code

// This work
struct A<#[cfg(test)] T> {
  #[cfg(test)]
  pub a: T
}

// This doesn't
impl<#[cfg(test)] T> A<#[cfg(test)] T> {}

Link to playground

Expected

The impl block compile successfully.

Being able to put attribute on a struct is an expected feature, so it is unexpected that it does not work correctly for impl block.

Analysis

Rust analyzer syntax tree errors on impl<#[cfg(test)] T> ConditionalGeneric<.
It recognize that the other parameters list have an attribute, but fail to do so at a PATH_SEGMENT for the impl.

Meta

Tested on stable 1.85.1 and 1.87.0-nightly (2025-03-18 75530e9).

Metadata

Metadata

Assignees

No one assigned

    Labels

    A-attributesArea: Attributes (`#[…]`, `#![…]`)A-cfgArea: `cfg` conditional compilationC-bugCategory: This is a bug.T-compilerRelevant to the compiler team, which will review and decide on the PR/issue.

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions