Skip to content

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

Closed
@jdmpapin

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(<[closure@lib.rs:21:19: 21:27 f:&[closure@lib.rs:25:15: 25:21]] as std::ops::Fn<(<&i32 as Foo<'_>>::Bar,)>>), Binder(<[closure@lib.rs:21:19: 21:27 f:&[closure@lib.rs:25:15: 25:21]] as std::ops::Fn<(&i32,)>>), Sorts(ExpectedFound { expected: &i32, found: <&i32 as Foo<'_>>::Bar }))` selecting `Binder(<[closure@lib.rs:21:19: 21:27 f:&[closure@lib.rs: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

Metadata

Metadata

Assignees

No one assigned

    Labels

    A-trait-systemArea: Trait systemC-bugCategory: This is a bug.I-ICEIssue: The compiler panicked, giving an Internal Compilation Error (ICE) ❄️P-mediumMedium priorityT-compilerRelevant to the compiler team, which will review and decide on the PR/issue.

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions