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

RPITIT with generic const constraint requires implied lifetime bounds to be explicitly specified in impls #138411

Open
alice-emerson opened this issue Mar 12, 2025 · 1 comment
Labels
C-bug Category: This is a bug. F-generic_const_exprs `#![feature(generic_const_exprs)]` needs-triage This issue may need triage. Remove it if it has been sufficiently triaged. requires-incomplete-features This issue requires the use of incomplete features. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.

Comments

@alice-emerson
Copy link

I tried this code:

#![feature(generic_const_exprs)]

trait Trait {}
struct Concrete;
impl Trait for Concrete {}

trait Demo<const N: usize> {
    fn neither() -> Concrete;
    fn bound_only() -> Concrete where [(); N-1]:;
    fn rpit_only() -> impl Trait;
    fn rpit_and_bound() -> impl Trait where [(); N-1]:;
}

impl<'a, T, const N: usize> Demo<N> for &'a T {
    // Compiles fine
    fn neither() -> Concrete { Concrete }
    fn bound_only() -> Concrete where [(); N-1]: { Concrete }
    fn rpit_only() -> impl Trait { Concrete }
    
    // Compile error
    fn rpit_and_bound() -> impl Trait where [(); N-1]: { Concrete }
}

I expected to see this happen: The code compiles without errors

Instead, this happened:

The [(); N-1]: bound in the trait definition is highlighted as an error complaining that T does not outlive 'a, and suggests adding T: 'a to the impl block.

Meta

rustc --version --verbose:

rustc 1.87.0-nightly (665025243 2025-03-11)
binary: rustc
commit-hash: 6650252439d4e03368b305c42a10006e36f1545e
commit-date: 2025-03-11
host: x86_64-unknown-linux-gnu
release: 1.87.0-nightly
LLVM version: 20.1.0
@alice-emerson alice-emerson added the C-bug Category: This is a bug. label Mar 12, 2025
@rustbot rustbot added the needs-triage This issue may need triage. Remove it if it has been sufficiently triaged. label Mar 12, 2025
@ShE3py
Copy link
Contributor

ShE3py commented Mar 12, 2025

Minimized:

#![feature(generic_const_exprs)]
// ^ compiles fine without this

trait Foo {
    fn foo() -> impl ToString where [(); 0 + 0]:;
}

impl<T> Foo for &'_ T {
    fn foo() -> impl ToString where [(); 0 + 0]: { "foo" }
}
error[E0311]: the parameter type `T` may not live long enough
 --> src/lib.rs:5:37
  |
5 |     fn foo() -> impl ToString where [(); 0 + 0]:;
  |                                     ^^^^^^^^^^^ ...so that the reference type `&T` does not outlive the data it points at
...
8 | impl<T> Foo for &'_ T {
  |                  -- the parameter type `T` must be valid for the anonymous lifetime as defined here...
  |
help: consider adding an explicit lifetime bound
  |
8 - impl<T> Foo for &'_ T {
8 + impl<'a, T: 'a> Foo for &'a T {
  |

@rustbot label +T-compiler +F-generic_const_exprs +requires-incomplete-features

@rustbot rustbot added F-generic_const_exprs `#![feature(generic_const_exprs)]` requires-incomplete-features This issue requires the use of incomplete features. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. labels Mar 12, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
C-bug Category: This is a bug. F-generic_const_exprs `#![feature(generic_const_exprs)]` needs-triage This issue may need triage. Remove it if it has been sufficiently triaged. requires-incomplete-features This issue requires the use of incomplete features. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.
Projects
None yet
Development

No branches or pull requests

3 participants