Skip to content
This repository was archived by the owner on May 28, 2025. It is now read-only.

Commit 1b77fcd

Browse files
committed
Auto merge of rust-lang#141030 - fmease:lta-no-variance, r=<try>
[WIP] Expand free alias types during variance computation Instead of computing variances for them since they can never be rigid anyway. Fixes rust-lang#140230.
2 parents 414482f + 1c11869 commit 1b77fcd

File tree

6 files changed

+13
-54
lines changed

6 files changed

+13
-54
lines changed

compiler/rustc_hir_analysis/src/check/check.rs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1533,8 +1533,7 @@ fn detect_discriminant_duplicate<'tcx>(tcx: TyCtxt<'tcx>, adt: ty::AdtDef<'tcx>)
15331533

15341534
fn check_type_alias_type_params_are_used<'tcx>(tcx: TyCtxt<'tcx>, def_id: LocalDefId) {
15351535
if tcx.type_alias_is_lazy(def_id) {
1536-
// Since we compute the variances for lazy type aliases and already reject bivariant
1537-
// parameters as unused, we can and should skip this check for lazy type aliases.
1536+
// FIXME(fmease): Explainer
15381537
return;
15391538
}
15401539

compiler/rustc_hir_analysis/src/check/wfcheck.rs

Lines changed: 6 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -322,9 +322,7 @@ fn check_item<'tcx>(tcx: TyCtxt<'tcx>, item: &'tcx hir::Item<'tcx>) -> Result<()
322322
hir::ItemKind::TraitAlias(..) => check_trait(tcx, item),
323323
// `ForeignItem`s are handled separately.
324324
hir::ItemKind::ForeignMod { .. } => Ok(()),
325-
hir::ItemKind::TyAlias(_, hir_ty, hir_generics)
326-
if tcx.type_alias_is_lazy(item.owner_id) =>
327-
{
325+
hir::ItemKind::TyAlias(_, hir_ty, _) if tcx.type_alias_is_lazy(item.owner_id) => {
328326
let res = enter_wf_checking_ctxt(tcx, item.span, def_id, |wfcx| {
329327
let ty = tcx.type_of(def_id).instantiate_identity();
330328
let item_ty =
@@ -337,7 +335,6 @@ fn check_item<'tcx>(tcx: TyCtxt<'tcx>, item: &'tcx hir::Item<'tcx>) -> Result<()
337335
check_where_clauses(wfcx, item.span, def_id);
338336
Ok(())
339337
});
340-
check_variances_for_type_defn(tcx, item, hir_generics);
341338
res
342339
}
343340
_ => Ok(()),
@@ -2044,15 +2041,7 @@ fn check_variances_for_type_defn<'tcx>(
20442041
hir_generics: &hir::Generics<'tcx>,
20452042
) {
20462043
match item.kind {
2047-
ItemKind::Enum(..) | ItemKind::Struct(..) | ItemKind::Union(..) => {
2048-
// Ok
2049-
}
2050-
ItemKind::TyAlias(..) => {
2051-
assert!(
2052-
tcx.type_alias_is_lazy(item.owner_id),
2053-
"should not be computing variance of non-free type alias"
2054-
);
2055-
}
2044+
ItemKind::Enum(..) | ItemKind::Struct(..) | ItemKind::Union(..) => {} // OK
20562045
kind => span_bug!(item.span, "cannot compute the variances of {kind:?}"),
20572046
}
20582047

@@ -2195,7 +2184,6 @@ fn report_bivariance<'tcx>(
21952184
errors::UnusedGenericParameterHelp::AdtNoPhantomData { param_name }
21962185
}
21972186
}
2198-
ItemKind::TyAlias(..) => errors::UnusedGenericParameterHelp::TyAlias { param_name },
21992187
item_kind => bug!("report_bivariance: unexpected item kind: {item_kind:?}"),
22002188
};
22012189

@@ -2268,9 +2256,6 @@ impl<'tcx> IsProbablyCyclical<'tcx> {
22682256
self.tcx.type_of(field.did).instantiate_identity().visit_with(self)
22692257
})
22702258
}
2271-
DefKind::TyAlias if self.tcx.type_alias_is_lazy(def_id) => {
2272-
self.tcx.type_of(def_id).instantiate_identity().visit_with(self)
2273-
}
22742259
_ => ControlFlow::Continue(()),
22752260
}
22762261
}
@@ -2280,17 +2265,12 @@ impl<'tcx> TypeVisitor<TyCtxt<'tcx>> for IsProbablyCyclical<'tcx> {
22802265
type Result = ControlFlow<(), ()>;
22812266

