Skip to content

Commit ad57f88

Browse files
committed
Add helper to create the trait ref for a lang item
1 parent 25c4760 commit ad57f88

File tree

10 files changed

+51
-66
lines changed

10 files changed

+51
-66
lines changed

compiler/rustc_borrowck/src/type_check/mod.rs

Lines changed: 9 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -547,11 +547,7 @@ impl<'a, 'b, 'tcx> TypeVerifier<'a, 'b, 'tcx> {
547547

548548
if let PlaceContext::NonMutatingUse(NonMutatingUseContext::Copy) = context {
549549
let tcx = self.tcx();
550-
let trait_ref = tcx.mk_trait_ref(
551-
tcx.require_lang_item(LangItem::Copy, Some(self.last_span)),
552-
place_ty.ty,
553-
[],
554-
);
550+
let trait_ref = tcx.at(self.last_span).mk_trait_ref(LangItem::Copy, place_ty.ty, []);
555551

556552
// To have a `Copy` operand, the type `T` of the
557553
// value must be `Copy`. Note that we prove that `T: Copy`,
@@ -1274,11 +1270,8 @@ impl<'a, 'tcx> TypeChecker<'a, 'tcx> {
12741270

12751271
self.check_rvalue(body, rv, location);
12761272
if !self.unsized_feature_enabled() {
1277-
let trait_ref = tcx.mk_trait_ref(
1278-
tcx.require_lang_item(LangItem::Sized, Some(self.last_span)),
1279-
place_ty,
1280-
[],
1281-
);
1273+
let trait_ref =
1274+
tcx.at(self.last_span).mk_trait_ref(LangItem::Sized, place_ty, []);
12821275
self.prove_trait_ref(
12831276
trait_ref,
12841277
location.to_locations(),
@@ -1842,6 +1835,7 @@ impl<'a, 'tcx> TypeChecker<'a, 'tcx> {
18421835
#[instrument(skip(self, body), level = "debug")]
18431836
fn check_rvalue(&mut self, body: &Body<'tcx>, rvalue: &Rvalue<'tcx>, location: Location) {
18441837
let tcx = self.tcx();
1838+
let span = body.source_info(location).span;
18451839

18461840
match rvalue {
18471841
Rvalue::Aggregate(ak, ops) => {
@@ -1865,13 +1859,8 @@ impl<'a, 'tcx> TypeChecker<'a, 'tcx> {
18651859
}
18661860
Operand::Move(place) => {
18671861
// Make sure that repeated elements implement `Copy`.
1868-
let span = body.source_info(location).span;
18691862
let ty = place.ty(body, tcx).ty;
1870-
let trait_ref = tcx.mk_trait_ref(
1871-
tcx.require_lang_item(LangItem::Copy, Some(span)),
1872-
ty,
1873-
[],
1874-
);
1863+
let trait_ref = tcx.at(span).mk_trait_ref(LangItem::Copy, ty, []);
18751864

18761865
self.prove_trait_ref(
18771866
trait_ref,
@@ -1884,11 +1873,7 @@ impl<'a, 'tcx> TypeChecker<'a, 'tcx> {
18841873
}
18851874

18861875
&Rvalue::NullaryOp(NullOp::SizeOf | NullOp::AlignOf, ty) => {
1887-
let trait_ref = tcx.mk_trait_ref(
1888-
tcx.require_lang_item(LangItem::Sized, Some(self.last_span)),
1889-
ty,
1890-
[],
1891-
);
1876+
let trait_ref = tcx.at(span).mk_trait_ref(LangItem::Sized, ty, []);
18921877

18931878
self.prove_trait_ref(
18941879
trait_ref,
@@ -1900,11 +1885,7 @@ impl<'a, 'tcx> TypeChecker<'a, 'tcx> {
19001885
Rvalue::ShallowInitBox(operand, ty) => {
19011886
self.check_operand(operand, location);
19021887

1903-
let trait_ref = tcx.mk_trait_ref(
1904-
tcx.require_lang_item(LangItem::Sized, Some(self.last_span)),
1905-
*ty,
1906-
[],
1907-
);
1888+
let trait_ref = tcx.at(span).mk_trait_ref(LangItem::Sized, *ty, []);
19081889

19091890
self.prove_trait_ref(
19101891
trait_ref,
@@ -2001,8 +1982,8 @@ impl<'a, 'tcx> TypeChecker<'a, 'tcx> {
20011982

20021983
CastKind::Pointer(PointerCast::Unsize) => {
20031984
let &ty = ty;
2004-
let trait_ref = tcx.mk_trait_ref(
2005-
tcx.require_lang_item(LangItem::CoerceUnsized, Some(self.last_span)),
1985+
let trait_ref = tcx.at(span).mk_trait_ref(
1986+
LangItem::CoerceUnsized,
20061987
op.ty(body, tcx),
20071988
[ty.into()],
20081989
);

compiler/rustc_const_eval/src/transform/check_consts/qualifs.rs

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -153,14 +153,12 @@ impl Qualif for NeedsNonConstDrop {
153153
return false;
154154
}
155155

156-
let destruct = cx.tcx.require_lang_item(LangItem::Destruct, None);
157-
158156
let obligation = Obligation::new(
159157
cx.tcx,
160-
ObligationCause::dummy(),
158+
ObligationCause::dummy_with_span(cx.body.span),
161159
cx.param_env,
162160
ty::Binder::dummy(ty::TraitPredicate {
163-
trait_ref: cx.tcx.mk_trait_ref(destruct, ty, []),
161+
trait_ref: cx.tcx.at(cx.body.span).mk_trait_ref(LangItem::Destruct, ty, []),
164162
constness: ty::BoundConstness::ConstIfConst,
165163
polarity: ty::ImplPolarity::Positive,
166164
}),

compiler/rustc_hir_analysis/src/bounds.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -60,10 +60,10 @@ impl<'tcx> Bounds<'tcx> {
6060
{
6161
// If it could be sized, and is, add the `Sized` predicate.
6262
let sized_predicate = self.implicitly_sized.and_then(|span| {
63-
tcx.lang_items().sized_trait().map(move |sized| {
64-
let trait_ref = ty::Binder::dummy(tcx.mk_trait_ref(sized, param_ty, []));
65-
(trait_ref.without_const().to_predicate(tcx), span)
66-
})
63+
// FIXME: use tcx.at(span).mk_trait_ref(LangItem::Sized) here? This may make no-core code harder to write.
64+
let sized = tcx.lang_items().sized_trait()?;
65+
let trait_ref = ty::Binder::dummy(tcx.mk_trait_ref(sized, param_ty, []));
66+
Some((trait_ref.without_const().to_predicate(tcx), span))
6767
});
6868

6969
let region_preds = self.region_bounds.iter().map(move |&(region_bound, span)| {

compiler/rustc_hir_analysis/src/check/wfcheck.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1722,7 +1722,7 @@ fn receiver_is_valid<'tcx>(
17221722
// The first type is `receiver_ty`, which we know its not equal to `self_ty`; skip it.
17231723
autoderef.next();
17241724

1725-
let receiver_trait_def_id = tcx.require_lang_item(LangItem::Receiver, None);
1725+
let receiver_trait_def_id = tcx.require_lang_item(LangItem::Receiver, Some(span));
17261726

17271727
// Keep dereferencing `receiver_ty` until we get to `self_ty`.
17281728
loop {

compiler/rustc_middle/src/ty/context.rs

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2990,6 +2990,16 @@ impl<'tcx> TyCtxtAt<'tcx> {
29902990
pub fn ty_error_with_message(self, msg: &str) -> Ty<'tcx> {
29912991
self.tcx.ty_error_with_message(self.span, msg)
29922992
}
2993+
2994+
pub fn mk_trait_ref(
2995+
self,
2996+
trait_lang_item: LangItem,
2997+
self_ty: Ty<'tcx>,
2998+
rest: impl IntoIterator<Item = ty::GenericArg<'tcx>, IntoIter: ExactSizeIterator>,
2999+
) -> ty::TraitRef<'tcx> {
3000+
let trait_def_id = self.require_lang_item(trait_lang_item, Some(self.span));
3001+
self.tcx.mk_trait_ref(trait_def_id, self_ty, rest)
3002+
}
29933003
}
29943004

29953005
/// Parameter attributes that can only be determined by examining the body of a function instead

compiler/rustc_monomorphize/src/collector.rs

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -187,6 +187,7 @@ use rustc_middle::mir::visit::Visitor as MirVisitor;
187187
use rustc_middle::mir::{self, Local, Location};
188188
use rustc_middle::ty::adjustment::{CustomCoerceUnsized, PointerCast};
189189
use rustc_middle::ty::print::with_no_trimmed_paths;
190+
use rustc_middle::ty::query::TyCtxtAt;
190191
use rustc_middle::ty::subst::{GenericArgKind, InternalSubsts};
191192
use rustc_middle::ty::{
192193
self, GenericParamDefKind, Instance, Ty, TyCtxt, TypeFoldable, TypeVisitable, VtblEntry,
@@ -688,7 +689,7 @@ impl<'a, 'tcx> MirVisitor<'tcx> for MirNeighborCollector<'a, 'tcx> {
688689
let source_ty = operand.ty(self.body, self.tcx);
689690
let source_ty = self.monomorphize(source_ty);
690691
let (source_ty, target_ty) =
691-
find_vtable_types_for_unsizing(self.tcx, source_ty, target_ty);
692+
find_vtable_types_for_unsizing(self.tcx.at(span), source_ty, target_ty);
692693
// This could also be a different Unsize instruction, like
693694
// from a fixed sized array to a slice. But we are only
694695
// interested in things that produce a vtable.
@@ -1053,14 +1054,14 @@ fn should_codegen_locally<'tcx>(tcx: TyCtxt<'tcx>, instance: &Instance<'tcx>) ->
10531054
/// Finally, there is also the case of custom unsizing coercions, e.g., for
10541055
/// smart pointers such as `Rc` and `Arc`.
10551056
fn find_vtable_types_for_unsizing<'tcx>(
1056-
tcx: TyCtxt<'tcx>,
1057+
tcx: TyCtxtAt<'tcx>,
10571058
source_ty: Ty<'tcx>,
10581059
target_ty: Ty<'tcx>,
10591060
) -> (Ty<'tcx>, Ty<'tcx>) {
10601061
let ptr_vtable = |inner_source: Ty<'tcx>, inner_target: Ty<'tcx>| {
10611062
let param_env = ty::ParamEnv::reveal_all();
10621063
let type_has_metadata = |ty: Ty<'tcx>| -> bool {
1063-
if ty.is_sized(tcx, param_env) {
1064+
if ty.is_sized(tcx.tcx, param_env) {
10641065
return false;
10651066
}
10661067
let tail = tcx.struct_tail_erasing_lifetimes(ty, param_env);
@@ -1104,8 +1105,8 @@ fn find_vtable_types_for_unsizing<'tcx>(
11041105

11051106
find_vtable_types_for_unsizing(
11061107
tcx,
1107-
source_fields[coerce_index].ty(tcx, source_substs),
1108-
target_fields[coerce_index].ty(tcx, target_substs),
1108+
source_fields[coerce_index].ty(*tcx, source_substs),
1109+
target_fields[coerce_index].ty(*tcx, target_substs),
11091110
)
11101111
}
11111112
_ => bug!(

compiler/rustc_monomorphize/src/lib.rs

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,8 @@ extern crate rustc_middle;
1313
use rustc_hir::lang_items::LangItem;
1414
use rustc_middle::traits;
1515
use rustc_middle::ty::adjustment::CustomCoerceUnsized;
16-
use rustc_middle::ty::query::Providers;
17-
use rustc_middle::ty::{self, Ty, TyCtxt};
16+
use rustc_middle::ty::query::{Providers, TyCtxtAt};
17+
use rustc_middle::ty::{self, Ty};
1818

1919
mod collector;
2020
mod errors;
@@ -23,13 +23,12 @@ mod polymorphize;
2323
mod util;
2424

2525
fn custom_coerce_unsize_info<'tcx>(
26-
tcx: TyCtxt<'tcx>,
26+
tcx: TyCtxtAt<'tcx>,
2727
source_ty: Ty<'tcx>,
2828
target_ty: Ty<'tcx>,
2929
) -> CustomCoerceUnsized {
30-
let def_id = tcx.require_lang_item(LangItem::CoerceUnsized, None);
31-
32-
let trait_ref = ty::Binder::dummy(tcx.mk_trait_ref(def_id, source_ty, [target_ty.into()]));
30+
let trait_ref =
31+
ty::Binder::dummy(tcx.mk_trait_ref(LangItem::CoerceUnsized, source_ty, [target_ty.into()]));
3332

3433
match tcx.codegen_select_candidate((ty::ParamEnv::reveal_all(), trait_ref)) {
3534
Ok(traits::ImplSource::UserDefined(traits::ImplSourceUserDefinedData {

compiler/rustc_trait_selection/src/traits/project.rs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1710,8 +1710,8 @@ fn assemble_candidates_from_impls<'cx, 'tcx>(
17101710
if selcx.infcx().predicate_must_hold_modulo_regions(
17111711
&obligation.with(
17121712
selcx.tcx(),
1713-
ty::Binder::dummy(selcx.tcx().mk_trait_ref(
1714-
selcx.tcx().require_lang_item(LangItem::Sized, None),
1713+
ty::Binder::dummy(selcx.tcx().at(obligation.cause.span).mk_trait_ref(
1714+
LangItem::Sized,
17151715
self_ty, [],
17161716
))
17171717
.without_const(),
@@ -1966,8 +1966,8 @@ fn confirm_pointee_candidate<'cx, 'tcx>(
19661966
)
19671967
});
19681968
if check_is_sized {
1969-
let sized_predicate = ty::Binder::dummy(tcx.mk_trait_ref(
1970-
tcx.require_lang_item(LangItem::Sized, None),
1969+
let sized_predicate = ty::Binder::dummy(tcx.at(obligation.cause.span).mk_trait_ref(
1970+
LangItem::Sized,
19711971
self_ty,
19721972
[],
19731973
))
@@ -1976,7 +1976,7 @@ fn confirm_pointee_candidate<'cx, 'tcx>(
19761976
}
19771977

19781978
let substs = tcx.mk_substs([self_ty.into()].iter());
1979-
let metadata_def_id = tcx.require_lang_item(LangItem::Metadata, None);
1979+
let metadata_def_id = tcx.require_lang_item(LangItem::Metadata, Some(obligation.cause.span));
19801980

19811981
let predicate = ty::ProjectionPredicate {
19821982
projection_ty: ty::ProjectionTy { substs, item_def_id: metadata_def_id },

compiler/rustc_trait_selection/src/traits/select/confirmation.rs

Lines changed: 8 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -632,8 +632,8 @@ impl<'cx, 'tcx> SelectionContext<'cx, 'tcx> {
632632
output_ty,
633633
&mut nested,
634634
);
635-
let tr = ty::Binder::dummy(self.tcx().mk_trait_ref(
636-
self.tcx().require_lang_item(LangItem::Sized, None),
635+
let tr = ty::Binder::dummy(self.tcx().at(cause.span).mk_trait_ref(
636+
LangItem::Sized,
637637
output_ty,
638638
[],
639639
));
@@ -997,11 +997,8 @@ impl<'cx, 'tcx> SelectionContext<'cx, 'tcx> {
997997
);
998998

999999
// We can only make objects from sized types.
1000-
let tr = ty::Binder::dummy(tcx.mk_trait_ref(
1001-
tcx.require_lang_item(LangItem::Sized, None),
1002-
source,
1003-
[],
1004-
));
1000+
let tr =
1001+
ty::Binder::dummy(tcx.at(cause.span).mk_trait_ref(LangItem::Sized, source, []));
10051002
nested.push(predicate_to_obligation(tr.without_const().to_predicate(tcx)));
10061003

10071004
// If the type is `Foo + 'a`, ensure that the type
@@ -1255,8 +1252,8 @@ impl<'cx, 'tcx> SelectionContext<'cx, 'tcx> {
12551252
cause.clone(),
12561253
obligation.recursion_depth + 1,
12571254
self_ty.rebind(ty::TraitPredicate {
1258-
trait_ref: self.tcx().mk_trait_ref(
1259-
self.tcx().require_lang_item(LangItem::Destruct, None),
1255+
trait_ref: self.tcx().at(cause.span).mk_trait_ref(
1256+
LangItem::Destruct,
12601257
nested_ty,
12611258
[],
12621259
),
@@ -1280,8 +1277,8 @@ impl<'cx, 'tcx> SelectionContext<'cx, 'tcx> {
12801277
// or it's an ADT (and we need to check for a custom impl during selection)
12811278
_ => {
12821279
let predicate = self_ty.rebind(ty::TraitPredicate {
1283-
trait_ref: self.tcx().mk_trait_ref(
1284-
self.tcx().require_lang_item(LangItem::Destruct, None),
1280+
trait_ref: self.tcx().at(cause.span).mk_trait_ref(
1281+
LangItem::Destruct,
12851282
nested_ty,
12861283
[],
12871284
),

compiler/rustc_trait_selection/src/traits/wf.rs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -421,8 +421,7 @@ impl<'tcx> WfPredicates<'tcx> {
421421
fn require_sized(&mut self, subty: Ty<'tcx>, cause: traits::ObligationCauseCode<'tcx>) {
422422
if !subty.has_escaping_bound_vars() {
423423
let cause = self.cause(cause);
424-
let trait_ref =
425-
self.tcx.mk_trait_ref(self.tcx.require_lang_item(LangItem::Sized, None), subty, []);
424+
let trait_ref = self.tcx.at(cause.span).mk_trait_ref(LangItem::Sized, subty, []);
426425
self.out.push(traits::Obligation::with_depth(
427426
self.tcx,
428427
cause,

0 commit comments

Comments
 (0)