diff --git a/compiler/rustc_hir_analysis/src/check/check.rs b/compiler/rustc_hir_analysis/src/check/check.rs index f92b2aea160a1..42cf2acc064cc 100644 --- a/compiler/rustc_hir_analysis/src/check/check.rs +++ b/compiler/rustc_hir_analysis/src/check/check.rs @@ -1533,8 +1533,7 @@ fn detect_discriminant_duplicate<'tcx>(tcx: TyCtxt<'tcx>, adt: ty::AdtDef<'tcx>) fn check_type_alias_type_params_are_used<'tcx>(tcx: TyCtxt<'tcx>, def_id: LocalDefId) { if tcx.type_alias_is_lazy(def_id) { - // Since we compute the variances for lazy type aliases and already reject bivariant - // parameters as unused, we can and should skip this check for lazy type aliases. + // FIXME(fmease): Explainer return; } diff --git a/compiler/rustc_hir_analysis/src/check/wfcheck.rs b/compiler/rustc_hir_analysis/src/check/wfcheck.rs index 2ec14b2f018c3..311b7226ad6b1 100644 --- a/compiler/rustc_hir_analysis/src/check/wfcheck.rs +++ b/compiler/rustc_hir_analysis/src/check/wfcheck.rs @@ -322,9 +322,7 @@ fn check_item<'tcx>(tcx: TyCtxt<'tcx>, item: &'tcx hir::Item<'tcx>) -> Result<() hir::ItemKind::TraitAlias(..) => check_trait(tcx, item), // `ForeignItem`s are handled separately. hir::ItemKind::ForeignMod { .. } => Ok(()), - hir::ItemKind::TyAlias(_, hir_ty, hir_generics) - if tcx.type_alias_is_lazy(item.owner_id) => - { + hir::ItemKind::TyAlias(_, hir_ty, _) if tcx.type_alias_is_lazy(item.owner_id) => { let res = enter_wf_checking_ctxt(tcx, item.span, def_id, |wfcx| { let ty = tcx.type_of(def_id).instantiate_identity(); let item_ty = @@ -337,7 +335,6 @@ fn check_item<'tcx>(tcx: TyCtxt<'tcx>, item: &'tcx hir::Item<'tcx>) -> Result<() check_where_clauses(wfcx, item.span, def_id); Ok(()) }); - check_variances_for_type_defn(tcx, item, hir_generics); res } _ => Ok(()), @@ -2044,15 +2041,7 @@ fn check_variances_for_type_defn<'tcx>( hir_generics: &hir::Generics<'tcx>, ) { match item.kind { - ItemKind::Enum(..) | ItemKind::Struct(..) | ItemKind::Union(..) => { - // Ok - } - ItemKind::TyAlias(..) => { - assert!( - tcx.type_alias_is_lazy(item.owner_id), - "should not be computing variance of non-free type alias" - ); - } + ItemKind::Enum(..) | ItemKind::Struct(..) | ItemKind::Union(..) => {} // OK kind => span_bug!(item.span, "cannot compute the variances of {kind:?}"), } @@ -2195,7 +2184,6 @@ fn report_bivariance<'tcx>( errors::UnusedGenericParameterHelp::AdtNoPhantomData { param_name } } } - ItemKind::TyAlias(..) => errors::UnusedGenericParameterHelp::TyAlias { param_name }, item_kind => bug!("report_bivariance: unexpected item kind: {item_kind:?}"), }; @@ -2268,9 +2256,6 @@ impl<'tcx> IsProbablyCyclical<'tcx> { self.tcx.type_of(field.did).instantiate_identity().visit_with(self) }) } - DefKind::TyAlias if self.tcx.type_alias_is_lazy(def_id) => { - self.tcx.type_of(def_id).instantiate_identity().visit_with(self) - } _ => ControlFlow::Continue(()), } } @@ -2280,17 +2265,12 @@ impl<'tcx> TypeVisitor> for IsProbablyCyclical<'tcx> { type Result = ControlFlow<(), ()>; fn visit_ty(&mut self, ty: Ty<'tcx>) -> ControlFlow<(), ()> { - let def_id = match ty.kind() { - ty::Adt(adt_def, _) => Some(adt_def.did()), - ty::Alias(ty::Free, alias_ty) => Some(alias_ty.def_id), - _ => None, - }; - if let Some(def_id) = def_id { - if def_id == self.item_def_id { + if let Some(adt_def) = ty.ty_adt_def() { + if adt_def.did() == self.item_def_id { return ControlFlow::Break(()); } - if self.seen.insert(def_id) { - self.visit_def(def_id)?; + if self.seen.insert(adt_def.did()) { + self.visit_def(adt_def.did())?; } } ty.super_visit_with(self) diff --git a/compiler/rustc_hir_analysis/src/variance/constraints.rs b/compiler/rustc_hir_analysis/src/variance/constraints.rs index 92cfece77c473..ac9b99ce22f98 100644 --- a/compiler/rustc_hir_analysis/src/variance/constraints.rs +++ b/compiler/rustc_hir_analysis/src/variance/constraints.rs @@ -79,9 +79,6 @@ pub(crate) fn add_constraints_from_crate<'a, 'tcx>( } } DefKind::Fn | DefKind::AssocFn => constraint_cx.build_constraints_for_item(def_id), - DefKind::TyAlias if tcx.type_alias_is_lazy(def_id) => { - constraint_cx.build_constraints_for_item(def_id) - } _ => {} } } @@ -107,15 +104,6 @@ impl<'a, 'tcx> ConstraintContext<'a, 'tcx> { let current_item = &CurrentItem { inferred_start }; let ty = tcx.type_of(def_id).instantiate_identity(); - // The type as returned by `type_of` is the underlying type and generally not a free alias. - // Therefore we need to check the `DefKind` first. - if let DefKind::TyAlias = tcx.def_kind(def_id) - && tcx.type_alias_is_lazy(def_id) - { - self.add_constraints_from_ty(current_item, ty, self.covariant); - return; - } - match ty.kind() { ty::Adt(def, _) => { // Not entirely obvious: constraints on structs/enums do not @@ -223,6 +211,7 @@ impl<'a, 'tcx> ConstraintContext<'a, 'tcx> { variance: VarianceTermPtr<'a>, ) { debug!("add_constraints_from_ty(ty={:?}, variance={:?})", ty, variance); + let ty = self.tcx().expand_free_alias_tys(ty); match *ty.kind() { ty::Bool @@ -273,12 +262,11 @@ impl<'a, 'tcx> ConstraintContext<'a, 'tcx> { self.add_constraints_from_args(current, def.did(), args, variance); } - ty::Alias(ty::Projection | ty::Inherent | ty::Opaque, ref data) => { - self.add_constraints_from_invariant_args(current, data.args, variance); - } + // All free alias types should've been expanded beforehand. + ty::Alias(ty::Free, _) => panic!("unexpected free alias type"), - ty::Alias(ty::Free, ref data) => { - self.add_constraints_from_args(current, data.def_id, data.args, variance); + ty::Alias(_, ref alias_ty) => { + self.add_constraints_from_invariant_args(current, alias_ty.args, variance); } ty::Dynamic(data, r, _) => { diff --git a/compiler/rustc_hir_analysis/src/variance/mod.rs b/compiler/rustc_hir_analysis/src/variance/mod.rs index dbba45dc7bb58..5ac4b6b4b02b6 100644 --- a/compiler/rustc_hir_analysis/src/variance/mod.rs +++ b/compiler/rustc_hir_analysis/src/variance/mod.rs @@ -56,11 +56,6 @@ fn variances_of(tcx: TyCtxt<'_>, item_def_id: LocalDefId) -> &[ty::Variance] { let crate_map = tcx.crate_variances(()); return crate_map.variances.get(&item_def_id.to_def_id()).copied().unwrap_or(&[]); } - DefKind::TyAlias if tcx.type_alias_is_lazy(item_def_id) => { - // These are inferred. - let crate_map = tcx.crate_variances(()); - return crate_map.variances.get(&item_def_id.to_def_id()).copied().unwrap_or(&[]); - } DefKind::AssocTy => match tcx.opt_rpitit_info(item_def_id.to_def_id()) { Some(ty::ImplTraitInTraitData::Trait { opaque_def_id, .. }) => { return variance_of_opaque( diff --git a/compiler/rustc_hir_analysis/src/variance/terms.rs b/compiler/rustc_hir_analysis/src/variance/terms.rs index cf38957bf24aa..69020db8cf070 100644 --- a/compiler/rustc_hir_analysis/src/variance/terms.rs +++ b/compiler/rustc_hir_analysis/src/variance/terms.rs @@ -99,9 +99,6 @@ pub(crate) fn determine_parameters_to_be_inferred<'a, 'tcx>( } } DefKind::Fn | DefKind::AssocFn => terms_cx.add_inferreds_for_item(def_id), - DefKind::TyAlias if tcx.type_alias_is_lazy(def_id) => { - terms_cx.add_inferreds_for_item(def_id) - } _ => {} } } diff --git a/compiler/rustc_metadata/src/rmeta/encoder.rs b/compiler/rustc_metadata/src/rmeta/encoder.rs index 7ac72ef814a9c..0dd374bb40c40 100644 --- a/compiler/rustc_metadata/src/rmeta/encoder.rs +++ b/compiler/rustc_metadata/src/rmeta/encoder.rs @@ -1123,6 +1123,7 @@ fn should_encode_variances<'tcx>(tcx: TyCtxt<'tcx>, def_id: DefId, def_kind: Def | DefKind::Static { .. } | DefKind::Const | DefKind::ForeignMod + | DefKind::TyAlias | DefKind::Impl { .. } | DefKind::Trait | DefKind::TraitAlias @@ -1136,7 +1137,6 @@ fn should_encode_variances<'tcx>(tcx: TyCtxt<'tcx>, def_id: DefId, def_kind: Def | DefKind::Closure | DefKind::ExternCrate | DefKind::SyntheticCoroutineBody => false, - DefKind::TyAlias => tcx.type_alias_is_lazy(def_id), } }