Skip to content

Commit 297a6e5

Browse files
committed
Rollup merge of #49299 - SimonSapin:ubiquity, r=nikomatsakis
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. Tracking issue: #44490
2 parents 5454451 + 1efe0b3 commit 297a6e5

24 files changed

+26
-171
lines changed

src/libcore/clone.rs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,11 @@
6363
/// This trait can be used with `#[derive]` if all fields are `Clone`. The `derive`d
6464
/// implementation of [`clone`] calls [`clone`] on each field.
6565
///
66+
/// ## Closures
67+
///
68+
/// Closure types automatically implement `Clone` if they capture no value from the environment
69+
/// or if all such captured values implement `Clone` themselves.
70+
///
6671
/// ## How can I implement `Clone`?
6772
///
6873
/// Types that are [`Copy`] should have a trivial implementation of `Clone`. More formally:

src/libcore/marker.rs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -166,6 +166,11 @@ pub trait Unsize<T: ?Sized> {
166166
/// are allowed to access `x` after the assignment. Under the hood, both a copy and a move
167167
/// can result in bits being copied in memory, although this is sometimes optimized away.
168168
///
169+
/// ## Closures
170+
///
171+
/// Closure types automatically implement `Copy` if they capture no value from the environment
172+
/// or if all such captured values implement `Copy` themselves.
173+
///
169174
/// ## How can I implement `Copy`?
170175
///
171176
/// There are two ways to implement `Copy` on your type. The simplest is to use `derive`:

src/librustc/dep_graph/dep_node.rs

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -617,8 +617,6 @@ define_dep_nodes!( <'tcx>
617617
[input] MissingExternCrateItem(CrateNum),
618618
[input] UsedCrateSource(CrateNum),
619619
[input] PostorderCnums,
620-
[] HasCloneClosures(CrateNum),
621-
[] HasCopyClosures(CrateNum),
622620

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

src/librustc/lib.rs

Lines changed: 1 addition & 1 deletion
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

Lines changed: 3 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2086,14 +2086,9 @@ 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);
2095-
2096-
if copy_closures || clone_closures {
2089+
let is_copy_trait = Some(trait_id) == self.tcx().lang_items().copy_trait();
2090+
let is_clone_trait = Some(trait_id) == self.tcx().lang_items().clone_trait();
2091+
if is_copy_trait || is_clone_trait {
20972092
Where(ty::Binder(substs.upvar_tys(def_id, self.tcx()).collect()))
20982093
} else {
20992094
Never

src/librustc/ty/context.rs

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2562,14 +2562,6 @@ pub fn provide(providers: &mut ty::maps::Providers) {
25622562
assert_eq!(cnum, LOCAL_CRATE);
25632563
tcx.output_filenames.clone()
25642564
};
2565-
providers.has_copy_closures = |tcx, cnum| {
2566-
assert_eq!(cnum, LOCAL_CRATE);
2567-
tcx.features().copy_closures
2568-
};
2569-
providers.has_clone_closures = |tcx, cnum| {
2570-
assert_eq!(cnum, LOCAL_CRATE);
2571-
tcx.features().clone_closures
2572-
};
25732565
providers.features_query = |tcx, cnum| {
25742566
assert_eq!(cnum, LOCAL_CRATE);
25752567
Lrc::new(tcx.sess.features_untracked().clone())

src/librustc/ty/maps/config.rs

Lines changed: 0 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -610,24 +610,12 @@ impl<'tcx> QueryDescription<'tcx> for queries::output_filenames<'tcx> {
610610
}
611611
}
612612

613-
impl<'tcx> QueryDescription<'tcx> for queries::has_clone_closures<'tcx> {
614-
fn describe(_tcx: TyCtxt, _: CrateNum) -> String {
615-
format!("seeing if the crate has enabled `Clone` closures")
616-
}
617-
}
618-
619613
impl<'tcx> QueryDescription<'tcx> for queries::vtable_methods<'tcx> {
620614
fn describe(tcx: TyCtxt, key: ty::PolyTraitRef<'tcx> ) -> String {
621615
format!("finding all methods for trait {}", tcx.item_path_str(key.def_id()))
622616
}
623617
}
624618

625-
impl<'tcx> QueryDescription<'tcx> for queries::has_copy_closures<'tcx> {
626-
fn describe(_tcx: TyCtxt, _: CrateNum) -> String {
627-
format!("seeing if the crate has enabled `Copy` closures")
628-
}
629-
}
630-
631619
impl<'tcx> QueryDescription<'tcx> for queries::features_query<'tcx> {
632620
fn describe(_tcx: TyCtxt, _: CrateNum) -> String {
633621
format!("looking up enabled feature gates")

src/librustc/ty/maps/mod.rs

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -387,9 +387,6 @@ define_maps! { <'tcx>
387387
[] fn output_filenames: output_filenames_node(CrateNum)
388388
-> Arc<OutputFilenames>,
389389

390-
[] fn has_copy_closures: HasCopyClosures(CrateNum) -> bool,
391-
[] fn has_clone_closures: HasCloneClosures(CrateNum) -> bool,
392-
393390
// Erases regions from `ty` to yield a new type.
394391
// Normally you would just use `tcx.erase_regions(&value)`,
395392
// however, which uses this query as a kind of cache.

src/librustc/ty/maps/plumbing.rs

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -920,8 +920,6 @@ pub fn force_from_dep_node<'a, 'gcx, 'lcx>(tcx: TyCtxt<'a, 'gcx, 'lcx>,
920920
}
921921
DepKind::UsedCrateSource => { force!(used_crate_source, krate!()); }
922922
DepKind::PostorderCnums => { force!(postorder_cnums, LOCAL_CRATE); }
923-
DepKind::HasCloneClosures => { force!(has_clone_closures, krate!()); }
924-
DepKind::HasCopyClosures => { force!(has_copy_closures, krate!()); }
925923

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

src/librustc_metadata/cstore.rs

Lines changed: 0 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -226,16 +226,6 @@ impl CrateMetadata {
226226
attr::contains_name(&attrs, "no_builtins")
227227
}
228228

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

0 commit comments

Comments
 (0)