22822267
fn visit_ty(&mut self, ty: Ty<'tcx>) -> ControlFlow<(), ()> {
2283-
let def_id = match ty.kind() {
2284-
ty::Adt(adt_def, _) => Some(adt_def.did()),
2285-
ty::Alias(ty::Free, alias_ty) => Some(alias_ty.def_id),
2286-
_ => None,
2287-
};
2288-
if let Some(def_id) = def_id {
2289-
if def_id == self.item_def_id {
2268+
if let Some(adt_def) = ty.ty_adt_def() {
2269+
if adt_def.did() == self.item_def_id {
22902270
return ControlFlow::Break(());
22912271
}
2292-
if self.seen.insert(def_id) {
2293-
self.visit_def(def_id)?;
2272+
if self.seen.insert(adt_def.did()) {
2273+
self.visit_def(adt_def.did())?;
22942274
}
22952275
}
22962276
ty.super_visit_with(self)

compiler/rustc_hir_analysis/src/variance/constraints.rs

Lines changed: 5 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -79,9 +79,6 @@ pub(crate) fn add_constraints_from_crate<'a, 'tcx>(
7979
}
8080
}
8181
DefKind::Fn | DefKind::AssocFn => constraint_cx.build_constraints_for_item(def_id),
82-
DefKind::TyAlias if tcx.type_alias_is_lazy(def_id) => {
83-
constraint_cx.build_constraints_for_item(def_id)
84-
}
8582
_ => {}
8683
}
8784
}
@@ -107,15 +104,6 @@ impl<'a, 'tcx> ConstraintContext<'a, 'tcx> {
107104
let current_item = &CurrentItem { inferred_start };
108105
let ty = tcx.type_of(def_id).instantiate_identity();
109106

110-
// The type as returned by `type_of` is the underlying type and generally not a free alias.
111-
// Therefore we need to check the `DefKind` first.
112-
if let DefKind::TyAlias = tcx.def_kind(def_id)
113-
&& tcx.type_alias_is_lazy(def_id)
114-
{
115-
self.add_constraints_from_ty(current_item, ty, self.covariant);
116-
return;
117-
}
118-
119107
match ty.kind() {
120108
ty::Adt(def, _) => {
121109
// Not entirely obvious: constraints on structs/enums do not
@@ -223,6 +211,7 @@ impl<'a, 'tcx> ConstraintContext<'a, 'tcx> {
223211
variance: VarianceTermPtr<'a>,
224212
) {
225213
debug!("add_constraints_from_ty(ty={:?}, variance={:?})", ty, variance);
214+
let ty = self.tcx().expand_free_alias_tys(ty);
226215

227216
match *ty.kind() {
228217
ty::Bool
@@ -273,12 +262,11 @@ impl<'a, 'tcx> ConstraintContext<'a, 'tcx> {
273262
self.add_constraints_from_args(current, def.did(), args, variance);
274263
}
275264

276-
ty::Alias(ty::Projection | ty::Inherent | ty::Opaque, ref data) => {
277-
self.add_constraints_from_invariant_args(current, data.args, variance);
278-
}
265+
// All free alias types should've been expanded beforehand.
266+
ty::Alias(ty::Free, _) => panic!("unexpected free alias type"),
279267

280-
ty::Alias(ty::Free, ref data) => {
281-
self.add_constraints_from_args(current, data.def_id, data.args, variance);
268+
ty::Alias(_, ref alias_ty) => {
269+
self.add_constraints_from_invariant_args(current, alias_ty.args, variance);
282270
}
283271

284272
ty::Dynamic(data, r, _) => {

compiler/rustc_hir_analysis/src/variance/mod.rs

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -56,11 +56,6 @@ fn variances_of(tcx: TyCtxt<'_>, item_def_id: LocalDefId) -> &[ty::Variance] {
5656
let crate_map = tcx.crate_variances(());
5757
return crate_map.variances.get(&item_def_id.to_def_id()).copied().unwrap_or(&[]);
5858
}
59-
DefKind::TyAlias if tcx.type_alias_is_lazy(item_def_id) => {
60-
// These are inferred.
61-
let crate_map = tcx.crate_variances(());
62-
return crate_map.variances.get(&item_def_id.to_def_id()).copied().unwrap_or(&[]);
63-
}
6459
DefKind::AssocTy => match tcx.opt_rpitit_info(item_def_id.to_def_id()) {
6560
Some(ty::ImplTraitInTraitData::Trait { opaque_def_id, .. }) => {
6661
return variance_of_opaque(

compiler/rustc_hir_analysis/src/variance/terms.rs

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -99,9 +99,6 @@ pub(crate) fn determine_parameters_to_be_inferred<'a, 'tcx>(
9999
}
100100
}
101101
DefKind::Fn | DefKind::AssocFn => terms_cx.add_inferreds_for_item(def_id),
102-
DefKind::TyAlias if tcx.type_alias_is_lazy(def_id) => {
103-
terms_cx.add_inferreds_for_item(def_id)
104-
}
105102
_ => {}
106103
}
107104
}

compiler/rustc_metadata/src/rmeta/encoder.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1123,6 +1123,7 @@ fn should_encode_variances<'tcx>(tcx: TyCtxt<'tcx>, def_id: DefId, def_kind: Def
11231123
| DefKind::Static { .. }
11241124
| DefKind::Const
11251125
| DefKind::ForeignMod
1126+
| DefKind::TyAlias
11261127
| DefKind::Impl { .. }
11271128
| DefKind::Trait
11281129
| DefKind::TraitAlias
@@ -1136,7 +1137,6 @@ fn should_encode_variances<'tcx>(tcx: TyCtxt<'tcx>, def_id: DefId, def_kind: Def
11361137
| DefKind::Closure
11371138
| DefKind::ExternCrate
11381139
| DefKind::SyntheticCoroutineBody => false,
1139-
DefKind::TyAlias => tcx.type_alias_is_lazy(def_id),
11401140
}
11411141
}
11421142

0 commit comments

Comments
 (0)