mirror of
https://github.com/rust-lang/rust.git
synced 2026-04-27 18:57:42 +03:00
automatically implement TrivialClone for closures and tuples
If each of the component types is `TrivialClone`, the closure/tuple itself can be trivially cloned.
This commit is contained in:
@@ -176,6 +176,7 @@ pub fn extract(attrs: &[impl AttributeExt]) -> Option<(Symbol, Span)> {
|
||||
Clone, sym::clone, clone_trait, Target::Trait, GenericRequirement::None;
|
||||
CloneFn, sym::clone_fn, clone_fn, Target::Method(MethodKind::Trait { body: false }), GenericRequirement::None;
|
||||
UseCloned, sym::use_cloned, use_cloned_trait, Target::Trait, GenericRequirement::None;
|
||||
TrivialClone, sym::trivial_clone, trivial_clone_trait, Target::Trait, GenericRequirement::None;
|
||||
Sync, sym::sync, sync_trait, Target::Trait, GenericRequirement::Exact(0);
|
||||
DiscriminantKind, sym::discriminant_kind, discriminant_kind_trait, Target::Trait, GenericRequirement::None;
|
||||
/// The associated item of the `DiscriminantKind` trait.
|
||||
|
||||
@@ -856,6 +856,7 @@ fn $to_solver(lang_item: LangItem) -> Option<$solver_ty> {
|
||||
PointeeTrait,
|
||||
Sized,
|
||||
TransmuteTrait,
|
||||
TrivialClone,
|
||||
Tuple,
|
||||
Unpin,
|
||||
Unsize,
|
||||
|
||||
@@ -547,9 +547,11 @@ fn assemble_builtin_impl_candidates<G: GoalKind<D>>(
|
||||
Some(SolverTraitLangItem::PointeeSized) => {
|
||||
unreachable!("`PointeeSized` is removed during lowering");
|
||||
}
|
||||
Some(SolverTraitLangItem::Copy | SolverTraitLangItem::Clone) => {
|
||||
G::consider_builtin_copy_clone_candidate(self, goal)
|
||||
}
|
||||
Some(
|
||||
SolverTraitLangItem::Copy
|
||||
| SolverTraitLangItem::Clone
|
||||
| SolverTraitLangItem::TrivialClone,
|
||||
) => G::consider_builtin_copy_clone_candidate(self, goal),
|
||||
Some(SolverTraitLangItem::Fn) => {
|
||||
G::consider_builtin_fn_trait_candidates(self, goal, ty::ClosureKind::Fn)
|
||||
}
|
||||
|
||||
@@ -2271,6 +2271,7 @@
|
||||
transparent_enums,
|
||||
transparent_unions,
|
||||
trivial_bounds,
|
||||
trivial_clone,
|
||||
truncf16,
|
||||
truncf32,
|
||||
truncf64,
|
||||
|
||||
@@ -67,7 +67,7 @@ pub(super) fn assemble_candidates<'o>(
|
||||
|
||||
let lang_item = tcx.as_lang_item(def_id);
|
||||
match lang_item {
|
||||
Some(LangItem::Copy | LangItem::Clone) => {
|
||||
Some(LangItem::Copy | LangItem::Clone | LangItem::TrivialClone) => {
|
||||
debug!(obligation_self_ty = ?obligation.predicate.skip_binder().self_ty());
|
||||
|
||||
// User-defined copy impls are permitted, but only for
|
||||
|
||||
@@ -249,7 +249,9 @@ fn confirm_builtin_candidate(
|
||||
Some(LangItem::PointeeSized) => {
|
||||
bug!("`PointeeSized` is removing during lowering");
|
||||
}
|
||||
Some(LangItem::Copy | LangItem::Clone) => self.copy_clone_conditions(self_ty),
|
||||
Some(LangItem::Copy | LangItem::Clone | LangItem::TrivialClone) => {
|
||||
self.copy_clone_conditions(self_ty)
|
||||
}
|
||||
Some(LangItem::FusedIterator) => {
|
||||
if self.coroutine_is_gen(self_ty) {
|
||||
ty::Binder::dummy(vec![])
|
||||
|
||||
@@ -48,6 +48,7 @@ pub enum SolverTraitLangItem {
|
||||
PointeeTrait,
|
||||
Sized,
|
||||
TransmuteTrait,
|
||||
TrivialClone,
|
||||
Tuple,
|
||||
Unpin,
|
||||
Unsize,
|
||||
|
||||
@@ -269,6 +269,7 @@ fn clone_from(&mut self, source: &Self)
|
||||
issue = "none"
|
||||
)]
|
||||
#[rustc_const_unstable(feature = "const_clone", issue = "142757")]
|
||||
#[lang = "trivial_clone"]
|
||||
// SAFETY:
|
||||
// It is sound to specialize on this because the `clone` implementation cannot be
|
||||
// lifetime-dependent. Therefore, if `TrivialClone` is implemented for any lifetime,
|
||||
|
||||
@@ -0,0 +1,13 @@
|
||||
//@ run-pass
|
||||
// Check that closures implement `TrivialClone`.
|
||||
|
||||
#![feature(trivial_clone)]
|
||||
|
||||
use std::clone::TrivialClone;
|
||||
|
||||
fn require_trivial_clone<T: TrivialClone>(_t: T) {}
|
||||
|
||||
fn main() {
|
||||
let some_trivial_clone_value = 42i32;
|
||||
require_trivial_clone(move || some_trivial_clone_value);
|
||||
}
|
||||
Reference in New Issue
Block a user