Skip to content

Closure syntax sneaks associated type under HRTB past type checking #44005

Closed

Description

@jdmpapin

The following code is rejected with E0277, which I think is due to a known issue with unifying associated type projections under HRTBs:

pub trait Foo<'a> {
    type Bar;
    fn foo(&'a self) -> Self::Bar;
}

impl<'a, 'b, T: 'a> Foo<'a> for &'b T {
    type Bar = &'a T;
    fn foo(&'a self) -> &'a T {
        self
    }
}

pub fn uncallable<T, F>(x: T, f: F)
    where T: for<'a> Foo<'a>,
          F: for<'a> Fn(<T as Foo<'a>>::Bar)
{
    f(x.foo());
}

pub fn broken<F: Fn(&i32)>(x: &i32, f: F) {
    uncallable(x, f);
}

pub fn catalyst(x: &i32) {
    broken(x, |_| {})
}

"Eta-expanding" f in broken like so:

pub fn broken<F: Fn(&i32)>(x: &i32, f: F) {
    uncallable(x, |y| f(y));
}

allows the code to type check, but the compiler panics later with expected: &i32, found: <&i32 as Foo<'_>>::Bar:

error: internal compiler error: /checkout/src/librustc/traits/trans/mod.rs:75: Encountered error `OutputTypeParameterMismatch(Binder(<[[email protected]:21:19: 21:27 f:&[[email protected]:25:15: 25:21]] as std::ops::Fn<(<&i32 as Foo<'_>>::Bar,)>>), Binder(<[[email protected]:21:19: 21:27 f:&[[email protected]:25:15: 25:21]] as std::ops::Fn<(&i32,)>>), Sorts(ExpectedFound { expected: &i32, found: <&i32 as Foo<'_>>::Bar }))` selecting `Binder(<[[email protected]:21:19: 21:27 f:&[[email protected]:25:15: 25:21]] as std::ops::Fn<(&i32,)>>)` during trans

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.21.0-nightly (5af17242c 2017-08-19) running on x86_64-unknown-linux-gnu

note: run with `RUST_BACKTRACE=1` for a backtrace

thread 'rustc' panicked at 'Box<Any>', /checkout/src/librustc_errors/lib.rs:434:8
note: Some details are omitted, run with `RUST_BACKTRACE=full` for a verbose backtrace.
stack backtrace:
   0: std::sys::imp::backtrace::tracing::imp::unwind_backtrace
             at /checkout/src/libstd/sys/unix/backtrace/tracing/gcc_s.rs:49
   1: std::sys_common::backtrace::_print
             at /checkout/src/libstd/sys_common/backtrace.rs:71
   2: std::panicking::default_hook::{{closure}}
             at /checkout/src/libstd/sys_common/backtrace.rs:60
             at /checkout/src/libstd/panicking.rs:381
   3: std::panicking::default_hook
             at /checkout/src/libstd/panicking.rs:391
   4: std::panicking::rust_panic_with_hook
             at /checkout/src/libstd/panicking.rs:611
   5: std::panicking::begin_panic
   6: rustc_errors::Handler::span_bug
   7: rustc::session::opt_span_bug_fmt::{{closure}}
   8: rustc::session::opt_span_bug_fmt
   9: rustc::session::span_bug_fmt
  10: rustc::traits::trans::<impl rustc::ty::context::TyCtxt<'a, 'tcx, 'tcx>>::trans_fulfill_obligation::{{closure}}
  11: rustc::traits::trans::<impl rustc::ty::context::TyCtxt<'a, 'tcx, 'tcx>>::trans_fulfill_obligation
  12: rustc_trans::monomorphize::resolve
  13: <rustc_trans::collector::MirNeighborCollector<'a, 'tcx> as rustc::mir::visit::Visitor<'tcx>>::visit_terminator_kind
  14: rustc::mir::visit::Visitor::visit_mir
  15: rustc_trans::collector::collect_items_rec
  16: rustc_trans::collector::collect_items_rec
  17: rustc_trans::collector::collect_items_rec
  18: rustc_trans::base::collect_and_partition_translation_items::{{closure}}
  19: rustc_trans::base::trans_crate
  20: rustc_driver::driver::phase_4_translate_to_llvm
  21: rustc_driver::driver::compile_input::{{closure}}
  22: rustc::ty::context::TyCtxt::create_and_enter
  23: rustc_driver::driver::compile_input
  24: rustc_driver::run_compiler

This output is from nightly:

$ rustc --version --verbose
rustc 1.21.0-nightly (5af17242c 2017-08-19)
binary: rustc
commit-hash: 5af17242ccb151e136122b2231df2fe4031d340e
commit-date: 2017-08-19
host: x86_64-unknown-linux-gnu
release: 1.21.0-nightly
LLVM version: 4.0

I get a similar panic from 1.19, but not during trans

