Skip to content

Commit 24202c6

Browse files
committed
Auto merge of #28131 - arielb1:static-upvars, r=pnkfelix
Fixes #23057 Fixes #27890 Fixes #28099 Fixes #28113 r? @pnkfelix
2 parents 8719f50 + c01e050 commit 24202c6

File tree

5 files changed

+73
-1
lines changed

5 files changed

+73
-1
lines changed

src/librustc_typeck/check/mod.rs

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4124,8 +4124,13 @@ fn check_const_with_ty<'a, 'tcx>(fcx: &FnCtxt<'a, 'tcx>,
41244124

41254125
check_expr_with_hint(fcx, e, declty);
41264126
demand::coerce(fcx, e.span, declty, e);
4127-
fcx.select_all_obligations_or_error();
4127+
4128+
fcx.select_all_obligations_and_apply_defaults();
4129+
upvar::closure_analyze_const(&fcx, e);
4130+
fcx.select_obligations_where_possible();
41284131
fcx.check_casts();
4132+
fcx.select_all_obligations_or_error();
4133+
41294134
regionck::regionck_expr(fcx, e);
41304135
writeback::resolve_type_vars_in_expr(fcx, e);
41314136
}

src/librustc_typeck/check/upvar.rs

Lines changed: 14 additions & 0 deletions
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

Lines changed: 22 additions & 0 deletions
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

Lines changed: 15 additions & 0 deletions
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

Lines changed: 16 additions & 0 deletions
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)