Skip to content

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

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

Activity

  1. PixelDust22 commented on Jul 27, 2022

    @PixelDust22
    ContributorAuthor

    I did some digging and this might be related to

    // In `generics_of` we set the generics' parent to be our parent's parent which means that
    // we lose out on the predicates of our actual parent if we dont return those predicates here.
    // (See comment in `generics_of` for more information on why the parent shenanigans is necessary)
    //
    // struct Foo<T, const N: usize = { <T as Trait>::ASSOC }>(T) where T: Trait;
    // ^^^ ^^^^^^^^^^^^^^^^^^^^^^^ the def id we are calling
    // ^^^ explicit_predicates_of on
    // parent item we dont have set as the
    // parent of generics returned by `generics_of`
    //
    // In the above code we want the anon const to have predicates in its param env for `T: Trait`
    let item_def_id = tcx.hir().get_parent_item(hir_id);
    // In the above code example we would be calling `explicit_predicates_of(Foo)` here

    // We don't inherit predicates from the parent here:
    // If we have, say `fn f<'a, T: 'a>() -> impl Sized {}`
    // then the return type is `f::<'static, T>::{{opaque}}`.
    //
    // If we inherited the predicates of `f` then we would
    // require that `T: 'static` to show that the return
    // type is well-formed.
    //
    // The only way to have something with this opaque type
    // is from the return type of the containing function,
    // which will ensure that the function's predicates
    // hold.

    @BoxyUwU Mind taking a look?

  2. PixelDust22 commented on Jul 28, 2022

    @PixelDust22
    ContributorAuthor

    #76560

    A fix exists here at #99801 but we're waiting on @spastorino 's pending RPIT refactor work to fix this. In the mean time, developers who need this feature can use the branch in #99801.

    https://rust-lang.github.io/rustup/concepts/toolchains.html#custom-toolchains

  3. added a commit that references this issue on Nov 2, 2022
    214d6b6
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-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