Skip to content

Commit f4f57bf

Browse files
committed
Make Clone::clone a lang item
1 parent 92c6c03 commit f4f57bf

File tree

4 files changed

+7
-2
lines changed

4 files changed

+7
-2
lines changed

compiler/rustc_hir/src/lang_items.rs

+1
Original file line numberDiff line numberDiff line change
@@ -162,6 +162,7 @@ language_item_table! {
162162
StructuralPeq, sym::structural_peq, structural_peq_trait, Target::Trait, GenericRequirement::None;
163163
Copy, sym::copy, copy_trait, Target::Trait, GenericRequirement::Exact(0);
164164
Clone, sym::clone, clone_trait, Target::Trait, GenericRequirement::None;
165+
CloneFn, sym::clone_fn, clone_fn, Target::Method(MethodKind::Trait { body: false }), GenericRequirement::None;
165166
Sync, sym::sync, sync_trait, Target::Trait, GenericRequirement::Exact(0);
166167
DiscriminantKind, sym::discriminant_kind, discriminant_kind_trait, Target::Trait, GenericRequirement::None;
167168
/// The associated item of the `DiscriminantKind` trait.

compiler/rustc_mir_transform/src/instsimplify.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
use crate::simplify::simplify_duplicate_switch_targets;
44
use crate::take_array;
55
use rustc_ast::attr;
6+
use rustc_hir::LangItem;
67
use rustc_middle::bug;
78
use rustc_middle::mir::*;
89
use rustc_middle::ty::layout;
@@ -271,8 +272,7 @@ impl<'tcx> InstSimplifyContext<'tcx, '_> {
271272
return;
272273
}
273274

274-
let trait_def_id = self.tcx.trait_of_item(fn_def_id);
275-
if trait_def_id.is_none() || trait_def_id != self.tcx.lang_items().clone_trait() {
275+
if !self.tcx.is_lang_item(fn_def_id, LangItem::CloneFn) {
276276
return;
277277
}
278278

compiler/rustc_span/src/symbol.rs

+1
Original file line numberDiff line numberDiff line change
@@ -557,6 +557,7 @@ symbols! {
557557
clobber_abi,
558558
clone,
559559
clone_closures,
560+
clone_fn,
560561
clone_from,
561562
closure,
562563
closure_lifetime_binder,

library/core/src/clone.rs

+3
Original file line numberDiff line numberDiff line change
@@ -160,6 +160,9 @@ pub trait Clone: Sized {
160160
/// ```
161161
#[stable(feature = "rust1", since = "1.0.0")]
162162
#[must_use = "cloning is often expensive and is not expected to have side effects"]
163+
// Clone::clone is special because the compiler generates MIR to implement it for some types.
164+
// See InstanceKind::CloneShim.
165+
#[cfg_attr(not(bootstrap), lang = "clone_fn")]
163166
fn clone(&self) -> Self;
164167

165168
/// Performs copy-assignment from `source`.

0 commit comments

Comments
 (0)