Skip to content

Commit f04a108

Browse files
Auto merge of #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 #140230.
2 parents 119574f + 9429c14 commit f04a108

File tree

6 files changed

+14
-55
lines changed

6 files changed

+14
-55
lines changed

compiler/rustc_hir_analysis/src/check/check.rs

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -905,7 +905,6 @@ pub(crate) fn check_item_type(tcx: TyCtxt<'_>, def_id: LocalDefId) -> Result<(),
905905
tcx.ensure_ok().generics_of(def_id);
906906
tcx.ensure_ok().type_of(def_id);
907907
tcx.ensure_ok().predicates_of(def_id);
908-
check_type_alias_type_params_are_used(tcx, def_id);
909908
if tcx.type_alias_is_lazy(def_id) {
910909
res = res.and(enter_wf_checking_ctxt(tcx, def_id, |wfcx| {
911910
let ty = tcx.type_of(def_id).instantiate_identity();
@@ -919,7 +918,8 @@ pub(crate) fn check_item_type(tcx: TyCtxt<'_>, def_id: LocalDefId) -> Result<(),
919918
check_where_clauses(wfcx, def_id);
920919
Ok(())
921920
}));
922-
check_variances_for_type_defn(tcx, def_id);
921+
} else {
922+
check_type_alias_type_params_are_used(tcx, def_id);
923923
}
924924
}
925925
DefKind::ForeignMod => {
@@ -1763,12 +1763,6 @@ fn detect_discriminant_duplicate<'tcx>(tcx: TyCtxt<'tcx>, adt: ty::AdtDef<'tcx>)
17631763
}
17641764

17651765
fn check_type_alias_type_params_are_used<'tcx>(tcx: TyCtxt<'tcx>, def_id: LocalDefId) {
1766-
if tcx.type_alias_is_lazy(def_id) {
1767-
// Since we compute the variances for lazy type aliases and already reject bivariant
1768-
// parameters as unused, we can and should skip this check for lazy type aliases.
1769-
return;
1770-
}
1771-
17721766
let generics = tcx.generics_of(def_id);
17731767
if generics.own_counts().types == 0 {
17741768
return;

compiler/rustc_hir_analysis/src/check/wfcheck.rs

Lines changed: 5 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,7 @@ impl<'tcx> WfCheckingCtxt<'_, 'tcx> {
7373
)
7474
}
7575

76-
/// Convenience function to *deeply* normalize during wfcheck. In the old solver,
76+
/// Convenience function to *deeply* gormalize during wfcheck. In the old solver,
7777
/// this just dispatches to [`WfCheckingCtxt::normalize`], but in the new solver
7878
/// this calls `deeply_normalize` and reports errors if they are encountered.
7979
///
@@ -1910,12 +1910,6 @@ pub(super) fn check_variances_for_type_defn<'tcx>(tcx: TyCtxt<'tcx>, def_id: Loc
19101910
DefKind::Enum | DefKind::Struct | DefKind::Union => {
19111911
// Ok
19121912
}
1913-
DefKind::TyAlias => {
1914-
assert!(
1915-
tcx.type_alias_is_lazy(def_id),
1916-
"should not be computing variance of non-free type alias"
1917-
);
1918-
}
19191913
kind => span_bug!(tcx.def_span(def_id), "cannot compute the variances of {kind:?}"),
19201914
}
19211915

@@ -2062,7 +2056,6 @@ fn report_bivariance<'tcx>(
20622056
errors::UnusedGenericParameterHelp::AdtNoPhantomData { param_name }
20632057
}
20642058
}
2065-
ItemKind::TyAlias(..) => errors::UnusedGenericParameterHelp::TyAlias { param_name },
20662059
item_kind => bug!("report_bivariance: unexpected item kind: {item_kind:?}"),
20672060
};
20682061

@@ -2135,9 +2128,6 @@ impl<'tcx> IsProbablyCyclical<'tcx> {
21352128
self.tcx.type_of(field.did).instantiate_identity().visit_with(self)
21362129
})
21372130
}
2138-
DefKind::TyAlias if self.tcx.type_alias_is_lazy(def_id) => {
2139-
self.tcx.type_of(def_id).instantiate_identity().visit_with(self)
2140-
}
21412131
_ => ControlFlow::Continue(()),
21422132
}
21432133
}
@@ -2147,17 +2137,12 @@ impl<'tcx> TypeVisitor<TyCtxt<'tcx>> for IsProbablyCyclical<'tcx> {
21472137
type Result = ControlFlow<(), ()>;
21482138

