Skip to content

rustc doesn't seem to understand ~const bounds on associated types #88655

Closed as not planned

Description

@Techcable

I tried this code (playground]:

#![feature(const_trait_impl, const_fn_trait_bound)]

pub trait ConstableTrait {
    fn foo(&self) -> u32;
}

pub trait ExTrait {
    type Assoc: ~const ConstableTrait;
}

struct ShouldBeInferred<T: ExTrait> {
    a: T::Assoc
}
impl<T: ExTrait> ShouldBeInferred<T> {
    pub const fn should_infer(&self) -> u32 {
        self.a.foo()
    }
}

I expected to see this happen:
ShouldBeInferred::should_infer should understand that T::AssocTrait: ~const ConstableTrait and self.a.foo() is a const-fn.

Instead, this happened:
The compiler doesn't seem to infer the T::AssocTrait: ~const ConstableTrait bound gives an error self.a.foo() is not callable in a constant context.

I might be misunderstanding the purpose of ~const bounds on associated types. Please correct me if I am :)

Also great work so far on const fn. It's really starting to shine :)

Meta

rustc --version --verbose:

rustc 1.56.0-nightly (b7404c898 2021-09-03)
binary: rustc
commit-hash: b7404c898a1a6933b71c72428a6dce551bcc1be7
commit-date: 2021-09-03
host: x86_64-unknown-linux-gnu
release: 1.56.0-nightly
LLVM version: 13.0.0

Activity

  1. added
    requires-nightlyThis issue requires a nightly compiler in some way. When possible, use a F-* label instead.
    A-associated-itemsArea: Associated items (types, constants & functions)
    T-compilerRelevant to the compiler team, which will review and decide on the PR/issue.
    on Sep 7, 2021
  2. fee1-dead commented on Sep 9, 2021

    @fee1-dead
    Member

    With the change to ~const, these implicitness have been removed. So you can't rely on the inherent impl method being const, to assume that the impl's bounds are all inferred to be ~const.

    And currently I don't think it is possible to achieve what you want. But the ideal solution is to use where clauses on the const fn and bound T: ~const ExTrait. See also #88424, and we will allow these where clauses on const fns on inherent impls once we land #88558.

  3. added
    A-const-evalArea: Constant evaluation, covers all const contexts (static, const fn, ...)
    and removed on Dec 1, 2024
  4. nxsaken commented on Nov 16, 2025

    @nxsaken
    Contributor

    Seems to be a problem still:

    const trait Trait {
        type Type: Copy + [const] Clone;
    }
    
    #[derive(Copy)]
    #[derive_const(Clone)] // unsatisfied trait bound introduced here
    struct Struct<T: Trait>(T::Type); // note: required for `Struct<T>` to implement `[const] Clone`
    
    #[derive(Copy)]
    #[derive_const(Clone)]
    struct StructStruct<T: Trait>(Struct<T>); // error[E0277]: the trait bound `T: [const] Trait` is not satisfied
  5. fee1-dead commented on Nov 16, 2025

    @fee1-dead
    Member

    @nxsaken The generated code from the derive never makes a T: [const] Trait bound: (play)

    #![feature(const_clone)]
    #![feature(const_trait_impl)]
    #![feature(derive_const)]
    
    const trait Trait {
        type Type: Copy + [const] Clone;
    }
    
    #[derive(Copy)]
    // #[derive_const(Clone)]
    struct Struct<T: Trait>(T::Type);
    
    impl<T: [const] Clone + Trait> const Clone for Struct<T>
    where T::Type: [const] Clone {
        fn clone(&self) -> Self { Struct(self.0.clone()) }
    }
    
    #[derive(Copy)]
    // #[derive_const(Clone)]
    struct StructStruct<T: Trait>(Struct<T>); 
    
    impl<T: [const] Clone + Trait> const Clone for StructStruct<T> {
        fn clone(&self) -> Self { StructStruct(self.0.clone()) }
    }

    And [const] is only implied via [const] bounds. If you want it to always be applicable, use type Type: Copy + const Clone, or use T: [const] Trait on a manual impl

  6. fee1-dead commented on Nov 16, 2025

    @fee1-dead
    Member

    Closing the original issue as won't fix. T::Assoc can be assumed to have [const] ConstableTrait if we have T: [const] ExTrait. The original code snippet does not have T: [const] ExTrait so this cannot be assumed.

    To fix: either use type Assoc: const ConstableTrait always const bound on the assoc, or bound T: [const] ExTrait.

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-associated-itemsArea: Associated items (types, constants & functions)A-const-evalArea: Constant evaluation, covers all const contexts (static, const fn, ...)A-trait-systemArea: Trait systemC-bugCategory: This is a bug.F-const_trait_impl`#![feature(const_trait_impl)]`T-compilerRelevant to the compiler team, which will review and decide on the PR/issue.requires-nightlyThis issue requires a nightly compiler in some way. When possible, use a F-* label instead.

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions