Skip to content

obligations_for_self_ty leaks placeholders #124385

Open
@lcnr

Description

@lcnr
trait Foo<'a> {}
fn needs_foo<T>(_: T)
where
    for<'a> Wrap<T>: Foo<'a>,
{}

struct Wrap<T>(T);
impl<'a, T> Foo<'a> for Wrap<T> where T: Fn(&'a i32) {}

fn main() {
    needs_foo(|x| println!("{x}"));
}

During typeck, the signature of |x| println!("{x}") gets inferred to fn(&'!a i32) where '!a is a placeholder which is not nameable by the closure. This is unsound as in "it breaks a core invariant of the type system". As all regions are erased after hir typeck and rechecked during mir borrowck it should not cause any "allows for UB at runtime" unsoundness.

This is still something we should fix, ideally by "pulling down" the inference variables of the closure signature into the root universe when deducing it. The same issue exists for all uses of obligations_for_self_ty.

This may cause some ICEs in the future if we improve our validation of type system invariants in the future.

cc @rust-lang/types

edit: for now this is actually not too different from how higher ranked relate works in general. However, the same issue exists with feature(non_lifetime_binders) and we want to more eagerly handle these placeholder constraints in the future to handle where-bounds on binders

Metadata

Metadata

Assignees

No one assigned

    Labels

    A-closuresArea: Closures (`|…| { … }`)A-inferenceArea: Type inferenceP-lowLow priorityT-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