Skip to content

Internal Compiler Error caused by Generic Associated Types #79768

New issue

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

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

Already on GitHub? # to your account

Closed
Tracked by #44265
Sawchord opened this issue Dec 6, 2020 · 8 comments · Fixed by #82752
Closed
Tracked by #44265

Internal Compiler Error caused by Generic Associated Types #79768

Sawchord opened this issue Dec 6, 2020 · 8 comments · Fixed by #82752
Labels
A-GATs Area: Generic associated types (GATs) C-bug Category: This is a bug. E-needs-test Call for participation: An issue has been fixed and does not reproduce, but no test has been added. F-generic_associated_types `#![feature(generic_associated_types)]` a.k.a. GATs glacier ICE tracked in rust-lang/glacier. I-ICE Issue: The compiler panicked, giving an Internal Compilation Error (ICE) ❄️ requires-nightly This issue requires a nightly compiler in some way. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.

Comments

@Sawchord
Copy link

Sawchord commented Dec 6, 2020

Code

#![feature(generic_associated_types)]

trait Monad /* : Applicative (for pure/return, doesn't matter for this example) */ {
    // Self is like the "f a" in haskell

    /// extract the "a" from "f a"
    type Unplug;

    /// exchange the "a" in "f a" in the type of Self with B
    type Plug<B>: Monad;

    fn bind<B, F>(self, f: F) -> Self::Plug<B>
    where
        F: Fn(Self::Unplug) -> Self::Plug<B>;
}

impl<A> Monad for Option<A> {
    type Unplug = A;
    type Plug<B> = Option<B>;
    fn bind<B, F>(self, f: F) -> Option<B>
    where
        F: Fn(A) -> Option<B>,
    {
        self.and_then(f)
    }
}

// This function causes the compiler error, specifically, when i added the Plug = P constraint.
fn stringify<T, M1, P>(m: M1) -> M1::Plug<P>
where
    T: core::fmt::Display,
    M1: Monad<Unplug = T, Plug = P>,
{
    m.bind(|x| format!("{}", x))
}

fn main() {
    let x = Some(1).bind(|x| Some(x * 2));
    println!("{:?}", x);
}

Meta

rustc --version --verbose:

rustc 1.50.0-nightly (1c389ffef 2020-11-24)
binary: rustc
commit-hash: 1c389ffeff814726dec325f0f2b0c99107df2673
commit-date: 2020-11-24
host: x86_64-unknown-linux-gnu
release: 1.50.0-nightly

Error output

warning: the feature `generic_associated_types` is incomplete and may not be safe to use and/or cause compiler crashes
 --> crash.rs:1:12
  |
1 | #![feature(generic_associated_types)]
  |            ^^^^^^^^^^^^^^^^^^^^^^^^
  |
  = note: `#[warn(incomplete_features)]` on by default
  = note: see issue #44265 <https://github.com/rust-lang/rust/issues/44265> for more information

error: internal compiler error: compiler/rustc_middle/src/ty/subst.rs:529:17: type parameter `B/#1` (B/1) out of range when substituting, substs=[M1]

Backtrace

thread 'rustc' panicked at 'Box<Any>', /rustc/1c389ffeff814726dec325f0f2b0c99107df2673/compiler/rustc_errors/src/lib.rs:904:9
stack backtrace:
   0: std::panicking::begin_panic
   1: rustc_errors::HandlerInner::span_bug
   2: rustc_errors::Handler::span_bug
   3: rustc_middle::util::bug::opt_span_bug_fmt::{{closure}}
   4: rustc_middle::ty::context::tls::with_opt::{{closure}}
   5: rustc_middle::ty::context::tls::with_opt
   6: rustc_middle::util::bug::opt_span_bug_fmt
   7: rustc_middle::util::bug::span_bug_fmt
   8: <rustc_middle::ty::subst::SubstFolder as rustc_middle::ty::fold::TypeFolder>::fold_ty
   9: rustc_middle::ty::fold::TypeFoldable::fold_with
  10: rustc_middle::ty::fold::TypeFoldable::fold_with
  11: rustc_middle::ty::subst::Subst::subst
  12: rustc_middle::ty::GenericPredicates::instantiate_into
  13: rustc_middle::ty::GenericPredicates::instantiate
  14: rustc_trait_selection::traits::wf::WfPredicates::nominal_obligations
  15: rustc_trait_selection::traits::wf::WfPredicates::compute_projection
  16: rustc_trait_selection::traits::wf::predicate_obligations
  17: rustc_typeck::check::wfcheck::check_where_clauses
  18: rustc_typeck::check::wfcheck::check_fn_or_method
  19: rustc_infer::infer::InferCtxtBuilder::enter
  20: rustc_typeck::check::wfcheck::check_item_well_formed
  21: rustc_middle::ty::query::<impl rustc_query_system::query::config::QueryAccessors<rustc_middle::ty::context::TyCtxt> for rustc_middle::ty::query::queries::check_item_well_formed>::compute
  22: rustc_query_system::dep_graph::graph::DepGraph<K>::with_task
  23: rustc_data_structures::stack::ensure_sufficient_stack
  24: rustc_query_system::query::plumbing::get_query_impl
  25: rustc_query_system::query::plumbing::ensure_query_impl
  26: <std::panic::AssertUnwindSafe<F> as core::ops::function::FnOnce<()>>::call_once
  27: rustc_hir::hir::Crate::par_visit_all_item_likes
  28: rustc_typeck::check_crate
  29: rustc_interface::passes::analysis
  30: rustc_middle::ty::query::<impl rustc_query_system::query::config::QueryAccessors<rustc_middle::ty::context::TyCtxt> for rustc_middle::ty::query::queries::analysis>::compute
  31: rustc_query_system::dep_graph::graph::DepGraph<K>::with_eval_always_task
  32: rustc_data_structures::stack::ensure_sufficient_stack
  33: rustc_query_system::query::plumbing::get_query_impl
  34: rustc_interface::passes::QueryContext::enter
  35: rustc_interface::queries::<impl rustc_interface::interface::Compiler>::enter
  36: rustc_span::with_source_map
  37: scoped_tls::ScopedKey<T>::set

