Skip to content

Possible regression with type_alias_impl_trait #97651

Closed
@FrozenDroid

Description

@FrozenDroid

Code

I tried this code:

#![feature(type_alias_impl_trait)]

trait Printy: core::fmt::Debug {
    fn test(self: &mut Self) {
        println!("{:?}", self);
    }
}

pub trait Foo {
    type Ty: Printy;
    fn bar(self) -> Self::Ty;
}

#[derive(Debug)]
struct Quix<'a>(&'a u8);

impl Printy for &mut Quix<'_> {}

impl Foo for &mut Quix<'_> {
    type Ty = impl Printy;

    fn bar(self) -> Self::Ty {
        self
    }
}

fn main() {
    let mut q = Quix(&1);
    (&mut q).test();
}

I expected to see this happen: Successful compile

Instead, this happened:

error[E0491]: in type `&mut Quix<'_>`, reference has a longer lifetime than the data it references
  --> src/main.rs:20:15
   |
20 |     type Ty = impl Printy;
   |               ^^^^^^^^^^^
   |
note: the pointer is valid for the lifetime `'_` as defined here
  --> src/main.rs:19:14
   |
19 | impl Foo for &mut Quix<'_> {
   |              ^
note: but the referenced data is only valid for the lifetime `'_` as defined here
  --> src/main.rs:19:14
   |
19 | impl Foo for &mut Quix<'_> {
   |              ^

Version it worked on

It most recently worked on: nightly-2021-10-21
It's very possible there are more recent nightlies where this compiles, but I don't know how to easily find this.

Version with regression

rustc --version --verbose:

rustc 1.62.0-nightly (de1026a67 2022-04-23)
binary: rustc
commit-hash: de1026a67b0a3f5d6b61a1f77093af97d4799376
commit-date: 2022-04-23
host: aarch64-apple-darwin
release: 1.62.0-nightly
LLVM version: 14.0.1

Additional observations

Works with:

#![feature(type_alias_impl_trait)]

trait Printy: core::fmt::Debug {
    fn test(self: &mut Self) {
        println!("{:?}", self);
    }
}

pub trait Foo {
    type Ty: Printy;
    fn bar(self) -> Self::Ty;
}

#[derive(Debug)]
struct Quix<'a>(&'a u8);

impl Printy for &mut Quix<'_> {}

impl<'a> Foo for &'a mut Quix<'a> {
    type Ty = impl Printy;

    fn bar(self) -> Self::Ty {
        self
    }
}

fn main() {
    let mut q = Quix(&1);
    (&mut q).test();
}

but also if the implementations are on an immutable reference to Quix

Metadata

Metadata

Assignees

No one assigned

    Labels

    C-bugCategory: This is a bug.F-type_alias_impl_trait`#[feature(type_alias_impl_trait)]`T-typesRelevant to the types team, which will review and decide on the PR/issue.regression-untriagedUntriaged performance or correctness regression.requires-nightlyThis issue requires a nightly compiler in some way.

    Type

    No type

    Projects

    Status

    Done

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions