Skip to content

Commit 0b33626

Browse files
committed
ty::Alias review comments
1 parent ed35dc3 commit 0b33626

5 files changed

Lines changed: 38 additions & 65 deletions

File tree

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

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,6 @@ use rustc_abi::ExternAbi;
5454
use rustc_data_structures::fx::{FxIndexMap, FxIndexSet};
5555
use rustc_errors::{Applicability, Diag, DiagStyledString, IntoDiagArg, StringPart, pluralize};
5656
use rustc_hir as hir;
57-
use rustc_hir::def::DefKind;
5857
use rustc_hir::def_id::{CRATE_DEF_ID, DefId};
5958
use rustc_hir::intravisit::Visitor;
6059
use rustc_hir::lang_items::LangItem;
@@ -182,11 +181,7 @@ impl<'a, 'tcx> TypeErrCtxt<'a, 'tcx> {
182181

183182
pub fn get_impl_future_output_ty(&self, ty: Ty<'tcx>) -> Option<Ty<'tcx>> {
184183
let (def_id, args) = match *ty.kind() {
185-
ty::Alias(ty::AliasTy { kind, args, .. })
186-
if self.tcx.def_kind(kind.def_id()) == DefKind::OpaqueTy =>
187-
{
188-
(kind.def_id(), args)
189-
}
184+
ty::Alias(ty::AliasTy { kind: ty::Opaque { def_id }, args, .. }) => (def_id, args),
190185
ty::Alias(ty::AliasTy { kind, args, .. })
191186
if self.tcx.is_impl_trait_in_trait(kind.def_id()) =>
192187
{

compiler/rustc_type_ir/src/interner.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -196,6 +196,7 @@ pub trait Interner:
196196
type VariancesOf: Copy + Debug + SliceLike<Item = ty::Variance>;
197197
fn variances_of(self, def_id: Self::DefId) -> Self::VariancesOf;
198198

199+
// FIXME: remove `def_id` param after `AliasTermKind` contains `def_id` within
199200
fn opt_alias_variances(
200201
self,
201202
kind: impl Into<ty::AliasTermKind>,

compiler/rustc_type_ir/src/predicate.rs

Lines changed: 17 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -644,52 +644,26 @@ impl<I: Interner> AliasTerm<I> {
644644
}
645645

646646
pub fn to_term(self, interner: I) -> I::Term {
647-
match self.kind(interner) {
648-
AliasTermKind::ProjectionTy => Ty::new_alias(
649-
interner,
650-
ty::AliasTy {
651-
kind: ty::AliasTyKind::Projection { def_id: self.def_id },
652-
args: self.args,
653-
_use_alias_ty_new_instead: (),
654-
},
655-
)
656-
.into(),
657-
AliasTermKind::InherentTy => Ty::new_alias(
658-
interner,
659-
ty::AliasTy {
660-
kind: ty::AliasTyKind::Inherent { def_id: self.def_id },
661-
args: self.args,
662-
_use_alias_ty_new_instead: (),
663-
},
664-
)
665-
.into(),
666-
AliasTermKind::OpaqueTy => Ty::new_alias(
667-
interner,
668-
ty::AliasTy {
669-
kind: ty::AliasTyKind::Opaque { def_id: self.def_id },
670-
args: self.args,
671-
_use_alias_ty_new_instead: (),
672-
},
673-
)
674-
.into(),
675-
AliasTermKind::FreeTy => Ty::new_alias(
676-
interner,
677-
ty::AliasTy {
678-
kind: ty::AliasTyKind::Free { def_id: self.def_id },
679-
args: self.args,
680-
_use_alias_ty_new_instead: (),
681-
},
682-
)
683-
.into(),
647+
let alias_ty_kind = match self.kind(interner) {
684648
AliasTermKind::FreeConst
685649
| AliasTermKind::InherentConst
686650
| AliasTermKind::UnevaluatedConst
687-
| AliasTermKind::ProjectionConst => I::Const::new_unevaluated(
688-
interner,
689-
ty::UnevaluatedConst::new(self.def_id.try_into().unwrap(), self.args),
690-
)
691-
.into(),
692-
}
651+
| AliasTermKind::ProjectionConst => {
652+
return I::Const::new_unevaluated(
653+
interner,
654+
ty::UnevaluatedConst::new(self.def_id.try_into().unwrap(), self.args),
655+
)
656+
.into();
657+
}
658+
659+
AliasTermKind::ProjectionTy => ty::Projection { def_id: self.def_id },
660+
AliasTermKind::InherentTy => ty::Inherent { def_id: self.def_id },
661+
AliasTermKind::OpaqueTy => ty::Opaque { def_id: self.def_id },
662+
AliasTermKind::FreeTy => ty::Free { def_id: self.def_id },
663+
};
664+
665+
Ty::new_alias(interner, ty::AliasTy::new_from_args(interner, alias_ty_kind, self.args))
666+
.into()
693667
}
694668
}
695669

compiler/rustc_type_ir/src/relate.rs

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -504,8 +504,6 @@ pub fn structurally_relate_tys<I: Interner, R: TypeRelation<I>>(
504504
// Alias tend to mostly already be handled downstream due to normalization.
505505
(ty::Alias(a), ty::Alias(b)) => {
506506
let alias_ty = relation.relate(a, b)?;
507-
assert_eq!(a.kind, b.kind);
508-
assert_eq!(a.kind, alias_ty.kind);
509507
Ok(Ty::new_alias(cx, alias_ty))
510508
}
511509

compiler/rustc_type_ir/src/ty_kind.rs

Lines changed: 19 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -31,25 +31,30 @@ pub enum AliasTyKind<I: Interner> {
3131
/// A projection `<Type as Trait>::AssocType`.
3232
///
3333
/// Can get normalized away if monomorphic enough.
34-
Projection {
35-
/// The `DefId` of the `TraitItem` or `ImplItem` for the associated type `N` depending on whether
36-
/// this is a projection or an inherent projection or the `DefId` of the `OpaqueType` item if
37-
/// this is an opaque.
38-
///
39-
/// During codegen, `interner.type_of(def_id)` can be used to get the type of the
40-
/// underlying type if the type is an opaque.
41-
///
42-
/// Note that if this is an associated type, this is not the `DefId` of the
43-
/// `TraitRef` containing this associated type, which is in `interner.associated_item(def_id).container`,
44-
/// aka. `interner.parent(def_id)`.
45-
def_id: I::DefId,
46-
},
34+
///
35+
/// The `def_id` is the `DefId` of the `TraitItem` for the associated type.
36+
///
37+
/// Note that the `def_id` is not the `DefId` of the `TraitRef` containing this
38+
/// associated type, which is in `interner.associated_item(def_id).container`,
39+
/// aka. `interner.parent(def_id)`.
40+
Projection { def_id: I::DefId },
41+
4742
/// An associated type in an inherent `impl`
43+
///
44+
/// The `def_id` is the `DefId` of the `ImplItem` for the associated type.
4845
Inherent { def_id: I::DefId },
46+
4947
/// An opaque type (usually from `impl Trait` in type aliases or function return types)
5048
///
51-
/// Can only be normalized away in PostAnalysis mode or its defining scope.
49+
/// `def_id` is the `DefId` of the `OpaqueType` item.
50+
///
51+
///
52+
/// Can only be normalized away in `PostAnalysis` mode or its defining scope.
53+
///
54+
/// During codegen, `interner.type_of(def_id)` can be used to get the type of the
55+
/// underlying type if the type is an opaque.
5256
Opaque { def_id: I::DefId },
57+
5358
/// A type alias that actually checks its trait bounds.
5459
///
5560
/// Currently only used if the type alias references opaque types.

0 commit comments

Comments
 (0)