Skip to content

Commit d27c057

Browse files
committed
Make variance wfcheck only use the HIR in the error path
1 parent 362d4dd commit d27c057

File tree

1 file changed

+26
-28
lines changed

1 file changed

+26
-28
lines changed

compiler/rustc_hir_analysis/src/check/wfcheck.rs

Lines changed: 26 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -292,26 +292,24 @@ fn check_item<'tcx>(tcx: TyCtxt<'tcx>, item: &'tcx hir::Item<'tcx>) -> Result<()
292292
}
293293
hir::ItemKind::Fn { ident, sig, .. } => check_item_fn(tcx, def_id, ident, sig.decl),
294294
hir::ItemKind::Const(_, _, ty, _) => check_const_item(tcx, def_id, ty.span, item.span),
295-
hir::ItemKind::Struct(_, generics, _) => {
295+
hir::ItemKind::Struct(..) => {
296296
let res = check_type_defn(tcx, item, false);
297-
check_variances_for_type_defn(tcx, item, generics);
297+
check_variances_for_type_defn(tcx, def_id);
298298
res
299299
}
300-
hir::ItemKind::Union(_, generics, _) => {
300+
hir::ItemKind::Union(..) => {
301301
let res = check_type_defn(tcx, item, true);
302-
check_variances_for_type_defn(tcx, item, generics);
302+
check_variances_for_type_defn(tcx, def_id);
303303
res
304304
}
305-
hir::ItemKind::Enum(_, generics, _) => {
305+
hir::ItemKind::Enum(..) => {
306306
let res = check_type_defn(tcx, item, true);
307-
check_variances_for_type_defn(tcx, item, generics);
307+
check_variances_for_type_defn(tcx, def_id);
308308
res
309309
}
310310
hir::ItemKind::Trait(..) => check_trait(tcx, item),
311311
hir::ItemKind::TraitAlias(..) => check_trait(tcx, item),
312-
// `ForeignItem`s are handled separately.
313-
hir::ItemKind::ForeignMod { .. } => Ok(()),
314-
hir::ItemKind::TyAlias(_, generics, hir_ty) if tcx.type_alias_is_lazy(item.owner_id) => {
312+
hir::ItemKind::TyAlias(.., hir_ty) if tcx.type_alias_is_lazy(item.owner_id) => {
315313
let res = enter_wf_checking_ctxt(tcx, def_id, |wfcx| {
316314
let ty = tcx.type_of(def_id).instantiate_identity();
317315
let item_ty =
@@ -324,7 +322,7 @@ fn check_item<'tcx>(tcx: TyCtxt<'tcx>, item: &'tcx hir::Item<'tcx>) -> Result<()
324322
check_where_clauses(wfcx, item.span, def_id);
325323
Ok(())
326324
});
327-
check_variances_for_type_defn(tcx, item, generics);
325+
check_variances_for_type_defn(tcx, def_id);
328326
res
329327
}
330328
_ => Ok(()),
@@ -1979,27 +1977,23 @@ fn legacy_receiver_is_implemented<'tcx>(
19791977
}
19801978
}
19811979

1982-
fn check_variances_for_type_defn<'tcx>(
1983-
tcx: TyCtxt<'tcx>,
1984-
item: &'tcx hir::Item<'tcx>,
1985-
hir_generics: &hir::Generics<'tcx>,
1986-
) {
1987-
match item.kind {
1988-
ItemKind::Enum(..) | ItemKind::Struct(..) | ItemKind::Union(..) => {
1980+
fn check_variances_for_type_defn<'tcx>(tcx: TyCtxt<'tcx>, def_id: LocalDefId) {
1981+
match tcx.def_kind(def_id) {
1982+
DefKind::Enum | DefKind::Struct | DefKind::Union => {
19891983
// Ok
19901984
}
1991-
ItemKind::TyAlias(..) => {
1985+
DefKind::TyAlias => {
19921986
assert!(
1993-
tcx.type_alias_is_lazy(item.owner_id),
1987+
tcx.type_alias_is_lazy(def_id),
19941988
"should not be computing variance of non-free type alias"
19951989
);
19961990
}
1997-
kind => span_bug!(item.span, "cannot compute the variances of {kind:?}"),
1991+
kind => span_bug!(tcx.def_span(def_id), "cannot compute the variances of {kind:?}"),
19981992
}
19991993

2000-
let ty_predicates = tcx.predicates_of(item.owner_id);
1994+
let ty_predicates = tcx.predicates_of(def_id);
20011995
assert_eq!(ty_predicates.parent, None);
2002-
let variances = tcx.variances_of(item.owner_id);
1996+
let variances = tcx.variances_of(def_id);
20031997

20041998
let mut constrained_parameters: FxHashSet<_> = variances
20051999
.iter()
@@ -2012,8 +2006,10 @@ fn check_variances_for_type_defn<'tcx>(
20122006

20132007
// Lazily calculated because it is only needed in case of an error.
20142008
let explicitly_bounded_params = LazyCell::new(|| {
2015-
let icx = crate::collect::ItemCtxt::new(tcx, item.owner_id.def_id);
2016-
hir_generics
2009+
let icx = crate::collect::ItemCtxt::new(tcx, def_id);
2010+
tcx.hir_node_by_def_id(def_id)
2011+
.generics()
2012+
.unwrap()
20172013
.predicates
20182014
.iter()
20192015
.filter_map(|predicate| match predicate.kind {
@@ -2028,18 +2024,20 @@ fn check_variances_for_type_defn<'tcx>(
20282024
.collect::<FxHashSet<_>>()
20292025
});
20302026

2031-
let ty_generics = tcx.generics_of(item.owner_id);
2032-
20332027
for (index, _) in variances.iter().enumerate() {
20342028
let parameter = Parameter(index as u32);
20352029

20362030
if constrained_parameters.contains(&parameter) {
20372031
continue;
20382032
}
20392033

2040-
let ty_param = &ty_generics.own_params[index];
2034+
let node = tcx.hir_node_by_def_id(def_id);
2035+
let item = node.expect_item();
2036+
let hir_generics = node.generics().unwrap();
20412037
let hir_param = &hir_generics.params[index];
20422038

2039+
let ty_param = &tcx.generics_of(item.owner_id).own_params[index];
2040+
20432041
if ty_param.def_id != hir_param.def_id.into() {
20442042
// Valid programs always have lifetimes before types in the generic parameter list.
20452043
// ty_generics are normalized to be in this required order, and variances are built
@@ -2057,7 +2055,7 @@ fn check_variances_for_type_defn<'tcx>(
20572055

20582056
// Look for `ErrorGuaranteed` deeply within this type.
20592057
if let ControlFlow::Break(ErrorGuaranteed { .. }) = tcx
2060-
.type_of(item.owner_id)
2058+
.type_of(def_id)
20612059
.instantiate_identity()
20622060
.visit_with(&mut HasErrorDeep { tcx, seen: Default::default() })
20632061
{

0 commit comments

Comments
 (0)