Skip to content

derive(Clone) with HRTB and GAT #89188

Closed
@audunhalland

Description

@audunhalland

I tried this code:

#![feature(generic_associated_types)]

trait CallWithShim: Sized {
    type Shim<'s>
    where
        Self: 's;
        
    fn call(&mut self, method: ShimMethod<Self>);
}

#[derive(Clone)]
struct ShimMethod<T: CallWithShim + 'static>(
    pub &'static dyn for<'s> Fn(&'s mut T::Shim<'s>),
);

fn main() {}

I expected to see this happen: it compiles

Instead, this happened: it fails to compile, with

  --> src/main.rs:13:49
   |
11 | #[derive(Clone)]
   |          - help: consider introducing lifetime `'s` here: `'s,`
12 | struct ShimMethod<T: CallWithShim + 'static>(
13 |     pub &'static dyn for<'s> Fn(&'s mut T::Shim<'s>),
   |                                                 ^^ undeclared lifetime

Removing #[derive(Clone)] makes it compile.

A bit longer playground demonstrating what this code snippet can be used for:
https://play.rust-lang.org/?version=nightly&mode=debug&edition=2018&gist=a20f6a74ae1c1e8b2838aa428beee1fe

I'm not an experienced user of HRTBs (nor GATs, obviously). What I'm trying to express is that the data borrowed by Shim does not outlive the ShimMethod call. I think the HRTB is the right way to express that, and it all compiles and works without the #[derive(Clone)], but I'm a bit unsure if I'm really doing something wrong and stupid and the compiler fails to catch it because potential GAT bugs 🤷

Anyway, it seems there is something fishy about the derive code here..

Meta

rustc --version --verbose:

rustc 1.57.0-nightly (ac2d9fc50 2021-09-21)
binary: rustc
commit-hash: ac2d9fc509e36d1b32513744adf58c34bcc4f43c
commit-date: 2021-09-21
host: x86_64-apple-darwin
release: 1.57.0-nightly
LLVM version: 13.0.0

Metadata

Metadata

Assignees

Labels

A-GATsArea: Generic associated types (GATs)C-bugCategory: This is a bug.F-generic_associated_types`#![feature(generic_associated_types)]` a.k.a. GATs

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions