1
Fork 0

Auto merge of #110806 - WaffleLapkin:unmkI, r=lcnr

Replace `tcx.mk_trait_ref` with `TraitRef::new`

First step in implementing https://github.com/rust-lang/compiler-team/issues/616
r? `@lcnr`
This commit is contained in:
bors 2023-05-04 05:54:09 +00:00
commit 6f8c0557e0
49 changed files with 246 additions and 181 deletions

View file

@ -66,7 +66,7 @@ impl<'tcx> InferCtxtExt<'tcx> for InferCtxt<'tcx> {
params: impl IntoIterator<Item: Into<GenericArg<'tcx>>>,
param_env: ty::ParamEnv<'tcx>,
) -> traits::EvaluationResult {
let trait_ref = self.tcx.mk_trait_ref(trait_def_id, params);
let trait_ref = ty::TraitRef::new(self.tcx, trait_def_id, params);
let obligation = traits::Obligation {
cause: traits::ObligationCause::dummy(),

View file

@ -274,8 +274,9 @@ impl<'tcx> assembly::GoalKind<'tcx> for ProjectionPredicate<'tcx> {
.evaluate_added_goals_and_make_canonical_response(Certainty::AMBIGUOUS);
}
};
let output_is_sized_pred = tupled_inputs_and_output
.map_bound(|(_, output)| tcx.at(DUMMY_SP).mk_trait_ref(LangItem::Sized, [output]));
let output_is_sized_pred = tupled_inputs_and_output.map_bound(|(_, output)| {
ty::TraitRef::from_lang_item(tcx, LangItem::Sized, DUMMY_SP, [output])
});
let pred = tupled_inputs_and_output
.map_bound(|(inputs, output)| ty::ProjectionPredicate {
@ -333,10 +334,12 @@ impl<'tcx> assembly::GoalKind<'tcx> for ProjectionPredicate<'tcx> {
ty::Alias(_, _) | ty::Param(_) | ty::Placeholder(..) => {
// FIXME(ptr_metadata): It would also be possible to return a `Ok(Ambig)` with no constraints.
let sized_predicate = ty::Binder::dummy(tcx.at(DUMMY_SP).mk_trait_ref(
let sized_predicate = ty::TraitRef::from_lang_item(
tcx,
LangItem::Sized,
DUMMY_SP,
[ty::GenericArg::from(goal.predicate.self_ty())],
));
);
ecx.add_goal(goal.with(tcx, sized_predicate));
tcx.types.unit
}

View file

@ -272,12 +272,13 @@ impl<'tcx> assembly::GoalKind<'tcx> for TraitPredicate<'tcx> {
.evaluate_added_goals_and_make_canonical_response(Certainty::AMBIGUOUS);
}
};
let output_is_sized_pred = tupled_inputs_and_output
.map_bound(|(_, output)| tcx.at(DUMMY_SP).mk_trait_ref(LangItem::Sized, [output]));
let output_is_sized_pred = tupled_inputs_and_output.map_bound(|(_, output)| {
ty::TraitRef::from_lang_item(tcx, LangItem::Sized, DUMMY_SP, [output])
});
let pred = tupled_inputs_and_output
.map_bound(|(inputs, _)| {
tcx.mk_trait_ref(goal.predicate.def_id(), [goal.predicate.self_ty(), inputs])
ty::TraitRef::new(tcx, goal.predicate.def_id(), [goal.predicate.self_ty(), inputs])
})
.to_predicate(tcx);
// A built-in `Fn` impl only holds if the output is sized.
@ -358,10 +359,8 @@ impl<'tcx> assembly::GoalKind<'tcx> for TraitPredicate<'tcx> {
Self::consider_implied_clause(
ecx,
goal,
ty::Binder::dummy(
tcx.mk_trait_ref(goal.predicate.def_id(), [self_ty, generator.resume_ty()]),
)
.to_predicate(tcx),
ty::TraitRef::new(tcx, goal.predicate.def_id(), [self_ty, generator.resume_ty()])
.to_predicate(tcx),
// Technically, we need to check that the generator types are Sized,
// but that's already proven by the generator being WF.
[],
@ -410,9 +409,7 @@ impl<'tcx> assembly::GoalKind<'tcx> for TraitPredicate<'tcx> {
data.iter().map(|pred| goal.with(tcx, pred.with_self_ty(tcx, a_ty))),
);
// The type must be Sized to be unsized.
ecx.add_goal(
goal.with(tcx, ty::Binder::dummy(tcx.mk_trait_ref(sized_def_id, [a_ty]))),
);
ecx.add_goal(goal.with(tcx, ty::TraitRef::new(tcx, sized_def_id, [a_ty])));
// The type must outlive the lifetime of the `dyn` we're unsizing into.
ecx.add_goal(
goal.with(tcx, ty::Binder::dummy(ty::OutlivesPredicate(a_ty, region))),
@ -461,9 +458,7 @@ impl<'tcx> assembly::GoalKind<'tcx> for TraitPredicate<'tcx> {
ecx.eq(goal.param_env, unsized_a_ty, b_ty)?;
ecx.add_goal(goal.with(
tcx,
ty::Binder::dummy(
tcx.mk_trait_ref(goal.predicate.def_id(), [a_tail_ty, b_tail_ty]),
),
ty::TraitRef::new(tcx, goal.predicate.def_id(), [a_tail_ty, b_tail_ty]),
));
ecx.evaluate_added_goals_and_make_canonical_response(Certainty::Yes)
}
@ -482,9 +477,7 @@ impl<'tcx> assembly::GoalKind<'tcx> for TraitPredicate<'tcx> {
// Similar to ADTs, require that the rest of the fields are equal.
ecx.add_goal(goal.with(
tcx,
ty::Binder::dummy(
tcx.mk_trait_ref(goal.predicate.def_id(), [*a_last_ty, *b_last_ty]),
),
ty::TraitRef::new(tcx, goal.predicate.def_id(), [*a_last_ty, *b_last_ty]),
));
ecx.evaluate_added_goals_and_make_canonical_response(Certainty::Yes)
}

