Skip to content

derive(Clone) with HRTB and GAT #89188

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

Closed
audunhalland opened this issue Sep 22, 2021 · 2 comments · Fixed by #89341
Closed

derive(Clone) with HRTB and GAT #89188

audunhalland opened this issue Sep 22, 2021 · 2 comments · Fixed by #89341
Assignees
Labels
A-GATs Area: Generic associated types (GATs) C-bug Category: This is a bug. F-generic_associated_types `#![feature(generic_associated_types)]` a.k.a. GATs

Comments

@audunhalland
Copy link
Contributor

audunhalland commented Sep 22, 2021

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
@audunhalland audunhalland added the C-bug Category: This is a bug. label Sep 22, 2021
@jonas-schievink jonas-schievink added the F-generic_associated_types `#![feature(generic_associated_types)]` a.k.a. GATs label Sep 23, 2021
@audunhalland
Copy link
Contributor Author

The expanded impl's where clause is using the 's without bringing it into scope. I think the where clause should introduce the parameter:

impl<T: ::core::clone::Clone + CallWithShim + 'static> ::core::clone::Clone for ShimMethod<T>
where
    for<'s> T::Shim<'s>: ::core::clone::Clone,
//  ^^^^^^^-------- insert here?
{
    #[inline]
    fn clone(&self) -> ShimMethod<T> {
        match *self {
            ShimMethod(ref __self_0_0) => ShimMethod(::core::clone::Clone::clone(&(*__self_0_0))),
        }
    }
}

@audunhalland
Copy link
Contributor Author

@rustbot claim

@bors bors closed this as completed in f03eb6b Oct 2, 2021
@fmease fmease added the A-GATs Area: Generic associated types (GATs) label Nov 2, 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) C-bug Category: This is a bug. F-generic_associated_types `#![feature(generic_associated_types)]` a.k.a. GATs
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants