Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

3 "broken MIR" ICEs when combining futures and assoc. types #61320

Closed
jonas-schievink opened this issue May 29, 2019 · 0 comments · Fixed by #61488
Closed

3 "broken MIR" ICEs when combining futures and assoc. types #61320

jonas-schievink opened this issue May 29, 2019 · 0 comments · Fixed by #61488
Assignees
Labels
A-associated-items Area: Associated items such as associated types and consts. A-mir Area: Mid-level IR (MIR) - https://blog.rust-lang.org/2016/04/19/MIR.html C-bug Category: This is a bug. I-ICE Issue: The compiler panicked, giving an Internal Compilation Error (ICE) ❄️ T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.

Comments

@jonas-schievink
Copy link
Contributor

jonas-schievink commented May 29, 2019

The following code, which uses futures, a few self-defined traits with associated types, and where-clauses on those, causes 3 "broken MIR" ICEs when built (Playground link):

use futures::{Future, IntoFuture};

struct Request<T>(T);

trait RequestContext {}
impl<T> RequestContext for T {}
struct NoContext;
impl AsRef<NoContext> for NoContext {
    fn as_ref(&self) -> &Self { &NoContext }
}

type BoxedError = Box<dyn ::std::error::Error + Send + Sync>;
type DefaultFuture<T, E> = Box<dyn Future<Item=T, Error=E> + Send>;

trait Guard: Sized {
    type Result: IntoFuture<Item = Self, Error = BoxedError>;
    fn from_request(
        request: &Request<()>, 
    ) -> Self::Result;
}

trait FromRequest: Sized {
    type Context;
    type Future: Future<Item = Self, Error = BoxedError> + Send;
    fn from_request(
        request: Request<()>, 
    ) -> Self::Future;
}

struct MyGuard;
impl Guard for MyGuard {
    type Result = Result<Self, BoxedError>;
    fn from_request(_request: &Request<()>) -> Self::Result {
        Ok(MyGuard)
    }
}

struct Generic<I> {
    _inner: I,
}

impl<I> FromRequest for Generic<I>
where
    MyGuard: Guard,
    <MyGuard as Guard>::Result:
        futures::IntoFuture<Item = MyGuard, Error = BoxedError>,
    <<MyGuard as Guard>::Result as futures::IntoFuture>::Future:
        Send,
    I: FromRequest<Context = NoContext>
{
    type Future = DefaultFuture<Self, BoxedError>;
    type Context = NoContext;
    fn from_request(
        headers: Request<()>,
    ) -> DefaultFuture<Self, BoxedError> {
        let _future = <MyGuard as Guard>::from_request(&headers)
            .into_future()
            .and_then(move |_| {
                <I as FromRequest>::from_request(headers)
                    .into_future()
                    .and_then(move |fld_inner| {
                        Ok(Generic {
                            _inner: fld_inner,
                        })
                        .into_future()
                    })
            });
        panic!();
    }
}

Compiler output:

error: internal compiler error: broken MIR in DefId(0/0:29 ~ playground[aa0a]::{{impl}}[3]::from_request[0]) (Terminator { source_info: SourceInfo { span: src/lib.rs:56:23: 57:27, scope: scope[0] }, kind: _4 = const futures::future::IntoFuture::into_future(move _5) -> [return: bb3, unwind: bb4] }): call dest mismatch (futures::future::result_::FutureResult<MyGuard, std::boxed::Box<dyn std::error::Error + std::marker::Send + std::marker::Sync>> <- <std::result::Result<MyGuard, std::boxed::Box<dyn std::error::Error + std::marker::Send + std::marker::Sync>> as futures::future::IntoFuture>::Future): NoSolution
  --> src/lib.rs:56:64
   |
56 |         let _future = <MyGuard as Guard>::from_request(&headers)
   |                                                                ^