@Sawchord Sawchord added 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. labels Dec 6, 2020
@jonas-schievink jonas-schievink added F-generic_associated_types `#![feature(generic_associated_types)]` a.k.a. GATs requires-nightly This issue requires a nightly compiler in some way. labels Dec 6, 2020
@camelid
Copy link
Member

camelid commented Dec 7, 2020

rustbot added a commit to rustbot/glacier that referenced this issue Dec 7, 2020
camelid added a commit to rust-lang/glacier that referenced this issue Dec 7, 2020
@rust-lang-glacier-bot rust-lang-glacier-bot added the glacier ICE tracked in rust-lang/glacier. label Dec 7, 2020
@cynecx
Copy link
Contributor

cynecx commented Dec 9, 2020

#79554 might be related.

@camelid
Copy link
Member

camelid commented Dec 9, 2020

If #79554 fixes it, then we'll find out in rust-lang/glacier!

@cynecx
Copy link
Contributor

cynecx commented Feb 5, 2021

I expected this to work (tested on top of #79554):

fn stringify<T, M1>(m: M1) -> <M1 as Monad>::Plug<String>
where
    T: core::fmt::Display,
    M1: Monad<Unplug = T>,
{
    m.bind(|x| Some(format!("{}", x)))
}

But it errors with:

error[E0308]: mismatched types
  --> a.rs:32:16
   |
32 |     m.bind(|x| Some(format!("{}", x)))
   |                ^^^^^^^^^^^^^^^^^^^^^^ expected associated type, found enum `Option`
   |
   = note: expected associated type `<M1 as Monad>::Plug<_>`
                         found enum `Option<String>`
   = help: consider constraining the associated type `<M1 as Monad>::Plug<_>` to `Option<String>`
   = note: for more information, visit https://doc.rust-lang.org/book/ch19-03-advanced-traits.html

Am I missing something? (Or is this a bug?) @jackh726

EDIT: I think the issue here is that we can't constraint the generic argument for Plug to the concrete type String. But I also kinda expected <M1 as Monad>::Plug<String> would get normalized to Option<String>

@jackh726
Copy link
Member

jackh726 commented Feb 5, 2021

@cynecx looks like the same bug as #76407 (working on it now)

@Alexendoo Alexendoo added the E-needs-test Call for participation: An issue has been fixed and does not reproduce, but no test has been added. label Feb 6, 2021
@Alexendoo
Copy link
Member

Fixed by #79554

JohnTitor added a commit to JohnTitor/rust that referenced this issue Mar 5, 2021
Add a regression test for issue-81712

Fixes rust-lang#81712, also fixes rust-lang#79768 as duplicate.
r? ``@jackh726``
m-ou-se added a commit to m-ou-se/rust that referenced this issue Mar 5, 2021
Add a regression test for issue-81712

Fixes rust-lang#81712, also fixes rust-lang#79768 as duplicate.
r? ```@jackh726```
@bors bors closed this as completed in e89276b Mar 5, 2021
@cynecx
Copy link
Contributor

cynecx commented Mar 5, 2021

Huh? This isn't fully fixed by #79554. This original code also requires normalization which doesn't happen right now.

@memoryruins
Copy link
Contributor

@cynecx this issue was only tracking the ICE, which is fixed on nightly and now has a regression test. #76407 is tracking the issue you're referring to, right? Worth re-posting the examples on the other issue.

# for free to join this conversation on GitHub. Already have an account? # to comment
Labels
A-GATs Area: Generic associated types (GATs) C-bug Category: This is a bug. E-needs-test Call for participation: An issue has been fixed and does not reproduce, but no test has been added. F-generic_associated_types `#![feature(generic_associated_types)]` a.k.a. GATs glacier ICE tracked in rust-lang/glacier. I-ICE Issue: The compiler panicked, giving an Internal Compilation Error (ICE) ❄️ requires-nightly This issue requires a nightly compiler in some way. 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.

9 participants