Skip to content

Commit 45aa5c0

Browse files
Auto and alias traits
1 parent 3d87a8e commit 45aa5c0

File tree

4 files changed

+169
-21
lines changed

4 files changed

+169
-21
lines changed

compiler/rustc_middle/src/ty/mod.rs

+5
Original file line numberDiff line numberDiff line change
@@ -2382,6 +2382,11 @@ impl<'tcx> TyCtxt<'tcx> {
23822382
self.trait_def(trait_def_id).has_auto_impl
23832383
}
23842384

2385+
/// Returns `true` if this is a trait alias.
2386+
pub fn trait_is_alias(self, trait_def_id: DefId) -> bool {
2387+
self.def_kind(trait_def_id) == DefKind::TraitAlias
2388+
}
2389+
23852390
pub fn trait_is_coinductive(self, trait_def_id: DefId) -> bool {
23862391
self.trait_is_auto(trait_def_id) || self.lang_items().sized_trait() == Some(trait_def_id)
23872392
}

compiler/rustc_trait_selection/src/solve/assembly.rs

+19-6
Original file line numberDiff line numberDiff line change
@@ -92,15 +92,25 @@ pub(super) trait GoalKind<'tcx>: TypeFoldable<'tcx> + Copy {
9292
impl_def_id: DefId,
9393
) -> QueryResult<'tcx>;
9494

95-
fn consider_builtin_sized_candidate(
95+
fn consider_assumption(
9696
ecx: &mut EvalCtxt<'_, 'tcx>,
9797
goal: Goal<'tcx, Self>,
98+
assumption: ty::Predicate<'tcx>,
9899
) -> QueryResult<'tcx>;
99100

100-
fn consider_assumption(
101+
fn consider_auto_trait_candidate(
102+
ecx: &mut EvalCtxt<'_, 'tcx>,
103+
goal: Goal<'tcx, Self>,
104+
) -> QueryResult<'tcx>;
105+
106+
fn consider_trait_alias_candidate(
107+
ecx: &mut EvalCtxt<'_, 'tcx>,
108+
goal: Goal<'tcx, Self>,
109+
) -> QueryResult<'tcx>;
110+
111+
fn consider_builtin_sized_candidate(
101112
ecx: &mut EvalCtxt<'_, 'tcx>,
102113
goal: Goal<'tcx, Self>,
103-
assumption: ty::Predicate<'tcx>,
104114
) -> QueryResult<'tcx>;
105115
}
106116
impl<'tcx> EvalCtxt<'_, 'tcx> {
@@ -198,7 +208,11 @@ impl<'tcx> EvalCtxt<'_, 'tcx> {
198208
) {
199209
let lang_items = self.tcx().lang_items();
200210
let trait_def_id = goal.predicate.trait_def_id(self.tcx());
201-
let result = if lang_items.sized_trait() == Some(trait_def_id) {
211+
let result = if self.tcx().trait_is_auto(trait_def_id) {
212+
G::consider_auto_trait_candidate(self, goal)
213+
} else if self.tcx().trait_is_alias(trait_def_id) {
214+
G::consider_trait_alias_candidate(self, goal)
215+
} else if lang_items.sized_trait() == Some(trait_def_id) {
202216
G::consider_builtin_sized_candidate(self, goal)
203217
} else {
204218
Err(NoSolution)
@@ -315,8 +329,7 @@ impl<'tcx> EvalCtxt<'_, 'tcx> {
315329
for assumption in
316330
elaborate_predicates(tcx, bounds.iter().map(|bound| bound.with_self_ty(tcx, self_ty)))
317331
{
318-
match G::consider_assumption(self, goal, assumption.predicate)
319-
{
332+
match G::consider_assumption(self, goal, assumption.predicate) {
320333
Ok(result) => {
321334
candidates.push(Candidate { source: CandidateSource::BuiltinImpl, result })
322335
}

compiler/rustc_trait_selection/src/solve/project_goals.rs

+21-7
Original file line numberDiff line numberDiff line change
@@ -290,13 +290,6 @@ impl<'tcx> assembly::GoalKind<'tcx> for ProjectionPredicate<'tcx> {
290290
})
291291
}
292292

293-
fn consider_builtin_sized_candidate(
294-
_ecx: &mut EvalCtxt<'_, 'tcx>,
295-
goal: Goal<'tcx, Self>,
296-
) -> QueryResult<'tcx> {
297-
bug!("`Sized` does not have an associated type: {:?}", goal);
298-
}
299-
300293
fn consider_assumption(
301294
ecx: &mut EvalCtxt<'_, 'tcx>,
302295
goal: Goal<'tcx, Self>,
@@ -333,6 +326,27 @@ impl<'tcx> assembly::GoalKind<'tcx> for ProjectionPredicate<'tcx> {
333326
Err(NoSolution)
334327
}
335328
}
329+
330+
fn consider_auto_trait_candidate(
331+
_ecx: &mut EvalCtxt<'_, 'tcx>,
332+
goal: Goal<'tcx, Self>,
333+
) -> QueryResult<'tcx> {
334+
bug!("auto traits do not have associated types: {:?}", goal);
335+
}
336+
337+
fn consider_trait_alias_candidate(
338+
_ecx: &mut EvalCtxt<'_, 'tcx>,
339+
goal: Goal<'tcx, Self>,
340+
) -> QueryResult<'tcx> {
341+
bug!("trait aliases do not have associated types: {:?}", goal);
342+
}
343+
344+
fn consider_builtin_sized_candidate(
345+
_ecx: &mut EvalCtxt<'_, 'tcx>,
346+
goal: Goal<'tcx, Self>,
347+
) -> QueryResult<'tcx> {
348+
bug!("`Sized` does not have an associated type: {:?}", goal);
349+
}
336350
}
337351

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

compiler/rustc_trait_selection/src/solve/trait_goals.rs

+124-8
Original file line numberDiff line numberDiff line change
@@ -6,7 +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_infer::infer::LateBoundRegionConversionTime;
9+
use rustc_infer::infer::InferCtxt;
1010
use rustc_infer::traits::query::NoSolution;
1111
use rustc_middle::ty::fast_reject::{DeepRejectCtxt, TreatParams};
1212
use rustc_middle::ty::{self, Ty, TyCtxt};
@@ -58,13 +58,6 @@ impl<'tcx> assembly::GoalKind<'tcx> for TraitPredicate<'tcx> {
5858
})
5959
}
6060

61-
fn consider_builtin_sized_candidate(
62-
_ecx: &mut EvalCtxt<'_, 'tcx>,
63-
_goal: Goal<'tcx, Self>,
64-
) -> QueryResult<'tcx> {
65-
unimplemented!();
66-
}
67-
6861
fn consider_assumption(
6962
ecx: &mut EvalCtxt<'_, 'tcx>,
7063
goal: Goal<'tcx, Self>,
@@ -84,9 +77,61 @@ impl<'tcx> assembly::GoalKind<'tcx> for TraitPredicate<'tcx> {
8477
Err(NoSolution)
8578
}
8679
}
80+
81+
fn consider_auto_trait_candidate(
82+
ecx: &mut EvalCtxt<'_, 'tcx>,
83+
goal: Goal<'tcx, Self>,
84+
) -> QueryResult<'tcx> {
85+
ecx.infcx.probe(|_| {
86+
let constituent_tys =
87+
instantiate_constituent_tys_for_auto_trait(ecx.infcx, goal.predicate.self_ty())?;
88+
ecx.evaluate_goal_for_constituent_tys_and_make_canonical_response(goal, constituent_tys)
89+
})
90+
}
91+
92+
fn consider_trait_alias_candidate(
93+
ecx: &mut EvalCtxt<'_, 'tcx>,
94+
goal: Goal<'tcx, Self>,
95+
) -> QueryResult<'tcx> {
96+
let tcx = ecx.tcx();
97+
98+
ecx.infcx.probe(|_| {
99+
let nested_obligations = tcx
100+
.predicates_of(goal.predicate.def_id())
101+
.instantiate(tcx, goal.predicate.trait_ref.substs);
102+
ecx.evaluate_all_and_make_canonical_response(
103+
nested_obligations.predicates.into_iter().map(|p| goal.with(tcx, p)).collect(),
104+
)
105+
})
106+
}
107+
108+
fn consider_builtin_sized_candidate(
109+
_ecx: &mut EvalCtxt<'_, 'tcx>,
110+
_goal: Goal<'tcx, Self>,
111+
) -> QueryResult<'tcx> {
112+
unimplemented!();
113+
}
87114
}
88115

89116
impl<'tcx> EvalCtxt<'_, 'tcx> {
117+
fn evaluate_goal_for_constituent_tys_and_make_canonical_response(
118+
&mut self,
119+
goal: Goal<'tcx, TraitPredicate<'tcx>>,
120+
constituent_tys: Vec<Ty<'tcx>>,
121+
) -> QueryResult<'tcx> {
122+
self.evaluate_all_and_make_canonical_response(
123+
constituent_tys
124+
.into_iter()
125+
.map(|ty| {
126+
goal.with(
127+
self.tcx(),
128+
ty::Binder::dummy(goal.predicate.with_self_ty(self.tcx(), ty)),
129+
)
130+
})
131+
.collect(),
132+
)
133+
}
134+
90135
pub(super) fn compute_trait_goal(
91136
&mut self,
92137
goal: Goal<'tcx, TraitPredicate<'tcx>>,
@@ -162,3 +207,74 @@ impl<'tcx> EvalCtxt<'_, 'tcx> {
162207
candidate
163208
}
164209
}
210+
211+
// Calculates the constituent types of a type for `auto trait` purposes.
212+
//
213+
// For types with an "existential" binder, i.e. generator witnesses, we also
214+
// instantiate the binder with placeholders eagerly.
215+
fn instantiate_constituent_tys_for_auto_trait<'tcx>(
216+
infcx: &InferCtxt<'tcx>,
217+
ty: Ty<'tcx>,
218+
) -> Result<Vec<Ty<'tcx>>, NoSolution> {
219+
let tcx = infcx.tcx;
220+
match *ty.kind() {
221+
ty::Uint(_)
222+
| ty::Int(_)
223+
| ty::Bool
224+
| ty::Float(_)
225+
| ty::FnDef(..)
226+
| ty::FnPtr(_)
227+
| ty::Str
228+
| ty::Error(_)
229+
| ty::Infer(ty::IntVar(_) | ty::FloatVar(_))
230+
| ty::Never
231+
| ty::Char => Ok(vec![]),
232+
233+
ty::Placeholder(..)
234+
| ty::Dynamic(..)
235+
| ty::Param(..)
236+
| ty::Foreign(..)
237+
| ty::Alias(ty::Projection, ..)
238+
| ty::Bound(..)
239+
| ty::Infer(ty::TyVar(_)) => {
240+
// FIXME: Do we need to mark anything as ambiguous here? Yeah?
241+
Err(NoSolution)
242+
}
243+
244+
ty::Infer(ty::FreshTy(_) | ty::FreshIntTy(_) | ty::FreshFloatTy(_)) => bug!(),
245+
246+
ty::RawPtr(ty::TypeAndMut { ty: element_ty, .. }) | ty::Ref(_, element_ty, _) => {
247+
Ok(vec![element_ty])
248+
}
249+
250+
ty::Array(element_ty, _) | ty::Slice(element_ty) => Ok(vec![element_ty]),
251+
252+
ty::Tuple(ref tys) => {
253+
// (T1, ..., Tn) -- meets any bound that all of T1...Tn meet
254+
Ok(tys.iter().collect())
255+
}
256+
257+
ty::Closure(_, ref substs) => Ok(vec![substs.as_closure().tupled_upvars_ty()]),
258+
259+
ty::Generator(_, ref substs, _) => {
260+
let generator_substs = substs.as_generator();
261+
Ok(vec![generator_substs.tupled_upvars_ty(), generator_substs.witness()])
262+
}
263+
264+
ty::GeneratorWitness(types) => {
265+
Ok(infcx.replace_bound_vars_with_placeholders(types).to_vec())
266+
}
267+
268+
// For `PhantomData<T>`, we pass `T`.
269+
ty::Adt(def, substs) if def.is_phantom_data() => Ok(vec![substs.type_at(0)]),
270+
271+
ty::Adt(def, substs) => Ok(def.all_fields().map(|f| f.ty(tcx, substs)).collect()),
272+
273+
ty::Alias(ty::Opaque, ty::AliasTy { def_id, substs, .. }) => {
274+
// We can resolve the `impl Trait` to its concrete type,
275+
// which enforces a DAG between the functions requiring
276+
// the auto trait bounds in question.
277+
Ok(vec![tcx.bound_type_of(def_id).subst(tcx, substs)])
278+
}
279+
}
280+
}

0 commit comments

Comments
 (0)