Skip to content

Commit ee67e14

Browse files
committed
Stabilize the copy_closures and clone_closures features
In addition to the `Fn*` family of traits, closures now implement `Copy` (and similarly `Clone`) if all of the captures do.
1 parent 52f7e88 commit ee67e14

22 files changed

+15
-169
lines changed

src/librustc/dep_graph/dep_node.rs

-2
Original file line numberDiff line numberDiff line change
@@ -616,8 +616,6 @@ define_dep_nodes!( <'tcx>
616616
[input] MissingExternCrateItem(CrateNum),
617617
[input] UsedCrateSource(CrateNum),
618618
[input] PostorderCnums,
619-
[] HasCloneClosures(CrateNum),
620-
[] HasCopyClosures(CrateNum),
621619

622620
// This query is not expected to have inputs -- as a result, it's
623621
// not a good candidate for "replay" because it's essentially a

src/librustc/lib.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@
4545
#![feature(box_syntax)]
4646
#![feature(conservative_impl_trait)]
4747
#![feature(const_fn)]
48-
#![feature(copy_closures, clone_closures)]
48+
#![cfg_attr(stage0, feature(copy_closures, clone_closures))]
4949
#![feature(core_intrinsics)]
5050
#![feature(drain_filter)]
5151
#![feature(dyn_trait)]

src/librustc/traits/select.rs

+2-6
Original file line numberDiff line numberDiff line change
@@ -2086,12 +2086,8 @@ impl<'cx, 'gcx, 'tcx> SelectionContext<'cx, 'gcx, 'tcx> {
20862086

20872087
ty::TyClosure(def_id, substs) => {
20882088
let trait_id = obligation.predicate.def_id();
2089-
let copy_closures =
2090-
Some(trait_id) == self.tcx().lang_items().copy_trait() &&
2091-
self.tcx().has_copy_closures(def_id.krate);
2092-
let clone_closures =
2093-
Some(trait_id) == self.tcx().lang_items().clone_trait() &&
2094-
self.tcx().has_clone_closures(def_id.krate);
2089+
let copy_closures = Some(trait_id) == self.tcx().lang_items().copy_trait();
2090+
let clone_closures = Some(trait_id) == self.tcx().lang_items().clone_trait();
20952091

20962092
if copy_closures || clone_closures {
20972093
Where(ty::Binder(substs.upvar_tys(def_id, self.tcx()).collect()))

src/librustc/ty/context.rs

-8
Original file line numberDiff line numberDiff line change
@@ -2520,14 +2520,6 @@ pub fn provide(providers: &mut ty::maps::Providers) {
25202520
assert_eq!(cnum, LOCAL_CRATE);
25212521
tcx.output_filenames.clone()
25222522
};
2523-
providers.has_copy_closures = |tcx, cnum| {
2524-
assert_eq!(cnum, LOCAL_CRATE);
2525-
tcx.features().copy_closures
2526-
};
2527-
providers.has_clone_closures = |tcx, cnum| {
2528-
assert_eq!(cnum, LOCAL_CRATE);
2529-
tcx.features().clone_closures
2530-
};
25312523
providers.features_query = |tcx, cnum| {
25322524
assert_eq!(cnum, LOCAL_CRATE);
25332525
Lrc::new(tcx.sess.features_untracked().clone())

src/librustc/ty/maps/config.rs

-12
Original file line numberDiff line numberDiff line change
@@ -604,24 +604,12 @@ impl<'tcx> QueryDescription<'tcx> for queries::output_filenames<'tcx> {
604604
}
605605
}
606606

607-
impl<'tcx> QueryDescription<'tcx> for queries::has_clone_closures<'tcx> {
608-
fn describe(_tcx: TyCtxt, _: CrateNum) -> String {
609-
format!("seeing if the crate has enabled `Clone` closures")
610-
}
611-
}
612-
613607
impl<'tcx> QueryDescription<'tcx> for queries::vtable_methods<'tcx> {
614608
fn describe(tcx: TyCtxt, key: ty::PolyTraitRef<'tcx> ) -> String {
615609
format!("finding all methods for trait {}", tcx.item_path_str(key.def_id()))
616610
}
617611
}
618612

619-
impl<'tcx> QueryDescription<'tcx> for queries::has_copy_closures<'tcx> {
620-
fn describe(_tcx: TyCtxt, _: CrateNum) -> String {
621-
format!("seeing if the crate has enabled `Copy` closures")
622-
}
623-
}
624-
625613
impl<'tcx> QueryDescription<'tcx> for queries::features_query<'tcx> {
626614
fn describe(_tcx: TyCtxt, _: CrateNum) -> String {
627615
format!("looking up enabled feature gates")

src/librustc/ty/maps/mod.rs

-3
Original file line numberDiff line numberDiff line change
@@ -382,9 +382,6 @@ define_maps! { <'tcx>
382382
[] fn output_filenames: output_filenames_node(CrateNum)
383383
-> Arc<OutputFilenames>,
384384

385-
[] fn has_copy_closures: HasCopyClosures(CrateNum) -> bool,
386-
[] fn has_clone_closures: HasCloneClosures(CrateNum) -> bool,
387-
388385
// Erases regions from `ty` to yield a new type.
389386
// Normally you would just use `tcx.erase_regions(&value)`,
390387
// however, which uses this query as a kind of cache.

src/librustc/ty/maps/plumbing.rs

-2
Original file line numberDiff line numberDiff line change
@@ -917,8 +917,6 @@ pub fn force_from_dep_node<'a, 'gcx, 'lcx>(tcx: TyCtxt<'a, 'gcx, 'lcx>,
917917
}
918918
DepKind::UsedCrateSource => { force!(used_crate_source, krate!()); }
919919
DepKind::PostorderCnums => { force!(postorder_cnums, LOCAL_CRATE); }
920-
DepKind::HasCloneClosures => { force!(has_clone_closures, krate!()); }
921-
DepKind::HasCopyClosures => { force!(has_copy_closures, krate!()); }
922920

923921
DepKind::Freevars => { force!(freevars, def_id!()); }
924922
DepKind::MaybeUnusedTraitImport => {

src/librustc_metadata/cstore.rs

-10
Original file line numberDiff line numberDiff line change
@@ -228,16 +228,6 @@ impl CrateMetadata {
228228
attr::contains_name(&attrs, "no_builtins")
229229
}
230230

231-
pub fn has_copy_closures(&self, sess: &Session) -> bool {
232-
let attrs = self.get_item_attrs(CRATE_DEF_INDEX, sess);
233-
attr::contains_feature_attr(&attrs, "copy_closures")
234-
}
235-
236-
pub fn has_clone_closures(&self, sess: &Session) -> bool {
237-
let attrs = self.get_item_attrs(CRATE_DEF_INDEX, sess);
238-
attr::contains_feature_attr(&attrs, "clone_closures")
239-
}
240-
241231
pub fn panic_strategy(&self) -> PanicStrategy {
242232
self.root.panic_strategy.clone()
243233
}

src/librustc_metadata/cstore_impl.rs

-3
Original file line numberDiff line numberDiff line change
@@ -256,9 +256,6 @@ provide! { <'tcx> tcx, def_id, other, cdata,
256256

257257
used_crate_source => { Lrc::new(cdata.source.clone()) }
258258

259-
has_copy_closures => { cdata.has_copy_closures(tcx.sess) }
260-
has_clone_closures => { cdata.has_clone_closures(tcx.sess) }
261-
262259
exported_symbols => {
263260
let cnum = cdata.cnum;
264261
assert!(cnum != LOCAL_CRATE);

src/librustc_typeck/lib.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,7 @@ This API is completely unstable and subject to change.
7676
#![feature(box_patterns)]
7777
#![feature(box_syntax)]
7878
#![feature(conservative_impl_trait)]
79-
#![feature(copy_closures, clone_closures)]
79+
#![cfg_attr(stage0, feature(copy_closures, clone_closures))]
8080
#![feature(crate_visibility_modifier)]
8181
#![feature(from_ref)]
8282
#![feature(match_default_bindings)]

src/libsyntax/feature_gate.rs

+3-23
Original file line numberDiff line numberDiff line change
@@ -391,10 +391,6 @@ declare_features! (
391391
// Future-proofing enums/structs with #[non_exhaustive] attribute (RFC 2008)
392392
(active, non_exhaustive, "1.22.0", Some(44109), None),
393393

394-
// Copy/Clone closures (RFC 2132)
395-
(active, clone_closures, "1.22.0", Some(44490), None),
396-
(active, copy_closures, "1.22.0", Some(44490), None),
397-
398394
// allow `'_` placeholder lifetimes
399395
(active, underscore_lifetimes, "1.22.0", Some(44524), None),
400396

@@ -556,6 +552,9 @@ declare_features! (
556552
(accepted, inclusive_range_syntax, "1.26.0", Some(28237), None),
557553
// allow `..=` in patterns (RFC 1192)
558554
(accepted, dotdoteq_in_patterns, "1.26.0", Some(28237), None),
555+
// Copy/Clone closures (RFC 2132)
556+
(accepted, clone_closures, "1.26.0", Some(44490), None),
557+
(accepted, copy_closures, "1.26.0", Some(44490), None),
559558
);
560559

561560
// If you change this, please modify src/doc/unstable-book as well. You must
@@ -1867,8 +1866,6 @@ pub fn get_features(span_handler: &Handler, krate_attrs: &[ast::Attribute],
18671866
struct FeatureChecker {
18681867
proc_macro: Option<Span>,
18691868
custom_attribute: Option<Span>,
1870-
copy_closures: Option<Span>,
1871-
clone_closures: Option<Span>,
18721869
}
18731870

18741871
impl FeatureChecker {
@@ -1884,14 +1881,6 @@ impl FeatureChecker {
18841881
if features.custom_attribute {
18851882
self.custom_attribute = self.custom_attribute.or(Some(span));
18861883
}
1887-
1888-
if features.copy_closures {
1889-
self.copy_closures = self.copy_closures.or(Some(span));
1890-
}
1891-
1892-
if features.clone_closures {
1893-
self.clone_closures = self.clone_closures.or(Some(span));
1894-
}
18951884
}
18961885

18971886
fn check(self, handler: &Handler) {
@@ -1903,15 +1892,6 @@ impl FeatureChecker {
19031892

19041893
FatalError.raise();
19051894
}
1906-
1907-
if let (Some(span), None) = (self.copy_closures, self.clone_closures) {
1908-
handler.struct_span_err(span, "`#![feature(copy_closures)]` can only be used with \
1909-
`#![feature(clone_closures)]`")
1910-
.span_note(span, "`#![feature(copy_closures)]` declared here")
1911-
.emit();
1912-
1913-
FatalError.raise();
1914-
}
19151895
}
19161896
}
19171897

src/test/compile-fail/not-clone-closure.rs

-2
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,6 @@
1010

1111
// Check that closures do not implement `Clone` if their environment is not `Clone`.
1212

13-
#![feature(clone_closures)]
14-
1513
struct S(i32);
1614

1715
fn main() {

src/test/compile-fail/not-copy-closure.rs

-3
Original file line numberDiff line numberDiff line change
@@ -10,9 +10,6 @@
1010

1111
// Check that closures do not implement `Copy` if their environment is not `Copy`.
1212

13-
#![feature(copy_closures)]
14-
#![feature(clone_closures)]
15-
1613
fn main() {
1714
let mut a = 5;
1815
let hello = || {

src/test/compile-fail/unboxed-closer-non-implicit-copyable.rs

-19
This file was deleted.

src/test/run-pass/clone-closure.rs

-2
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,6 @@
1010

1111
// Check that closures implement `Clone`.
1212

13-
#![feature(clone_closures)]
14-
1513
#[derive(Clone)]
1614
struct S(i32);
1715

src/test/run-pass/copy-closure.rs

-3
Original file line numberDiff line numberDiff line change
@@ -10,9 +10,6 @@
1010

1111
// Check that closures implement `Copy`.
1212

13-
#![feature(copy_closures)]
14-
#![feature(clone_closures)]
15-
1613
fn call<T, F: FnOnce() -> T>(f: F) -> T { f() }
1714

1815
fn main() {

src/test/ui/feature-gate-clone-closures.rs

-21
This file was deleted.

src/test/ui/feature-gate-clone-closures.stderr

-11
This file was deleted.

src/test/ui/feature-gate-copy-closures.rs

-19
This file was deleted.

src/test/ui/feature-gate-copy-closures.stderr

-13
This file was deleted.

src/test/ui/span/borrowck-call-is-borrow-issue-12224.rs

+4-1
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,10 @@ fn test6() {
5858

5959
fn test7() {
6060
fn foo<F>(_: F) where F: FnMut(Box<FnMut(isize)>, isize) {}
61-
let mut f = |g: Box<FnMut(isize)>, b: isize| {};
61+
let s = String::new(); // Capture to make f !Copy
62+
let mut f = move |g: Box<FnMut(isize)>, b: isize| {
63+
let _ = s.len();
64+
};
6265
f(Box::new(|a| {
6366
foo(f);
6467
//~^ ERROR cannot move `f` into closure because it is borrowed

src/test/ui/span/borrowck-call-is-borrow-issue-12224.stderr

+4-4
Original file line numberDiff line numberDiff line change
@@ -28,19 +28,19 @@ LL | f.f.call_mut(())
2828
| ^^^ cannot borrow as mutable
2929

3030
error[E0504]: cannot move `f` into closure because it is borrowed
31-
--> $DIR/borrowck-call-is-borrow-issue-12224.rs:63:13
31+
--> $DIR/borrowck-call-is-borrow-issue-12224.rs:66:13
3232
|
3333
LL | f(Box::new(|a| {
3434
| - borrow of `f` occurs here
3535
LL | foo(f);
3636
| ^ move into closure occurs here
3737

3838
error[E0507]: cannot move out of captured outer variable in an `FnMut` closure
39-
--> $DIR/borrowck-call-is-borrow-issue-12224.rs:63:13
39+
--> $DIR/borrowck-call-is-borrow-issue-12224.rs:66:13
4040
|
41-
LL | let mut f = |g: Box<FnMut(isize)>, b: isize| {};
41+
LL | let mut f = move |g: Box<FnMut(isize)>, b: isize| {
4242
| ----- captured outer variable
43-
LL | f(Box::new(|a| {
43+
...
4444
LL | foo(f);
4545
| ^ cannot move out of captured outer variable in an `FnMut` closure
4646

0 commit comments

Comments
 (0)