Skip to content

Commit 3d99330

Browse files
trait_goals: Some builtin traits
1 parent d14b648 commit 3d99330

File tree

3 files changed

+62
-1
lines changed

3 files changed

+62
-1
lines changed

compiler/rustc_trait_selection/src/solve/assembly.rs

+7
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,11 @@ pub(super) trait GoalKind<'tcx>: TypeFoldable<'tcx> + Copy {
8080
bound_sig: ty::PolyFnSig<'tcx>,
8181
tuple_arguments: TupleArgumentsFlag,
8282
);
83+
84+
fn consider_builtin_trait_candidates(
85+
acx: &mut AssemblyCtxt<'_, 'tcx, Self>,
86+
goal: Goal<'tcx, Self>,
87+
);
8388
}
8489

8590
/// An abstraction which correctly deals with the canonical results for candidates.
@@ -115,6 +120,8 @@ impl<'a, 'tcx, G: GoalKind<'tcx>> AssemblyCtxt<'a, 'tcx, G> {
115120

116121
acx.assemble_fn_like_candidates(goal);
117122

123+
G::consider_builtin_trait_candidates(&mut acx, goal);
124+
118125
acx.candidates
119126
}
120127

compiler/rustc_trait_selection/src/solve/project_goals.rs

+7
Original file line numberDiff line numberDiff line change
@@ -257,6 +257,13 @@ impl<'tcx> assembly::GoalKind<'tcx> for ProjectionPredicate<'tcx> {
257257
) {
258258
todo!()
259259
}
260+
261+
fn consider_builtin_trait_candidates(
262+
_acx: &mut AssemblyCtxt<'_, 'tcx, Self>,
263+
_goal: Goal<'tcx, Self>,
264+
) {
265+
todo!();
266+
}
260267
}
261268

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

compiler/rustc_trait_selection/src/solve/trait_goals.rs

+48-1
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,8 @@ use rustc_infer::traits::query::NoSolution;
1313
use rustc_infer::traits::util::supertraits;
1414
use rustc_infer::traits::ObligationCause;
1515
use rustc_middle::ty::fast_reject::{DeepRejectCtxt, TreatParams};
16-
use rustc_middle::ty::TraitPredicate;
1716
use rustc_middle::ty::{self, Ty, TyCtxt};
17+
use rustc_middle::ty::{TraitPredicate, TypeVisitable};
1818
use rustc_span::DUMMY_SP;
1919
use rustc_target::spec::abi::Abi;
2020

@@ -274,6 +274,53 @@ impl<'tcx> assembly::GoalKind<'tcx> for TraitPredicate<'tcx> {
274274
match_poly_trait_ref_against_goal(acx, goal, found_trait_ref, CandidateSource::Fn);
275275
})
276276
}
277+
278+
fn consider_builtin_trait_candidates(
279+
acx: &mut AssemblyCtxt<'_, 'tcx, Self>,
280+
goal: Goal<'tcx, Self>,
281+
) {
282+
let lang_items = acx.cx.tcx.lang_items();
283+
let trait_def_id = goal.predicate.def_id();
284+
let self_ty = goal.predicate.self_ty();
285+
286+
if Some(trait_def_id) == lang_items.sized_trait() {
287+
if self_ty.is_trivially_sized(acx.cx.tcx) {
288+
acx.try_insert_candidate(CandidateSource::Builtin, Certainty::Yes);
289+
}
290+
} else if Some(trait_def_id) == lang_items.copy_trait()
291+
|| Some(trait_def_id) == lang_items.clone_trait()
292+
{
293+
// FIXME
294+
} else if Some(trait_def_id) == lang_items.discriminant_kind_trait()
295+
|| Some(trait_def_id) == lang_items.pointee_trait()
296+
{
297+
// `Pointee` and `DiscriminantKind` are implemented by all traits unconditionally
298+
acx.try_insert_candidate(CandidateSource::Builtin, Certainty::Yes);
299+
} else if Some(trait_def_id) == lang_items.tuple_trait() {
300+
match *self_ty.kind() {
301+
ty::Infer(ty::TyVar(_)) => todo!("ambiguous"),
302+
ty::Tuple(_) => acx.try_insert_candidate(CandidateSource::Builtin, Certainty::Yes),
303+
_ => {}
304+
}
305+
} else if Some(trait_def_id) == lang_items.pointer_sized() {
306+
let erased_self_ty = acx.cx.tcx.erase_regions(self_ty);
307+
if erased_self_ty.has_non_region_infer() {
308+
todo!("ambiguous")
309+
}
310+
let usize_layout =
311+
acx.cx.tcx.layout_of(ty::ParamEnv::empty().and(acx.cx.tcx.types.usize)).unwrap();
312+
if let Ok(layout) = acx.cx.tcx.layout_of(goal.param_env.and(self_ty))
313+
&& layout.layout.size() == usize_layout.layout.size()
314+
&& layout.layout.align().abi == usize_layout.layout.align().abi
315+
{
316+
acx.try_insert_candidate(CandidateSource::Builtin, Certainty::Yes);
317+
}
318+
} else if Some(trait_def_id) == lang_items.coerce_unsized_trait()
319+
|| Some(trait_def_id) == lang_items.unsize_trait()
320+
{
321+
// FIXME
322+
}
323+
}
277324
}
278325

279326
fn match_poly_trait_ref_against_goal<'tcx>(

0 commit comments

Comments
 (0)