Skip to content

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

Closed
@hjgz

Description

@hjgz

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.com-1ecc6299db9ec823/backtrace-0.3.46/src/backtrace/libunwind.rs:86
   1: backtrace::backtrace::trace_unsynchronized
             at /cargo/registry/src/github.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.

Metadata

Metadata

Assignees

No one assigned

    Labels

    C-bugCategory: This is a bug.I-ICEIssue: The compiler panicked, giving an Internal Compilation Error (ICE) ❄️T-compilerRelevant to the compiler team, which will review and decide on the PR/issue.glacierICE tracked in rust-lang/glacier.

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions