Skip to content

Commit 19dacee

Browse files
committed
Auto merge of rust-lang#124982 - compiler-errors:uplift-trait-ref, r=lcnr
Uplift `TraitRef` into `rustc_type_ir` Emotional rollercoaster r? lcnr
2 parents 6e1d947 + b55d8a3 commit 19dacee

File tree

55 files changed

+492
-313
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

55 files changed

+492
-313
lines changed

compiler/rustc_borrowck/src/diagnostics/conflict_errors.rs

+1
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ use rustc_middle::mir::{
2222
Operand, Place, PlaceRef, ProjectionElem, Rvalue, Statement, StatementKind, Terminator,
2323
TerminatorKind, VarBindingForm,
2424
};
25+
use rustc_middle::ty::print::PrintTraitRefExt as _;
2526
use rustc_middle::ty::{
2627
self, suggest_constraining_type_params, PredicateKind, ToPredicate, Ty, TyCtxt,
2728
TypeSuperVisitable, TypeVisitor,

compiler/rustc_borrowck/src/type_check/mod.rs

+24-12
Original file line numberDiff line numberDiff line change
@@ -532,8 +532,11 @@ impl<'a, 'b, 'tcx> TypeVerifier<'a, 'b, 'tcx> {
532532

533533
if let PlaceContext::NonMutatingUse(NonMutatingUseContext::Copy) = context {
534534
let tcx = self.tcx();
535-
let trait_ref =
536-
ty::TraitRef::from_lang_item(tcx, LangItem::Copy, self.last_span, [place_ty.ty]);
535+
let trait_ref = ty::TraitRef::new(
536+
tcx,
537+
tcx.require_lang_item(LangItem::Copy, Some(self.last_span)),
538+
[place_ty.ty],
539+
);
537540

538541
// To have a `Copy` operand, the type `T` of the
539542
// value must be `Copy`. Note that we prove that `T: Copy`,
@@ -1273,10 +1276,9 @@ impl<'a, 'tcx> TypeChecker<'a, 'tcx> {
12731276

12741277
self.check_rvalue(body, rv, location);
12751278
if !self.unsized_feature_enabled() {
1276-
let trait_ref = ty::TraitRef::from_lang_item(
1279+
let trait_ref = ty::TraitRef::new(
12771280
tcx,
1278-
LangItem::Sized,
1279-
self.last_span,
1281+
tcx.require_lang_item(LangItem::Sized, Some(self.last_span)),
12801282
[place_ty],
12811283
);
12821284
self.prove_trait_ref(
@@ -1925,8 +1927,11 @@ impl<'a, 'tcx> TypeChecker<'a, 'tcx> {
19251927
Operand::Move(place) => {
19261928
// Make sure that repeated elements implement `Copy`.
19271929
let ty = place.ty(body, tcx).ty;
1928-
let trait_ref =
1929-
ty::TraitRef::from_lang_item(tcx, LangItem::Copy, span, [ty]);
1930+
let trait_ref = ty::TraitRef::new(
1931+
tcx,
1932+
tcx.require_lang_item(LangItem::Copy, Some(span)),
1933+
[ty],
1934+
);
19301935

19311936
self.prove_trait_ref(
19321937
trait_ref,
@@ -1939,7 +1944,11 @@ impl<'a, 'tcx> TypeChecker<'a, 'tcx> {
19391944
}
19401945

19411946
&Rvalue::NullaryOp(NullOp::SizeOf | NullOp::AlignOf, ty) => {
1942-
let trait_ref = ty::TraitRef::from_lang_item(tcx, LangItem::Sized, span, [ty]);
1947+
let trait_ref = ty::TraitRef::new(
1948+
tcx,
1949+
tcx.require_lang_item(LangItem::Sized, Some(span)),
1950+
[ty],
1951+
);
19431952

19441953
self.prove_trait_ref(
19451954
trait_ref,
@@ -1952,7 +1961,11 @@ impl<'a, 'tcx> TypeChecker<'a, 'tcx> {
19521961
Rvalue::ShallowInitBox(operand, ty) => {
19531962
self.check_operand(operand, location);
19541963

1955-
let trait_ref = ty::TraitRef::from_lang_item(tcx, LangItem::Sized, span, [*ty]);
1964+
let trait_ref = ty::TraitRef::new(
1965+
tcx,
1966+
tcx.require_lang_item(LangItem::Sized, Some(span)),
1967+
[*ty],
1968+
);
19561969

19571970
self.prove_trait_ref(
19581971
trait_ref,
@@ -2050,10 +2063,9 @@ impl<'a, 'tcx> TypeChecker<'a, 'tcx> {
20502063

20512064
CastKind::PointerCoercion(PointerCoercion::Unsize) => {
20522065
let &ty = ty;
2053-
let trait_ref = ty::TraitRef::from_lang_item(
2066+
let trait_ref = ty::TraitRef::new(
20542067
tcx,
2055-
LangItem::CoerceUnsized,
2056-
span,
2068+
tcx.require_lang_item(LangItem::CoerceUnsized, Some(span)),
20572069
[op.ty(body, tcx), ty],
20582070
);
20592071

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

+1-1
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ use rustc_hir::def_id::DefId;
88
use rustc_infer::infer::TyCtxtInferExt;
99
use rustc_infer::traits::{ImplSource, Obligation, ObligationCause};
1010
use rustc_middle::mir::{self, CallSource};
11-
use rustc_middle::ty::print::with_no_trimmed_paths;
11+
use rustc_middle::ty::print::{with_no_trimmed_paths, PrintTraitRefExt as _};
1212
use rustc_middle::ty::{
1313
self, suggest_constraining_type_param, Closure, FnDef, FnPtr, GenericArgKind, GenericArgsRef,
1414
Param, TraitRef, Ty,

compiler/rustc_errors/src/diagnostic_impls.rs

+6
Original file line numberDiff line numberDiff line change
@@ -94,6 +94,12 @@ into_diag_arg_using_display!(
9494
ErrCode,
9595
);
9696

97+
impl<I: rustc_type_ir::Interner> IntoDiagArg for rustc_type_ir::TraitRef<I> {
98+
fn into_diag_arg(self) -> DiagArgValue {
99+
self.to_string().into_diag_arg()
100+
}
101+
}
102+
97103
into_diag_arg_for_number!(i8, u8, i16, u16, i32, u32, i64, u64, i128, u128, isize, usize);
98104

99105
impl IntoDiagArg for bool {

compiler/rustc_hir_analysis/src/coherence/builtin.rs

+1
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ use rustc_infer::infer::TyCtxtInferExt;
1414
use rustc_infer::infer::{self, RegionResolutionError};
1515
use rustc_infer::traits::Obligation;
1616
use rustc_middle::ty::adjustment::CoerceUnsizedInfo;
17+
use rustc_middle::ty::print::PrintTraitRefExt as _;
1718
use rustc_middle::ty::{self, suggest_constraining_type_params, Ty, TyCtxt, TypeVisitableExt};
1819
use rustc_span::{Span, DUMMY_SP};
1920
use rustc_trait_selection::traits::error_reporting::TypeErrCtxtExt;

compiler/rustc_hir_analysis/src/coherence/unsafety.rs

+1
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
44
use rustc_errors::{codes::*, struct_span_code_err};
55
use rustc_hir::Unsafety;
6+
use rustc_middle::ty::print::PrintTraitRefExt as _;
67
use rustc_middle::ty::{ImplPolarity::*, ImplTraitHeader, TraitDef, TyCtxt};
78
use rustc_span::def_id::LocalDefId;
89
use rustc_span::ErrorGuaranteed;

compiler/rustc_hir_analysis/src/hir_ty_lowering/bounds.rs

+1
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ use rustc_errors::{codes::*, struct_span_code_err};
55
use rustc_hir as hir;
66
use rustc_hir::def::{DefKind, Res};
77
use rustc_hir::def_id::{DefId, LocalDefId};
8+
use rustc_middle::ty::print::PrintTraitRefExt as _;
89
use rustc_middle::ty::{self as ty, IsSuggestable, Ty, TyCtxt};
910
use rustc_span::symbol::Ident;
1011
use rustc_span::{ErrorGuaranteed, Span, Symbol};

compiler/rustc_hir_analysis/src/hir_ty_lowering/errors.rs

+1
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ use rustc_hir::def::{DefKind, Res};
1717
use rustc_hir::def_id::{DefId, LocalDefId};
1818
use rustc_infer::traits::FulfillmentError;
1919
use rustc_middle::query::Key;
20+
use rustc_middle::ty::print::PrintTraitRefExt as _;
2021
use rustc_middle::ty::GenericParamDefKind;
2122
use rustc_middle::ty::{self, suggest_constraining_type_param};
2223
use rustc_middle::ty::{AdtDef, Ty, TyCtxt, TypeVisitableExt};

compiler/rustc_hir_typeck/src/coercion.rs

+2-3
Original file line numberDiff line numberDiff line change
@@ -758,10 +758,9 @@ impl<'f, 'tcx> Coerce<'f, 'tcx> {
758758
self.tcx,
759759
self.cause.clone(),
760760
self.param_env,
761-
ty::TraitRef::from_lang_item(
761+
ty::TraitRef::new(
762762
self.tcx,
763-
hir::LangItem::PointerLike,
764-
self.cause.span,
763+
self.tcx.require_lang_item(hir::LangItem::PointerLike, Some(self.cause.span)),
765764
[a],
766765
),
767766
));

compiler/rustc_hir_typeck/src/method/suggest.rs

+3-1
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,9 @@ use rustc_hir::{ExprKind, Node, QPath};
2525
use rustc_infer::infer::{self, RegionVariableOrigin};
2626
use rustc_middle::ty::fast_reject::DeepRejectCtxt;
2727
use rustc_middle::ty::fast_reject::{simplify_type, TreatParams};
28-
use rustc_middle::ty::print::{with_crate_prefix, with_forced_trimmed_paths};
28+
use rustc_middle::ty::print::{
29+
with_crate_prefix, with_forced_trimmed_paths, PrintTraitRefExt as _,
30+
};
2931
use rustc_middle::ty::IsSuggestable;
3032
use rustc_middle::ty::{self, GenericArgKind, Ty, TyCtxt, TypeVisitableExt};
3133
use rustc_span::def_id::DefIdSet;

compiler/rustc_infer/src/infer/error_reporting/mod.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,7 @@ use rustc_hir::intravisit::Visitor;
7070
use rustc_hir::lang_items::LangItem;
7171
use rustc_macros::extension;
7272
use rustc_middle::dep_graph::DepContext;
73-
use rustc_middle::ty::print::{with_forced_trimmed_paths, PrintError};
73+
use rustc_middle::ty::print::{with_forced_trimmed_paths, PrintError, PrintTraitRefExt as _};
7474
use rustc_middle::ty::relate::{self, RelateResult, TypeRelation};
7575
use rustc_middle::ty::ToPredicate;
7676
use rustc_middle::ty::{

compiler/rustc_infer/src/infer/error_reporting/nice_region_error/placeholder_error.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ use rustc_errors::{Diag, IntoDiagArg};
1212
use rustc_hir::def::Namespace;
1313
use rustc_hir::def_id::DefId;
1414
use rustc_middle::ty::error::ExpectedFound;
15-
use rustc_middle::ty::print::{FmtPrinter, Print, RegionHighlightMode};
15+
use rustc_middle::ty::print::{FmtPrinter, Print, PrintTraitRefExt as _, RegionHighlightMode};
1616
use rustc_middle::ty::GenericArgsRef;
1717
use rustc_middle::ty::{self, RePlaceholder, Region, TyCtxt};
1818

compiler/rustc_lint/src/context.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ use rustc_hir::definitions::{DefPathData, DisambiguatedDefPathData};
3030
use rustc_middle::bug;
3131
use rustc_middle::middle::privacy::EffectiveVisibilities;
3232
use rustc_middle::ty::layout::{LayoutError, LayoutOfHelpers, TyAndLayout};
33-
use rustc_middle::ty::print::{with_no_trimmed_paths, PrintError};
33+
use rustc_middle::ty::print::{with_no_trimmed_paths, PrintError, PrintTraitRefExt as _};
3434
use rustc_middle::ty::{self, print::Printer, GenericArg, RegisteredTools, Ty, TyCtxt};
3535
use rustc_session::lint::{BuiltinLintDiag, LintExpectationId};
3636
use rustc_session::lint::{FutureIncompatibleInfo, Level, Lint, LintBuffer, LintId};

compiler/rustc_macros/src/lift.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ pub fn lift_derive(mut s: synstructure::Structure<'_>) -> proc_macro2::TokenStre
4141

4242
s.add_impl_generic(newtcx);
4343
s.bound_impl(
44-
quote!(::rustc_middle::ty::Lift<'__lifted>),
44+
quote!(::rustc_middle::ty::Lift<::rustc_middle::ty::TyCtxt<'__lifted>>),
4545
quote! {
4646
type Lifted = #lifted;
4747

compiler/rustc_middle/src/macros.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@ macro_rules! span_bug {
5757
macro_rules! TrivialLiftImpls {
5858
($($ty:ty),+ $(,)?) => {
5959
$(
60-
impl<'tcx> $crate::ty::Lift<'tcx> for $ty {
60+
impl<'tcx> $crate::ty::Lift<$crate::ty::TyCtxt<'tcx>> for $ty {
6161
type Lifted = Self;
6262
fn lift_to_tcx(self, _: $crate::ty::TyCtxt<'tcx>) -> Option<Self> {
6363
Some(self)

compiler/rustc_middle/src/ty/consts.rs

+7-9
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ use rustc_hir::def::{DefKind, Res};
88
use rustc_hir::def_id::LocalDefId;
99
use rustc_macros::{HashStable, TyDecodable, TyEncodable};
1010
use rustc_type_ir::ConstKind as IrConstKind;
11-
use rustc_type_ir::{ConstTy, IntoKind, TypeFlags, WithCachedTypeInfo};
11+
use rustc_type_ir::{TypeFlags, WithCachedTypeInfo};
1212

1313
mod int;
1414
mod kind;
@@ -30,7 +30,7 @@ rustc_data_structures::static_assert_size!(ConstKind<'_>, 32);
3030
#[rustc_pass_by_value]
3131
pub struct Const<'tcx>(pub(super) Interned<'tcx, WithCachedTypeInfo<ConstData<'tcx>>>);
3232

33-
impl<'tcx> IntoKind for Const<'tcx> {
33+
impl<'tcx> rustc_type_ir::inherent::IntoKind for Const<'tcx> {
3434
type Kind = ConstKind<'tcx>;
3535

3636
fn kind(self) -> ConstKind<'tcx> {
@@ -48,12 +48,6 @@ impl<'tcx> rustc_type_ir::visit::Flags for Const<'tcx> {
4848
}
4949
}
5050

51-
impl<'tcx> ConstTy<TyCtxt<'tcx>> for Const<'tcx> {
52-
fn ty(self) -> Ty<'tcx> {
53-
self.ty()
54-
}
55-
}
56-
5751
/// Typed constant value.
5852
#[derive(Copy, Clone, PartialEq, Eq, Hash)]
5953
#[derive(HashStable, TyEncodable, TyDecodable)]
@@ -180,7 +174,7 @@ impl<'tcx> Const<'tcx> {
180174
}
181175
}
182176

183-
impl<'tcx> rustc_type_ir::new::Const<TyCtxt<'tcx>> for Const<'tcx> {
177+
impl<'tcx> rustc_type_ir::inherent::Const<TyCtxt<'tcx>> for Const<'tcx> {
184178
fn new_anon_bound(
185179
tcx: TyCtxt<'tcx>,
186180
debruijn: ty::DebruijnIndex,
@@ -189,6 +183,10 @@ impl<'tcx> rustc_type_ir::new::Const<TyCtxt<'tcx>> for Const<'tcx> {
189183
) -> Self {
190184
Const::new_bound(tcx, debruijn, var, ty)
191185
}
186+
187+
fn ty(self) -> Ty<'tcx> {
188+
self.ty()
189+
}
192190
}
193191

194192
impl<'tcx> Const<'tcx> {

compiler/rustc_middle/src/ty/context.rs

+19-26
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@
44

55
pub mod tls;
66

7+
pub use rustc_type_ir::lift::Lift;
8+
79
use crate::arena::Arena;
810
use crate::dep_graph::{DepGraph, DepKindStruct};
911
use crate::infer::canonical::{CanonicalParamEnvCache, CanonicalVarInfo, CanonicalVarInfos};
@@ -138,6 +140,19 @@ impl<'tcx> Interner for TyCtxt<'tcx> {
138140
fn mk_canonical_var_infos(self, infos: &[ty::CanonicalVarInfo<Self>]) -> Self::CanonicalVars {
139141
self.mk_canonical_var_infos(infos)
140142
}
143+
144+
type GenericsOf = &'tcx ty::Generics;
145+
fn generics_of(self, def_id: DefId) -> &'tcx ty::Generics {
146+
self.generics_of(def_id)
147+
}
148+
149+
fn check_and_mk_args(
150+
self,
151+
def_id: DefId,
152+
args: impl IntoIterator<Item: Into<ty::GenericArg<'tcx>>>,
153+
) -> ty::GenericArgsRef<'tcx> {
154+
self.check_and_mk_args(def_id, args)
155+
}
141156
}
142157

143158
type InternedSet<'tcx, T> = ShardedHashMap<InternedInSet<'tcx, T>, ()>;
@@ -917,7 +932,7 @@ impl<'tcx> TyCtxt<'tcx> {
917932
)
918933
}
919934

920-
pub fn lift<T: Lift<'tcx>>(self, value: T) -> Option<T::Lifted> {
935+
pub fn lift<T: Lift<TyCtxt<'tcx>>>(self, value: T) -> Option<T::Lifted> {
921936
value.lift_to_tcx(self)
922937
}
923938

@@ -1524,31 +1539,9 @@ impl<'tcx> TyCtxt<'tcx> {
15241539
}
15251540
}
15261541

1527-
/// A trait implemented for all `X<'a>` types that can be safely and
1528-
/// efficiently converted to `X<'tcx>` as long as they are part of the
1529-
/// provided `TyCtxt<'tcx>`.
1530-
/// This can be done, for example, for `Ty<'tcx>` or `GenericArgsRef<'tcx>`
1531-
/// by looking them up in their respective interners.
1532-
///
1533-
/// However, this is still not the best implementation as it does
1534-
/// need to compare the components, even for interned values.
1535-
/// It would be more efficient if `TypedArena` provided a way to
1536-
/// determine whether the address is in the allocated range.
1537-
///
1538-
/// `None` is returned if the value or one of the components is not part
1539-
/// of the provided context.
1540-
/// For `Ty`, `None` can be returned if either the type interner doesn't
1541-
/// contain the `TyKind` key or if the address of the interned
1542-
/// pointer differs. The latter case is possible if a primitive type,
1543-
/// e.g., `()` or `u8`, was interned in a different context.
1544-
pub trait Lift<'tcx>: fmt::Debug {
1545-
type Lifted: fmt::Debug + 'tcx;
1546-
fn lift_to_tcx(self, tcx: TyCtxt<'tcx>) -> Option<Self::Lifted>;
1547-
}
1548-
15491542
macro_rules! nop_lift {
15501543
($set:ident; $ty:ty => $lifted:ty) => {
1551-
impl<'a, 'tcx> Lift<'tcx> for $ty {
1544+
impl<'a, 'tcx> Lift<TyCtxt<'tcx>> for $ty {
15521545
type Lifted = $lifted;
15531546
fn lift_to_tcx(self, tcx: TyCtxt<'tcx>) -> Option<Self::Lifted> {
15541547
// Assert that the set has the right type.
@@ -1583,7 +1576,7 @@ macro_rules! nop_lift {
15831576

15841577
macro_rules! nop_list_lift {
15851578
($set:ident; $ty:ty => $lifted:ty) => {
1586-
impl<'a, 'tcx> Lift<'tcx> for &'a List<$ty> {
1579+
impl<'a, 'tcx> Lift<TyCtxt<'tcx>> for &'a List<$ty> {
15871580
type Lifted = &'tcx List<$lifted>;
15881581
fn lift_to_tcx(self, tcx: TyCtxt<'tcx>) -> Option<Self::Lifted> {
15891582
// Assert that the set has the right type.
@@ -1621,7 +1614,7 @@ nop_list_lift! {args; GenericArg<'a> => GenericArg<'tcx>}
16211614

16221615
macro_rules! nop_slice_lift {
16231616
($ty:ty => $lifted:ty) => {
1624-
impl<'a, 'tcx> Lift<'tcx> for &'a [$ty] {
1617+
impl<'a, 'tcx> Lift<TyCtxt<'tcx>> for &'a [$ty] {
16251618
type Lifted = &'tcx [$lifted];
16261619
fn lift_to_tcx(self, tcx: TyCtxt<'tcx>) -> Option<Self::Lifted> {
16271620
if self.is_empty() {

compiler/rustc_middle/src/ty/generic_args.rs

+11-1
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,16 @@ pub struct GenericArg<'tcx> {
3939
marker: PhantomData<(Ty<'tcx>, ty::Region<'tcx>, ty::Const<'tcx>)>,
4040
}
4141

42+
impl<'tcx> rustc_type_ir::inherent::GenericArgs<TyCtxt<'tcx>> for ty::GenericArgsRef<'tcx> {
43+
fn type_at(self, i: usize) -> Ty<'tcx> {
44+
self.type_at(i)
45+
}
46+
47+
fn identity_for_item(tcx: TyCtxt<'tcx>, def_id: DefId) -> ty::GenericArgsRef<'tcx> {
48+
GenericArgs::identity_for_item(tcx, def_id)
49+
}
50+
}
51+
4252
#[cfg(parallel_compiler)]
4353
unsafe impl<'tcx> rustc_data_structures::sync::DynSend for GenericArg<'tcx> where
4454
&'tcx (Ty<'tcx>, ty::Region<'tcx>, ty::Const<'tcx>): rustc_data_structures::sync::DynSend
@@ -205,7 +215,7 @@ impl<'tcx> GenericArg<'tcx> {
205215
}
206216
}
207217

208-
impl<'a, 'tcx> Lift<'tcx> for GenericArg<'a> {
218+
impl<'a, 'tcx> Lift<TyCtxt<'tcx>> for GenericArg<'a> {
209219
type Lifted = GenericArg<'tcx>;
210220

211221
fn lift_to_tcx(self, tcx: TyCtxt<'tcx>) -> Option<Self::Lifted> {

compiler/rustc_middle/src/ty/generics.rs

+6
Original file line numberDiff line numberDiff line change
@@ -145,6 +145,12 @@ pub struct Generics {
145145
pub host_effect_index: Option<usize>,
146146
}
147147

148+
impl<'tcx> rustc_type_ir::inherent::GenericsOf<TyCtxt<'tcx>> for &'tcx Generics {
149+
fn count(&self) -> usize {
150+
self.parent_count + self.own_params.len()
151+
}
152+
}
153+
148154
impl<'tcx> Generics {
149155
/// Looks through the generics and all parents to find the index of the
150156
/// given param def-id. This is in comparison to the `param_def_id_to_index`

0 commit comments

Comments
 (0)