Skip to content

Commit 61705b3

Browse files
Add TyKind::TyAlias support in rustc_middle
1 parent a197c45 commit 61705b3

File tree

11 files changed

+91
-190
lines changed

11 files changed

+91
-190
lines changed

compiler/rustc_middle/src/ty/context.rs

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1726,6 +1726,16 @@ impl<'tcx> TyCtxt<'tcx> {
17261726
pub fn local_visibility(self, def_id: LocalDefId) -> Visibility {
17271727
self.visibility(def_id).expect_local()
17281728
}
1729+
1730+
/// As long as the kind of `ty` is `TyAlias`, then it'll continue to peel it off and return
1731+
/// the type below it.
1732+
pub fn peel_off_ty_alias(self, mut ty: Ty<'tcx>) -> Ty<'tcx> {
1733+
while let ty::TyAlias(def_id, substs) = *ty.kind() {
1734+
let binder_ty = self.bound_type_of(def_id);
1735+
ty = binder_ty.subst(self, substs);
1736+
}
1737+
ty
1738+
}
17291739
}
17301740

17311741
/// A trait implemented for all `X<'a>` types that can be safely and
@@ -1996,7 +2006,7 @@ pub mod tls {
19962006
}
19972007

19982008
macro_rules! sty_debug_print {
1999-
($fmt: expr, $ctxt: expr, $($variant: ident),*) => {{
2009+
($fmt: expr, $ctxt: expr, $($variant: ident),* $(,)?) => {{
20002010
// Curious inner module to allow variant names to be used as
20012011
// variable names.
20022012
#[allow(non_snake_case)]
@@ -2095,7 +2105,8 @@ impl<'tcx> TyCtxt<'tcx> {
20952105
Infer,
20962106
Projection,
20972107
Opaque,
2098-
Foreign
2108+
Foreign,
2109+
TyAlias,
20992110
)?;
21002111

21012112
writeln!(fmt, "InternalSubsts interner: #{}", self.0.interners.substs.len())?;
@@ -2633,6 +2644,11 @@ impl<'tcx> TyCtxt<'tcx> {
26332644
self.mk_ty(Opaque(def_id, substs))
26342645
}
26352646

2647+
#[inline]
2648+
pub fn mk_ty_alias(self, def_id: DefId, substs: SubstsRef<'tcx>) -> Ty<'tcx> {
2649+
self.mk_ty(TyAlias(def_id, substs))
2650+
}
2651+
26362652
pub fn mk_place_field(self, place: Place<'tcx>, f: Field, ty: Ty<'tcx>) -> Place<'tcx> {
26372653
self.mk_place_elem(place, PlaceElem::Field(f, ty))
26382654
}

compiler/rustc_middle/src/ty/error.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -329,6 +329,7 @@ impl<'tcx> Ty<'tcx> {
329329
ty::Param(p) => format!("type parameter `{}`", p).into(),
330330
ty::Opaque(..) => "opaque type".into(),
331331
ty::Error(_) => "type error".into(),
332+
ty::TyAlias(_, _) => "type alias".into(),
332333
}
333334
}
334335

@@ -366,6 +367,7 @@ impl<'tcx> Ty<'tcx> {
366367
ty::Projection(_) => "associated type".into(),
367368
ty::Param(_) => "type parameter".into(),
368369
ty::Opaque(..) => "opaque type".into(),
370+
ty::TyAlias(_, _) => "type alias".into(),
369371
}
370372
}
371373
}

compiler/rustc_middle/src/ty/fast_reject.rs

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -127,6 +127,9 @@ pub fn simplify_type<'tcx>(
127127
TreatParams::AsPlaceholder => Some(PlaceholderSimplifiedType),
128128
TreatParams::AsInfer => None,
129129
},
130+
// A `TyAlias` is never fully normalized as you can always normalize it further so returning
131+
// `None` all the time.
132+
ty::TyAlias(..) => None,
130133
ty::Projection(_) => match treat_params {
131134
// When treating `ty::Param` as a placeholder, projections also
132135
// don't unify with anything else as long as they are fully normalized.
@@ -229,7 +232,7 @@ impl DeepRejectCtxt {
229232
match impl_ty.kind() {
230233
// Start by checking whether the type in the impl may unify with
231234
// pretty much everything. Just return `true` in that case.
232-
ty::Param(_) | ty::Projection(_) | ty::Error(_) => return true,
235+
ty::Param(_) | ty::Projection(_) | ty::TyAlias(_, _) | ty::Error(_) => return true,
233236
// These types only unify with inference variables or their own
234237
// variant.
235238
ty::Bool
@@ -352,7 +355,7 @@ impl DeepRejectCtxt {
352355
// projections can unify with other stuff.
353356
//
354357
// Looking forward to lazy normalization this is the safer strategy anyways.
355-
ty::Projection(_) => true,
358+
ty::Projection(_) | ty::TyAlias(_, _) => true,
356359

357360
ty::Error(_) => true,
358361

compiler/rustc_middle/src/ty/flags.rs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -155,6 +155,11 @@ impl FlagComputation {
155155
self.add_substs(substs);
156156
}
157157

158+
&ty::TyAlias(_, substs) => {
159+
self.add_flags(TypeFlags::HAS_TY_PROJECTION);
160+
self.add_substs(substs);
161+
}
162+
158163
&ty::Projection(data) => {
159164
self.add_flags(TypeFlags::HAS_TY_PROJECTION);
160165
self.add_projection_ty(data);

compiler/rustc_middle/src/ty/layout.rs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -637,7 +637,9 @@ where
637637
}
638638
};
639639

640-
match *this.ty.kind() {
640+
let ty = tcx.peel_off_ty_alias(this.ty);
641+
642+
match *ty.kind() {
641643
ty::Bool
642644
| ty::Char
643645
| ty::Int(_)
@@ -770,6 +772,7 @@ where
770772
| ty::Opaque(..)
771773
| ty::Param(_)
772774
| ty::Infer(_)
775+
| ty::TyAlias(_, _)
773776
| ty::Error(_) => bug!("TyAndLayout::field: unexpected type `{}`", this.ty),
774777
}
775778
}

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

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -264,6 +264,10 @@ fn characteristic_def_id_of_type_cached<'a>(
264264
return None;
265265
}),
266266

267+
// `TyAlias` always return `None` since the type alias' defining module is irrelevant for
268+
// any decisions.
269+
ty::TyAlias(..) => None,
270+
267271
ty::FnDef(def_id, _)
268272
| ty::Closure(def_id, _)
269273
| ty::Generator(def_id, _, _)

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -780,6 +780,7 @@ pub trait PrettyPrinter<'tcx>:
780780
p!("]")
781781
}
782782
ty::Slice(ty) => p!("[", print(ty), "]"),
783+
ty::TyAlias(def_id, substs) => p!(print_def_path(def_id, substs)),
783784
}
784785

785786
Ok(self)

compiler/rustc_middle/src/ty/structural_impls.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -639,6 +639,7 @@ impl<'tcx> TypeSuperFoldable<'tcx> for Ty<'tcx> {
639639
ty::Closure(did, substs) => ty::Closure(did, substs.try_fold_with(folder)?),
640640
ty::Projection(data) => ty::Projection(data.try_fold_with(folder)?),
641641
ty::Opaque(did, substs) => ty::Opaque(did, substs.try_fold_with(folder)?),
642+
ty::TyAlias(did, substs) => ty::TyAlias(did, substs.try_fold_with(folder)?),
642643

643644
ty::Bool
644645
| ty::Char
@@ -685,6 +686,7 @@ impl<'tcx> TypeSuperVisitable<'tcx> for Ty<'tcx> {
685686
ty::Closure(_did, ref substs) => substs.visit_with(visitor),
686687
ty::Projection(ref data) => data.visit_with(visitor),
687688
ty::Opaque(_, ref substs) => substs.visit_with(visitor),
689+
ty::TyAlias(_, ref substs) => substs.visit_with(visitor),
688690

689691
ty::Bool
690692
| ty::Char

0 commit comments

Comments
 (0)