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

GAT/extended: wrong lifetime analysis #113721

Closed
dhardy opened this issue Jul 15, 2023 · 1 comment
Closed

GAT/extended: wrong lifetime analysis #113721

dhardy opened this issue Jul 15, 2023 · 1 comment
Labels
A-GATs Area: Generic associated types (GATs) A-lifetimes Area: Lifetimes / regions C-bug Category: This is a bug. F-generic_associated_types_extended `#![feature(generic_associated_types_extended)]` T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.

Comments

@dhardy
Copy link
Contributor

dhardy commented Jul 15, 2023

Using generic_associated_types_extended (#95451), this code fails:

#![feature(generic_associated_types_extended)]

trait Trait {
    type Ret<'r> where Self: 'r;
    fn get<'r>(&'r self) -> Self::Ret<'r>;
}

struct Person<'a> {
    name: &'a str,
    age: u16,
}

impl<'a> Trait for Person<'a> {
    type Ret<'r> = (&'r str, u16) where Self: 'r;
    fn get(&self) -> (&str, u16) {
        (self.name, self.age)
    }
}

fn a(p: &Person) {
    let (name, age) = p.get();
    println!("Hello, {name} (age = {age})!");
}

fn b(p: &dyn for<'r> Trait<Ret<'r> = (&'r str, u16)>) {
    let (name, age) = p.get();
    println!("Hello, {name} (age = {age})!");
}

fn main() {
    let name = "Andrew";
    let p = Person { name: &name, age: 6 };
    a(&p);
    b(&p);
}

with error:

error[E0521]: borrowed data escapes outside of function
  --> src/main.rs:26:23
   |
25 | fn b(p: &dyn for<'r> Trait<Ret<'r> = (&'r str, u16)>) {
   |      -  - let's call the lifetime of this reference `'1`
   |      |
   |      `p` is a reference that is only valid in the function body
26 |     let (name, age) = p.get();
   |                       ^^^^^^^
   |                       |
   |                       `p` escapes the function body here
   |                       argument requires that `'1` must outlive `'static`
   |
   = note: due to current limitations in the borrow checker, this implies a `'static` lifetime
@dhardy dhardy added the C-bug Category: This is a bug. label Jul 15, 2023
@rustbot rustbot added the needs-triage This issue may need triage. Remove it if it has been sufficiently triaged. label Jul 15, 2023
@workingjubilee workingjubilee added A-lifetimes Area: Lifetimes / regions T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. F-generic_associated_types_extended `#![feature(generic_associated_types_extended)]` and removed needs-triage This issue may need triage. Remove it if it has been sufficiently triaged. labels Jul 15, 2023
@fmease fmease added the A-GATs Area: Generic associated types (GATs) label Nov 2, 2024
@fmease
Copy link
Member

fmease commented Dec 7, 2024

The feature has been removed (#133768). Not reproducible without it.

@fmease fmease closed this as not planned Won't fix, can't repro, duplicate, stale Dec 7, 2024
@fmease fmease changed the title GATE: wrong lifetime analysis GAT/extended: wrong lifetime analysis Dec 7, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-GATs Area: Generic associated types (GATs) A-lifetimes Area: Lifetimes / regions C-bug Category: This is a bug. F-generic_associated_types_extended `#![feature(generic_associated_types_extended)]` 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

4 participants