目录
bors

Auto merge of #143545 - compiler-errors:coroutine-obl, r=oli-obk

-Zhigher-ranked-assumptions: Consider WF of coroutine witness when proving outlives assumptions

TL;DR

This PR introduces an unstable flag -Zhigher-ranked-assumptions which tests out a new algorithm for dealing with some of the higher-ranked outlives problems that come from auto trait bounds on coroutines. See:

  • rust-lang/rust#110338

While it doesn’t fix all of the issues, it certainly fixed many of them, so I’d like to get this landed so people can test the flag on their own code.

Background

Consider, for example:

use std::future::Future;

trait Client {
    type Connecting<'a>: Future + Send
    where
        Self: 'a;

    fn connect(&self) -> Self::Connecting<'_>;
}

fn call_connect<C>(c: C) -> impl Future + Send
where
    C: Client + Send + Sync,
{
    async move { c.connect().await }
}

Due to the fact that we erase the lifetimes in a coroutine, we can think of the interior type of the async block as something like: exists<'r, 's> { C, &'r C, C::Connecting<'s> }. The first field is the c we capture, the second is the auto-ref that we perform on the call to .connect(), and the third is the resulting future we’re awaiting at the first and only await point. Note that every region is uniquified differently in the interior types.

For the async block to be Send, we must prove that both of the interior types are Send. First, we have an exists<'r, 's> binder, which needs to be instantiated universally since we treat the regions in this binder as unknown[^exist]. This gives us two types: { &'!r C, C::Connecting<'!s> }. Proving &'!r C: Send is easy due to a Send impl for references.

Proving C::Connecting<'!s>: Send can only be done via the item bound, which then requires C: '!s to hold (due to the where Self: 'a on the associated type definition). Unfortunately, we don’t know that C: '!s since we stripped away any relationship between the interior type and the param C. This leads to a bogus borrow checker error today!

Approach

Coroutine interiors are well-formed by virtue of them being borrow-checked, as long as their callers are invoking their parent functions in a well-formed way, then substitutions should also be well-formed. Therefore, in our example above, we should be able to deduce the assumption that C: '!s holds from the well-formedness of the interior type C::Connecting<'!s>.

This PR introduces the notion of coroutine assumptions, which are the outlives assumptions that we can assume hold due to the well-formedness of a coroutine’s interior types. These are computed alongside the coroutine types in the CoroutineWitnessTypes struct. When we instantiate the binder when proving an auto trait for a coroutine, we instantiate the CoroutineWitnessTypes and stash these newly instantiated assumptions in the region storage in the InferCtxt. Later on in lexical region resolution or MIR borrowck, we use these registered assumptions to discharge any placeholder outlives obligations that we would otherwise not be able to prove.

How well does it work?

I’ve added a ton of tests of different reported situations that users have shared on issues like rust-lang/rust#110338, and an (anecdotally) large number of those examples end up working straight out of the box! Some limitations are described below.

How badly does it not work?

The behavior today is quite rudimentary, since we currently discharge the placeholder assumptions pretty early in region resolution. This manifests itself as some limitations on the code that we accept.

For example, tests/ui/async-await/higher-ranked-auto-trait-11.rs continues to fail. In that test, we must prove that a placeholder is equal to a universal for a param-env candidate to hold when proving an auto trait, e.g. '!1 = 'a is required to prove T: Trait<'!1> in a param-env that has T: Trait<'a>. Unfortunately, at that point in the MIR body, we only know that the placeholder is equal to some body-local existential NLL var '?2, which only gets equated to the universal 'a when being stored into the return local later on in MIR borrowck.

This could be fixed by integrating these assumptions into the type outlives machinery in a more first-class way, and delaying things to the end of MIR typeck when we know the full relationship between existential and universal NLL vars. Doing this integration today is quite difficult today.

tests/ui/async-await/higher-ranked-auto-trait-11.rs fails because we don’t compute the full transitive outlives relations between placeholders. In that test, we have in our region assumptions that some '!1 = '!2 and '!2 = '!3, but we must prove '!1 = '!3.

This can be fixed by computing the set of coroutine outlives assumptions in a more transitive way, or as I mentioned above, integrating these assumptions into the type outlives machinery in a more first-class way, since it’s already responsible for the transitive outlives assumptions of universals.

Moving forward

I’m still quite happy with this implementation, and I’d like to land it for testing. I may work on overhauling both the way we compute these coroutine assumptions and also how we deal with the assumptions during (lexical/nll) region checking. But for now, I’d like to give users a chance to try out this new -Zhigher-ranked-assumptions flag to uncover more shortcomings.

[^exist]: Instantiating this binder with infer regions would be incomplete, since we’d be asking for some instantiation of the interior types, not proving something for all instantiations of the interior types.

1天前300306次提交
目录README.md

This is the main source code repository for Rust. It contains the compiler, standard library, and documentation.

Why Rust?

  • Performance: Fast and memory-efficient, suitable for critical services, embedded devices, and easily integrated with other languages.

  • Reliability: Our rich type system and ownership model ensure memory and thread safety, reducing bugs at compile-time.

  • Productivity: Comprehensive documentation, a compiler committed to providing great diagnostics, and advanced tooling including package manager and build tool (Cargo), auto-formatter (rustfmt), linter (Clippy) and editor support (rust-analyzer).

Quick Start

Read “Installation” from The Book.

Installing from Source

If you really want to install from source (though this is not recommended), see INSTALL.md.

Getting Help

See https://www.rust-lang.org/community for a list of chat platforms and forums.

Contributing

See CONTRIBUTING.md.

License

Rust is primarily distributed under the terms of both the MIT license and the Apache License (Version 2.0), with portions covered by various BSD-like licenses.

See LICENSE-APACHE, LICENSE-MIT, and COPYRIGHT for details.

Trademark

The Rust Foundation owns and protects the Rust and Cargo trademarks and logos (the “Rust Trademarks”).

If you want to use these names or brands, please read the Rust language trademark policy.

Third-party logos may be subject to third-party copyrights and trademarks. See Licenses for details.

关于
1.8 GB
邀请码
    Gitlink(确实开源)
  • 加入我们
  • 官网邮箱:gitlink@ccf.org.cn
  • QQ群
  • QQ群
  • 公众号
  • 公众号

©Copyright 2023 CCF 开源发展委员会
Powered by Trustie& IntelliDE 京ICP备13000930号