-
Notifications
You must be signed in to change notification settings - Fork 13.9k
Add built-in const impls for Clone and Copy
#147866
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
fee1-dead
wants to merge
1
commit into
rust-lang:master
Choose a base branch
from
fee1-dead-contrib:constclonebuiltin
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+133
−8
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -8,7 +8,7 @@ use rustc_middle::span_bug; | |
| use rustc_middle::traits::query::NoSolution; | ||
| use rustc_middle::ty::elaborate::elaborate; | ||
| use rustc_middle::ty::fast_reject::DeepRejectCtxt; | ||
| use rustc_middle::ty::{self, TypingMode}; | ||
| use rustc_middle::ty::{self, Ty, TypingMode}; | ||
| use thin_vec::{ThinVec, thin_vec}; | ||
|
|
||
| use super::SelectionContext; | ||
|
|
@@ -303,6 +303,9 @@ fn evaluate_host_effect_from_builtin_impls<'tcx>( | |
| obligation: &HostEffectObligation<'tcx>, | ||
| ) -> Result<ThinVec<PredicateObligation<'tcx>>, EvaluationFailure> { | ||
| match selcx.tcx().as_lang_item(obligation.predicate.def_id()) { | ||
| Some(LangItem::Copy | LangItem::Clone) => { | ||
| evaluate_host_effect_for_copy_clone_goal(selcx, obligation) | ||
| } | ||
| Some(LangItem::Destruct) => evaluate_host_effect_for_destruct_goal(selcx, obligation), | ||
| Some(LangItem::Fn | LangItem::FnMut | LangItem::FnOnce) => { | ||
| evaluate_host_effect_for_fn_goal(selcx, obligation) | ||
|
|
@@ -311,6 +314,95 @@ fn evaluate_host_effect_from_builtin_impls<'tcx>( | |
| } | ||
| } | ||
|
|
||
| fn evaluate_host_effect_for_copy_clone_goal<'tcx>( | ||
| selcx: &mut SelectionContext<'_, 'tcx>, | ||
| obligation: &HostEffectObligation<'tcx>, | ||
| ) -> Result<ThinVec<PredicateObligation<'tcx>>, EvaluationFailure> { | ||
| let tcx = selcx.tcx(); | ||
| let self_ty = obligation.predicate.self_ty(); | ||
| let constituent_tys = match *self_ty.kind() { | ||
| // impl Copy/Clone for FnDef, FnPtr | ||
| ty::FnDef(..) | ty::FnPtr(..) | ty::Error(_) => Ok(ty::Binder::dummy(vec![])), | ||
|
|
||
| // Implementations are provided in core | ||
| ty::Uint(_) | ||
| | ty::Int(_) | ||
| | ty::Infer(ty::IntVar(_) | ty::FloatVar(_)) | ||
| | ty::Bool | ||
| | ty::Float(_) | ||
| | ty::Char | ||
| | ty::RawPtr(..) | ||
| | ty::Never | ||
| | ty::Ref(_, _, ty::Mutability::Not) | ||
| | ty::Array(..) => Err(EvaluationFailure::NoSolution), | ||
|
|
||
| // Cannot implement in core, as we can't be generic over patterns yet, | ||
| // so we'd have to list all patterns and type combinations. | ||
| ty::Pat(ty, ..) => Ok(ty::Binder::dummy(vec![ty])), | ||
|
|
||
| ty::Dynamic(..) | ||
| | ty::Str | ||
| | ty::Slice(_) | ||
| | ty::Foreign(..) | ||
| | ty::Ref(_, _, ty::Mutability::Mut) | ||
| | ty::Adt(_, _) | ||
| | ty::Alias(_, _) | ||
| | ty::Param(_) | ||
| | ty::Placeholder(..) => Err(EvaluationFailure::NoSolution), | ||
|
|
||
| ty::Bound(..) | ||
| | ty::Infer(ty::TyVar(_) | ty::FreshTy(_) | ty::FreshIntTy(_) | ty::FreshFloatTy(_)) => { | ||
| panic!("unexpected type `{self_ty:?}`") | ||
| } | ||
|
|
||
| // impl Copy/Clone for (T1, T2, .., Tn) where T1: Copy/Clone, T2: Copy/Clone, .. Tn: Copy/Clone | ||
| ty::Tuple(tys) => Ok(ty::Binder::dummy(tys.to_vec())), | ||
|
|
||
| // impl Copy/Clone for Closure where Self::TupledUpvars: Copy/Clone | ||
| ty::Closure(_, args) => Ok(ty::Binder::dummy(vec![args.as_closure().tupled_upvars_ty()])), | ||
|
|
||
| // impl Copy/Clone for CoroutineClosure where Self::TupledUpvars: Copy/Clone | ||
| ty::CoroutineClosure(_, args) => { | ||
| Ok(ty::Binder::dummy(vec![args.as_coroutine_closure().tupled_upvars_ty()])) | ||
| } | ||
|
|
||
| // only when `coroutine_clone` is enabled and the coroutine is movable | ||
| // impl Copy/Clone for Coroutine where T: Copy/Clone forall T in (upvars, witnesses) | ||
| ty::Coroutine(def_id, args) => match tcx.coroutine_movability(def_id) { | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. need to check rn checking |
||
| ty::Movability::Static => Err(EvaluationFailure::NoSolution), | ||
| ty::Movability::Movable => { | ||
| if tcx.features().coroutine_clone() { | ||
| Ok(ty::Binder::dummy(vec![ | ||
| args.as_coroutine().tupled_upvars_ty(), | ||
| Ty::new_coroutine_witness_for_coroutine(tcx, def_id, args), | ||
| ])) | ||
| } else { | ||
| Err(EvaluationFailure::NoSolution) | ||
| } | ||
| } | ||
| }, | ||
|
|
||
| ty::UnsafeBinder(_) => Err(EvaluationFailure::NoSolution), | ||
|
|
||
| // impl Copy/Clone for CoroutineWitness where T: Copy/Clone forall T in coroutine_hidden_types | ||
| ty::CoroutineWitness(def_id, args) => Ok(tcx | ||
| .coroutine_hidden_types(def_id) | ||
| .instantiate(tcx, args) | ||
| .map_bound(|bound| bound.types.to_vec())), | ||
| }?; | ||
|
|
||
| Ok(constituent_tys | ||
| .iter() | ||
| .map(|ty| { | ||
| obligation.with( | ||
| tcx, | ||
| ty.map_bound(|ty| ty::TraitRef::new(tcx, obligation.predicate.def_id(), [ty])) | ||
| .to_host_effect_clause(tcx, obligation.predicate.constness), | ||
| ) | ||
| }) | ||
| .collect()) | ||
| } | ||
|
|
||
| // NOTE: Keep this in sync with `const_conditions_for_destruct` in the new solver. | ||
| fn evaluate_host_effect_for_destruct_goal<'tcx>( | ||
| selcx: &mut SelectionContext<'_, 'tcx>, | ||
|
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -121,7 +121,7 @@ impl<I: Interner> ty::Binder<I, TraitRef<I>> { | |
| } | ||
|
|
||
| pub fn to_host_effect_clause(self, cx: I, constness: BoundConstness) -> I::Clause { | ||
| self.map_bound(|trait_ref| { | ||
| self.map_bound(|trait_ref: TraitRef<I>| { | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. why? |
||
| ty::ClauseKind::HostEffect(HostEffectPredicate { trait_ref, constness }) | ||
| }) | ||
| .upcast(cx) | ||
|
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,4 +1,4 @@ | ||
| //@ run-pass | ||
| //@ check-pass | ||
| #![feature(const_trait_impl, const_default)] | ||
| #![allow(dead_code)] | ||
| // alloc::string | ||
|
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
use
SelectionContext::copy_clone_conditionsinstead of duplicating the match here?We do the same in the new solver, so I'd expect either duplicating this match in both solvers or in none of them.
I guess the issue is that
copy_clone_conditionsonly expects self types for which assembly succeeded?