Skip to content

Lifetime error returning reference to associated type from trait with const generic parameter. #71547

Closed
@rodrimati1992

Description

@rodrimati1992

I tried this code:
https://play.rust-lang.org/?version=nightly&mode=debug&edition=2018&gist=5638c99e5d0c0c6af0b5fd8773cefffb

#![feature(const_generics)]

trait GetType<const N:&'static str>{
    type Ty;
    fn get(&self)->&Self::Ty;
}

fn get_val<T>(value:&T)->&T::Ty
where
    T: GetType<"hello">,
{
    value.get()
}

I expected the code to compile successfully(like it did in previous nightly versions)

Instead, the code failed to compile with this error:

error[E0311]: the associated type `<T as GetType<"hello">>::Ty` may not live long enough
  --> src/lib.rs:12:5
   |
12 |     value.get()
   |     ^^^^^^^^^^^
   |
   = help: consider adding an explicit lifetime bound for `<T as GetType<"hello">>::Ty`
note: the associated type `<T as GetType<"hello">>::Ty` must be valid for the anonymous lifetime #1 defined on the function body at 8:1...
  --> src/lib.rs:8:1
   |
8  | / fn get_val<T>(value:&T)->&T::Ty
9  | | where
10 | |     T: GetType<"hello">,
11 | | {
12 | |     value.get()
13 | | }
   | |_^

Meta

Tried this on rustc 1.44.0-nightly (2020-04-24 3360cc3)

The way I work around this bug is to write the function like this:

fn get_val<T,R>(value:&T)->&R
where
    T: GetType<"hello",Ty=R>,
{
    value.get()
}

Metadata

Metadata

Assignees

No one assigned

    Labels

    A-const-genericsArea: const generics (parameters and arguments)A-lifetimesArea: Lifetimes / regionsC-bugCategory: This is a bug.E-needs-testCall for participation: An issue has been fixed and does not reproduce, but no test has been added.F-adt_const_params`#![feature(adt_const_params)]`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.

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions