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

Box<dyn FnOnce(T)> doesn't seem to be really contravariant with T #100728

Open
fakedrake opened this issue Aug 18, 2022 · 2 comments
Open

Box<dyn FnOnce(T)> doesn't seem to be really contravariant with T #100728

fakedrake opened this issue Aug 18, 2022 · 2 comments
Labels
A-trait-objects Area: trait objects, vtable layout A-trait-system Area: Trait system A-variance Area: Variance (https://doc.rust-lang.org/nomicon/subtyping.html) C-bug Category: This is a bug. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. T-types Relevant to the types team, which will review and decide on the PR/issue.

Comments

@fakedrake
Copy link

I tried this code:

type F<'arg> = Box<dyn FnOnce(&'arg ())>;
fn contravar<'small, 'large: 'small>(f: F<'small>) -> F<'large> {
    f
}

I expected it to compile but I got the following:

error: lifetime may not live long enough
 --> src/lib.rs:3:5
  |
2 | fn contravar<'small, 'large: 'small>(f: F<'small>) -> F<'large> {
  |              ------  ------ lifetime `'large` defined here
  |              |
  |              lifetime `'small` defined here
3 |     f
  |     ^ function was supposed to return data with lifetime `'large` but it is returning data with lifetime `'small`
  |
  = help: consider adding the following bound: `'small: 'large`

playground

$ rustc --version --verbose
rustc 1.65.0-nightly (29e4a9ee0 2022-08-10)
binary: rustc
commit-hash: 29e4a9ee0253cd39e552a77f51f11f9a5f1c41e6
commit-date: 2022-08-10
host: x86_64-unknown-linux-gnu
release: 1.65.0-nightly
LLVM version: 14.0.6
`rustc --version --verbose`:

I asked this on stackoverflow too hopefully I am not misinterpreting something about variance.

@fakedrake fakedrake added the C-bug Category: This is a bug. label Aug 18, 2022
@fakedrake
Copy link
Author

fakedrake commented Aug 19, 2022

I looked into the issue a bit carefully and the problem seems to be that the type checker seems to treat the Fn/FnMut/FnOnce traits just like any other trait w.r.t. to variance, making them invariant to their argument lifetimes. Unfortunately by the point when the point when variance is checked I can't find a way to get the TyCtxt in order to check that the trait is one of Fn, FnMut, FnOnce so that it can check for contravariance instead of invariance.

I don't even know if that is good enough because what about traits like:

trait MyFn<'a>: Fn(&'a ()) {}

But it would be a start. As things stands, it seems impossible to have a Vec of closures that behaves as expected w.r.t. to variance.

@zirconium-n
Copy link
Contributor

Might be related #23938 (comment)

@fmease fmease added A-trait-system Area: Trait system T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. A-variance Area: Variance (https://doc.rust-lang.org/nomicon/subtyping.html) T-types Relevant to the types team, which will review and decide on the PR/issue. A-trait-objects Area: trait objects, vtable layout and removed needs-triage-legacy labels Sep 8, 2023
@madsmtm madsmtm mentioned this issue Feb 4, 2024
11 tasks
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-trait-objects Area: trait objects, vtable layout A-trait-system Area: Trait system A-variance Area: Variance (https://doc.rust-lang.org/nomicon/subtyping.html) C-bug Category: This is a bug. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. T-types Relevant to the types team, which will review and decide on the PR/issue.
Projects
None yet
Development

No branches or pull requests

4 participants