error: internal compiler error: broken MIR in DefId(0/1:20 ~ playground[aa0a]::{{impl}}[3]::from_request[0]::{{closure}}[0]) (NoSolution): could not prove Binder(TraitPredicate(<<std::result::Result<MyGuard, std::boxed::Box<(dyn std::error::Error + std::marker::Send + std::marker::Sync + 'static)>> as futures::future::IntoFuture>::Future as std::marker::Send>))
  --> src/lib.rs:61:31
   |
61 |                       .and_then(move |fld_inner| {
   |  _______________________________^
62 | |                         Ok(Generic {
63 | |                             _inner: fld_inner,
64 | |                         })
65 | |                         .into_future()
66 | |                     })
   | |_____________________^

error: internal compiler error: broken MIR in DefId(0/0:29 ~ playground[aa0a]::{{impl}}[3]::from_request[0]) (NoSolution): could not prove Binder(TraitPredicate(<<std::result::Result<MyGuard, std::boxed::Box<(dyn std::error::Error + std::marker::Send + std::marker::Sync + 'static)>> as futures::future::IntoFuture>::Future as std::marker::Send>))
  --> src/lib.rs:58:23
   |
58 |               .and_then(move |_| {
   |  _______________________^
59 | |                 <I as FromRequest>::from_request(headers)
60 | |                     .into_future()
61 | |                     .and_then(move |fld_inner| {
...  |
66 | |                     })
67 | |             });
   | |_____________^

thread 'rustc' panicked at 'no errors encountered even though `delay_span_bug` issued', src/librustc_errors/lib.rs:354:17
note: Run with `RUST_BACKTRACE=1` environment variable to display a backtrace.

error: internal compiler error: unexpected panic

note: the compiler unexpectedly panicked. this is a bug.

note: we would appreciate a bug report: https://github.com/rust-lang/rust/blob/master/CONTRIBUTING.md#bug-reports

note: rustc 1.35.0 (3c235d560 2019-05-20) running on x86_64-unknown-linux-gnu

note: compiler flags: -C codegen-units=1 -C debuginfo=2 --crate-type lib

note: some of the compiler flags provided by cargo are hidden

error: Could not compile `playground`.

Any help on minifying the code would be welcome. Inlining the futures trait definitions into the crate makes one of the ICEs go away, so this version pulls it in as an external crate.

@jonas-schievink jonas-schievink added I-ICE Issue: The compiler panicked, giving an Internal Compilation Error (ICE) ❄️ I-nominated A-associated-items Area: Associated items such as associated types and consts. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. A-mir Area: Mid-level IR (MIR) - https://blog.rust-lang.org/2016/04/19/MIR.html C-bug Category: This is a bug. labels May 29, 2019
@matthewjasper matthewjasper self-assigned this May 29, 2019
Centril added a commit to Centril/rust that referenced this issue Jun 4, 2019
…r=pnkfelix

Fix NLL typeck ICEs

* Don't ICE when a type containing a region is constrained by nothing
* Don't ICE trying to normalize a type in a `ParamEnv` containing global bounds.

To explain what was happening in the `issue-61311-normalize.rs` case:

* When borrow checking the `the_fn` in the last `impl` we would try to normalize `Self::Proj` (`<Unit as HasProjFn>::Proj`).
* We would find the `impl` that we're checking and and check its `where` clause.
* This would need us to check `<Box<dyn Obj + 'static> as HasProj>::Proj: Bound`
* We find two possible implementations, the blanket impl and the bound in our `ParamEnv`.
* The bound in our `ParamEnv` was canonicalized, so we don't see it as a global bound. As such we prefer it to the `impl`.
* This means that we cannot normalize `<Box<dyn Obj + 'static> as HasProj>::Proj` to `Unit`.
* The `<Box<dyn Obj + 'static> as HasProj>::Proj: Bound` bound, which looks like it should be in our `ParamEnv` has been normalized to `Unit: Bound`.
* We fail to prove `<Box<dyn Obj + 'static> as HasProj>::Proj: Bound`.
* We ICE, since we believe typeck have errored.

Closes rust-lang#61311
Closes rust-lang#61315
Closes rust-lang#61320

r? @pnkfelix
cc @nikomatsakis
pietroalbini added a commit to pietroalbini/rust that referenced this issue Jun 4, 2019
…r=pnkfelix

Fix NLL typeck ICEs

* Don't ICE when a type containing a region is constrained by nothing
* Don't ICE trying to normalize a type in a `ParamEnv` containing global bounds.

To explain what was happening in the `issue-61311-normalize.rs` case:

* When borrow checking the `the_fn` in the last `impl` we would try to normalize `Self::Proj` (`<Unit as HasProjFn>::Proj`).
* We would find the `impl` that we're checking and and check its `where` clause.
* This would need us to check `<Box<dyn Obj + 'static> as HasProj>::Proj: Bound`
* We find two possible implementations, the blanket impl and the bound in our `ParamEnv`.
* The bound in our `ParamEnv` was canonicalized, so we don't see it as a global bound. As such we prefer it to the `impl`.
* This means that we cannot normalize `<Box<dyn Obj + 'static> as HasProj>::Proj` to `Unit`.
* The `<Box<dyn Obj + 'static> as HasProj>::Proj: Bound` bound, which looks like it should be in our `ParamEnv` has been normalized to `Unit: Bound`.
* We fail to prove `<Box<dyn Obj + 'static> as HasProj>::Proj: Bound`.
* We ICE, since we believe typeck have errored.

Closes rust-lang#61311
Closes rust-lang#61315
Closes rust-lang#61320

r? @pnkfelix
cc @nikomatsakis
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-associated-items Area: Associated items such as associated types and consts. A-mir Area: Mid-level IR (MIR) - https://blog.rust-lang.org/2016/04/19/MIR.html C-bug Category: This is a bug. I-ICE Issue: The compiler panicked, giving an Internal Compilation Error (ICE) ❄️ T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants