Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Closed
Neo-Zhixing opened this issue Jul 25, 2022 · 2 comments · Fixed by #99801
Labels
A-const-generics Area: const generics (parameters and arguments) C-bug Category: This is a bug. F-generic_const_exprs `#![feature(generic_const_exprs)]`

Comments

@Neo-Zhixing
Copy link
Contributor

Neo-Zhixing commented Jul 25, 2022

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
@Neo-Zhixing Neo-Zhixing added the C-bug Category: This is a bug. label Jul 25, 2022
@Neo-Zhixing
Copy link
Contributor Author

Neo-Zhixing commented Jul 27, 2022

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?

@BoxyUwU BoxyUwU added F-generic_const_exprs `#![feature(generic_const_exprs)]` A-const-generics Area: const generics (parameters and arguments) labels Jul 27, 2022
@Neo-Zhixing
Copy link
Contributor Author

#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

@bors bors closed this as completed in 214d6b6 Nov 3, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-const-generics Area: const generics (parameters and arguments) C-bug Category: This is a bug. F-generic_const_exprs `#![feature(generic_const_exprs)]`
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants