Skip to content

Commit 712e7c3

Browse files
Rollup merge of #125088 - compiler-errors:uplift-alias-ty, r=lcnr
Uplift `AliasTy` and `AliasTerm` Follow-up from #125076. r? lcnr
2 parents 8c64acd + 9f8cdb2 commit 712e7c3

File tree

15 files changed

+574
-534
lines changed

15 files changed

+574
-534
lines changed

compiler/rustc_errors/src/diagnostic_impls.rs

+6
Original file line numberDiff line numberDiff line change
@@ -106,6 +106,12 @@ impl<I: rustc_type_ir::Interner> IntoDiagArg for rustc_type_ir::ExistentialTrait
106106
}
107107
}
108108

109+
impl<I: rustc_type_ir::Interner> IntoDiagArg for rustc_type_ir::UnevaluatedConst<I> {
110+
fn into_diag_arg(self) -> rustc_errors::DiagArgValue {
111+
format!("{self:?}").into_diag_arg()
112+
}
113+
}
114+
109115
into_diag_arg_for_number!(i8, u8, i16, u16, i32, u32, i64, u64, i128, u128, isize, usize);
110116

111117
impl IntoDiagArg for bool {

compiler/rustc_middle/src/ty/consts.rs

+11-3
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,7 @@ use rustc_hir as hir;
77
use rustc_hir::def::{DefKind, Res};
88
use rustc_hir::def_id::LocalDefId;
99
use rustc_macros::{HashStable, TyDecodable, TyEncodable};
10-
use rustc_type_ir::ConstKind as IrConstKind;
11-
use rustc_type_ir::{TypeFlags, WithCachedTypeInfo};
10+
use rustc_type_ir::{self as ir, TypeFlags, WithCachedTypeInfo};
1211

1312
mod int;
1413
mod kind;
@@ -20,7 +19,8 @@ use rustc_span::Span;
2019
use rustc_span::DUMMY_SP;
2120
pub use valtree::*;
2221

23-
pub type ConstKind<'tcx> = IrConstKind<TyCtxt<'tcx>>;
22+
pub type ConstKind<'tcx> = ir::ConstKind<TyCtxt<'tcx>>;
23+
pub type UnevaluatedConst<'tcx> = ir::UnevaluatedConst<TyCtxt<'tcx>>;
2424

2525
#[cfg(target_pointer_width = "64")]
2626
rustc_data_structures::static_assert_size!(ConstKind<'_>, 32);
@@ -184,6 +184,14 @@ impl<'tcx> rustc_type_ir::inherent::Const<TyCtxt<'tcx>> for Const<'tcx> {
184184
Const::new_bound(tcx, debruijn, var, ty)
185185
}
186186

187+
fn new_unevaluated(
188+
interner: TyCtxt<'tcx>,
189+
uv: ty::UnevaluatedConst<'tcx>,
190+
ty: Ty<'tcx>,
191+
) -> Self {
192+
Const::new_unevaluated(interner, uv, ty)
193+
}
194+
187195
fn ty(self) -> Ty<'tcx> {
188196
self.ty()
189197
}

compiler/rustc_middle/src/ty/consts/kind.rs

+4-26
Original file line numberDiff line numberDiff line change
@@ -1,30 +1,15 @@
11
use super::Const;
22
use crate::mir;
33
use crate::ty::abstract_const::CastKind;
4-
use crate::ty::GenericArgsRef;
54
use crate::ty::{self, visit::TypeVisitableExt as _, List, Ty, TyCtxt};
6-
use rustc_hir::def_id::DefId;
7-
use rustc_macros::{HashStable, TyDecodable, TyEncodable, TypeFoldable, TypeVisitable};
5+
use rustc_macros::{extension, HashStable, TyDecodable, TyEncodable, TypeFoldable, TypeVisitable};
86

9-
/// An unevaluated (potentially generic) constant used in the type-system.
10-
#[derive(Copy, Clone, Eq, PartialEq, TyEncodable, TyDecodable)]
11-
#[derive(Hash, HashStable, TypeFoldable, TypeVisitable)]
12-
pub struct UnevaluatedConst<'tcx> {
13-
pub def: DefId,
14-
pub args: GenericArgsRef<'tcx>,
15-
}
16-
17-
impl rustc_errors::IntoDiagArg for UnevaluatedConst<'_> {
18-
fn into_diag_arg(self) -> rustc_errors::DiagArgValue {
19-
format!("{self:?}").into_diag_arg()
20-
}
21-
}
22-
23-
impl<'tcx> UnevaluatedConst<'tcx> {
7+
#[extension(pub(crate) trait UnevaluatedConstEvalExt<'tcx>)]
8+
impl<'tcx> ty::UnevaluatedConst<'tcx> {
249
/// FIXME(RalfJung): I cannot explain what this does or why it makes sense, but not doing this
2510
/// hurts performance.
2611
#[inline]
27-
pub(crate) fn prepare_for_eval(
12+
fn prepare_for_eval(
2813
self,
2914
tcx: TyCtxt<'tcx>,
3015
param_env: ty::ParamEnv<'tcx>,
@@ -55,13 +40,6 @@ impl<'tcx> UnevaluatedConst<'tcx> {
5540
}
5641
}
5742

58-
impl<'tcx> UnevaluatedConst<'tcx> {
59-
#[inline]
60-
pub fn new(def: DefId, args: GenericArgsRef<'tcx>) -> UnevaluatedConst<'tcx> {
61-
UnevaluatedConst { def, args }
62-
}
63-
}
64-
6543
#[derive(Copy, Clone, Eq, PartialEq, Hash)]
6644
#[derive(HashStable, TyEncodable, TyDecodable, TypeVisitable, TypeFoldable)]
6745
pub enum Expr<'tcx> {

compiler/rustc_middle/src/ty/context.rs

+67-9
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,7 @@ use rustc_type_ir::TyKind::*;
7676
use rustc_type_ir::WithCachedTypeInfo;
7777
use rustc_type_ir::{CollectAndApply, Interner, TypeFlags};
7878

79+
use std::assert_matches::assert_matches;
7980
use std::borrow::Borrow;
8081
use std::cmp::Ordering;
8182
use std::fmt;
@@ -91,67 +92,124 @@ impl<'tcx> Interner for TyCtxt<'tcx> {
9192
type DefiningOpaqueTypes = &'tcx ty::List<LocalDefId>;
9293
type AdtDef = ty::AdtDef<'tcx>;
9394
type GenericArgs = ty::GenericArgsRef<'tcx>;
95+
type GenericArgsSlice = &'tcx [ty::GenericArg<'tcx>];
9496
type GenericArg = ty::GenericArg<'tcx>;
95-
type Term = ty::Term<'tcx>;
9697

98+
type Term = ty::Term<'tcx>;
9799
type Binder<T: TypeVisitable<TyCtxt<'tcx>>> = Binder<'tcx, T>;
98100
type BoundVars = &'tcx List<ty::BoundVariableKind>;
99101
type BoundVar = ty::BoundVariableKind;
100-
type CanonicalVars = CanonicalVarInfos<'tcx>;
101102

103+
type CanonicalVars = CanonicalVarInfos<'tcx>;
102104
type Ty = Ty<'tcx>;
103105
type Tys = &'tcx List<Ty<'tcx>>;
104-
type AliasTy = ty::AliasTy<'tcx>;
105106
type ParamTy = ParamTy;
106107
type BoundTy = ty::BoundTy;
107108
type PlaceholderTy = ty::PlaceholderType;
108-
type ErrorGuaranteed = ErrorGuaranteed;
109109

110+
type ErrorGuaranteed = ErrorGuaranteed;
110111
type BoundExistentialPredicates = &'tcx List<PolyExistentialPredicate<'tcx>>;
111112
type PolyFnSig = PolyFnSig<'tcx>;
112113
type AllocId = crate::mir::interpret::AllocId;
113-
type Pat = Pattern<'tcx>;
114114

115+
type Pat = Pattern<'tcx>;
115116
type Const = ty::Const<'tcx>;
116117
type AliasConst = ty::UnevaluatedConst<'tcx>;
117118
type PlaceholderConst = ty::PlaceholderConst;
118119
type ParamConst = ty::ParamConst;
119120
type BoundConst = ty::BoundVar;
120121
type ValueConst = ty::ValTree<'tcx>;
121-
type ExprConst = ty::Expr<'tcx>;
122122

123+
type ExprConst = ty::Expr<'tcx>;
123124
type Region = Region<'tcx>;
124125
type EarlyParamRegion = ty::EarlyParamRegion;
125126
type LateParamRegion = ty::LateParamRegion;
126127
type BoundRegion = ty::BoundRegion;
127128
type InferRegion = ty::RegionVid;
128-
type PlaceholderRegion = ty::PlaceholderRegion;
129129

130+
type PlaceholderRegion = ty::PlaceholderRegion;
130131
type Predicate = Predicate<'tcx>;
131132
type TraitPredicate = ty::TraitPredicate<'tcx>;
132133
type RegionOutlivesPredicate = ty::RegionOutlivesPredicate<'tcx>;
133134
type TypeOutlivesPredicate = ty::TypeOutlivesPredicate<'tcx>;
134135
type ProjectionPredicate = ty::ProjectionPredicate<'tcx>;
135-
type AliasTerm = ty::AliasTerm<'tcx>;
136136
type NormalizesTo = ty::NormalizesTo<'tcx>;
137137
type SubtypePredicate = ty::SubtypePredicate<'tcx>;
138138
type CoercePredicate = ty::CoercePredicate<'tcx>;
139139
type ClosureKind = ty::ClosureKind;
140-
type Clauses = ty::Clauses<'tcx>;
141140

141+
type Clauses = ty::Clauses<'tcx>;
142142
fn mk_canonical_var_infos(self, infos: &[ty::CanonicalVarInfo<Self>]) -> Self::CanonicalVars {
143143
self.mk_canonical_var_infos(infos)
144144
}
145145

146146
type GenericsOf = &'tcx ty::Generics;
147+
147148
fn generics_of(self, def_id: DefId) -> &'tcx ty::Generics {
148149
self.generics_of(def_id)
149150
}
150151

152+
fn type_of_instantiated(self, def_id: DefId, args: ty::GenericArgsRef<'tcx>) -> Ty<'tcx> {
153+
self.type_of(def_id).instantiate(self, args)
154+
}
155+
156+
fn alias_ty_kind(self, alias: ty::AliasTy<'tcx>) -> ty::AliasTyKind {
157+
match self.def_kind(alias.def_id) {
158+
DefKind::AssocTy => {
159+
if let DefKind::Impl { of_trait: false } = self.def_kind(self.parent(alias.def_id))
160+
{
161+
ty::Inherent
162+
} else {
163+
ty::Projection
164+
}
165+
}
166+
DefKind::OpaqueTy => ty::Opaque,
167+
DefKind::TyAlias => ty::Weak,
168+
kind => bug!("unexpected DefKind in AliasTy: {kind:?}"),
169+
}
170+
}
171+
172+
fn alias_term_kind(self, alias: ty::AliasTerm<'tcx>) -> ty::AliasTermKind {
173+
match self.def_kind(alias.def_id) {
174+
DefKind::AssocTy => {
175+
if let DefKind::Impl { of_trait: false } = self.def_kind(self.parent(alias.def_id))
176+
{
177+
ty::AliasTermKind::InherentTy
178+
} else {
179+
ty::AliasTermKind::ProjectionTy
180+
}
181+
}
182+
DefKind::OpaqueTy => ty::AliasTermKind::OpaqueTy,
183+
DefKind::TyAlias => ty::AliasTermKind::WeakTy,
184+
DefKind::AssocConst => ty::AliasTermKind::ProjectionConst,
185+
DefKind::AnonConst => ty::AliasTermKind::UnevaluatedConst,
186+
kind => bug!("unexpected DefKind in AliasTy: {kind:?}"),
187+
}
188+
}
189+
190+
fn trait_ref_and_own_args_for_alias(
191+
self,
192+
def_id: Self::DefId,
193+
args: Self::GenericArgs,
194+
) -> (rustc_type_ir::TraitRef<Self>, Self::GenericArgsSlice) {
195+
assert_matches!(self.def_kind(def_id), DefKind::AssocTy | DefKind::AssocConst);
196+
let trait_def_id = self.parent(def_id);
197+
assert_matches!(self.def_kind(trait_def_id), DefKind::Trait);
198+
let trait_generics = self.generics_of(trait_def_id);
199+
(
200+
ty::TraitRef::new(self, trait_def_id, args.truncate_to(self, trait_generics)),
201+
&args[trait_generics.count()..],
202+
)
203+
}
204+
151205
fn mk_args(self, args: &[Self::GenericArg]) -> Self::GenericArgs {
152206
self.mk_args(args)
153207
}
154208

209+
fn mk_args_from_iter(self, args: impl Iterator<Item = Self::GenericArg>) -> Self::GenericArgs {
210+
self.mk_args_from_iter(args)
211+
}
212+
155213
fn check_and_mk_args(
156214
self,
157215
def_id: DefId,

compiler/rustc_middle/src/ty/mod.rs

+8-8
Original file line numberDiff line numberDiff line change
@@ -96,9 +96,9 @@ pub use self::list::{List, ListWithCachedTypeInfo};
9696
pub use self::parameterized::ParameterizedOverTcx;
9797
pub use self::pattern::{Pattern, PatternKind};
9898
pub use self::predicate::{
99-
Clause, ClauseKind, CoercePredicate, ExistentialPredicate, ExistentialPredicateStableCmpExt,
100-
ExistentialProjection, ExistentialTraitRef, NormalizesTo, OutlivesPredicate,
101-
PolyCoercePredicate, PolyExistentialPredicate, PolyExistentialProjection,
99+
AliasTerm, Clause, ClauseKind, CoercePredicate, ExistentialPredicate,
100+
ExistentialPredicateStableCmpExt, ExistentialProjection, ExistentialTraitRef, NormalizesTo,
101+
OutlivesPredicate, PolyCoercePredicate, PolyExistentialPredicate, PolyExistentialProjection,
102102
PolyExistentialTraitRef, PolyProjectionPredicate, PolyRegionOutlivesPredicate,
103103
PolySubtypePredicate, PolyTraitPredicate, PolyTraitRef, PolyTypeOutlivesPredicate, Predicate,
104104
PredicateKind, ProjectionPredicate, RegionOutlivesPredicate, SubtypePredicate, ToPolyTraitRef,
@@ -110,11 +110,11 @@ pub use self::region::{
110110
};
111111
pub use self::rvalue_scopes::RvalueScopes;
112112
pub use self::sty::{
113-
AliasTerm, AliasTy, Article, Binder, BoundTy, BoundTyKind, BoundVariableKind,
114-
CanonicalPolyFnSig, ClosureArgs, ClosureArgsParts, CoroutineArgs, CoroutineArgsParts,
115-
CoroutineClosureArgs, CoroutineClosureArgsParts, CoroutineClosureSignature, FnSig, GenSig,
116-
InlineConstArgs, InlineConstArgsParts, ParamConst, ParamTy, PolyFnSig, TyKind, TypeAndMut,
117-
UpvarArgs, VarianceDiagInfo,
113+
AliasTy, Article, Binder, BoundTy, BoundTyKind, BoundVariableKind, CanonicalPolyFnSig,
114+
ClosureArgs, ClosureArgsParts, CoroutineArgs, CoroutineArgsParts, CoroutineClosureArgs,
115+
CoroutineClosureArgsParts, CoroutineClosureSignature, FnSig, GenSig, InlineConstArgs,
116+
InlineConstArgsParts, ParamConst, ParamTy, PolyFnSig, TyKind, TypeAndMut, UpvarArgs,
117+
VarianceDiagInfo,
118118
};
119119
pub use self::trait_def::TraitDef;
120120
pub use self::typeck_results::{

compiler/rustc_middle/src/ty/predicate.rs

+1
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ use crate::ty::{
1313
};
1414

1515
pub type TraitRef<'tcx> = ir::TraitRef<TyCtxt<'tcx>>;
16+
pub type AliasTerm<'tcx> = ir::AliasTerm<TyCtxt<'tcx>>;
1617
pub type ProjectionPredicate<'tcx> = ir::ProjectionPredicate<TyCtxt<'tcx>>;
1718
pub type ExistentialPredicate<'tcx> = ir::ExistentialPredicate<TyCtxt<'tcx>>;
1819
pub type ExistentialTraitRef<'tcx> = ir::ExistentialTraitRef<TyCtxt<'tcx>>;

compiler/rustc_middle/src/ty/print/pretty.rs

+27-27
Original file line numberDiff line numberDiff line change
@@ -3038,6 +3038,33 @@ define_print! {
30383038
p!(write("<{} as {}>", self.self_ty(), self.print_only_trait_path()))
30393039
}
30403040

3041+
ty::AliasTy<'tcx> {
3042+
let alias_term: ty::AliasTerm<'tcx> = (*self).into();
3043+
p!(print(alias_term))
3044+
}
3045+
3046+
ty::AliasTerm<'tcx> {
3047+
match self.kind(cx.tcx()) {
3048+
ty::AliasTermKind::InherentTy => p!(pretty_print_inherent_projection(*self)),
3049+
ty::AliasTermKind::ProjectionTy
3050+
| ty::AliasTermKind::WeakTy
3051+
| ty::AliasTermKind::OpaqueTy
3052+
| ty::AliasTermKind::UnevaluatedConst
3053+
| ty::AliasTermKind::ProjectionConst => {
3054+
// If we're printing verbosely, or don't want to invoke queries
3055+
// (`is_impl_trait_in_trait`), then fall back to printing the def path.
3056+
// This is likely what you want if you're debugging the compiler anyways.
3057+
if !(cx.should_print_verbose() || with_reduced_queries())
3058+
&& cx.tcx().is_impl_trait_in_trait(self.def_id)
3059+
{
3060+
return cx.pretty_print_opaque_impl_type(self.def_id, self.args);
3061+
} else {
3062+
p!(print_def_path(self.def_id, self.args));
3063+
}
3064+
}
3065+
}
3066+
}
3067+
30413068
ty::TraitPredicate<'tcx> {
30423069
p!(print(self.trait_ref.self_ty()), ": ");
30433070
p!(pretty_print_bound_constness(self.trait_ref));
@@ -3205,33 +3232,6 @@ define_print_and_forward_display! {
32053232
}
32063233
}
32073234

3208-
ty::AliasTy<'tcx> {
3209-
let alias_term: ty::AliasTerm<'tcx> = (*self).into();
3210-
p!(print(alias_term))
3211-
}
3212-
3213-
ty::AliasTerm<'tcx> {
3214-
match self.kind(cx.tcx()) {
3215-
ty::AliasTermKind::InherentTy => p!(pretty_print_inherent_projection(*self)),
3216-
ty::AliasTermKind::ProjectionTy
3217-
| ty::AliasTermKind::WeakTy
3218-
| ty::AliasTermKind::OpaqueTy
3219-
| ty::AliasTermKind::UnevaluatedConst
3220-
| ty::AliasTermKind::ProjectionConst => {
3221-
// If we're printing verbosely, or don't want to invoke queries
3222-
// (`is_impl_trait_in_trait`), then fall back to printing the def path.
3223-
// This is likely what you want if you're debugging the compiler anyways.
3224-
if !(cx.should_print_verbose() || with_reduced_queries())
3225-
&& cx.tcx().is_impl_trait_in_trait(self.def_id)
3226-
{
3227-
return cx.pretty_print_opaque_impl_type(self.def_id, self.args);
3228-
} else {
3229-
p!(print_def_path(self.def_id, self.args));
3230-
}
3231-
}
3232-
}
3233-
}
3234-
32353235
ty::Predicate<'tcx> {
32363236
p!(print(self.kind()))
32373237
}

0 commit comments

Comments
 (0)