Skip to content

apply_member_constraints is incomplete #142073

Open
@lcnr

Description

@lcnr

Applying member constraints can result in overly strong constraints, causing the following test to fail:

fn new_defining_use<'a, F: FnOnce(T) -> &'a (), T>(_: F) {}
fn rpit<'a,  'b: 'b>(x: &'b ()) -> impl Sized + use<'a, 'b> {
    // currently a separate defining use, chooses `'static`
    new_defining_use(rpit::<'a, 'b>);
    x // this defining use needs to be `&'b ()`
}

This is caused by the following code:

// At this point we can pick any member of `choice_regions` and would like to choose
// it to be a small as possible. To avoid potential non-determinism we will pick the
// smallest such choice.
//
// Because universal regions are only partially ordered (i.e, not every two regions are
// comparable), we will ignore any region that doesn't compare to all others when picking
// the minimum choice.
//
// For example, consider `choice_regions = ['static, 'a, 'b, 'c, 'd, 'e]`, where
// `'static: 'a, 'static: 'b, 'a: 'c, 'b: 'c, 'c: 'd, 'c: 'e`.
// `['d, 'e]` are ignored because they do not compare - the same goes for `['a, 'b]`.
let totally_ordered_subset = choice_regions.iter().copied().filter(|&r1| {
choice_regions.iter().all(|&r2| {
self.universal_region_relations.outlives(r1, r2)
|| self.universal_region_relations.outlives(r2, r1)
})
});

This sort of incompleteness is unfortunately required as we can use this to lift otherwise unconstrained regions to 'static, e.g. the following snippet otherwise fails to compile

fn foo<'a, 'b>() -> impl Sized + use<'a, 'b> {
    &()
}

This may become a larger concern as we're working towards the new solver which introduces a significant amount of additional uses of opaques.

Metadata

Metadata

Assignees

No one assigned

    Labels

    A-impl-traitArea: `impl Trait`. Universally / existentially quantified anonymous types with static dispatch.A-member-constraints`#[feature(member_constraints)]`T-typesRelevant to the types 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