Skip to content

No associated item found when the associated item was used as const generic parameter in return type #99705

Closed
@PixelDust22

Description

@PixelDust22

I tried this code:

// build-pass
#![crate_type = "lib"]
#![feature(generic_const_exprs)]
#![allow(incomplete_features)]
trait MyIterator {
    type Output;
}

trait Foo {
    const ABC: usize;
}

struct IteratorStruct<const N: usize>{

}

struct BarStruct<const N: usize> {
    data: [usize; N]
}

impl<const N: usize> MyIterator for IteratorStruct<N> {
    type Output = BarStruct<N>;
}

fn test<T: Foo>() -> impl MyIterator<Output = BarStruct<{T::ABC}>> where [(); {T::ABC}]: Sized {
    IteratorStruct::<{T::ABC}>{}
}

This code should compile normally.

Instead, this happened:

error[E0599]: no associated item named `ABC` found for type parameter `T` in the current scope
  --> /home/neo/rust/src/test/ui/const-generics/generic_const_exprs/my_test.rs:28:61
   |
LL | fn test<T: Foo>() -> impl MyIterator<Output = BarStruct<{T::ABC}>> {
   |         -                                                   ^^^ associated item not found in `T`
   |         |
   |         associated item `ABC` not found for this type parameter
   |
   = help: items from traits can only be used if the type parameter is bounded by the trait

error: aborting due to previous error

For more information about this error, try `rustc --explain E0599`.

In the test function, T was already bounded by the Foo trait which does have an associated item named ABC. However, Rust complains that no associated item named ABC was found for T.

Meta

rustc --version --verbose:

rustc 1.64.0-nightly (62b272d25 2022-07-21)
binary: rustc
commit-hash: 62b272d25c5bb8b6bb8ac73797d82b8b9a1eabda
commit-date: 2022-07-21
host: x86_64-pc-windows-msvc
release: 1.64.0-nightly
LLVM version: 14.0.6

Metadata

Metadata

Assignees

No one assigned

    Labels

    A-const-genericsArea: const generics (parameters and arguments)C-bugCategory: This is a bug.F-generic_const_exprs`#![feature(generic_const_exprs)]`

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions