Skip to content

Commit c3bd628

Browse files
committed
Auto merge of #154758 - WaffleLapkin:aliassss, r=lcnr
`ty::Alias` refactor This PR changes the following alias-related types from this: ```rust pub enum AliasTyKind { Projection, Inherent, Opaque, Free, } pub struct AliasTy<I: Interner> { pub args: I::GenericArgs, pub def_id: I::DefId, } pub enum TyKind<I: Interner> { ... Alias(AliasTyKind, AliasTy<I>), } ``` Into this: ```rust pub enum AliasTyKind<I: Interner> { Projection { def_id: I::DefId }, Inherent { def_id: I::DefId }, Opaque { def_id: I::DefId }, Free { def_id: I::DefId }, } pub struct AliasTy<I: Interner> { pub args: I::GenericArgs, pub kind: AliasTyKind<I>, } pub enum TyKind<I: Interner> { ... Alias(AliasTy<I>), } ``` ... and then does a thousand other changes to accommodate for this change everywhere. This brings us closer to being able to have `AliasTyKind`s which don't require a `DefId` (and thus can be more easily created, etc). Although notably we depend on both `AliasTyKind -> DefId` and `DefId -> AliasTyKind` conversions in a bunch of places still. r? lcnr ---- A lot of these changes were done either by search & replace (via `ast-grep`) or on auto pilot, so I'm not quite sure I didn't mess up somewhere, but at least tests pass...
2 parents 49b6ac0 + 6ab50d5 commit c3bd628

124 files changed

Lines changed: 991 additions & 691 deletions

File tree

Some content is hidden

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

compiler/rustc_borrowck/src/diagnostics/opaque_types.rs

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -219,11 +219,11 @@ impl<'tcx> TypeVisitor<TyCtxt<'tcx>> for FindOpaqueRegion<'_, 'tcx> {
219219
fn visit_ty(&mut self, ty: Ty<'tcx>) -> Self::Result {
220220
// If we find an opaque in a local ty, then for each of its captured regions,
221221
// try to find a path between that captured regions and our borrow region...
222-
if let ty::Alias(ty::Opaque, opaque) = *ty.kind()
222+
if let ty::Alias(opaque @ ty::AliasTy { kind: ty::Opaque { def_id }, .. }) = *ty.kind()
223223
&& let hir::OpaqueTyOrigin::FnReturn { parent, in_trait_or_impl: None } =
224-
self.tcx.opaque_ty_origin(opaque.def_id)
224+
self.tcx.opaque_ty_origin(def_id)
225225
{
226-
let variances = self.tcx.variances_of(opaque.def_id);
226+
let variances = self.tcx.variances_of(def_id);
227227
for (idx, (arg, variance)) in std::iter::zip(opaque.args, variances).enumerate() {
228228
// Skip uncaptured args.
229229
if *variance == ty::Bivariant {
@@ -252,7 +252,7 @@ impl<'tcx> TypeVisitor<TyCtxt<'tcx>> for FindOpaqueRegion<'_, 'tcx> {
252252
&& call_def_id == parent
253253
&& let Locations::Single(location) = constraint.locations
254254
{
255-
return ControlFlow::Break((opaque.def_id, idx, location));
255+
return ControlFlow::Break((def_id, idx, location));
256256
}
257257
}
258258
}
@@ -276,11 +276,11 @@ impl<'tcx> TypeVisitor<TyCtxt<'tcx>> for CheckExplicitRegionMentionAndCollectGen
276276

277277
fn visit_ty(&mut self, ty: Ty<'tcx>) -> Self::Result {
278278
match *ty.kind() {
279-
ty::Alias(ty::Opaque, opaque) => {
280-
if self.seen_opaques.insert(opaque.def_id) {
279+
ty::Alias(opaque @ ty::AliasTy { kind: ty::Opaque { def_id }, .. }) => {
280+
if self.seen_opaques.insert(def_id) {
281281
for (bound, _) in self
282282
.tcx
283-
.explicit_item_bounds(opaque.def_id)
283+
.explicit_item_bounds(def_id)
284284
.iter_instantiated_copied(self.tcx, opaque.args)
285285
{
286286
bound.visit_with(self)?;

compiler/rustc_borrowck/src/diagnostics/region_errors.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -594,7 +594,7 @@ impl<'infcx, 'tcx> MirBorrowckCtxt<'_, 'infcx, 'tcx> {
594594
let ErrorConstraintInfo { outlived_fr, span, .. } = errci;
595595

596596
let mut output_ty = self.regioncx.universal_regions().unnormalized_output_ty;
597-
if let ty::Alias(ty::Opaque, ty::AliasTy { def_id, .. }) = *output_ty.kind() {
597+
if let ty::Alias(ty::AliasTy { kind: ty::Opaque { def_id }, .. }) = *output_ty.kind() {
598598
output_ty = self.infcx.tcx.type_of(def_id).instantiate_identity()
599599
};
600600

compiler/rustc_borrowck/src/lib.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1899,7 +1899,7 @@ impl<'a, 'tcx> MirBorrowckCtxt<'a, '_, 'tcx> {
18991899
| ty::Never
19001900
| ty::Tuple(_)
19011901
| ty::UnsafeBinder(_)
1902-
| ty::Alias(_, _)
1902+
| ty::Alias(_)
19031903
| ty::Param(_)
19041904
| ty::Bound(_, _)
19051905
| ty::Infer(_)
@@ -1941,7 +1941,7 @@ impl<'a, 'tcx> MirBorrowckCtxt<'a, '_, 'tcx> {
19411941
| ty::CoroutineWitness(..)
19421942
| ty::Never
19431943
| ty::UnsafeBinder(_)
1944-
| ty::Alias(_, _)
1944+
| ty::Alias(_)
19451945
| ty::Param(_)
19461946
| ty::Bound(_, _)
19471947
| ty::Infer(_)

compiler/rustc_borrowck/src/region_infer/opaque_types/member_constraints.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -176,8 +176,8 @@ impl<'tcx> TypeVisitor<TyCtxt<'tcx>> for CollectMemberConstraintsVisitor<'_, '_,
176176
| ty::CoroutineClosure(def_id, args)
177177
| ty::Coroutine(def_id, args) => self.visit_closure_args(def_id, args),
178178

179-
ty::Alias(kind, ty::AliasTy { def_id, args, .. })
180-
if let Some(variances) = self.cx().opt_alias_variances(kind, def_id) =>
179+
ty::Alias(ty::AliasTy { kind, args, .. })
180+
if let Some(variances) = self.cx().opt_alias_variances(kind, kind.def_id()) =>
181181
{
182182
// Skip lifetime parameters that are not captured, since they do
183183
// not need member constraints registered for them; we'll erase

compiler/rustc_borrowck/src/region_infer/opaque_types/mod.rs

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -367,9 +367,10 @@ fn compute_definition_site_hidden_types_from_defining_uses<'tcx>(
367367
// usage of the opaque type and we can ignore it. This check is mirrored in typeck's
368368
// writeback.
369369
if !rcx.infcx.tcx.use_typing_mode_borrowck() {
370-
if let ty::Alias(ty::Opaque, alias_ty) = hidden_type.ty.skip_binder().kind()
371-
&& alias_ty.def_id == opaque_type_key.def_id.to_def_id()
372-
&& alias_ty.args == opaque_type_key.args
370+
if let &ty::Alias(ty::AliasTy { kind: ty::Opaque { def_id }, args, .. }) =
371+
hidden_type.ty.skip_binder().kind()
372+
&& def_id == opaque_type_key.def_id.to_def_id()
373+
&& args == opaque_type_key.args
373374
{
374375
continue;
375376
}
@@ -499,8 +500,8 @@ impl<'tcx> FallibleTypeFolder<TyCtxt<'tcx>> for ToArgRegionsFolder<'_, 'tcx> {
499500
Ty::new_coroutine(tcx, def_id, self.fold_closure_args(def_id, args)?)
500501
}
501502

502-
ty::Alias(kind, ty::AliasTy { def_id, args, .. })
503-
if let Some(variances) = tcx.opt_alias_variances(kind, def_id) =>
503+
ty::Alias(ty::AliasTy { kind, args, .. })
504+
if let Some(variances) = tcx.opt_alias_variances(kind, kind.def_id()) =>
504505
{
505506
let args = tcx.mk_args_from_iter(std::iter::zip(variances, args.iter()).map(
506507
|(&v, s)| {
@@ -511,7 +512,7 @@ impl<'tcx> FallibleTypeFolder<TyCtxt<'tcx>> for ToArgRegionsFolder<'_, 'tcx> {
511512
}
512513
},
513514
))?;
514-
ty::AliasTy::new_from_args(tcx, def_id, args).to_ty(tcx)
515+
ty::AliasTy::new_from_args(tcx, kind, args).to_ty(tcx)
515516
}
516517

517518
_ => ty.try_super_fold_with(self)?,
@@ -540,9 +541,10 @@ pub(crate) fn apply_definition_site_hidden_types<'tcx>(
540541
for &(key, hidden_type) in opaque_types {
541542
let Some(expected) = hidden_types.get(&key.def_id) else {
542543
if !tcx.use_typing_mode_borrowck() {
543-
if let ty::Alias(ty::Opaque, alias_ty) = hidden_type.ty.kind()
544-
&& alias_ty.def_id == key.def_id.to_def_id()
545-
&& alias_ty.args == key.args
544+
if let &ty::Alias(ty::AliasTy { kind: ty::Opaque { def_id }, args, .. }) =
545+
hidden_type.ty.kind()
546+
&& def_id == key.def_id.to_def_id()
547+
&& args == key.args
546548
{
547549
continue;
548550
} else {

compiler/rustc_borrowck/src/type_check/mod.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -450,7 +450,8 @@ impl<'a, 'tcx> TypeChecker<'a, 'tcx> {
450450
// Necessary for non-trivial patterns whose user-type annotation is an opaque type,
451451
// e.g. `let (_a,): Tait = whatever`, see #105897
452452
if !self.infcx.next_trait_solver()
453-
&& let ty::Alias(ty::Opaque, ..) = curr_projected_ty.ty.kind()
453+
&& let ty::Alias(ty::AliasTy { kind: ty::Opaque { .. }, .. }) =
454+
curr_projected_ty.ty.kind()
454455
{
455456
// There is nothing that we can compare here if we go through an opaque type.
456457
// We're always in its defining scope as we can otherwise not project through

compiler/rustc_borrowck/src/type_check/relate_tys.rs

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -150,8 +150,12 @@ impl<'a, 'b, 'tcx> NllTypeRelating<'a, 'b, 'tcx> {
150150
};
151151

152152
let (a, b) = match (a.kind(), b.kind()) {
153-
(&ty::Alias(ty::Opaque, ..), _) => (a, enable_subtyping(b, true)?),
154-
(_, &ty::Alias(ty::Opaque, ..)) => (enable_subtyping(a, false)?, b),
153+
(&ty::Alias(ty::AliasTy { kind: ty::Opaque { .. }, .. }), _) => {
154+
(a, enable_subtyping(b, true)?)
155+
}
156+
(_, &ty::Alias(ty::AliasTy { kind: ty::Opaque { .. }, .. })) => {
157+
(enable_subtyping(a, false)?, b)
158+
}
155159
_ => unreachable!(
156160
"expected at least one opaque type in `relate_opaques`, got {a} and {b}."
157161
),
@@ -382,8 +386,8 @@ impl<'b, 'tcx> TypeRelation<TyCtxt<'tcx>> for NllTypeRelating<'_, 'b, 'tcx> {
382386
}
383387

384388
(
385-
&ty::Alias(ty::Opaque, ty::AliasTy { def_id: a_def_id, .. }),
386-
&ty::Alias(ty::Opaque, ty::AliasTy { def_id: b_def_id, .. }),
389+
&ty::Alias(ty::AliasTy { kind: ty::Opaque { def_id: a_def_id }, .. }),
390+
&ty::Alias(ty::AliasTy { kind: ty::Opaque { def_id: b_def_id }, .. }),
387391
) if a_def_id == b_def_id || infcx.next_trait_solver() => {
388392
super_combine_tys(&infcx.infcx, self, a, b).map(|_| ()).or_else(|err| {
389393
// This behavior is only there for the old solver, the new solver
@@ -397,8 +401,8 @@ impl<'b, 'tcx> TypeRelation<TyCtxt<'tcx>> for NllTypeRelating<'_, 'b, 'tcx> {
397401
if a_def_id.is_local() { self.relate_opaques(a, b) } else { Err(err) }
398402
})?;
399403
}
400-
(&ty::Alias(ty::Opaque, ty::AliasTy { def_id, .. }), _)
401-
| (_, &ty::Alias(ty::Opaque, ty::AliasTy { def_id, .. }))
404+
(&ty::Alias(ty::AliasTy { kind: ty::Opaque { def_id }, .. }), _)
405+
| (_, &ty::Alias(ty::AliasTy { kind: ty::Opaque { def_id }, .. }))
402406
if def_id.is_local() && !self.type_checker.infcx.next_trait_solver() =>
403407
{
404408
self.relate_opaques(a, b)?;

compiler/rustc_const_eval/src/util/type_name.rs

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -53,14 +53,22 @@ impl<'tcx> Printer<'tcx> for TypeNamePrinter<'tcx> {
5353
// Types with identity (print the module path).
5454
ty::Adt(ty::AdtDef(Interned(&ty::AdtDefData { did: def_id, .. }, _)), args)
5555
| ty::FnDef(def_id, args)
56-
| ty::Alias(ty::Projection | ty::Opaque, ty::AliasTy { def_id, args, .. })
56+
| ty::Alias(ty::AliasTy {
57+
kind: ty::Projection { def_id } | ty::Opaque { def_id },
58+
args,
59+
..
60+
})
5761
| ty::Closure(def_id, args)
5862
| ty::CoroutineClosure(def_id, args)
5963
| ty::Coroutine(def_id, args) => self.print_def_path(def_id, args),
6064
ty::Foreign(def_id) => self.print_def_path(def_id, &[]),
6165

62-
ty::Alias(ty::Free, _) => bug!("type_name: unexpected free alias"),
63-
ty::Alias(ty::Inherent, _) => bug!("type_name: unexpected inherent projection"),
66+
ty::Alias(ty::AliasTy { kind: ty::Free { .. }, .. }) => {
67+
bug!("type_name: unexpected free alias")
68+
}
69+
ty::Alias(ty::AliasTy { kind: ty::Inherent { .. }, .. }) => {
70+
bug!("type_name: unexpected inherent projection")
71+
}
6472
ty::CoroutineWitness(..) => bug!("type_name: unexpected `CoroutineWitness`"),
6573
}
6674
}

compiler/rustc_hir_analysis/src/check/check.rs

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -523,8 +523,8 @@ fn sanity_check_found_hidden_type<'tcx>(
523523
// Nothing was actually constrained.
524524
return Ok(());
525525
}
526-
if let ty::Alias(ty::Opaque, alias) = ty.ty.kind() {
527-
if alias.def_id == key.def_id.to_def_id() && alias.args == key.args {
526+
if let &ty::Alias(alias @ ty::AliasTy { kind: ty::Opaque { def_id }, .. }) = ty.ty.kind() {
527+
if def_id == key.def_id.to_def_id() && alias.args == key.args {
528528
// Nothing was actually constrained, this is an opaque usage that was
529529
// only discovered to be opaque after inference vars resolved.
530530
return Ok(());
@@ -2150,7 +2150,7 @@ fn opaque_type_cycle_error(tcx: TyCtxt<'_>, opaque_def_id: LocalDefId) -> ErrorG
21502150
impl<'tcx> ty::TypeVisitor<TyCtxt<'tcx>> for OpaqueTypeCollector {
21512151
fn visit_ty(&mut self, t: Ty<'tcx>) {
21522152
match *t.kind() {
2153-
ty::Alias(ty::Opaque, ty::AliasTy { def_id: def, .. }) => {
2153+
ty::Alias(ty::AliasTy { kind: ty::Opaque { def_id: def }, .. }) => {
21542154
self.opaques.push(def);
21552155
}
21562156
ty::Closure(def_id, ..) | ty::Coroutine(def_id, ..) => {
@@ -2183,10 +2183,10 @@ fn opaque_type_cycle_error(tcx: TyCtxt<'_>, opaque_def_id: LocalDefId) -> ErrorG
21832183
let mut label_match = |ty: Ty<'_>, span| {
21842184
for arg in ty.walk() {
21852185
if let ty::GenericArgKind::Type(ty) = arg.kind()
2186-
&& let ty::Alias(
2187-
ty::Opaque,
2188-
ty::AliasTy { def_id: captured_def_id, .. },
2189-
) = *ty.kind()
2186+
&& let ty::Alias(ty::AliasTy {
2187+
kind: ty::Opaque { def_id: captured_def_id },
2188+
..
2189+
}) = *ty.kind()
21902190
&& captured_def_id == opaque_def_id.to_def_id()
21912191
{
21922192
err.span_label(

compiler/rustc_hir_analysis/src/check/compare_impl_item.rs

Lines changed: 15 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -820,10 +820,10 @@ where
820820
}
821821

822822
fn fold_ty(&mut self, ty: Ty<'tcx>) -> Ty<'tcx> {
823-
if let ty::Alias(ty::Projection, proj) = ty.kind()
824-
&& self.cx().is_impl_trait_in_trait(proj.def_id)
823+
if let &ty::Alias(proj @ ty::AliasTy { kind: ty::Projection { def_id }, .. }) = ty.kind()
824+
&& self.cx().is_impl_trait_in_trait(def_id)
825825
{
826-
if let Some((ty, _)) = self.types.get(&proj.def_id) {
826+
if let Some((ty, _)) = self.types.get(&def_id) {
827827
return *ty;
828828
}
829829
//FIXME(RPITIT): Deny nested RPITIT in args too
@@ -832,11 +832,11 @@ where
832832
}
833833
// Replace with infer var
834834
let infer_ty = self.ocx.infcx.next_ty_var(self.span);
835-
self.types.insert(proj.def_id, (infer_ty, proj.args));
835+
self.types.insert(def_id, (infer_ty, proj.args));
836836
// Recurse into bounds
837837
for (pred, pred_span) in self
838838
.cx()
839-
.explicit_item_bounds(proj.def_id)
839+
.explicit_item_bounds(def_id)
840840
.iter_instantiated_copied(self.cx(), proj.args)
841841
{
842842
let pred = pred.fold_with(self);
@@ -851,7 +851,7 @@ where
851851
ObligationCause::new(
852852
self.span,
853853
self.body_id,
854-
ObligationCauseCode::WhereClause(proj.def_id, pred_span),
854+
ObligationCauseCode::WhereClause(def_id, pred_span),
855855
),
856856
self.param_env,
857857
pred,
@@ -922,8 +922,12 @@ impl<'tcx> ty::FallibleTypeFolder<TyCtxt<'tcx>> for RemapHiddenTyRegions<'tcx> {
922922
} else {
923923
let guar = match region.opt_param_def_id(self.tcx, self.impl_m_def_id) {
924924
Some(def_id) => {
925-
let return_span = if let ty::Alias(ty::Opaque, opaque_ty) = self.ty.kind() {
926-
self.tcx.def_span(opaque_ty.def_id)
925+
let return_span = if let &ty::Alias(ty::AliasTy {
926+
kind: ty::Opaque { def_id: opaque_ty_def_id },
927+
..
928+
}) = self.ty.kind()
929+
{
930+
self.tcx.def_span(opaque_ty_def_id)
927931
} else {
928932
self.return_span
929933
};
@@ -2703,8 +2707,8 @@ fn param_env_with_gat_bounds<'tcx>(
27032707
let bound_vars = tcx.mk_bound_variable_kinds(&bound_vars);
27042708

27052709
match normalize_impl_ty.kind() {
2706-
ty::Alias(ty::Projection, proj)
2707-
if proj.def_id == trait_ty.def_id && proj.args == rebased_args =>
2710+
&ty::Alias(proj @ ty::AliasTy { kind: ty::Projection { def_id }, .. })
2711+
if def_id == trait_ty.def_id && proj.args == rebased_args =>
27082712
{
27092713
// Don't include this predicate if the projected type is
27102714
// exactly the same as the projection. This can occur in
@@ -2746,7 +2750,7 @@ fn try_report_async_mismatch<'tcx>(
27462750
return Ok(());
27472751
}
27482752

2749-
let ty::Alias(ty::Projection, ty::AliasTy { def_id: async_future_def_id, .. }) =
2753+
let ty::Alias(ty::AliasTy { kind: ty::Projection { def_id: async_future_def_id }, .. }) =
27502754
*tcx.fn_sig(trait_m.def_id).skip_binder().skip_binder().output().kind()
27512755
else {
27522756
bug!("expected `async fn` to return an RPITIT");

0 commit comments

Comments
 (0)