Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Replace tcx.mk_trait_ref with TraitRef::new #110806

Merged
merged 13 commits into from
May 4, 2023
Merged
Show file tree
Hide file tree
Changes from 7 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion compiler/rustc_borrowck/src/diagnostics/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1111,7 +1111,7 @@ impl<'cx, 'tcx> MirBorrowckCtxt<'cx, 'tcx> {
});
}
if let Some(clone_trait) = tcx.lang_items().clone_trait()
&& let trait_ref = tcx.mk_trait_ref(clone_trait, [ty])
&& let trait_ref = ty::TraitRef::new(tcx, clone_trait, [ty])
&& let o = Obligation::new(
tcx,
ObligationCause::dummy(),
Expand Down
25 changes: 16 additions & 9 deletions compiler/rustc_borrowck/src/type_check/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -538,7 +538,8 @@ impl<'a, 'b, 'tcx> TypeVerifier<'a, 'b, 'tcx> {

if let PlaceContext::NonMutatingUse(NonMutatingUseContext::Copy) = context {
let tcx = self.tcx();
let trait_ref = tcx.at(self.last_span).mk_trait_ref(LangItem::Copy, [place_ty.ty]);
let trait_ref =
ty::TraitRef::from_lang_item(tcx.at(self.last_span), LangItem::Copy, [place_ty.ty]);
WaffleLapkin marked this conversation as resolved.
Show resolved Hide resolved

// To have a `Copy` operand, the type `T` of the
// value must be `Copy`. Note that we prove that `T: Copy`,
Expand Down Expand Up @@ -1237,8 +1238,11 @@ impl<'a, 'tcx> TypeChecker<'a, 'tcx> {

self.check_rvalue(body, rv, location);
if !self.unsized_feature_enabled() {
let trait_ref =
tcx.at(self.last_span).mk_trait_ref(LangItem::Sized, [place_ty]);
let trait_ref = ty::TraitRef::from_lang_item(
tcx.at(self.last_span),
LangItem::Sized,
[place_ty],
);
self.prove_trait_ref(
trait_ref,
location.to_locations(),
Expand Down Expand Up @@ -1810,7 +1814,8 @@ impl<'a, 'tcx> TypeChecker<'a, 'tcx> {
Operand::Move(place) => {
// Make sure that repeated elements implement `Copy`.
let ty = place.ty(body, tcx).ty;
let trait_ref = tcx.at(span).mk_trait_ref(LangItem::Copy, [ty]);
let trait_ref =
ty::TraitRef::from_lang_item(tcx.at(span), LangItem::Copy, [ty]);

self.prove_trait_ref(
trait_ref,
Expand All @@ -1823,7 +1828,7 @@ impl<'a, 'tcx> TypeChecker<'a, 'tcx> {
}

&Rvalue::NullaryOp(NullOp::SizeOf | NullOp::AlignOf, ty) => {
let trait_ref = tcx.at(span).mk_trait_ref(LangItem::Sized, [ty]);
let trait_ref = ty::TraitRef::from_lang_item(tcx.at(span), LangItem::Sized, [ty]);

self.prove_trait_ref(
trait_ref,
Expand All @@ -1835,7 +1840,7 @@ impl<'a, 'tcx> TypeChecker<'a, 'tcx> {
Rvalue::ShallowInitBox(operand, ty) => {
self.check_operand(operand, location);

let trait_ref = tcx.at(span).mk_trait_ref(LangItem::Sized, [*ty]);
let trait_ref = ty::TraitRef::from_lang_item(tcx.at(span), LangItem::Sized, [*ty]);

self.prove_trait_ref(
trait_ref,
Expand Down Expand Up @@ -1932,9 +1937,11 @@ impl<'a, 'tcx> TypeChecker<'a, 'tcx> {

CastKind::Pointer(PointerCast::Unsize) => {
let &ty = ty;
let trait_ref = tcx
.at(span)
.mk_trait_ref(LangItem::CoerceUnsized, [op.ty(body, tcx), ty]);
let trait_ref = ty::TraitRef::from_lang_item(
tcx.at(span),
LangItem::CoerceUnsized,
[op.ty(body, tcx), ty],
);

self.prove_trait_ref(
trait_ref,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -157,8 +157,12 @@ impl Qualif for NeedsNonConstDrop {
cx.tcx,
ObligationCause::dummy_with_span(cx.body.span),
cx.param_env,
ty::Binder::dummy(cx.tcx.at(cx.body.span).mk_trait_ref(LangItem::Destruct, [ty]))
.with_constness(ty::BoundConstness::ConstIfConst),
ty::Binder::dummy(ty::TraitRef::from_lang_item(
cx.tcx.at(cx.body.span),
LangItem::Destruct,
[ty],
))
.with_constness(ty::BoundConstness::ConstIfConst),
);

let infcx = cx.tcx.infer_ctxt().build();
Expand Down
4 changes: 2 additions & 2 deletions compiler/rustc_hir_analysis/src/astconv/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -689,7 +689,7 @@ impl<'o, 'tcx> dyn AstConv<'tcx> + 'o {
let assoc_bindings = self.create_assoc_bindings_for_generic_args(args);

let poly_trait_ref =
ty::Binder::bind_with_vars(tcx.mk_trait_ref(trait_def_id, substs), bound_vars);
ty::Binder::bind_with_vars(ty::TraitRef::new(tcx, trait_def_id, substs), bound_vars);

debug!(?poly_trait_ref, ?assoc_bindings);
bounds.push_trait_bound(tcx, poly_trait_ref, span, constness);
Expand Down Expand Up @@ -822,7 +822,7 @@ impl<'o, 'tcx> dyn AstConv<'tcx> + 'o {
if let Some(b) = trait_segment.args().bindings.first() {
prohibit_assoc_ty_binding(self.tcx(), b.span, Some((trait_segment, span)));
}
self.tcx().mk_trait_ref(trait_def_id, substs)
ty::TraitRef::new(self.tcx(), trait_def_id, substs)
}

#[instrument(level = "debug", skip(self, span))]
Expand Down
2 changes: 1 addition & 1 deletion compiler/rustc_hir_analysis/src/autoderef.rs
Original file line number Diff line number Diff line change
Expand Up @@ -123,7 +123,7 @@ impl<'a, 'tcx> Autoderef<'a, 'tcx> {
let tcx = self.infcx.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 cause = traits::ObligationCause::misc(self.span, self.body_id);

Expand Down
2 changes: 1 addition & 1 deletion compiler/rustc_hir_analysis/src/bounds.rs
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ impl<'tcx> Bounds<'tcx> {

pub fn push_sized(&mut self, tcx: TyCtxt<'tcx>, ty: Ty<'tcx>, span: Span) {
let sized_def_id = tcx.require_lang_item(LangItem::Sized, Some(span));
let trait_ref = ty::Binder::dummy(tcx.mk_trait_ref(sized_def_id, [ty]));
let trait_ref = ty::Binder::dummy(ty::TraitRef::new(tcx, sized_def_id, [ty]));
lcnr marked this conversation as resolved.
Show resolved Hide resolved
// Preferable to put this obligation first, since we report better errors for sized ambiguity.
self.predicates.insert(0, (trait_ref.without_const().to_predicate(tcx), span));
}
Expand Down
2 changes: 1 addition & 1 deletion compiler/rustc_hir_analysis/src/check/check.rs
Original file line number Diff line number Diff line change
Expand Up @@ -538,7 +538,7 @@ fn check_item_type(tcx: TyCtxt<'_>, id: hir::ItemId) {
tcx,
assoc_item,
assoc_item,
tcx.mk_trait_ref(id.owner_id.to_def_id(), trait_substs),
ty::TraitRef::new(tcx, id.owner_id.to_def_id(), trait_substs),
);
}
_ => {}
Expand Down
2 changes: 1 addition & 1 deletion compiler/rustc_hir_analysis/src/check/wfcheck.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1784,7 +1784,7 @@ fn receiver_is_implemented<'tcx>(
receiver_ty: Ty<'tcx>,
) -> bool {
let tcx = wfcx.tcx();
let trait_ref = ty::Binder::dummy(tcx.mk_trait_ref(receiver_trait_def_id, [receiver_ty]));
let trait_ref = ty::TraitRef::new(tcx, receiver_trait_def_id, [receiver_ty]);

let obligation = traits::Obligation::new(tcx, cause, wfcx.param_env, trait_ref);

Expand Down
13 changes: 9 additions & 4 deletions compiler/rustc_hir_analysis/src/coherence/builtin.rs
Original file line number Diff line number Diff line change
Expand Up @@ -340,10 +340,11 @@ fn visit_implementation_of_dispatch_from_dyn(tcx: TyCtxt<'_>, impl_did: LocalDef
tcx,
cause.clone(),
param_env,
ty::Binder::dummy(tcx.mk_trait_ref(
ty::TraitRef::new(
tcx,
dispatch_from_dyn_trait,
[field.ty(tcx, substs_a), field.ty(tcx, substs_b)],
)),
),
));
}
let errors = ocx.select_all_or_error();
Expand Down Expand Up @@ -579,8 +580,12 @@ pub fn coerce_unsized_info<'tcx>(tcx: TyCtxt<'tcx>, impl_did: LocalDefId) -> Coe
// Register an obligation for `A: Trait<B>`.
let ocx = ObligationCtxt::new(&infcx);
let cause = traits::ObligationCause::misc(span, impl_did);
let obligation =
Obligation::new(tcx, cause, param_env, tcx.mk_trait_ref(trait_def_id, [source, target]));
let obligation = Obligation::new(
tcx,
cause,
param_env,
ty::TraitRef::new(tcx, trait_def_id, [source, target]),
);
ocx.register_obligation(obligation);
let errors = ocx.select_all_or_error();
if !errors.is_empty() {
Expand Down
10 changes: 6 additions & 4 deletions compiler/rustc_hir_typeck/src/coercion.rs
Original file line number Diff line number Diff line change
Expand Up @@ -601,7 +601,7 @@ impl<'f, 'tcx> Coerce<'f, 'tcx> {
self.tcx,
cause,
self.fcx.param_env,
self.tcx.mk_trait_ref(coerce_unsized_did, [coerce_source, coerce_target])
ty::TraitRef::new(self.tcx, coerce_unsized_did, [coerce_source, coerce_target])
)];

let mut has_unsized_tuple_coercion = false;
Expand Down Expand Up @@ -764,9 +764,11 @@ impl<'f, 'tcx> Coerce<'f, 'tcx> {
self.tcx,
self.cause.clone(),
self.param_env,
ty::Binder::dummy(
self.tcx.at(self.cause.span).mk_trait_ref(hir::LangItem::PointerLike, [a]),
),
ty::Binder::dummy(ty::TraitRef::from_lang_item(
WaffleLapkin marked this conversation as resolved.
Show resolved Hide resolved
self.tcx.at(self.cause.span),
hir::LangItem::PointerLike,
[a],
)),
));

Ok(InferOk {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -481,7 +481,8 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
// For the purposes of this function, we hope that it is a `struct` type, and that our current `expr` is a literal of
// that struct type.
let impl_trait_self_ref = if self.tcx.is_trait_alias(obligation.impl_or_alias_def_id) {
self.tcx.mk_trait_ref(
ty::TraitRef::new(
self.tcx,
obligation.impl_or_alias_def_id,
ty::InternalSubsts::identity_for_item(self.tcx, obligation.impl_or_alias_def_id),
)
Expand Down
2 changes: 1 addition & 1 deletion compiler/rustc_hir_typeck/src/fn_ctxt/checks.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1911,7 +1911,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
_ => {
// Look for a user-provided impl of a `Fn` trait, and point to it.
let new_def_id = self.probe(|_| {
let trait_ref = self.tcx.mk_trait_ref(
let trait_ref = ty::TraitRef::new(self.tcx,
call_kind.to_def_id(self.tcx),
[
callee_ty,
Expand Down
6 changes: 3 additions & 3 deletions compiler/rustc_hir_typeck/src/fn_ctxt/suggestions.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1096,10 +1096,10 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
self.tcx,
self.misc(expr.span),
self.param_env,
ty::Binder::dummy(self.tcx.mk_trait_ref(
ty::TraitRef::new(self.tcx,
into_def_id,
[expr_ty, expected_ty]
)),
),
))
{
let sugg = if expr.precedence().order() >= PREC_POSTFIX {
Expand Down Expand Up @@ -1438,7 +1438,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
&& !results.expr_adjustments(callee_expr).iter().any(|adj| matches!(adj.kind, ty::adjustment::Adjust::Deref(..)))
// Check that we're in fact trying to clone into the expected type
&& self.can_coerce(*pointee_ty, expected_ty)
&& let trait_ref = ty::Binder::dummy(self.tcx.mk_trait_ref(clone_trait_did, [expected_ty]))
&& let trait_ref = ty::TraitRef::new(self.tcx, clone_trait_did, [expected_ty])
// And the expected type doesn't implement `Clone`
&& !self.predicate_must_hold_considering_regions(&traits::Obligation::new(
self.tcx,
Expand Down
2 changes: 1 addition & 1 deletion compiler/rustc_hir_typeck/src/method/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -316,7 +316,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
self.var_for_def(cause.span, param)
});

let trait_ref = self.tcx.mk_trait_ref(trait_def_id, substs);
let trait_ref = ty::TraitRef::new(self.tcx, trait_def_id, substs);

// Construct an obligation
let poly_trait_ref = ty::Binder::dummy(trait_ref);
Expand Down
2 changes: 1 addition & 1 deletion compiler/rustc_hir_typeck/src/method/probe.rs
Original file line number Diff line number Diff line change
Expand Up @@ -954,7 +954,7 @@ impl<'a, 'tcx> ProbeContext<'a, 'tcx> {
) {
debug!("assemble_extension_candidates_for_trait(trait_def_id={:?})", trait_def_id);
let trait_substs = self.fresh_item_substs(trait_def_id);
let trait_ref = self.tcx.mk_trait_ref(trait_def_id, trait_substs);
let trait_ref = ty::TraitRef::new(self.tcx, trait_def_id, trait_substs);

if self.tcx.is_trait_alias(trait_def_id) {
// For trait aliases, recursively assume all explicitly named traits are relevant
Expand Down
3 changes: 2 additions & 1 deletion compiler/rustc_hir_typeck/src/method/suggest.rs
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,8 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
self.autoderef(span, ty).any(|(ty, _)| {
info!("check deref {:?} impl FnOnce", ty);
self.probe(|_| {
let trait_ref = tcx.mk_trait_ref(
let trait_ref = ty::TraitRef::new(
tcx,
fn_once,
[
ty,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -261,11 +261,16 @@ impl<'tcx> NiceRegionError<'_, 'tcx> {
(false, None, None, Some(span), String::new())
};

let expected_trait_ref = self
.cx
.resolve_vars_if_possible(self.cx.tcx.mk_trait_ref(trait_def_id, expected_substs));
let actual_trait_ref =
self.cx.resolve_vars_if_possible(self.cx.tcx.mk_trait_ref(trait_def_id, actual_substs));
let expected_trait_ref = self.cx.resolve_vars_if_possible(ty::TraitRef::new(
self.cx.tcx,
trait_def_id,
expected_substs,
));
let actual_trait_ref = self.cx.resolve_vars_if_possible(ty::TraitRef::new(
self.cx.tcx,
trait_def_id,
actual_substs,
));

// Search the expected and actual trait references to see (a)
// whether the sub/sup placeholders appear in them (sometimes
Expand Down
2 changes: 1 addition & 1 deletion compiler/rustc_infer/src/traits/engine.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ pub trait TraitEngine<'tcx>: 'tcx {
def_id: DefId,
cause: ObligationCause<'tcx>,
) {
let trait_ref = infcx.tcx.mk_trait_ref(def_id, [ty]);
let trait_ref = ty::TraitRef::new(infcx.tcx, def_id, [ty]);
self.register_predicate_obligation(
infcx,
Obligation {
Expand Down
20 changes: 1 addition & 19 deletions compiler/rustc_middle/src/ty/context.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1861,7 +1861,7 @@ impl<'tcx> TyCtxt<'tcx> {
}

#[inline(always)]
fn check_and_mk_substs(
pub(crate) fn check_and_mk_substs(
self,
_def_id: DefId,
substs: impl IntoIterator<Item: Into<GenericArg<'tcx>>>,
Expand Down Expand Up @@ -2261,15 +2261,6 @@ impl<'tcx> TyCtxt<'tcx> {
self.mk_substs_from_iter(iter::once(self_ty.into()).chain(rest))
}

pub fn mk_trait_ref(
self,
trait_def_id: DefId,
substs: impl IntoIterator<Item: Into<GenericArg<'tcx>>>,
) -> ty::TraitRef<'tcx> {
let substs = self.check_and_mk_substs(trait_def_id, substs);
ty::TraitRef { def_id: trait_def_id, substs, _use_mk_trait_ref_instead: () }
}

pub fn mk_alias_ty(
self,
def_id: DefId,
Expand Down Expand Up @@ -2473,15 +2464,6 @@ impl<'tcx> TyCtxtAt<'tcx> {
pub fn ty_error_with_message(self, msg: &str) -> Ty<'tcx> {
self.tcx.ty_error_with_message(self.span, msg)
}

pub fn mk_trait_ref(
self,
trait_lang_item: LangItem,
substs: impl IntoIterator<Item: Into<ty::GenericArg<'tcx>>>,
) -> ty::TraitRef<'tcx> {
let trait_def_id = self.require_lang_item(trait_lang_item, Some(self.span));
self.tcx.mk_trait_ref(trait_def_id, substs)
}
}

/// Parameter attributes that can only be determined by examining the body of a function instead
Expand Down
7 changes: 5 additions & 2 deletions compiler/rustc_middle/src/ty/print/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -169,8 +169,11 @@ pub trait Printer<'tcx>: Sized {
self.path_append(
|cx: Self| {
if trait_qualify_parent {
let trait_ref =
cx.tcx().mk_trait_ref(parent_def_id, parent_substs.iter().copied());
let trait_ref = ty::TraitRef::new(
cx.tcx(),
parent_def_id,
parent_substs.iter().copied(),
);
cx.path_qualified(trait_ref.self_ty(), Some(trait_ref))
} else {
cx.print_def_path(parent_def_id, parent_substs)
Expand Down
2 changes: 1 addition & 1 deletion compiler/rustc_middle/src/ty/relate.rs
Original file line number Diff line number Diff line change
Expand Up @@ -315,7 +315,7 @@ impl<'tcx> Relate<'tcx> for ty::TraitRef<'tcx> {
Err(TypeError::Traits(expected_found(relation, a.def_id, b.def_id)))
} else {
let substs = relate_substs(relation, a.substs, b.substs)?;
Ok(relation.tcx().mk_trait_ref(a.def_id, substs))
Ok(ty::TraitRef::new(relation.tcx(), a.def_id, substs))
}
}
}
Expand Down
Loading