Skip to content

Commit f492cf7

Browse files
Ariel Ben-Yehudabrson
Ariel Ben-Yehuda
authored andcommitted
check upvars in closures that are in statics
Fixes rust-lang#27890 Fixes rust-lang#28099 Fixes rust-lang#28113
1 parent 667584d commit f492cf7

File tree

5 files changed

+73
-1
lines changed

5 files changed

+73
-1
lines changed

src/librustc_typeck/check/mod.rs

+6-1
Original file line numberDiff line numberDiff line change
@@ -4153,8 +4153,13 @@ fn check_const_with_ty<'a, 'tcx>(fcx: &FnCtxt<'a, 'tcx>,
41534153

41544154
check_expr_with_hint(fcx, e, declty);
41554155
demand::coerce(fcx, e.span, declty, e);
4156-
fcx.select_all_obligations_or_error();
4156+
4157+
fcx.select_all_obligations_and_apply_defaults();
4158+
upvar::closure_analyze_const(&fcx, e);
4159+
fcx.select_obligations_where_possible();
41574160
fcx.check_casts();
4161+
fcx.select_all_obligations_or_error();
4162+
41584163
regionck::regionck_expr(fcx, e);
41594164
writeback::resolve_type_vars_in_expr(fcx, e);
41604165
}

src/librustc_typeck/check/upvar.rs

+14
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,20 @@ pub fn closure_analyze_fn(fcx: &FnCtxt,
7373
assert!(fcx.inh.deferred_call_resolutions.borrow().is_empty());
7474
}
7575

76+
pub fn closure_analyze_const(fcx: &FnCtxt,
77+
body: &hir::Expr)
78+
{
79+
let mut seed = SeedBorrowKind::new(fcx);
80+
seed.visit_expr(body);
81+
let closures_with_inferred_kinds = seed.closures_with_inferred_kinds;
82+
83+
let mut adjust = AdjustBorrowKind::new(fcx, &closures_with_inferred_kinds);
84+
adjust.visit_expr(body);
85+
86+
// it's our job to process these.
87+
assert!(fcx.inh.deferred_call_resolutions.borrow().is_empty());
88+
}
89+
7690
///////////////////////////////////////////////////////////////////////////
7791
// SEED BORROW KIND
7892

Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
// Copyright 2015 The Rust Project Developers. See the COPYRIGHT
2+
// file at the top-level directory of this distribution and at
3+
// http://rust-lang.org/COPYRIGHT.
4+
//
5+
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
6+
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
7+
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
8+
// option. This file may not be copied, modified, or distributed
9+
// except according to those terms.
10+
11+
// check that borrowck looks inside consts/statics
12+
13+
static FN : &'static (Fn() -> (Box<Fn()->Box<i32>>) + Sync) = &|| {
14+
let x = Box::new(0);
15+
Box::new(|| x) //~ ERROR cannot move out of captured outer variable
16+
};
17+
18+
fn main() {
19+
let f = (FN)();
20+
f();
21+
f();
22+
}

src/test/compile-fail/issue-28113.rs

+15
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
// Copyright 2015 The Rust Project Developers. See the COPYRIGHT
2+
// file at the top-level directory of this distribution and at
3+
// http://rust-lang.org/COPYRIGHT.
4+
//
5+
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
6+
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
7+
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
8+
// option. This file may not be copied, modified, or distributed
9+
// except according to those terms.
10+
11+
const X: u8 =
12+
|| -> u8 { 5 }() //~ ERROR function calls in constants are limited
13+
;
14+
15+
fn main() {}

src/test/run-pass/issue-27890.rs

+16
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
// Copyright 2015 The Rust Project Developers. See the COPYRIGHT
2+
// file at the top-level directory of this distribution and at
3+
// http://rust-lang.org/COPYRIGHT.
4+
//
5+
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
6+
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
7+
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
8+
// option. This file may not be copied, modified, or distributed
9+
// except according to those terms.
10+
11+
static PLUS_ONE: &'static (Fn(i32) -> i32 + Sync) = (&|x: i32| { x + 1 })
12+
as &'static (Fn(i32) -> i32 + Sync);
13+
14+
fn main() {
15+
assert_eq!(PLUS_ONE(2), 3);
16+
}

0 commit comments

Comments
 (0)