Skip to content

Fn trait associated type (return type) considered unconstrained #86934

Closed as not planned
@afranchuk

Description

@afranchuk

I tried this code:

struct S<F>(F);

impl <'a, F, G> S<F>
where
    F: Fn(&'a u8) -> G
{
    fn call(&mut self) -> G {
        (self.0)(&1)
    }
}

I expected to see this happen: G should be considered a constrained type.

Instead, this happened:

error[E0207]: the type parameter `G` is not constrained by the impl trait, self type, or predicates

Note that if you use '_ as the lifetime instead of 'a, it works. It also works if there is no lifetime on the input parameter (e.g. if it's not a reference). However I want to constrain G to the same lifetime (eventually) so that's not an option. Is this an issue due to some lifetime assumptions about the return type from Fn traits, specifically that having G: 'a makes it unconstrained as it can't reason about that?

This is identical for FnOnce and FnMut as well.

Meta

rustc --version --verbose:

rustc 1.53.0 (53cb7b09b 2021-06-17)
binary: rustc
commit-hash: 53cb7b09b00cbea8754ffb78e7e3cb521cb8af4b
commit-date: 2021-06-17
host: x86_64-unknown-linux-gnu
release: 1.53.0
LLVM version: 12.0.1

Metadata

Metadata

Assignees

No one assigned

    Labels

    A-associated-itemsArea: Associated items (types, constants & functions)C-bugCategory: This is a bug.T-compilerRelevant to the compiler team, which will review and decide on the PR/issue.

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions