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

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

Closed
rodrimati1992 opened this issue Apr 25, 2020 · 3 comments · Fixed by #99481
Labels
A-const-generics Area: const generics (parameters and arguments) A-lifetimes Area: lifetime related C-bug Category: This is a bug. E-needs-test Call 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)]` requires-nightly This issue requires a nightly compiler in some way. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.

Comments

@rodrimati1992
Copy link
Contributor

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()
}
@rodrimati1992 rodrimati1992 added the C-bug Category: This is a bug. label Apr 25, 2020
@rodrimati1992 rodrimati1992 changed the title Lifetime error returning reference to associated type in trait with const generic parameter. Lifetime error returning reference to associated type from trait with const generic parameter. Apr 25, 2020
@csmoe csmoe added A-const-generics Area: const generics (parameters and arguments) E-needs-bisection Call for participation: This issue needs bisection: https://github.com/rust-lang/cargo-bisect-rustc labels Apr 25, 2020
@jonas-schievink
Copy link
Contributor

Interestingly none of these additional bounds fix the issue:

where
    T: GetType<"hello"> + 'static,
    <T as GetType<"hello">>::Ty: 'static,

@jonas-schievink jonas-schievink added A-lifetimes Area: lifetime related requires-nightly This issue requires a nightly compiler in some way. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. labels Apr 25, 2020
@bjorn3
Copy link
Member

bjorn3 commented May 6, 2020

searched nightlies: from nightly-2020-04-03 to nightly-2020-04-10
regressed nightly: nightly-2020-04-08
searched commits: from 6dee5f1 to 42abbd8
regressed commit: 209b2be

@rustbot modify labels:-E-needs-bisection

@rustbot rustbot removed the E-needs-bisection Call for participation: This issue needs bisection: https://github.com/rust-lang/cargo-bisect-rustc label May 6, 2020
@jonas-schievink jonas-schievink added the F-const_generics `#![feature(const_generics)]` label May 6, 2020
@lcnr lcnr added E-needs-test Call 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)]` and removed F-const_generics `#![feature(const_generics)]` labels Jun 28, 2022
@lcnr
Copy link
Contributor

lcnr commented Jun 28, 2022

this has now been fixed

#![feature(adt_const_params)]

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()
}

JohnTitor added a commit to JohnTitor/rust that referenced this issue Jul 19, 2022
Signed-off-by: Yuki Okushi <jtitor@2k36.org>
bors added a commit to rust-lang-ci/rust that referenced this issue Jul 20, 2022
Rollup of 5 pull requests

Successful merges:

 - rust-lang#98784 (Suggest returning local on "expected `ty`, found `()`" due to expr-less block)
 - rust-lang#98916 (Windows: Use `FindFirstFileW` for getting the metadata of locked system files)
 - rust-lang#99433 (Erase regions before comparing signatures of foreign fns.)
 - rust-lang#99452 (int_macros was only using to_xe_bytes_doc and not from_xe_bytes_doc)
 - rust-lang#99481 (Add regression test for rust-lang#71547)

Failed merges:

r? `@ghost`
`@rustbot` modify labels: rollup
@bors bors closed this as completed in d7a65a5 Jul 20, 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) A-lifetimes Area: lifetime related C-bug Category: This is a bug. E-needs-test Call 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)]` requires-nightly This issue requires a nightly compiler in some way. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.
Projects
None yet
Development

Successfully merging a pull request may close this issue.

6 participants