Activity

  1. added
    C-bug
    I-ICE
    T-compiler
    on Aug 21, 2017
  2. eddyb commented on Aug 25, 2017

    @eddyb
    Contributor
  3. nikomatsakis commented on Aug 31, 2017

    @nikomatsakis
    Contributor

    triage: P-medium

    I've not dug too deeply here. I do expect this would be addressed by the work I'm doing on reworking normalization, which will enable (among other things) normalizing under binders.

  4. added
    P-medium
    and removed on Aug 31, 2017
  5. jdmpapin commented on Mar 31, 2020

    @jdmpapin
    Author

    It seems like the test added in bbbbf2d is passing only because broken, uncallable, etc. aren't reachable from main. If I copy the test to the Rust playground, it compiles and runs as-is, but it produces an ICE again if I modify main to call catalyst like so:

    fn main() {
        catalyst(&0);
    }

    Here's the output from stable (1.42.0):

       Compiling playground v0.0.1 (/playground)
    error: internal compiler error: src/librustc/traits/codegen/mod.rs:57: Encountered error `OutputTypeParameterMismatch(Binder(<[closure@src/main.rs:25:19: 25:27 f:&[closure@src/main.rs:21:15: 21:21]] as std::ops::Fn<(<&i32 as Foo<'_>>::Bar,)>>), Binder(<[closure@src/main.rs:25:19: 25:27 f:&[closure@src/main.rs:21:15: 21:21]] as std::ops::Fn<(&i32,)>>), Sorts(ExpectedFound { expected: &i32, found: <&i32 as Foo<'_>>::Bar }))` selecting `Binder(<[closure@src/main.rs:25:19: 25:27 f:&[closure@src/main.rs:21:15: 21:21]] as std::ops::Fn<(&i32,)>>)` during codegen
    
    thread 'rustc' panicked at 'Box<Any>', src/librustc_errors/lib.rs:873:9
    stack backtrace:
       0: backtrace::backtrace::libunwind::trace
                 at /cargo/registry/src/github.com-1ecc6299db9ec823/backtrace-0.3.40/src/backtrace/libunwind.rs:88
       1: backtrace::backtrace::trace_unsynchronized
                 at /cargo/registry/src/github.com-1ecc6299db9ec823/backtrace-0.3.40/src/backtrace/mod.rs:66
       2: std::sys_common::backtrace::_print_fmt
                 at src/libstd/sys_common/backtrace.rs:77
       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:1052
       5: std::io::Write::write_fmt
                 at src/libstd/io/mod.rs:1426
       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:204
       9: std::panicking::default_hook
                 at src/libstd/panicking.rs:224
      10: rustc_driver::report_ice
      11: std::panicking::rust_panic_with_hook
                 at src/libstd/panicking.rs:476
      12: std::panicking::begin_panic
      13: rustc_errors::HandlerInner::bug
      14: rustc_errors::Handler::bug
      15: rustc::util::bug::opt_span_bug_fmt::{{closure}}
      16: rustc::ty::context::tls::with_opt::{{closure}}
      17: rustc::ty::context::tls::with_opt
      18: rustc::util::bug::opt_span_bug_fmt
      19: rustc::util::bug::bug_fmt
      20: rustc::ty::context::GlobalCtxt::enter_local
      21: rustc::traits::codegen::codegen_fulfill_obligation
      22: rustc::ty::query::__query_compute::codegen_fulfill_obligation
      23: rustc::ty::query::<impl rustc::ty::query::config::QueryAccessors for rustc::ty::query::queries::codegen_fulfill_obligation>::compute
      24: rustc::dep_graph::graph::DepGraph::with_task_impl
      25: rustc::ty::query::plumbing::<impl rustc::ty::context::TyCtxt>::get_query
      26: rustc::ty::instance::Instance::resolve
      27: <rustc_mir::monomorphize::collector::MirNeighborCollector as rustc::mir::visit::Visitor>::visit_terminator_kind
      28: rustc_mir::monomorphize::collector::collect_items_rec
      29: rustc_mir::monomorphize::collector::collect_items_rec
      30: rustc_mir::monomorphize::collector::collect_items_rec
      31: rustc_mir::monomorphize::collector::collect_items_rec
      32: rustc_session::utils::<impl rustc_session::session::Session>::time
      33: rustc_mir::monomorphize::collector::collect_crate_mono_items
      34: rustc_mir::monomorphize::partitioning::collect_and_partition_mono_items
      35: rustc::ty::query::__query_compute::collect_and_partition_mono_items
      36: rustc::dep_graph::graph::DepGraph::with_task_impl
      37: rustc::ty::query::plumbing::<impl rustc::ty::context::TyCtxt>::get_query
      38: rustc_codegen_ssa::base::codegen_crate
      39: <rustc_codegen_llvm::LlvmCodegenBackend as rustc_codegen_utils::codegen_backend::CodegenBackend>::codegen_crate
      40: rustc_session::utils::<impl rustc_session::session::Session>::time
      41: rustc_interface::passes::QueryContext::enter
      42: rustc_interface::queries::Queries::ongoing_codegen
      43: rustc_interface::interface::run_compiler_in_existing_thread_pool
      44: scoped_tls::ScopedKey<T>::set
      45: syntax::with_globals
    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.42.0 (b8cedc004 2020-03-09) running on x86_64-unknown-linux-gnu
    
    note: compiler flags: -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::Fn` fulfills its obligations
    #1 [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.
    
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    A-trait-systemC-bugI-ICEP-mediumT-compiler

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions