1
Fork 0

Replace Deref bounds on Interner in favor of a SliceLike trait

This commit is contained in:
Michael Goulet 2024-06-21 13:55:21 -04:00
parent f26cc349d9
commit 24e41f1d13
22 changed files with 221 additions and 159 deletions

View file

@ -527,7 +527,7 @@ where
};
for assumption in
self.cx().item_bounds(alias_ty.def_id).iter_instantiated(self.cx(), &alias_ty.args)
self.cx().item_bounds(alias_ty.def_id).iter_instantiated(self.cx(), alias_ty.args)
{
candidates.extend(G::probe_and_consider_implied_clause(
self,
@ -603,7 +603,7 @@ where
// Consider all of the auto-trait and projection bounds, which don't
// need to be recorded as a `BuiltinImplSource::Object` since they don't
// really have a vtable base...
for bound in bounds {
for bound in bounds.iter() {
match bound.skip_binder() {
ty::ExistentialPredicate::Trait(_) => {
// Skip principal

View file

@ -58,7 +58,7 @@ where
ty::Tuple(tys) => {
// (T1, ..., Tn) -- meets any bound that all of T1...Tn meet
Ok(tys.into_iter().map(ty::Binder::dummy).collect())
Ok(tys.iter().map(ty::Binder::dummy).collect())
}
ty::Closure(_, args) => Ok(vec![ty::Binder::dummy(args.as_closure().tupled_upvars_ty())]),
@ -79,23 +79,21 @@ where
.cx()
.bound_coroutine_hidden_types(def_id)
.into_iter()
.map(|bty| bty.instantiate(tcx, &args))
.map(|bty| bty.instantiate(tcx, args))
.collect()),
// For `PhantomData<T>`, we pass `T`.
ty::Adt(def, args) if def.is_phantom_data() => Ok(vec![ty::Binder::dummy(args.type_at(0))]),
ty::Adt(def, args) => Ok(def
.all_field_tys(tcx)
.iter_instantiated(tcx, &args)
.map(ty::Binder::dummy)
.collect()),
ty::Adt(def, args) => {
Ok(def.all_field_tys(tcx).iter_instantiated(tcx, args).map(ty::Binder::dummy).collect())
}
ty::Alias(ty::Opaque, ty::AliasTy { def_id, args, .. }) => {
// We can resolve the `impl Trait` to its concrete type,
// which enforces a DAG between the functions requiring
// the auto trait bounds in question.
Ok(vec![ty::Binder::dummy(tcx.type_of(def_id).instantiate(tcx, &args))])
Ok(vec![ty::Binder::dummy(tcx.type_of(def_id).instantiate(tcx, args))])
}
}
}
@ -147,7 +145,7 @@ where
// impl Sized for ()
// impl Sized for (T1, T2, .., Tn) where Tn: Sized if n >= 1
ty::Tuple(tys) => Ok(tys.last().map_or_else(Vec::new, |&ty| vec![ty::Binder::dummy(ty)])),
ty::Tuple(tys) => Ok(tys.last().map_or_else(Vec::new, |ty| vec![ty::Binder::dummy(ty)])),
// impl Sized for Adt<Args...> where sized_constraint(Adt)<Args...>: Sized
// `sized_constraint(Adt)` is the deepest struct trail that can be determined
@ -160,7 +158,7 @@ where
// if the ADT is sized for all possible args.
ty::Adt(def, args) => {
if let Some(sized_crit) = def.sized_constraint(ecx.cx()) {
Ok(vec![ty::Binder::dummy(sized_crit.instantiate(ecx.cx(), &args))])
Ok(vec![ty::Binder::dummy(sized_crit.instantiate(ecx.cx(), args))])
} else {
Ok(vec![])
}
@ -213,7 +211,7 @@ where
}
// impl Copy/Clone for (T1, T2, .., Tn) where T1: Copy/Clone, T2: Copy/Clone, .. Tn: Copy/Clone
ty::Tuple(tys) => Ok(tys.into_iter().map(ty::Binder::dummy).collect()),
ty::Tuple(tys) => Ok(tys.iter().map(ty::Binder::dummy).collect()),
// impl Copy/Clone for Closure where Self::TupledUpvars: Copy/Clone
ty::Closure(_, args) => Ok(vec![ty::Binder::dummy(args.as_closure().tupled_upvars_ty())]),
@ -242,7 +240,7 @@ where
.cx()
.bound_coroutine_hidden_types(def_id)
.into_iter()
.map(|bty| bty.instantiate(ecx.cx(), &args))
.map(|bty| bty.instantiate(ecx.cx(), args))
.collect()),
}
}
@ -259,7 +257,7 @@ pub(in crate::solve) fn extract_tupled_inputs_and_output_from_callable<I: Intern
let sig = tcx.fn_sig(def_id);
if sig.skip_binder().is_fn_trait_compatible() && !tcx.has_target_features(def_id) {
Ok(Some(
sig.instantiate(tcx, &args)
sig.instantiate(tcx, args)
.map_bound(|sig| (Ty::new_tup(tcx, &sig.inputs()), sig.output())),
))
} else {
@ -669,7 +667,7 @@ where
let tcx = ecx.cx();
let mut requirements = vec![];
requirements
.extend(tcx.super_predicates_of(trait_ref.def_id).iter_instantiated(tcx, &trait_ref.args));
.extend(tcx.super_predicates_of(trait_ref.def_id).iter_instantiated(tcx, trait_ref.args));
// FIXME(associated_const_equality): Also add associated consts to
// the requirements here.
@ -680,13 +678,12 @@ where
continue;
}
requirements.extend(
tcx.item_bounds(associated_type_def_id).iter_instantiated(tcx, &trait_ref.args),
);
requirements
.extend(tcx.item_bounds(associated_type_def_id).iter_instantiated(tcx, trait_ref.args));
}
let mut replace_projection_with = HashMap::default();
for bound in object_bounds {
for bound in object_bounds.iter() {
if let ty::ExistentialPredicate::Projection(proj) = bound.skip_binder() {
let proj = proj.with_self_ty(tcx, trait_ref.self_ty());
let old_ty = replace_projection_with.insert(proj.def_id(), bound.rebind(proj));

View file

@ -267,7 +267,9 @@ where
// We therefore instantiate the existential variable in the canonical response with the
// inference variable of the input right away, which is more performant.
let mut opt_values = IndexVec::from_elem_n(None, response.variables.len());
for (original_value, result_value) in iter::zip(original_values, var_values.var_values) {
for (original_value, result_value) in
iter::zip(original_values, var_values.var_values.iter())
{
match result_value.kind() {
ty::GenericArgKind::Type(t) => {
if let ty::Bound(debruijn, b) = t.kind() {
@ -291,7 +293,7 @@ where
}
let var_values = delegate.cx().mk_args_from_iter(
response.variables.into_iter().enumerate().map(|(index, info)| {
response.variables.iter().enumerate().map(|(index, info)| {
if info.universe() != ty::UniverseIndex::ROOT {
// A variable from inside a binder of the query. While ideally these shouldn't
// exist at all (see the FIXME at the start of this method), we have to deal with
@ -344,7 +346,7 @@ where
) {
assert_eq!(original_values.len(), var_values.len());
for (&orig, response) in iter::zip(original_values, var_values.var_values) {
for (&orig, response) in iter::zip(original_values, var_values.var_values.iter()) {
let goals =
delegate.eq_structurally_relating_aliases(param_env, orig, response).unwrap();
assert!(goals.is_empty());
@ -413,7 +415,8 @@ where
// In case any fresh inference variables have been created between `state`
// and the previous instantiation, extend `orig_values` for it.
assert!(orig_values.len() <= state.value.var_values.len());
for &arg in &state.value.var_values.var_values[orig_values.len()..state.value.var_values.len()]
for &arg in &state.value.var_values.var_values.as_slice()
[orig_values.len()..state.value.var_values.len()]
{
// FIXME: This is so ugly.
let unconstrained = delegate.fresh_var_for_kind_with_span(arg, span);

View file

@ -875,7 +875,7 @@ where
pub(super) fn fresh_args_for_item(&mut self, def_id: I::DefId) -> I::GenericArgs {
let args = self.delegate.fresh_args_for_item(def_id);
for arg in args {
for arg in args.iter() {
self.inspect.add_var_value(arg);
}
args
@ -979,7 +979,7 @@ where
result: *result,
})
.enter(|ecx| {
for (a, b) in std::iter::zip(candidate_key.args, key.args) {
for (a, b) in std::iter::zip(candidate_key.args.iter(), key.args.iter()) {
ecx.eq(param_env, a, b)?;
}
ecx.eq(param_env, candidate_ty, ty)?;

View file

@ -7,6 +7,7 @@
use std::marker::PhantomData;
use std::mem;
use rustc_type_ir::inherent::*;
use rustc_type_ir::{self as ty, Interner};
use crate::delegate::SolverDelegate;

View file

@ -182,7 +182,7 @@ where
return self.evaluate_added_goals_and_make_canonical_response(Certainty::Yes);
}
ty::ConstKind::Unevaluated(uv) => {
self.cx().type_of(uv.def).instantiate(self.cx(), &uv.args)
self.cx().type_of(uv.def).instantiate(self.cx(), uv.args)
}
ty::ConstKind::Expr(_) => unimplemented!(
"`feature(generic_const_exprs)` is not supported in the new trait solver"

View file

@ -29,7 +29,7 @@ where
self.eq(
goal.param_env,
inherent.self_ty(),
tcx.type_of(impl_def_id).instantiate(tcx, &impl_args),
tcx.type_of(impl_def_id).instantiate(tcx, impl_args),
)?;
// Equate IAT with the RHS of the project goal
@ -44,11 +44,11 @@ where
self.add_goals(
GoalSource::Misc,
tcx.predicates_of(inherent.def_id)
.iter_instantiated(tcx, &inherent_args)
.iter_instantiated(tcx, inherent_args)
.map(|pred| goal.with(tcx, pred)),
);
let normalized = tcx.type_of(inherent.def_id).instantiate(tcx, &inherent_args);
let normalized = tcx.type_of(inherent.def_id).instantiate(tcx, inherent_args);
self.instantiate_normalizes_to_term(goal, normalized.into());
self.evaluate_added_goals_and_make_canonical_response(Certainty::Yes)
}

View file

@ -121,7 +121,7 @@ where
ecx.add_goals(
GoalSource::Misc,
tcx.own_predicates_of(goal.predicate.def_id())
.iter_instantiated(tcx, &goal.predicate.alias.args)
.iter_instantiated(tcx, goal.predicate.alias.args)
.map(|pred| goal.with(tcx, pred)),
);
@ -163,13 +163,13 @@ where
ecx.probe_trait_candidate(CandidateSource::Impl(impl_def_id)).enter(|ecx| {
let impl_args = ecx.fresh_args_for_item(impl_def_id);
let impl_trait_ref = impl_trait_ref.instantiate(tcx, &impl_args);
let impl_trait_ref = impl_trait_ref.instantiate(tcx, impl_args);
ecx.eq(goal.param_env, goal_trait_ref, impl_trait_ref)?;
let where_clause_bounds = tcx
.predicates_of(impl_def_id)
.iter_instantiated(tcx, &impl_args)
.iter_instantiated(tcx, impl_args)
.map(|pred| goal.with(tcx, pred));
ecx.add_goals(GoalSource::ImplWhereBound, where_clause_bounds);
@ -177,7 +177,7 @@ where
ecx.add_goals(
GoalSource::Misc,
tcx.own_predicates_of(goal.predicate.def_id())
.iter_instantiated(tcx, &goal.predicate.alias.args)
.iter_instantiated(tcx, goal.predicate.alias.args)
.map(|pred| goal.with(tcx, pred)),
);
@ -254,7 +254,7 @@ where
kind => panic!("expected projection, found {kind:?}"),
};
ecx.instantiate_normalizes_to_term(goal, term.instantiate(tcx, &target_args));
ecx.instantiate_normalizes_to_term(goal, term.instantiate(tcx, target_args));
ecx.evaluate_added_goals_and_make_canonical_response(Certainty::Yes)
})
}
@ -467,7 +467,7 @@ where
tupled_inputs_ty,
tupled_upvars_ty,
coroutine_captures_by_ref_ty,
] = **goal.predicate.alias.args
] = *goal.predicate.alias.args.as_slice()
else {
panic!();
};
@ -567,14 +567,14 @@ where
ty::Adt(def, args) if def.is_struct() => match def.struct_tail_ty(tcx) {
None => Ty::new_unit(tcx),
Some(tail_ty) => {
Ty::new_projection(tcx, metadata_def_id, [tail_ty.instantiate(tcx, &args)])
Ty::new_projection(tcx, metadata_def_id, [tail_ty.instantiate(tcx, args)])
}
},
ty::Adt(_, _) => Ty::new_unit(tcx),
ty::Tuple(elements) => match elements.last() {
None => Ty::new_unit(tcx),
Some(&tail_ty) => Ty::new_projection(tcx, metadata_def_id, [tail_ty]),
Some(tail_ty) => Ty::new_projection(tcx, metadata_def_id, [tail_ty]),
},
ty::Infer(
@ -895,7 +895,7 @@ where
} else {
let target_args = self.fresh_args_for_item(target_container_def_id);
let target_trait_ref =
tcx.impl_trait_ref(target_container_def_id).instantiate(tcx, &target_args);
tcx.impl_trait_ref(target_container_def_id).instantiate(tcx, target_args);
// Relate source impl to target impl by equating trait refs.
self.eq(goal.param_env, impl_trait_ref, target_trait_ref)?;
// Also add predicates since they may be needed to constrain the
@ -903,7 +903,7 @@ where
self.add_goals(
GoalSource::Misc,
tcx.predicates_of(target_container_def_id)
.iter_instantiated(tcx, &target_args)
.iter_instantiated(tcx, target_args)
.map(|pred| goal.with(tcx, pred)),
);
goal.predicate.alias.args.rebase_onto(tcx, impl_trait_ref.def_id, target_args)

View file

@ -86,7 +86,7 @@ where
}
(Reveal::All, _) => {
// FIXME: Add an assertion that opaque type storage is empty.
let actual = tcx.type_of(opaque_ty.def_id).instantiate(tcx, &opaque_ty.args);
let actual = tcx.type_of(opaque_ty.def_id).instantiate(tcx, opaque_ty.args);
self.eq(goal.param_env, expected, actual)?;
self.evaluate_added_goals_and_make_canonical_response(Certainty::Yes)
}
@ -102,7 +102,7 @@ pub fn uses_unique_placeholders_ignoring_regions<I: Interner>(
args: I::GenericArgs,
) -> Result<(), NotUniqueParam<I>> {
let mut seen = GrowableBitSet::default();
for arg in args {
for arg in args.iter() {
match arg.kind() {
// Ignore regions, since we can't resolve those in a canonicalized
// query in the trait solver.

View file

@ -25,11 +25,11 @@ where
self.add_goals(
GoalSource::Misc,
tcx.predicates_of(weak_ty.def_id)
.iter_instantiated(tcx, &weak_ty.args)
.iter_instantiated(tcx, weak_ty.args)
.map(|pred| goal.with(tcx, pred)),
);
let actual = tcx.type_of(weak_ty.def_id).instantiate(tcx, &weak_ty.args);
let actual = tcx.type_of(weak_ty.def_id).instantiate(tcx, weak_ty.args);
self.instantiate_normalizes_to_term(goal, actual.into());
self.evaluate_added_goals_and_make_canonical_response(Certainty::Yes)

View file

@ -77,12 +77,12 @@ where
ecx.probe_trait_candidate(CandidateSource::Impl(impl_def_id)).enter(|ecx| {
let impl_args = ecx.fresh_args_for_item(impl_def_id);
ecx.record_impl_args(impl_args);
let impl_trait_ref = impl_trait_ref.instantiate(tcx, &impl_args);
let impl_trait_ref = impl_trait_ref.instantiate(tcx, impl_args);
ecx.eq(goal.param_env, goal.predicate.trait_ref, impl_trait_ref)?;
let where_clause_bounds = tcx
.predicates_of(impl_def_id)
.iter_instantiated(tcx, &impl_args)
.iter_instantiated(tcx, impl_args)
.map(|pred| goal.with(tcx, pred));
ecx.add_goals(GoalSource::ImplWhereBound, where_clause_bounds);
@ -186,7 +186,7 @@ where
ecx.probe_builtin_trait_candidate(BuiltinImplSource::Misc).enter(|ecx| {
let nested_obligations = tcx
.predicates_of(goal.predicate.def_id())
.iter_instantiated(tcx, &goal.predicate.trait_ref.args)
.iter_instantiated(tcx, goal.predicate.trait_ref.args)
.map(|p| goal.with(tcx, p));
// FIXME(-Znext-solver=coinductive): Should this be `GoalSource::ImplWhereBound`?
ecx.add_goals(GoalSource::Misc, nested_obligations);
@ -373,7 +373,7 @@ where
ecx: &mut EvalCtxt<'_, D>,
goal: Goal<I, Self>,
) -> Result<Candidate<I>, NoSolution> {
let [closure_fn_kind_ty, goal_kind_ty] = **goal.predicate.trait_ref.args else {
let [closure_fn_kind_ty, goal_kind_ty] = *goal.predicate.trait_ref.args.as_slice() else {
panic!();
};
@ -783,7 +783,7 @@ where
// (i.e. the principal, all of the associated types match, and any auto traits)
ecx.add_goals(
GoalSource::ImplWhereBound,
b_data.into_iter().map(|pred| goal.with(tcx, pred.with_self_ty(tcx, a_ty))),
b_data.iter().map(|pred| goal.with(tcx, pred.with_self_ty(tcx, a_ty))),
);
// The type must be `Sized` to be unsized.
@ -851,7 +851,7 @@ where
};
self.probe_trait_candidate(source).enter(|ecx| {
for bound in b_data {
for bound in b_data.iter() {
match bound.skip_binder() {
// Check that a's supertrait (upcast_principal) is compatible
// with the target (b_ty).
@ -953,18 +953,15 @@ where
let tail_field_ty = def.struct_tail_ty(tcx).unwrap();
let a_tail_ty = tail_field_ty.instantiate(tcx, &a_args);
let b_tail_ty = tail_field_ty.instantiate(tcx, &b_args);
let a_tail_ty = tail_field_ty.instantiate(tcx, a_args);
let b_tail_ty = tail_field_ty.instantiate(tcx, b_args);
// Instantiate just the unsizing params from B into A. The type after
// this instantiation must be equal to B. This is so we don't unsize
// unrelated type parameters.
let new_a_args = tcx.mk_args_from_iter(
a_args
.iter()
.enumerate()
.map(|(i, a)| if unsizing_params.contains(i as u32) { b_args[i] } else { *a }),
);
let new_a_args = tcx.mk_args_from_iter(a_args.iter().enumerate().map(|(i, a)| {
if unsizing_params.contains(i as u32) { b_args.get(i).unwrap() } else { a }
}));
let unsized_a_ty = Ty::new_adt(tcx, def, new_a_args);
// Finally, we require that `TailA: Unsize<TailB>` for the tail field
@ -1005,7 +1002,7 @@ where
let Goal { predicate: (_a_ty, b_ty), .. } = goal;
let (&a_last_ty, a_rest_tys) = a_tys.split_last().unwrap();
let &b_last_ty = b_tys.last().unwrap();
let b_last_ty = b_tys.last().unwrap();
// Instantiate just the tail field of B., and require that they're equal.
let unsized_a_ty =