View file

@ -86,7 +86,7 @@ impl<'tcx> AutoTraitFinder<'tcx> {
) -> AutoTraitResult<A> {
let tcx = self.tcx;
let trait_ref = tcx.mk_trait_ref(trait_did, [ty]);
let trait_ref = ty::TraitRef::new(tcx, trait_did, [ty]);
let infcx = tcx.infer_ctxt().build();
let mut selcx = SelectionContext::new(&infcx);
@ -263,7 +263,7 @@ impl<'tcx> AutoTraitFinder<'tcx> {
let mut already_visited = FxHashSet::default();
let mut predicates = VecDeque::new();
predicates.push_back(ty::Binder::dummy(ty::TraitPredicate {
trait_ref: infcx.tcx.mk_trait_ref(trait_did, [ty]),
trait_ref: ty::TraitRef::new(infcx.tcx, trait_did, [ty]),
constness: ty::BoundConstness::NotConst,
// Auto traits are positive

View file

@ -97,7 +97,7 @@ impl<'a, 'tcx> ObligationCtxt<'a, 'tcx> {
def_id: DefId,
) {
let tcx = self.infcx.tcx;
let trait_ref = tcx.mk_trait_ref(def_id, [ty]);
let trait_ref = ty::TraitRef::new(tcx, def_id, [ty]);
self.register_obligation(Obligation {
cause,
recursion_depth: 0,

View file

@ -87,7 +87,7 @@ pub fn recompute_applicable_impls<'tcx>(
if let ty::PredicateKind::Clause(ty::Clause::Trait(trait_pred)) = kind.skip_binder()
&& param_env_candidate_may_apply(kind.rebind(trait_pred))
{
if kind.rebind(trait_pred.trait_ref) == ty::TraitRef::identity(tcx, trait_pred.def_id()) {
if kind.rebind(trait_pred.trait_ref) == ty::Binder::dummy(ty::TraitRef::identity(tcx, trait_pred.def_id())) {
ambiguities.push(Ambiguity::ParamEnv(tcx.def_span(trait_pred.def_id())))
} else {
ambiguities.push(Ambiguity::ParamEnv(span))

View file

@ -358,7 +358,7 @@ impl<'tcx> InferCtxtExt<'tcx> for InferCtxt<'tcx> {
span: DUMMY_SP,
kind: TypeVariableOriginKind::MiscVariable,
});
let trait_ref = self.tcx.mk_trait_ref(trait_def_id, [ty.skip_binder(), var]);
let trait_ref = ty::TraitRef::new(self.tcx, trait_def_id, [ty.skip_binder(), var]);
let obligation = Obligation::new(
self.tcx,
ObligationCause::dummy(),

View file

@ -3502,7 +3502,7 @@ impl<'tcx> TypeErrCtxtExt<'tcx> for TypeErrCtxt<'_, 'tcx> {
_ => None,
};
let trait_pred = trait_pred.map_bound_ref(|tr| ty::TraitPredicate {
trait_ref: self.tcx.mk_trait_ref(
trait_ref: ty::TraitRef::new(self.tcx,
trait_pred.def_id(),
[field_ty].into_iter().chain(trait_substs),
),

View file

@ -127,7 +127,7 @@ pub fn type_known_to_meet_bound_modulo_regions<'tcx>(
ty: Ty<'tcx>,
def_id: DefId,
) -> bool {
let trait_ref = ty::Binder::dummy(infcx.tcx.mk_trait_ref(def_id, [ty]));
let trait_ref = ty::TraitRef::new(infcx.tcx, def_id, [ty]);
pred_known_to_hold_modulo_regions(infcx, param_env, trait_ref.without_const())
}

View file

@ -279,7 +279,7 @@ fn predicates_reference_self(
trait_def_id: DefId,
supertraits_only: bool,
) -> SmallVec<[Span; 1]> {
let trait_ref = ty::TraitRef::identity(tcx, trait_def_id);
let trait_ref = ty::Binder::dummy(ty::TraitRef::identity(tcx, trait_def_id));
let predicates = if supertraits_only {
tcx.super_predicates_of(trait_def_id)
} else {
@ -661,9 +661,9 @@ fn object_ty_for_trait<'tcx>(
let trait_ref = ty::TraitRef::identity(tcx, trait_def_id);
debug!(?trait_ref);
let trait_predicate = trait_ref.map_bound(|trait_ref| {
ty::ExistentialPredicate::Trait(ty::ExistentialTraitRef::erase_self_ty(tcx, trait_ref))
});
let trait_predicate = ty::Binder::dummy(ty::ExistentialPredicate::Trait(
ty::ExistentialTraitRef::erase_self_ty(tcx, trait_ref),
));
debug!(?trait_predicate);
let pred: ty::Predicate<'tcx> = trait_ref.to_predicate(tcx);
@ -769,11 +769,10 @@ fn receiver_is_dispatchable<'tcx>(
let param_env = tcx.param_env(method.def_id);
// Self: Unsize<U>
let unsize_predicate = ty::Binder::dummy(
tcx.mk_trait_ref(unsize_did, [tcx.types.self_param, unsized_self_ty]),
)
.without_const()
.to_predicate(tcx);
let unsize_predicate =
ty::TraitRef::new(tcx, unsize_did, [tcx.types.self_param, unsized_self_ty])
.without_const()
.to_predicate(tcx);
// U: Trait<Arg1, ..., ArgN>
let trait_predicate = {
@ -782,7 +781,7 @@ fn receiver_is_dispatchable<'tcx>(
if param.index == 0 { unsized_self_ty.into() } else { tcx.mk_param_from_def(param) }
});
ty::Binder::dummy(tcx.mk_trait_ref(trait_def_id, substs)).to_predicate(tcx)
ty::TraitRef::new(tcx, trait_def_id, substs).to_predicate(tcx)
};
let caller_bounds =
@ -797,9 +796,8 @@ fn receiver_is_dispatchable<'tcx>(
// Receiver: DispatchFromDyn<Receiver[Self => U]>
let obligation = {
let predicate = ty::Binder::dummy(
tcx.mk_trait_ref(dispatch_from_dyn_did, [receiver_ty, unsized_receiver_ty]),
);
let predicate =
ty::TraitRef::new(tcx, dispatch_from_dyn_did, [receiver_ty, unsized_receiver_ty]);
Obligation::new(tcx, ObligationCause::dummy(), param_env, predicate)
};
@ -882,7 +880,8 @@ fn contains_illegal_self_type_reference<'tcx, T: TypeVisitable<TyCtxt<'tcx>>>(
// Compute supertraits of current trait lazily.
if self.supertraits.is_none() {
let trait_ref = ty::TraitRef::identity(self.tcx, self.trait_def_id);
let trait_ref =
ty::Binder::dummy(ty::TraitRef::identity(self.tcx, self.trait_def_id));
self.supertraits = Some(
traits::supertraits(self.tcx, trait_ref).map(|t| t.def_id()).collect(),
);

View file

@ -1319,7 +1319,7 @@ fn assemble_candidate_for_impl_trait_in_trait<'cx, 'tcx>(
let trait_substs =
obligation.predicate.substs.truncate_to(tcx, tcx.generics_of(trait_def_id));
// FIXME(named-returns): Binders
let trait_predicate = ty::Binder::dummy(tcx.mk_trait_ref(trait_def_id, trait_substs));
let trait_predicate = ty::TraitRef::new(tcx, trait_def_id, trait_substs);
let _ = selcx.infcx.commit_if_ok(|_| {
match selcx.select(&obligation.with(tcx, trait_predicate)) {
@ -1682,10 +1682,8 @@ fn assemble_candidates_from_impls<'cx, 'tcx>(
if selcx.infcx.predicate_must_hold_modulo_regions(
&obligation.with(
selcx.tcx(),
ty::Binder::dummy(
selcx.tcx().at(obligation.cause.span()).mk_trait_ref(LangItem::Sized, [self_ty]),
)
.without_const(),
ty::TraitRef::from_lang_item(selcx.tcx(), LangItem::Sized, obligation.cause.span(),[self_ty])
.without_const(),
),
) =>
{
@ -1948,8 +1946,11 @@ fn confirm_builtin_candidate<'cx, 'tcx>(
)
});
if check_is_sized {
let sized_predicate = ty::Binder::dummy(
tcx.at(obligation.cause.span()).mk_trait_ref(LangItem::Sized, [self_ty]),
let sized_predicate = ty::TraitRef::from_lang_item(
tcx,
LangItem::Sized,
obligation.cause.span(),
[self_ty],
)
.without_const();
obligations.push(obligation.with(tcx, sized_predicate));

View file

@ -459,7 +459,7 @@ impl<'cx, 'tcx> SelectionContext<'cx, 'tcx> {
obligation.param_env,
self.tcx().mk_predicate(obligation.predicate.map_bound(|mut pred| {
pred.trait_ref =
self.tcx().mk_trait_ref(fn_ptr_trait, [pred.trait_ref.self_ty()]);
ty::TraitRef::new(self.tcx(), fn_ptr_trait, [pred.trait_ref.self_ty()]);
ty::PredicateKind::Clause(ty::Clause::Trait(pred))
})),
);
@ -634,7 +634,7 @@ impl<'cx, 'tcx> SelectionContext<'cx, 'tcx> {
}
// <ty as Deref>
let trait_ref = tcx.mk_trait_ref(tcx.lang_items().deref_trait()?, [ty]);
let trait_ref = ty::TraitRef::new(tcx, tcx.lang_items().deref_trait()?, [ty]);
let obligation =
traits::Obligation::new(tcx, cause.clone(), param_env, ty::Binder::dummy(trait_ref));

View file

@ -646,8 +646,7 @@ impl<'cx, 'tcx> SelectionContext<'cx, 'tcx> {
output_ty,
&mut nested,
);
let tr =
ty::Binder::dummy(self.tcx().at(cause.span).mk_trait_ref(LangItem::Sized, [output_ty]));
let tr = ty::TraitRef::from_lang_item(self.tcx(), LangItem::Sized, cause.span, [output_ty]);
nested.push(Obligation::new(self.infcx.tcx, cause, obligation.param_env, tr));
Ok(ImplSourceFnPointerData { fn_ty: self_ty, nested })
@ -1050,8 +1049,7 @@ impl<'cx, 'tcx> SelectionContext<'cx, 'tcx> {
);
// We can only make objects from sized types.
let tr =
ty::Binder::dummy(tcx.at(cause.span).mk_trait_ref(LangItem::Sized, [source]));
let tr = ty::TraitRef::from_lang_item(tcx, LangItem::Sized, cause.span, [source]);
nested.push(predicate_to_obligation(tr.without_const().to_predicate(tcx)));
// If the type is `Foo + 'a`, ensure that the type
@ -1121,7 +1119,11 @@ impl<'cx, 'tcx> SelectionContext<'cx, 'tcx> {
// Construct the nested `TailField<T>: Unsize<TailField<U>>` predicate.
let tail_unsize_obligation = obligation.with(
tcx,
tcx.mk_trait_ref(obligation.predicate.def_id(), [source_tail, target_tail]),
ty::TraitRef::new(
tcx,
obligation.predicate.def_id(),
[source_tail, target_tail],
),
);
nested.push(tail_unsize_obligation);
}
@ -1146,8 +1148,10 @@ impl<'cx, 'tcx> SelectionContext<'cx, 'tcx> {
nested.extend(obligations);
// Add a nested `T: Unsize<U>` predicate.
let last_unsize_obligation = obligation
.with(tcx, tcx.mk_trait_ref(obligation.predicate.def_id(), [a_last, b_last]));
let last_unsize_obligation = obligation.with(
tcx,
ty::TraitRef::new(tcx, obligation.predicate.def_id(), [a_last, b_last]),
);
nested.push(last_unsize_obligation);
}
@ -1271,10 +1275,12 @@ impl<'cx, 'tcx> SelectionContext<'cx, 'tcx> {
cause.clone(),
obligation.recursion_depth + 1,
self_ty.rebind(ty::TraitPredicate {
trait_ref: self
.tcx()
.at(cause.span)
.mk_trait_ref(LangItem::Destruct, [nested_ty]),
trait_ref: ty::TraitRef::from_lang_item(
self.tcx(),
LangItem::Destruct,
cause.span,
[nested_ty],
),
constness: ty::BoundConstness::ConstIfConst,
polarity: ty::ImplPolarity::Positive,
}),
@ -1295,10 +1301,12 @@ impl<'cx, 'tcx> SelectionContext<'cx, 'tcx> {
// or it's an ADT (and we need to check for a custom impl during selection)
_ => {
let predicate = self_ty.rebind(ty::TraitPredicate {
trait_ref: self
.tcx()
.at(cause.span)
.mk_trait_ref(LangItem::Destruct, [nested_ty]),
trait_ref: ty::TraitRef::from_lang_item(
self.tcx(),
LangItem::Destruct,
cause.span,
[nested_ty],
),
constness: ty::BoundConstness::ConstIfConst,
polarity: ty::ImplPolarity::Positive,
});

View file

@ -2413,7 +2413,7 @@ impl<'tcx> SelectionContext<'_, 'tcx> {
self.tcx(),
cause.clone(),
param_env,
self.tcx().mk_trait_ref(trait_def_id, [normalized_ty]),
ty::TraitRef::new(self.tcx(), trait_def_id, [normalized_ty]),
);
obligations.push(obligation);
obligations

View file

@ -262,7 +262,7 @@ pub fn closure_trait_ref_and_return_type<'tcx>(
TupleArgumentsFlag::No => sig.skip_binder().inputs()[0],
TupleArgumentsFlag::Yes => tcx.mk_tup(sig.skip_binder().inputs()),
};
let trait_ref = tcx.mk_trait_ref(fn_trait_def_id, [self_ty, arguments_tuple]);
let trait_ref = ty::TraitRef::new(tcx, fn_trait_def_id, [self_ty, arguments_tuple]);
sig.map_bound(|sig| (trait_ref, sig.output()))
}
@ -273,7 +273,7 @@ pub fn generator_trait_ref_and_outputs<'tcx>(
sig: ty::PolyGenSig<'tcx>,
) -> ty::Binder<'tcx, (ty::TraitRef<'tcx>, Ty<'tcx>, Ty<'tcx>)> {
assert!(!self_ty.has_escaping_bound_vars());
let trait_ref = tcx.mk_trait_ref(fn_trait_def_id, [self_ty, sig.skip_binder().resume_ty]);
let trait_ref = ty::TraitRef::new(tcx, fn_trait_def_id, [self_ty, sig.skip_binder().resume_ty]);
sig.map_bound(|sig| (trait_ref, sig.yield_ty, sig.return_ty))
}
@ -284,7 +284,7 @@ pub fn future_trait_ref_and_outputs<'tcx>(
sig: ty::PolyGenSig<'tcx>,
) -> ty::Binder<'tcx, (ty::TraitRef<'tcx>, Ty<'tcx>)> {
assert!(!self_ty.has_escaping_bound_vars());
let trait_ref = tcx.mk_trait_ref(fn_trait_def_id, [self_ty]);
let trait_ref = ty::TraitRef::new(tcx, fn_trait_def_id, [self_ty]);
sig.map_bound(|sig| (trait_ref, sig.return_ty))
}

View file

@ -359,7 +359,7 @@ pub(crate) fn vtable_trait_upcasting_coercion_new_vptr_slot<'tcx>(
// this has been typecked-before, so diagnostics is not really needed.
let unsize_trait_did = tcx.require_lang_item(LangItem::Unsize, None);
let trait_ref = tcx.mk_trait_ref(unsize_trait_did, [source, target]);
let trait_ref = ty::TraitRef::new(tcx, unsize_trait_did, [source, target]);
match tcx.codegen_select_candidate((ty::ParamEnv::reveal_all(), ty::Binder::dummy(trait_ref))) {
Ok(ImplSource::TraitUpcasting(implsrc_traitcasting)) => {

View file

@ -463,7 +463,8 @@ impl<'tcx> WfPredicates<'tcx> {
fn require_sized(&mut self, subty: Ty<'tcx>, cause: traits::ObligationCauseCode<'tcx>) {
if !subty.has_escaping_bound_vars() {
let cause = self.cause(cause);
let trait_ref = self.tcx.at(cause.span).mk_trait_ref(LangItem::Sized, [subty]);
let trait_ref =
ty::TraitRef::from_lang_item(self.tcx, LangItem::Sized, cause.span, [subty]);
self.out.push(traits::Obligation::with_depth(
self.tcx,
cause,