21492139
fn visit_ty(&mut self, ty: Ty<'tcx>) -> ControlFlow<(), ()> {
2150-
let def_id = match ty.kind() {
2151-
ty::Adt(adt_def, _) => Some(adt_def.did()),
2152-
ty::Alias(ty::Free, alias_ty) => Some(alias_ty.def_id),
2153-
_ => None,
2154-
};
2155-
if let Some(def_id) = def_id {
2156-
if def_id == self.item_def_id {
2140+
if let Some(adt_def) = ty.ty_adt_def() {
2141+
if adt_def.did() == self.item_def_id {
21572142
return ControlFlow::Break(());
21582143
}
2159-
if self.seen.insert(def_id) {
2160-
self.visit_def(def_id)?;
2144+
if self.seen.insert(adt_def.did()) {
2145+
self.visit_def(adt_def.did())?;
21612146
}
21622147
}
21632148
ty.super_visit_with(self)

compiler/rustc_hir_analysis/src/variance/constraints.rs

Lines changed: 6 additions & 18 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
@@ -216,14 +204,13 @@ impl<'a, 'tcx> ConstraintContext<'a, 'tcx> {
216204
/// Adds constraints appropriate for an instance of `ty` appearing
217205
/// in a context with the generics defined in `generics` and
218206
/// ambient variance `variance`
207+
#[instrument(level = "debug", skip(self, current))]
219208
fn add_constraints_from_ty(
220209
&mut self,
221210
current: &CurrentItem,
222211
ty: Ty<'tcx>,
223212
variance: VarianceTermPtr<'a>,
224213
) {
225-
debug!("add_constraints_from_ty(ty={:?}, variance={:?})", ty, variance);
226-
227214
match *ty.kind() {
228215
ty::Bool
229216
| ty::Char
@@ -273,12 +260,13 @@ impl<'a, 'tcx> ConstraintContext<'a, 'tcx> {
273260
self.add_constraints_from_args(current, def.did(), args, variance);
274261
}
275262

276-
ty::Alias(ty::Projection | ty::Inherent | ty::Opaque, ref data) => {
277-
self.add_constraints_from_invariant_args(current, data.args, variance);
263+
ty::Alias(ty::Free, alias_ty) => {
264+
let ty = self.tcx().type_of(alias_ty.def_id).instantiate(self.tcx(), alias_ty.args);
265+
self.add_constraints_from_ty(current, ty, variance);
278266
}
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
@@ -51,11 +51,6 @@ pub(super) fn variances_of(tcx: TyCtxt<'_>, item_def_id: LocalDefId) -> &[ty::Va
5151
let crate_map = tcx.crate_variances(());
5252
return crate_map.variances.get(&item_def_id.to_def_id()).copied().unwrap_or(&[]);
5353
}
54-
DefKind::TyAlias if tcx.type_alias_is_lazy(item_def_id) => {
55-
// These are inferred.
56-
let crate_map = tcx.crate_variances(());
57-
return crate_map.variances.get(&item_def_id.to_def_id()).copied().unwrap_or(&[]);
58-
}
5954
DefKind::AssocTy => match tcx.opt_rpitit_info(item_def_id.to_def_id()) {
6055
Some(ty::ImplTraitInTraitData::Trait { opaque_def_id, .. }) => {
6156
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
@@ -1163,6 +1163,7 @@ fn should_encode_variances<'tcx>(tcx: TyCtxt<'tcx>, def_id: DefId, def_kind: Def
11631163
| DefKind::Static { .. }
11641164
| DefKind::Const
11651165
| DefKind::ForeignMod
1166+
| DefKind::TyAlias
11661167
| DefKind::Impl { .. }
11671168
| DefKind::Trait
11681169
| DefKind::TraitAlias
@@ -1176,7 +1177,6 @@ fn should_encode_variances<'tcx>(tcx: TyCtxt<'tcx>, def_id: DefId, def_kind: Def
11761177
| DefKind::Closure
11771178
| DefKind::ExternCrate
11781179
| DefKind::SyntheticCoroutineBody => false,
1179-
DefKind::TyAlias => tcx.type_alias_is_lazy(def_id),
11801180
}
11811181
}
11821182

0 commit comments

Comments
 (0)