Skip to content

Commit 685c32f

Browse files
Sized, Copy/Clone
1 parent 45aa5c0 commit 685c32f

File tree

3 files changed

+151
-3
lines changed

3 files changed

+151
-3
lines changed

compiler/rustc_trait_selection/src/solve/assembly.rs

+10
Original file line numberDiff line numberDiff line change
@@ -112,7 +112,13 @@ pub(super) trait GoalKind<'tcx>: TypeFoldable<'tcx> + Copy {
112112
ecx: &mut EvalCtxt<'_, 'tcx>,
113113
goal: Goal<'tcx, Self>,
114114
) -> QueryResult<'tcx>;
115+
116+
fn consider_builtin_copy_clone_candidate(
117+
ecx: &mut EvalCtxt<'_, 'tcx>,
118+
goal: Goal<'tcx, Self>,
119+
) -> QueryResult<'tcx>;
115120
}
121+
116122
impl<'tcx> EvalCtxt<'_, 'tcx> {
117123
pub(super) fn assemble_and_evaluate_candidates<G: GoalKind<'tcx>>(
118124
&mut self,
@@ -214,6 +220,10 @@ impl<'tcx> EvalCtxt<'_, 'tcx> {
214220
G::consider_trait_alias_candidate(self, goal)
215221
} else if lang_items.sized_trait() == Some(trait_def_id) {
216222
G::consider_builtin_sized_candidate(self, goal)
223+
} else if lang_items.copy_trait() == Some(trait_def_id)
224+
|| lang_items.clone_trait() == Some(trait_def_id)
225+
{
226+
G::consider_builtin_copy_clone_candidate(self, goal)
217227
} else {
218228
Err(NoSolution)
219229
};

compiler/rustc_trait_selection/src/solve/project_goals.rs

+7
Original file line numberDiff line numberDiff line change
@@ -347,6 +347,13 @@ impl<'tcx> assembly::GoalKind<'tcx> for ProjectionPredicate<'tcx> {
347347
) -> QueryResult<'tcx> {
348348
bug!("`Sized` does not have an associated type: {:?}", goal);
349349
}
350+
351+
fn consider_builtin_copy_clone_candidate(
352+
_ecx: &mut EvalCtxt<'_, 'tcx>,
353+
goal: Goal<'tcx, Self>,
354+
) -> QueryResult<'tcx> {
355+
bug!("`Copy`/`Clone` does not have an associated type: {:?}", goal);
356+
}
350357
}
351358

352359
/// This behavior is also implemented in `rustc_ty_utils` and in the old `project` code.

compiler/rustc_trait_selection/src/solve/trait_goals.rs

+134-3
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ use super::assembly::{self, Candidate, CandidateSource};
66
use super::infcx_ext::InferCtxtExt;
77
use super::{EvalCtxt, Goal, QueryResult};
88
use rustc_hir::def_id::DefId;
9+
use rustc_hir::{Movability, Mutability};
910
use rustc_infer::infer::InferCtxt;
1011
use rustc_infer::traits::query::NoSolution;
1112
use rustc_middle::ty::fast_reject::{DeepRejectCtxt, TreatParams};
@@ -106,10 +107,27 @@ impl<'tcx> assembly::GoalKind<'tcx> for TraitPredicate<'tcx> {
106107
}
107108

108109
fn consider_builtin_sized_candidate(
109-
_ecx: &mut EvalCtxt<'_, 'tcx>,
110-
_goal: Goal<'tcx, Self>,
110+
ecx: &mut EvalCtxt<'_, 'tcx>,
111+
goal: Goal<'tcx, Self>,
111112
) -> QueryResult<'tcx> {
112-
unimplemented!();
113+
ecx.infcx.probe(|_| {
114+
let constituent_tys =
115+
instantiate_constituent_tys_for_sized_trait(ecx.infcx, goal.predicate.self_ty())?;
116+
ecx.evaluate_goal_for_constituent_tys_and_make_canonical_response(goal, constituent_tys)
117+
})
118+
}
119+
120+
fn consider_builtin_copy_clone_candidate(
121+
ecx: &mut EvalCtxt<'_, 'tcx>,
122+
goal: Goal<'tcx, Self>,
123+
) -> QueryResult<'tcx> {
124+
ecx.infcx.probe(|_| {
125+
let constituent_tys = instantiate_constituent_tys_for_copy_clone_trait(
126+
ecx.infcx,
127+
goal.predicate.self_ty(),
128+
)?;
129+
ecx.evaluate_goal_for_constituent_tys_and_make_canonical_response(goal, constituent_tys)
130+
})
113131
}
114132
}
115133

