Skip to content

impl blocks do not support conditional type parameters #119281

@andreisilviudragnea

Description

@andreisilviudragnea

I tried this code:

struct S<#[cfg(feature = "f")] T> {
    #[cfg(feature = "f")]
    t: T,
}

impl<#[cfg(feature = "f")] T> S<#[cfg(feature = "f")] T> {
    fn m(&self) {
        println!("m");
    }
}

I expected to see this happen: code compiles successfully with both feature "f" enabled and disabled.

Instead, this happened:

❯ cargo test
   Compiling rust-webapp v0.1.0 (/Users/dragnea/IdeaProjects/rust-webapp)
error: invalid const generic expression
 --> src/cfg_type_parameter.rs:6:55
  |
6 | impl<#[cfg(feature = "f")] T> S<#[cfg(feature = "f")] T> {
  |                                                       ^
  |
help: expressions must be enclosed in braces to be used as const generic arguments
  |
6 | impl<#[cfg(feature = "f")] T> S<#[cfg(feature = "f")] { T }> {
  |                                                       +   +

error[E0747]: constant provided when a type was expected
 --> src/cfg_type_parameter.rs:6:55
  |
6 | impl<#[cfg(feature = "f")] T> S<#[cfg(feature = "f")] T> {
  |                                                       ^

For more information about this error, try `rustc --explain E0747`.
error: could not compile `rust-webapp` (bin "rust-webapp" test) due to 2 previous errors

I managed to avoid this bug by using a macro hack, but this does not solve the original problem:

struct S<#[cfg(feature = "f")] T> {
    #[cfg(feature = "f")]
    t: T,
}

#[cfg(feature = "f")]
macro_rules! s_type {
    [$t:ident] => {
        S<$t>
    }
}

#[cfg(not(feature = "f"))]
macro_rules! s_type {
    [$t:ident] => {
        S
    }
}

impl<#[cfg(feature = "f")] T> s_type![T] {
    fn m(&self) {
        println!("m");
    }
}

I reproduced this bug here. I also saw this bug discussed two times on Rust forum: here and here.

Meta

rustc --version --verbose:

rustc 1.77.0-nightly (2d7be7393 2023-12-23)
binary: rustc
commit-hash: 2d7be73931e0978c8758a672cc7258b417a7e999
commit-date: 2023-12-23
host: aarch64-apple-darwin
release: 1.77.0-nightly
LLVM version: 17.0.6
Backtrace

<backtrace>

Activity

added
needs-triageThis issue may need triage. Remove it if it has been sufficiently triaged.
on Dec 24, 2023
jonaspleyer

jonaspleyer commented on Dec 24, 2023

@jonaspleyer
Contributor

I am the person from the second post. I noticed that for me, the solution I was looking for was not to be solved by features.

I redesigned my API and was afterwards running into the problem that no specialization is currently implemented in stable.

So far I believe in general that feature flags inside Generic Arguments should be avoided at all costs. But I can understand that if the feature is present and working in Structs, the language should also support it for other use-cases.

changed the title [-]Generic parameter dependent on a feature[/-] [+]`impl` blocks do not support conditional type parameters[/+] on Jan 31, 2024
added
T-langRelevant to the language team
C-feature-requestCategory: A feature request, i.e: not implemented / a PR.
and removed
C-bugCategory: This is a bug.
on Feb 26, 2024
added
A-attributesArea: Attributes (`#[…]`, `#![…]`)
A-grammarArea: The grammar of Rust
and removed
needs-triageThis issue may need triage. Remove it if it has been sufficiently triaged.
on Feb 29, 2024
added
A-cfgArea: `cfg` conditional compilation
T-compilerRelevant to the compiler team, which will review and decide on the PR/issue.
T-typesRelevant to the types team, which will review and decide on the PR/issue.
needs-rfcThis change is large or controversial enough that it should have an RFC accepted before doing it.
on May 1, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    A-attributesArea: Attributes (`#[…]`, `#![…]`)A-cfgArea: `cfg` conditional compilationA-grammarArea: The grammar of RustC-feature-requestCategory: A feature request, i.e: not implemented / a PR.T-compilerRelevant to the compiler team, which will review and decide on the PR/issue.T-langRelevant to the language teamT-typesRelevant to the types team, which will review and decide on the PR/issue.needs-rfcThis change is large or controversial enough that it should have an RFC accepted before doing it.

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

      Development

      No branches or pull requests

        Participants

        @andreisilviudragnea@fmease@jieyouxu@rustbot@jonaspleyer

        Issue actions

          `impl` blocks do not support conditional type parameters · Issue #119281 · rust-lang/rust