rust-lang / rust

Empowering everyone to build reliable and efficient software.
https://www.rust-lang.org
Other
98.68k stars 12.75k forks source link

min_type_alias_impl_trait: poor diagnostics when TAIT is unconstrained #86733

Open nikomatsakis opened 3 years ago

nikomatsakis commented 3 years ago

The minimized example (playground:

#![feature(min_type_alias_impl_trait)]

type Foo = impl Copy;

enum Wrapper<T> {
    First(T),
    Second
}

fn produce() -> Wrapper<Foo> {
    Wrapper::Second
}

gives what appears to me to be the correct error:

error: could not find defining uses
 --> src/lib.rs:3:12
  |
3 | type Foo = impl Copy;
  |            ^^^^^^^^^

but the diagnostics are not very good.

Originally posted by @nikomatsakis in https://github.com/rust-lang/rust/issues/66426#issuecomment-870924733

voidc commented 3 years ago

I'd be interested in taking a stab at this. However, I'm not quite sure what the desired behavior is. I looked at PR https://github.com/rust-lang/rust/pull/66431 which implemented the fallback to a non-defining use for unconstrained opaque types. One possibility would be to somewhere store every location at which such a fallback occurs, to later show a warning/note when no other defining use is found. I would also appreciate some mentoring instructions on how to approach this.

oli-obk commented 2 years ago

Yea, I think the only way to fix this is to add a new field to https://doc.rust-lang.org/nightly/nightly-rustc/rustc_middle/mir/struct.BorrowCheckResult.html that maps from opaque type def ids to spans and use that when reporting the unconstrained diagnostic.

dignifiedquire commented 1 year ago

I am trying to use TAIT to poll a future constructed by an async fn, but running into this error, I am unsure where the issue exactly is, would appreciate any guidance Example Code: https://play.rust-lang.org/?version=nightly&mode=debug&edition=2021&gist=b2a9d6f2493a79743f66e7852aeca81a

Error

error[[E0391]](https://doc.rust-lang.org/nightly/error-index.html#E0391): cycle detected when computing type of `BarFuture::{opaque#0}`
  --> src/lib.rs:7:18
   |
7  | type BarFuture = impl Future<Output = usize> + Send + Sync + 'static;
   |                  ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
   |
note: ...which requires type-checking `<impl at src/lib.rs:17:1: 17:11>::bar_project`...
  --> src/lib.rs:23:40
   |
23 |         let bar: &mut BarFuture = &mut self.bar;
   |                                        ^^^^
   = note: ...which requires evaluating trait selection obligation `core::pin::Pin<&'a mut State>: core::ops::deref::DerefMut`...
   = note: ...which again requires computing type of `BarFuture::{opaque#0}`, completing the cycle
note: cycle used when checking item types in top-level module
  --> src/lib.rs:1:1
   |
1  | / #![feature(type_alias_impl_trait)]
2  | |
3  | | [use std::future::Future;](https://play.rust-lang.org/?version=nightly&mode=debug&edition=2021&gist=b2a9d6f2493a79743f66e7852aeca81a#)
4  | | [use std::pin::Pin;](https://play.rust-lang.org/?version=nightly&mode=debug&edition=2021&gist=b2a9d6f2493a79743f66e7852aeca81a#)
...  |
33 | |     }
34 | | }
   | |_^

Edit

Sorry, this is likely the wrong issue, but wasn't sure on which of the TAIT issues to post 😅

oli-obk commented 1 year ago

Sorry, this is likely the wrong issue, but wasn't sure on which of the TAIT issues to post 😅

No worries 😆. If you aren't sure, you can just open a new issue. I'll move your post to a new issue