@@ -278,3 +296,116 @@ fn instantiate_constituent_tys_for_auto_trait<'tcx>(
278296
}
279297
}
280298
}
299+
300+
fn instantiate_constituent_tys_for_sized_trait<'tcx>(
301+
infcx: &InferCtxt<'tcx>,
302+
ty: Ty<'tcx>,
303+
) -> Result<Vec<Ty<'tcx>>, NoSolution> {
304+
match *ty.kind() {
305+
ty::Infer(ty::IntVar(_) | ty::FloatVar(_))
306+
| ty::Uint(_)
307+
| ty::Int(_)
308+
| ty::Bool
309+
| ty::Float(_)
310+
| ty::FnDef(..)
311+
| ty::FnPtr(_)
312+
| ty::RawPtr(..)
313+
| ty::Char
314+
| ty::Ref(..)
315+
| ty::Generator(..)
316+
| ty::GeneratorWitness(..)
317+
| ty::Array(..)
318+
| ty::Closure(..)
319+
| ty::Never
320+
| ty::Dynamic(_, _, ty::DynStar)
321+
| ty::Error(_) => Ok(vec![]),
322+
323+
ty::Str
324+
| ty::Slice(_)
325+
| ty::Dynamic(..)
326+
| ty::Foreign(..)
327+
| ty::Alias(..)
328+
| ty::Param(_) => Err(NoSolution),
329+
330+
ty::Infer(ty::TyVar(_)) => bug!("FIXME: ambiguous"),
331+
332+
ty::Placeholder(..)
333+
| ty::Bound(..)
334+
| ty::Infer(ty::FreshTy(_) | ty::FreshIntTy(_) | ty::FreshFloatTy(_)) => bug!(),
335+
336+
ty::Tuple(tys) => Ok(tys.to_vec()),
337+
338+
ty::Adt(def, substs) => {
339+
let sized_crit = def.sized_constraint(infcx.tcx);
340+
Ok(sized_crit
341+
.0
342+
.iter()
343+
.map(|ty| sized_crit.rebind(*ty).subst(infcx.tcx, substs))
344+
.collect())
345+
}
346+
}
347+
}
348+
349+
fn instantiate_constituent_tys_for_copy_clone_trait<'tcx>(
350+
infcx: &InferCtxt<'tcx>,
351+
ty: Ty<'tcx>,
352+
) -> Result<Vec<Ty<'tcx>>, NoSolution> {
353+
match *ty.kind() {
354+
ty::Infer(ty::IntVar(_) | ty::FloatVar(_))
355+
| ty::FnDef(..)
356+
| ty::FnPtr(_)
357+
| ty::Error(_) => Ok(vec![]),
358+
359+
// Implementations are provided in core
360+
ty::Uint(_)
361+
| ty::Int(_)
362+
| ty::Bool
363+
| ty::Float(_)
364+
| ty::Char
365+
| ty::RawPtr(..)
366+
| ty::Never
367+
| ty::Ref(_, _, Mutability::Not)
368+
| ty::Array(..) => Err(NoSolution),
369+
370+
ty::Dynamic(..)
371+
| ty::Str
372+
| ty::Slice(_)
373+
| ty::Generator(_, _, Movability::Static)
374+
| ty::Foreign(..)
375+
| ty::Ref(_, _, Mutability::Mut)
376+
| ty::Adt(_, _)
377+
| ty::Alias(_, _)
378+
| ty::Param(_) => Err(NoSolution),
379+
380+
ty::Infer(ty::TyVar(_)) => bug!("FIXME: ambiguous"),
381+
382+
ty::Placeholder(..)
383+
| ty::Bound(..)
384+
| ty::Infer(ty::FreshTy(_) | ty::FreshIntTy(_) | ty::FreshFloatTy(_)) => bug!(),
385+
386+
ty::Tuple(tys) => Ok(tys.to_vec()),
387+
388+
ty::Closure(_, substs) => match *substs.as_closure().tupled_upvars_ty().kind() {
389+
ty::Tuple(tys) => Ok(tys.to_vec()),
390+
ty::Infer(ty::TyVar(_)) => bug!("FIXME: ambiguous"),
391+
_ => bug!(),
392+
},
393+
394+
ty::Generator(_, substs, Movability::Movable) => {
395+
if infcx.tcx.features().generator_clone {
396+
let generator = substs.as_generator();
397+
match *generator.tupled_upvars_ty().kind() {
398+
ty::Tuple(tys) => Ok(tys.iter().chain([generator.witness()]).collect()),
399+
ty::Infer(ty::TyVar(_)) => bug!("FIXME: ambiguous"),
400+
_ => bug!(),
401+
}
402+
} else {
403+
Err(NoSolution)
404+
}
405+
}
406+
407+
ty::GeneratorWitness(types) => {
408+
Ok(infcx.replace_bound_vars_with_placeholders(types).to_vec())
409+
}
410+
}
411+
}

0 commit comments

Comments
 (0)