Skip to content

ICE on closures of trait bounds with indefinite lifetime parameters #74261

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
hjgz opened this issue Jul 12, 2020 · 5 comments · Fixed by #88441
Closed

ICE on closures of trait bounds with indefinite lifetime parameters #74261

hjgz opened this issue Jul 12, 2020 · 5 comments · Fixed by #88441
Labels
C-bug Category: This is a bug. glacier ICE tracked in rust-lang/glacier. 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

@hjgz
Copy link

hjgz commented Jul 12, 2020

It seems that a trait bound like for<'a> FnOnce(<TT as A<'a>>::B), when applied to a closure, will cause an internal compiler error.

Code

use std::marker::PhantomData;

trait A<'a> {
    type B;
    fn b(self) -> Self::B;
}

struct T;
struct S<'a>(PhantomData<&'a ()>);

impl<'a> A<'a> for T {
    type B = S<'a>;
    fn b(self) -> Self::B {
        S(PhantomData)
    }
}

fn s<TT, F>(t: TT, f: F) 
where 
    TT: for<'a> A<'a>, 
    F: for<'a> FnOnce(<TT as A<'a>>::B)
{
    f(t.b());
}

fn main() {
    s(T, |_| {});
}

Meta

Tested on the playground with stable, beta and nightly versions:

rustc 1.44.1 (c7087fe00 2020-06-17) running on x86_64-unknown-linux-gnu
rustc 1.45.0-beta.3 (8196407f0 2020-06-27) running on x86_64-unknown-linux-gnu
rustc 1.46.0-nightly (346aec9b0 2020-07-11) running on x86_64-unknown-linux-gnu

This error also occurs on my local pc.

Error output

   Compiling playground v0.0.1 (/playground)
error: internal compiler error: src/librustc_trait_selection/traits/codegen/mod.rs:62:17: Encountered error `OutputTypeParameterMismatch(Binder(<[closure@src/main.rs:27:10: 27:16] as std::ops::FnOnce<(<T as A<'_>>::B,)>>), Binder(<[closure@src/main.rs:27:10: 27:16] as std::ops::FnOnce<(S,)>>), Sorts(ExpectedFound { expected: S, found: <T as A<'_>>::B }))` selecting `Binder(<[closure@src/main.rs:27:10: 27:16] as std::ops::FnOnce<(S,)>>)` during codegen

thread 'rustc' panicked at 'Box<Any>', src/librustc_errors/lib.rs:916:9
note: run with `RUST_BACKTRACE=1` environment variable to display a backtrace

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.46.0-nightly (346aec9b0 2020-07-11) running on x86_64-unknown-linux-gnu

note: compiler flags: -C embed-bitcode=no -C codegen-units=1 -C debuginfo=2 --crate-type bin

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

error: aborting due to previous error

error: could not compile `playground`.

To learn more, run the command again with --verbose.
Backtrace

   Compiling playground v0.0.1 (/playground)
error: internal compiler error: src/librustc_trait_selection/traits/codegen/mod.rs:62:17: Encountered error `OutputTypeParameterMismatch(Binder(<[closure@src/main.rs:27:10: 27:16] as std::ops::FnOnce<(<T as A<'_>>::B,)>>), Binder(<[closure@src/main.rs:27:10: 27:16] as std::ops::FnOnce<(S,)>>), Sorts(ExpectedFound { expected: S, found: <T as A<'_>>::B }))` selecting `Binder(<[closure@src/main.rs:27:10: 27:16] as std::ops::FnOnce<(S,)>>)` during codegen

