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

The Output should not be an association type of the FnOnce #108490

Closed
823984418 opened this issue Feb 26, 2023 · 7 comments
Closed

The Output should not be an association type of the FnOnce #108490

823984418 opened this issue Feb 26, 2023 · 7 comments

Comments

@823984418
Copy link
Contributor

Currently, we require that the lifecycle that appears in an association type appear in a generic type.
This is to fix #32330 .
But this because the Output is an associative type of the function.


trait F {
    type Output;
}

Except for functions, no same type can implement different lifecycle Output.
So the Output as an association type is just an error.

See #33685 (comment)

@workingjubilee
Copy link
Member

...But what exactly would we gain by making this change?

@823984418
Copy link
Contributor Author

...But what exactly would we gain by making this change?

Make it a legal statement:

fn a<F: for<'s> Fn() -> Vec<&'s i32>>(f: F) 

And don't need to be like this:

fn a<F: for<'s> Fn(PhantomData<&'s ()>) -> Vec<&'s i32>>(f: F)

@823984418
Copy link
Contributor Author

@fmease
Copy link
Member

fmease commented Feb 27, 2023

The invalid bound for<'s> Fn() -> Vec<&'s i32> can just be expressed as Fn() -> Vec<&'static i32> or am I missing something? A closure that returns a vector of references that live at least as long as any possible lifetime that the caller can choose can only return 'static references since without any function arguments that are of that lifetime, any other lifetime that the callee would select, would simply be incorrect in general.

@823984418
Copy link
Contributor Author

The invalid bound for<'s> Fn() -> Vec<&'s i32> can just be expressed as Fn() -> Vec<&'static i32> or am I missing something?

I have to admit that I am a bad example, I mean, 's is invariant for return type.

@823984418
Copy link
Contributor Author

823984418 commented Feb 27, 2023

any other lifetime that the callee would select, would simply be incorrect in general.

I can say clear, this is wrong, the life cycle of designated by the caller is exist and reasonable.

One example is: RR::default

#[derive(Default)]
struct RR<'a, 'b> {
    r: Option<&'a mut &'b i32>,
}

Although it doesn't seem to make much sense, what about self-referencing life cycle?

#[derive(Default)]
struct RR<'a> {
    r: Cell<Option<&'a RR<'a>>>,
}

@jyn514
Copy link
Member

jyn514 commented Apr 8, 2023

This appears to be a breaking change: https://internals.rust-lang.org/t/reopen-fnonce-why-is-output-an-associated-type/18430/8?u=jyn514

Given that, it doesn't seem strongly enough motivated to be worth changing.

@jyn514 jyn514 closed this as not planned Won't fix, can't repro, duplicate, stale Apr 8, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants