Skip to content

Commit

Permalink
Mark closures return via impl-trait as reachable.
Browse files Browse the repository at this point in the history
  • Loading branch information
michaelwoerister committed Aug 14, 2017
1 parent bae4faf commit f2df185
Show file tree
Hide file tree
Showing 4 changed files with 16 additions and 0 deletions.
3 changes: 3 additions & 0 deletions src/librustc/middle/reachable.rs
Original file line number Diff line number Diff line change
Expand Up @@ -296,6 +296,9 @@ impl<'a, 'tcx> ReachableContext<'a, 'tcx> {
hir::ImplItemKind::Type(_) => {}
}
}
hir_map::NodeExpr(&hir::Expr { node: hir::ExprClosure(.., body, _), .. }) => {
self.visit_nested_body(body);
}
// Nothing to recurse on for these
hir_map::NodeForeignItem(_) |
hir_map::NodeVariant(_) |
Expand Down
1 change: 1 addition & 0 deletions src/librustc_privacy/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -448,6 +448,7 @@ impl<'b, 'a, 'tcx> TypeVisitor<'tcx> for ReachEverythingInTheInterfaceVisitor<'b
ty::TyDynamic(ref obj, ..) => obj.principal().map(|p| p.def_id()),
ty::TyProjection(ref proj) => Some(proj.item_def_id),
ty::TyFnDef(def_id, ..) |
ty::TyClosure(def_id, ..) |
ty::TyAnon(def_id, _) => Some(def_id),
_ => None
};
Expand Down
11 changes: 11 additions & 0 deletions src/test/run-pass/impl-trait/auxiliary/xcrate.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,3 +13,14 @@
pub fn fourway_add(a: i32) -> impl Fn(i32) -> impl Fn(i32) -> impl Fn(i32) -> i32 {
move |b| move |c| move |d| a + b + c + d
}

fn some_internal_fn() -> u32 {
1
}

// See #40839
pub fn return_closure_accessing_internal_fn() -> impl Fn() -> u32 {
|| {
some_internal_fn() + 1
}
}
1 change: 1 addition & 0 deletions src/test/run-pass/impl-trait/xcrate.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,4 +14,5 @@ extern crate xcrate;

fn main() {
assert_eq!(xcrate::fourway_add(1)(2)(3)(4), 10);
xcrate::return_closure_accessing_internal_fn()();
}

0 comments on commit f2df185

Please sign in to comment.