thread 'rustc' panicked at 'Box<Any>', src/librustc_errors/lib.rs:916:9
stack backtrace:
   0: backtrace::backtrace::libunwind::trace
             at /cargo/registry/src/github.heygears.com-1ecc6299db9ec823/backtrace-0.3.46/src/backtrace/libunwind.rs:86
   1: backtrace::backtrace::trace_unsynchronized
             at /cargo/registry/src/github.heygears.com-1ecc6299db9ec823/backtrace-0.3.46/src/backtrace/mod.rs:66
   2: std::sys_common::backtrace::_print_fmt
             at src/libstd/sys_common/backtrace.rs:78
   3: <std::sys_common::backtrace::_print::DisplayBacktrace as core::fmt::Display>::fmt
             at src/libstd/sys_common/backtrace.rs:59
   4: core::fmt::write
             at src/libcore/fmt/mod.rs:1076
   5: std::io::Write::write_fmt
             at src/libstd/io/mod.rs:1537
   6: std::sys_common::backtrace::_print
             at src/libstd/sys_common/backtrace.rs:62
   7: std::sys_common::backtrace::print
             at src/libstd/sys_common/backtrace.rs:49
   8: std::panicking::default_hook::{{closure}}
             at src/libstd/panicking.rs:198
   9: std::panicking::default_hook
             at src/libstd/panicking.rs:217
  10: rustc_driver::report_ice
  11: std::panicking::rust_panic_with_hook
             at src/libstd/panicking.rs:530
  12: std::panicking::begin_panic
  13: rustc_errors::HandlerInner::bug
  14: rustc_errors::Handler::bug
  15: rustc_middle::util::bug::opt_span_bug_fmt::{{closure}}
  16: rustc_middle::ty::context::tls::with_opt::{{closure}}
  17: rustc_middle::ty::context::tls::with_opt
  18: rustc_middle::util::bug::opt_span_bug_fmt
  19: rustc_middle::util::bug::bug_fmt
  20: rustc_infer::infer::InferCtxtBuilder::enter
  21: rustc_trait_selection::traits::codegen::codegen_fulfill_obligation
  22: rustc_middle::ty::query::<impl rustc_query_system::query::config::QueryAccessors<rustc_middle::ty::context::TyCtxt> for rustc_middle::ty::query::queries::codegen_fulfill_obligation>::compute
  23: rustc_query_system::dep_graph::graph::DepGraph<K>::with_task_impl
  24: rustc_data_structures::stack::ensure_sufficient_stack
  25: rustc_query_system::query::plumbing::get_query_impl
  26: rustc_ty::instance::resolve_instance
  27: rustc_middle::ty::query::<impl rustc_query_system::query::config::QueryAccessors<rustc_middle::ty::context::TyCtxt> for rustc_middle::ty::query::queries::resolve_instance>::compute
  28: rustc_query_system::dep_graph::graph::DepGraph<K>::with_task_impl
  29: rustc_data_structures::stack::ensure_sufficient_stack
  30: rustc_query_system::query::plumbing::get_query_impl
  31: rustc_middle::ty::instance::Instance::resolve
  32: <rustc_mir::monomorphize::collector::MirNeighborCollector as rustc_middle::mir::visit::Visitor>::visit_terminator
  33: rustc_mir::monomorphize::collector::collect_neighbours
  34: rustc_mir::monomorphize::collector::collect_items_rec
  35: rustc_mir::monomorphize::collector::collect_items_rec
  36: rustc_session::utils::<impl rustc_session::session::Session>::time
  37: rustc_mir::monomorphize::collector::collect_crate_mono_items
  38: rustc_mir::monomorphize::partitioning::collect_and_partition_mono_items
  39: rustc_middle::ty::query::<impl rustc_query_system::query::config::QueryAccessors<rustc_middle::ty::context::TyCtxt> for rustc_middle::ty::query::queries::collect_and_partition_mono_items>::compute
  40: rustc_query_system::dep_graph::graph::DepGraph<K>::with_task_impl
  41: rustc_query_system::query::plumbing::get_query_impl
  42: rustc_codegen_ssa::base::codegen_crate
  43: <rustc_codegen_llvm::LlvmCodegenBackend as rustc_codegen_ssa::traits::backend::CodegenBackend>::codegen_crate
  44: rustc_session::utils::<impl rustc_session::session::Session>::time
  45: rustc_interface::passes::start_codegen
  46: rustc_middle::ty::context::tls::enter_global
  47: rustc_interface::queries::Queries::ongoing_codegen
  48: rustc_interface::queries::<impl rustc_interface::interface::Compiler>::enter
  49: rustc_span::with_source_map
  50: rustc_interface::interface::create_compiler_and_run
  51: scoped_tls::ScopedKey<T>::set
