Skip to content

Commit

Permalink
collector: apply param substs to closures cast to fn items
Browse files Browse the repository at this point in the history
  • Loading branch information
arielb1 authored and brson committed Jun 22, 2017
1 parent d9b47c3 commit 8551427
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 0 deletions.
4 changes: 4 additions & 0 deletions src/librustc_trans/collector.rs
Original file line number Diff line number Diff line change
Expand Up @@ -493,6 +493,8 @@ impl<'a, 'tcx> MirVisitor<'tcx> for MirNeighborCollector<'a, 'tcx> {
}
mir::Rvalue::Cast(mir::CastKind::ClosureFnPointer, ref operand, _) => {
let source_ty = operand.ty(self.mir, self.scx.tcx());
let source_ty = self.scx.tcx().trans_apply_param_substs(self.param_substs,
&source_ty);
match source_ty.sty {
ty::TyClosure(def_id, substs) => {
let instance = monomorphize::resolve_closure(
Expand Down Expand Up @@ -543,6 +545,8 @@ impl<'a, 'tcx> MirVisitor<'tcx> for MirNeighborCollector<'a, 'tcx> {
block: mir::BasicBlock,
kind: &mir::TerminatorKind<'tcx>,
location: Location) {
debug!("visiting terminator {:?} @ {:?}", kind, location);

let tcx = self.scx.tcx();
match *kind {
mir::TerminatorKind::Call { ref func, .. } => {
Expand Down
7 changes: 7 additions & 0 deletions src/test/run-pass/closure-to-fn-coercion.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@
// option. This file may not be copied, modified, or distributed
// except according to those terms.

use std::mem;

const FOO: fn(u8) -> u8 = |v: u8| { v };

const BAR: [fn(&mut u32); 5] = [
Expand All @@ -21,6 +23,10 @@ fn func_specific() -> (fn() -> u32) {
|| return 42
}

fn generic<T>(_: T) -> fn() -> usize {
|| mem::size_of::<T>()
}

fn main() {
// Items
assert_eq!(func_specific()(), 42);
Expand All @@ -34,4 +40,5 @@ fn main() {
assert_eq!({ BAR[2](&mut a); a }, 3);
assert_eq!({ BAR[3](&mut a); a }, 6);
assert_eq!({ BAR[4](&mut a); a }, 10);
assert_eq!(generic(0i8)(), 1);
}

0 comments on commit 8551427

Please sign in to comment.