note: Some details are omitted, run with `RUST_BACKTRACE=full` for a verbose backtrace.

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.46.0-nightly (346aec9b0 2020-07-11) running on x86_64-unknown-linux-gnu

note: compiler flags: -C embed-bitcode=no -C codegen-units=1 -C debuginfo=2 --crate-type bin

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

query stack during panic:
#0 [codegen_fulfill_obligation] checking if `std::ops::FnOnce` fulfills its obligations
#1 [resolve_instance] resolving instance `<[closure@src/main.rs:27:10: 27:16] as std::ops::FnOnce<(S,)>>::call_once`
#2 [collect_and_partition_mono_items] collect_and_partition_mono_items
end of query stack
error: aborting due to previous error

error: could not compile `playground`.

To learn more, run the command again with --verbose.

@hjgz hjgz 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 Jul 12, 2020
@jonas-schievink
Copy link
Contributor

Presumably part of #62529

@rust-lang-glacier-bot rust-lang-glacier-bot added the glacier ICE tracked in rust-lang/glacier. label Jul 14, 2020
@udoprog
Copy link
Contributor

udoprog commented Oct 4, 2020

Encountered the same issues, here's the minimal example I managed to put together (playground):

pub trait Trait<'a> {
    type Output;

    fn output(self) -> Self::Output;
}

impl<'a> Trait<'a> for () {
    type Output = ();

    fn output(self) -> () {
        ()
    }
}

fn confused_hrtb<T, F>(value: T, f: F)
where
    T: for<'a> Trait<'a>,
    F: for<'b> FnOnce(<T as Trait<'b>>::Output),
{
    f(value.output())
}

fn main() -> () {
    confused_hrtb((), |_| ());
}

@aldanor
Copy link

aldanor commented Nov 22, 2020

Encountered a seemingly related ICE as well :(

pub trait Ref<'a> {
    type Type: Clone;
}
impl<'a, T: 'static + Clone> Ref<'a> for T {
    type Type = T;
}

pub type EvalFn<I> = dyn FnMut(&<I as Ref<'_>>::Type);

pub trait Evaluator<T: for<'a> Ref<'a>> {
    fn make_evaluator(&self) -> Box<EvalFn<T>>;
}

pub struct A;

impl Evaluator<i32> for A {
    fn make_evaluator(&self) -> Box<EvalFn<i32>> {
        Box::new(|_x| {})
    }
}

fn main() {
    let mut x: Vec<Box<EvalFn<i32>>> = vec![];
    x.push(A.make_evaluator());
}

resulting in

error: internal compiler error: compiler/rustc_trait_selection/src/traits/codegen/mod.rs:75:17: Encountered error `OutputTypeParameterMismatch(Binder(<[closure@src/main.rs:19:22: 19:29] as std::ops::FnMut<(&<i32 as main::Ref<'_>>::Type,)>>), Binder(<[closure@src/main.rs:19:22: 19:29] as std::ops::FnMut<(&i32,)>>), Sorts(ExpectedFound { expected: i32, found: <i32 as main::Ref<'_>>::Type }))` selecting `Binder(<[closure@src/main.rs:19:22: 19:29] as std::ops::FnMut<(&i32,)>>)` during codegen

@aldanor
Copy link

aldanor commented Nov 22, 2020

(Updated the link from the previously posted comment)

One way to work around the ICE - wrapping closures in structs: example that seems to avoid the ICE in the above snippet.

@jackh726
Copy link
Member

No longer ICEs with #85499, but emits an error. Need to look more into it to see if expected.

# for free to join this conversation on GitHub. Already have an account? # to comment
Labels
C-bug Category: This is a bug. glacier ICE tracked in rust-lang/glacier. 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.

6 participants